Skip to content

Commit

Permalink
Merge pull request #90 from codecov/scott/refactor-notifier-caching
Browse files Browse the repository at this point in the history
ref: Make sure both base status notifiers cache notifications
  • Loading branch information
scott-codecov committed Sep 7, 2023
2 parents 4a4bb80 + d8b34be commit c828650
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
2 changes: 1 addition & 1 deletion services/notification/notifiers/checks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ async def notify(self, comparison: Comparison):
payload["url"] = get_pull_url(comparison.pull)
else:
payload["url"] = get_commit_url(comparison.head.commit)
return await self.send_notification(comparison, payload)
return await self.maybe_send_notification(comparison, payload)
except TorngitClientError as e:
if e.code == 403:
raise e
Expand Down
83 changes: 43 additions & 40 deletions services/notification/notifiers/status/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,46 +212,7 @@ async def notify(self, comparison: Comparison):
else:
payload["url"] = get_commit_url(comparison.head.commit)

base_commit = comparison.base.commit if comparison.base else None
head_commit = comparison.head.commit if comparison.head else None

cache_key = make_hash_sha256(
dict(
type="status_check_notification",
repoid=head_commit.repoid,
base_commitid=base_commit.commitid if base_commit else None,
head_commitid=head_commit.commitid if head_commit else None,
notifier_name=self.name,
notifier_title=self.title,
)
)

last_payload = cache.get_backend().get(cache_key)
if last_payload is NO_VALUE or last_payload != payload:
ttl = int(
get_config(
"setup", "cache", "send_status_notification", default=600
)
) # 10 min default
cache.get_backend().set(cache_key, ttl, payload)
return await self.send_notification(comparison, payload)
else:
log.info(
"Notification payload unchanged. Skipping notification.",
extra=dict(
repoid=head_commit.repoid,
base_commitid=base_commit.commitid if base_commit else None,
head_commitid=head_commit.commitid if head_commit else None,
notifier_name=self.name,
notifier_title=self.title,
),
)
return NotificationResult(
notification_attempted=False,
notification_successful=None,
explanation="payload_unchanged",
data_sent=None,
)
return await self.maybe_send_notification(comparison, payload)
except TorngitClientError:
log.warning(
"Unable to send status notification to user due to a client-side error",
Expand Down Expand Up @@ -302,6 +263,48 @@ def get_status_external_name(self) -> str:
status_piece = f"/{self.title}" if self.title != "default" else ""
return f"codecov/{self.context}{status_piece}"

async def maybe_send_notification(
self, comparison: Comparison, payload: dict
) -> NotificationResult:
base_commit = comparison.base.commit if comparison.base else None
head_commit = comparison.head.commit if comparison.head else None

cache_key = make_hash_sha256(
dict(
type="status_check_notification",
repoid=head_commit.repoid,
base_commitid=base_commit.commitid if base_commit else None,
head_commitid=head_commit.commitid if head_commit else None,
notifier_name=self.name,
notifier_title=self.title,
)
)

last_payload = cache.get_backend().get(cache_key)
if last_payload is NO_VALUE or last_payload != payload:
ttl = int(
get_config("setup", "cache", "send_status_notification", default=600)
) # 10 min default
cache.get_backend().set(cache_key, ttl, payload)
return await self.send_notification(comparison, payload)
else:
log.info(
"Notification payload unchanged. Skipping notification.",
extra=dict(
repoid=head_commit.repoid,
base_commitid=base_commit.commitid if base_commit else None,
head_commitid=head_commit.commitid if head_commit else None,
notifier_name=self.name,
notifier_title=self.title,
),
)
return NotificationResult(
notification_attempted=False,
notification_successful=None,
explanation="payload_unchanged",
data_sent=None,
)

async def send_notification(self, comparison: Comparison, payload):
title = self.get_status_external_name()
repository_service = self.repository_service
Expand Down

0 comments on commit c828650

Please sign in to comment.