Skip to content

Commit

Permalink
feat: actuvate bundle analysis commit status (#638)
Browse files Browse the repository at this point in the history
giovanni-guidini authored Aug 26, 2024
1 parent 7d8a365 commit f9b65c7
Showing 2 changed files with 82 additions and 34 deletions.
20 changes: 18 additions & 2 deletions services/bundle_analysis/new_notify/__init__.py
Original file line number Diff line number Diff line change
@@ -14,13 +14,19 @@
from services.bundle_analysis.new_notify.contexts.comment import (
BundleAnalysisPRCommentContextBuilder,
)
from services.bundle_analysis.new_notify.contexts.commit_status import (
CommitStatusNotificationContextBuilder,
)
from services.bundle_analysis.new_notify.helpers import (
get_notification_types_configured,
)
from services.bundle_analysis.new_notify.messages import MessageStrategyInterface
from services.bundle_analysis.new_notify.messages.comment import (
BundleAnalysisCommentMarkdownStrategy,
)
from services.bundle_analysis.new_notify.messages.commit_status import (
CommitStatusMessageStrategy,
)
from services.bundle_analysis.new_notify.types import (
NotificationSuccess,
NotificationType,
@@ -102,12 +108,22 @@ def create_context_for_notification(
NotificationType.PR_COMMENT: (
BundleAnalysisPRCommentContextBuilder,
BundleAnalysisCommentMarkdownStrategy,
)
),
NotificationType.COMMIT_STATUS: (
CommitStatusNotificationContextBuilder,
CommitStatusMessageStrategy,
),
# The commit-check API is more powerful than COMMIT_STATUS
# but we currently don't differentiate between them
NotificationType.GITHUB_COMMIT_CHECK: (
CommitStatusNotificationContextBuilder,
CommitStatusMessageStrategy,
),
}
notifier_strategy = notifier_lookup.get(notification_type)

if notifier_strategy is None:
msg = f"No context builder for {notification_type.name}. Skipping"
msg = f"No context builder for {notification_type}. Skipping"
log.error(msg)
return None
builder_class, message_strategy_class = notifier_strategy
96 changes: 64 additions & 32 deletions services/bundle_analysis/new_notify/tests/test_notify_service.py
Original file line number Diff line number Diff line change
@@ -44,9 +44,46 @@ def override_comment_builder_and_message_strategy(mocker):
"services.bundle_analysis.new_notify.BundleAnalysisCommentMarkdownStrategy",
return_value=mock_markdown_strategy,
)
mock_comment_builder.return_value.get_result.return_value = MagicMock(
name="fake_context", notification_type=NotificationType.PR_COMMENT
)
mock_markdown_strategy.build_message.return_value = "D. Message"
mock_markdown_strategy.send_message.return_value = NotificationResult(
notification_attempted=True,
notification_successful=True,
github_app_used=None,
)
return (mock_comment_builder, mock_markdown_strategy)


def override_commit_status_builder_and_message_strategy(mocker):
mock_commit_status_builder = MagicMock(name="fake_commit_status_builder")
mock_commit_status_builder.get_result.return_value = "D. Context"
mock_commit_status_builder.build_context.return_value = mock_commit_status_builder
mock_commit_status_builder.initialize_from_context.return_value = (
mock_commit_status_builder
)
mock_commit_status_builder = mocker.patch(
"services.bundle_analysis.new_notify.CommitStatusNotificationContextBuilder",
return_value=mock_commit_status_builder,
)
commit_status_message_strategy = AsyncMock(name="fake_markdown_strategy")
commit_status_message_strategy = mocker.patch(
"services.bundle_analysis.new_notify.CommitStatusMessageStrategy",
return_value=commit_status_message_strategy,
)
mock_commit_status_builder.return_value.get_result.return_value = MagicMock(
name="fake_context", notification_type=NotificationType.COMMIT_STATUS
)
commit_status_message_strategy.build_message.return_value = "D. Message"
commit_status_message_strategy.send_message.return_value = NotificationResult(
notification_attempted=True,
notification_successful=True,
github_app_used=None,
)
return (mock_commit_status_builder, commit_status_message_strategy)


@pytest.fixture
def mock_base_context():
context_requirements = (
@@ -126,21 +163,12 @@ def test_create_context_success(self, dbsession, mock_storage, mocker):
== head_commit_report.external_id
)

@pytest.mark.parametrize(
"unknown_notification",
[
NotificationType.COMMIT_STATUS,
NotificationType.GITHUB_COMMIT_CHECK,
],
)
def test_create_contexts_unknown_notification(
self, mock_base_context, unknown_notification
):
def test_create_contexts_unknown_notification(self, mock_base_context):
current_yaml = UserYaml.from_dict({})
service = BundleAnalysisNotifyService(mock_base_context.commit, current_yaml)
assert (
service.create_context_for_notification(
mock_base_context, unknown_notification
mock_base_context, "unknown_notification_type"
)
is None
)
@@ -201,11 +229,31 @@ def test_skip_all_notification_base_context_failed(
"current_yaml, expected_configured_count, expected_success_count",
[
pytest.param(
{"comment": {"require_bundle_changes": False}},
{
"comment": {"require_bundle_changes": False},
"bundle_analysis": {"status": "informational"},
},
2,
2,
id="comment_and_status",
),
pytest.param(
{
"comment": {"require_bundle_changes": False},
"bundle_analysis": {"status": False},
},
1,
1,
id="only_comment_sent",
)
),
pytest.param(
{
"comment": False,
},
1,
1,
id="only_commit_status",
),
],
)
def test_notify(
@@ -216,18 +264,9 @@ def test_notify(
mocker,
mock_base_context,
):
mock_comment_builder, mock_markdown_strategy = (
override_comment_builder_and_message_strategy(mocker)
)
mock_comment_builder.return_value.get_result.return_value = MagicMock(
name="fake_context", notification_type=NotificationType.PR_COMMENT
)
mock_markdown_strategy.build_message.return_value = "D. Message"
mock_markdown_strategy.send_message.return_value = NotificationResult(
notification_attempted=True,
notification_successful=True,
github_app_used=None,
)
override_comment_builder_and_message_strategy(mocker)
override_commit_status_builder_and_message_strategy(mocker)

mocker.patch.object(
BundleAnalysisNotifyService,
"build_base_context",
@@ -237,13 +276,6 @@ def test_notify(
mock_base_context.current_yaml = current_yaml
service = BundleAnalysisNotifyService(mock_base_context.commit, current_yaml)
result = service.notify()
assert result == BundleAnalysisNotifyReturn(
notifications_configured=(
NotificationType.COMMIT_STATUS,
NotificationType.PR_COMMENT,
),
notifications_successful=(NotificationType.PR_COMMENT,),
)
assert len(result.notifications_configured) == expected_configured_count
assert len(result.notifications_successful) == expected_success_count

0 comments on commit f9b65c7

Please sign in to comment.