Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: swap the notification services #584

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion services/bundle_analysis/new_notify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
from services.bundle_analysis.new_notify.messages.comment import (
BundleAnalysisCommentMarkdownStrategy,
)
from services.bundle_analysis.new_notify.types import NotificationType
from services.bundle_analysis.new_notify.types import (
NotificationSuccess,
NotificationType,
)

log = logging.getLogger(__name__)

Expand All @@ -35,6 +38,15 @@ class BundleAnalysisNotifyReturn(NamedTuple):
notifications_configured: tuple[NotificationType]
notifications_successful: tuple[NotificationType]

def to_NotificationSuccess(self) -> NotificationSuccess:
notification_configured_count = len(self.notifications_configured)
notifications_successful_count = len(self.notifications_successful)
if notification_configured_count == 0:
return NotificationSuccess.NOTHING_TO_NOTIFY
if notification_configured_count == notifications_successful_count:
return NotificationSuccess.FULL_SUCCESS
return NotificationSuccess.PARTIAL_SUCCESS


class BundleAnalysisNotifyService:
def __init__(
Expand Down
23 changes: 23 additions & 0 deletions services/bundle_analysis/new_notify/tests/test_notify_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
BundleAnalysisNotifyReturn,
BundleAnalysisNotifyService,
NotificationFullContext,
NotificationSuccess,
)
from services.bundle_analysis.new_notify.conftest import (
get_commit_pair,
Expand Down Expand Up @@ -208,3 +209,25 @@ def test_notify(
)
assert len(result.notifications_configured) == expected_configured_count
assert len(result.notifications_successful) == expected_success_count

@pytest.mark.parametrize(
"result, success_value",
[
(BundleAnalysisNotifyReturn([], []), NotificationSuccess.NOTHING_TO_NOTIFY),
(
BundleAnalysisNotifyReturn(
[NotificationType.COMMIT_STATUS], [NotificationType.COMMIT_STATUS]
),
NotificationSuccess.FULL_SUCCESS,
),
(
BundleAnalysisNotifyReturn(
[NotificationType.COMMIT_STATUS, NotificationType.PR_COMMENT],
[NotificationType.COMMIT_STATUS],
),
NotificationSuccess.PARTIAL_SUCCESS,
),
],
)
def test_to_NotificationSuccess(self, result, success_value):
assert result.to_NotificationSuccess() == success_value
6 changes: 6 additions & 0 deletions services/bundle_analysis/new_notify/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ class NotificationType(Enum):
# See docs on the difference between COMMIT_STATUS and GITHUB_COMMIT_CHECK
# https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/collaborating-on-repositories-with-code-quality-features/about-status-checks#types-of-status-checks-on-github
GITHUB_COMMIT_CHECK = "github_commit_check"


class NotificationSuccess(Enum):
NOTHING_TO_NOTIFY = "nothing_to_notify"
FULL_SUCCESS = "full_success"
PARTIAL_SUCCESS = "partial_success"
14 changes: 8 additions & 6 deletions tasks/bundle_analysis_notify.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import logging
from typing import Any, Dict

from asgiref.sync import async_to_sync
from shared.yaml import UserYaml

from app import celery_app
from database.enums import ReportType
from database.models import Commit
from helpers.github_installation import get_installation_name_for_owner_for_task
from services.bundle_analysis.notify import Notifier as BundleNotifier
from services.bundle_analysis.new_notify import BundleAnalysisNotifyService
from services.lock_manager import LockManager, LockRetry, LockType
from tasks.base import BaseCodecovTask

Expand Down Expand Up @@ -99,15 +98,14 @@ def process_impl_within_lock(
# every processor errored, nothing to notify on
notify = False

success = None
if notify:
installation_name_to_use = get_installation_name_for_owner_for_task(
db_session, self.name, commit.repository.owner
)
notifier = BundleNotifier(
notifier = BundleAnalysisNotifyService(
commit, commit_yaml, gh_app_installation_name=installation_name_to_use
)
success = async_to_sync(notifier.notify)()
result = notifier.notify()

log.info(
"Finished bundle analysis notify",
Expand All @@ -116,10 +114,14 @@ def process_impl_within_lock(
commit=commitid,
commit_yaml=commit_yaml,
parent_task=self.request.parent_id,
result=result,
),
)

return {"notify_attempted": notify, "notify_succeeded": success}
return {
"notify_attempted": notify,
"notify_succeeded": result.to_NotificationSuccess(),
}


RegisteredBundleAnalysisNotifyTask = celery_app.register_task(
Expand Down
15 changes: 13 additions & 2 deletions tasks/tests/unit/test_bundle_analysis_notify_task.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from database.tests.factories import CommitFactory
from services.bundle_analysis.new_notify import BundleAnalysisNotifyReturn
from services.bundle_analysis.new_notify.types import (
NotificationSuccess,
NotificationType,
)
from tasks.bundle_analysis_notify import BundleAnalysisNotifyTask


Expand All @@ -14,7 +19,13 @@ def test_bundle_analysis_notify_task(
dbsession.add(commit)
dbsession.flush()

mocker.patch("services.bundle_analysis.notify.Notifier.notify", return_value=True)
mocker.patch(
"services.bundle_analysis.new_notify.BundleAnalysisNotifyService.notify",
return_value=BundleAnalysisNotifyReturn(
notifications_configured=(NotificationType.PR_COMMENT,),
notifications_successful=(NotificationType.PR_COMMENT,),
),
)

result = BundleAnalysisNotifyTask().run_impl(
dbsession,
Expand All @@ -25,5 +36,5 @@ def test_bundle_analysis_notify_task(
)
assert result == {
"notify_attempted": True,
"notify_succeeded": True,
"notify_succeeded": NotificationSuccess.FULL_SUCCESS,
}
Loading