Skip to content

Commit

Permalink
feat: swap the notification services
Browse files Browse the repository at this point in the history
Activates the new notification service for bundle analysis
  • Loading branch information
giovanni-guidini committed Aug 9, 2024
1 parent 13e7e28 commit ab309a6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
29 changes: 23 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 @@ -64,6 +63,15 @@ def run_impl(
except LockRetry as retry:
self.retry(max_retries=5, countdown=retry.countdown)

def get_success_value(
self, configured_notifications_count: int, successful_notifications_count: int
) -> str:
if configured_notifications_count == 0:
return "nothing_to_notify"
if configured_notifications_count == successful_notifications_count:
return "full_success"
return "partial_success"

def process_impl_within_lock(
self,
*,
Expand All @@ -90,6 +98,8 @@ def process_impl_within_lock(
assert commit, "commit not found"

notify = True
configured_notifications_count = 0
successful_notifications_count = 0

# these are the task results from prior processor tasks in the chain
# (they get accumulated as we execute each task in succession)
Expand All @@ -99,15 +109,16 @@ 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()
configured_notifications_count = len(result.notifications_configured)
successful_notifications_count = len(result.notifications_successful)

log.info(
"Finished bundle analysis notify",
Expand All @@ -116,10 +127,16 @@ 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": self.get_success_value(
configured_notifications_count, successful_notifications_count
),
}


RegisteredBundleAnalysisNotifyTask = celery_app.register_task(
Expand Down
34 changes: 32 additions & 2 deletions tasks/tests/unit/test_bundle_analysis_notify_task.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
import pytest

from database.tests.factories import CommitFactory
from services.bundle_analysis.new_notify import BundleAnalysisNotifyReturn
from services.bundle_analysis.new_notify.types import NotificationType
from tasks.bundle_analysis_notify import BundleAnalysisNotifyTask


@pytest.mark.parametrize(
"configured_notifications_count, successful_notifications_count, expected",
[
(0, 0, "nothing_to_notify"),
(2, 2, "full_success"),
(2, 1, "partial_success"),
],
)
def test_bundle_analysis_notify_task_get_success(
configured_notifications_count, successful_notifications_count, expected
):
task = BundleAnalysisNotifyTask()
assert (
task.get_success_value(
configured_notifications_count, successful_notifications_count
)
== expected
)


def test_bundle_analysis_notify_task(
mocker,
dbsession,
Expand All @@ -14,7 +38,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 +55,5 @@ def test_bundle_analysis_notify_task(
)
assert result == {
"notify_attempted": True,
"notify_succeeded": True,
"notify_succeeded": "full_success",
}

0 comments on commit ab309a6

Please sign in to comment.