From 124c78f1c32622bcc325aa7792b9e6b4874aa728 Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Tue, 29 Oct 2024 14:04:49 +0100 Subject: [PATCH] try to fix tests --- .../notification/notifiers/status/base.py | 7 +- .../tests/integration/test_comment.py | 7 ++ .../notifiers/tests/unit/test_checks.py | 78 ++++++++++++---- .../tests/unit/test_codecov_slack_app.py | 6 ++ .../notifiers/tests/unit/test_comment.py | 84 +++++++++++++++-- .../tests/unit/test_comment_conditions.py | 19 +++- .../notifiers/tests/unit/test_generics.py | 15 +++ .../notifiers/tests/unit/test_gitter.py | 69 +++++++------- .../notifiers/tests/unit/test_hipchat.py | 3 + .../notifiers/tests/unit/test_irc.py | 2 + .../notifiers/tests/unit/test_slack.py | 8 +- .../notifiers/tests/unit/test_status.py | 93 ++++++++++++++++--- .../notifiers/tests/unit/test_webhook.py | 31 ++++++- .../tests/unit/test_commit_notifications.py | 5 + .../tests/unit/test_notification_service.py | 78 ++++++++++------ tasks/save_report_results.py | 6 +- tasks/tests/unit/test_notify_task.py | 30 +++--- 17 files changed, 409 insertions(+), 132 deletions(-) diff --git a/services/notification/notifiers/status/base.py b/services/notification/notifiers/status/base.py index e15f66a06..e7e519fec 100644 --- a/services/notification/notifiers/status/base.py +++ b/services/notification/notifiers/status/base.py @@ -4,7 +4,6 @@ from asgiref.sync import async_to_sync from shared.config import get_config from shared.helpers.cache import NO_VALUE, make_hash_sha256 -from shared.torngit.base import TorngitBaseAdapter from shared.torngit.exceptions import TorngitClientError, TorngitError from helpers.cache import cache @@ -22,10 +21,6 @@ class StatusNotifier(AbstractBaseNotifier): - def __init__(self, *args, **kwargs) -> None: - super().__init__(*args, **kwargs) - self._repository_service: TorngitBaseAdapter = None - def is_enabled(self) -> bool: return True @@ -139,7 +134,7 @@ def required_builds(self, comparison: ComparisonProxy) -> bool: return True def get_github_app_used(self) -> int | None: - torngit = self._repository_service + torngit = self.repository_service if torngit is None: return None torngit_installation = torngit.data.get("installation") diff --git a/services/notification/notifiers/tests/integration/test_comment.py b/services/notification/notifiers/tests/integration/test_comment.py index 3035aca46..fdb9cc77a 100644 --- a/services/notification/notifiers/tests/integration/test_comment.py +++ b/services/notification/notifiers/tests/integration/test_comment.py @@ -351,6 +351,7 @@ def test_notify(self, sample_comparison, codecov_vcr, mock_configuration): notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=comparison.repository_service, ) result = notifier.notify(comparison) assert result.notification_attempted @@ -425,6 +426,7 @@ def test_notify_test_results_error( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=comparison.repository_service, ) result = notifier.notify(comparison) assert result.notification_attempted @@ -492,6 +494,7 @@ def test_notify_upgrade( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=comparison.repository_service, decoration_type=Decoration.upgrade, ) result = notifier.notify(comparison) @@ -532,6 +535,7 @@ def test_notify_upload_limited( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=comparison.repository_service, decoration_type=Decoration.upload_limit, ) result = notifier.notify(comparison) @@ -570,6 +574,7 @@ def test_notify_gitlab( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=comparison.repository_service, ) result = notifier.notify(comparison) assert result.notification_attempted @@ -635,6 +640,7 @@ def test_notify_new_layout( }, notifier_site_settings=True, current_yaml={}, + repository_service=comparison.repository_service, ) result = notifier.notify(comparison) assert result.notification_attempted @@ -710,6 +716,7 @@ def test_notify_with_components( ] } }, + repository_service=comparison.repository_service, ) result = notifier.notify(comparison) assert result.notification_attempted diff --git a/services/notification/notifiers/tests/unit/test_checks.py b/services/notification/notifiers/tests/unit/test_checks.py index ca1ccbd0a..5575f1d3f 100644 --- a/services/notification/notifiers/tests/unit/test_checks.py +++ b/services/notification/notifiers/tests/unit/test_checks.py @@ -277,6 +277,7 @@ def test_checks_403_failure(self, sample_comparison, mocker, mock_repo_provider) notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) status_notifier = mocker.MagicMock( PatchStatusNotifier( @@ -285,6 +286,7 @@ def test_checks_403_failure(self, sample_comparison, mocker, mock_repo_provider) notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) ) status_notifier.notify.return_value = "success" @@ -321,6 +323,7 @@ def test_checks_failure(self, sample_comparison, mocker, mock_repo_provider): notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) status_notifier = mocker.MagicMock( PatchStatusNotifier( @@ -329,6 +332,7 @@ def test_checks_failure(self, sample_comparison, mocker, mock_repo_provider): notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) ) status_notifier.notify.return_value = "success" @@ -365,6 +369,7 @@ def test_checks_no_pull(self, sample_comparison_without_pull, mocker): notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=None, ) status_notifier = mocker.MagicMock( PatchStatusNotifier( @@ -373,6 +378,7 @@ def test_checks_no_pull(self, sample_comparison_without_pull, mocker): notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=None, ) ) status_notifier.notify.return_value = "success" @@ -393,6 +399,7 @@ def test_notify_pull_request_not_in_provider( notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=None, ) status_notifier = mocker.MagicMock( PatchStatusNotifier( @@ -401,6 +408,7 @@ def test_notify_pull_request_not_in_provider( notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=None, ) ) status_notifier.notify.return_value = "success" @@ -420,6 +428,7 @@ def test_notify_closed_pull_request(self, dbsession, sample_comparison, mocker): notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=None, ) status_notifier = mocker.MagicMock( PatchStatusNotifier( @@ -428,6 +437,7 @@ def test_notify_closed_pull_request(self, dbsession, sample_comparison, mocker): notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=None, ) ) status_notifier.notify.return_value = "success" @@ -447,6 +457,7 @@ def test_create_annotations_single_segment(self, sample_comparison): notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=None, ) diff = { "files": { @@ -494,6 +505,7 @@ def test_create_annotations_multiple_segments(self, sample_comparison): notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=None, ) diff = { "files": { @@ -570,6 +582,7 @@ def test_get_lines_to_annotate_no_consecutive_lines(self, sample_comparison): notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=None, ) files_with_change = [ { @@ -611,6 +624,7 @@ def test_get_lines_to_annotate_consecutive_lines(self, sample_comparison): notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=None, ) report = Report() first_deleted_file = ReportFile("file_1.go") @@ -665,6 +679,7 @@ def test_paginate_annotations( notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) sample_array = list(range(1, 61, 1)) expected_result = [list(range(1, 51, 1)), list(range(51, 61, 1))] @@ -681,6 +696,7 @@ def test_build_flag_payload( notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "state": "success", @@ -705,6 +721,7 @@ def test_build_upgrade_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, decoration_type=Decoration.upgrade, ) expected_result = { @@ -727,6 +744,7 @@ def test_build_default_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "state": "success", @@ -758,6 +776,7 @@ def test_build_payload_target_coverage_failure( notifier_yaml_settings={"target": "70%", "paths": ["pathone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "state": "failure", @@ -783,6 +802,7 @@ def test_build_payload_without_base_report( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "state": "success", @@ -819,6 +839,7 @@ def test_build_payload_target_coverage_failure_witinh_threshold( notifier_yaml_settings={"threshold": "5"}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "state": "success", @@ -860,6 +881,7 @@ def test_build_payload_with_multiple_changes( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "state": "failure", @@ -926,6 +948,7 @@ def test_build_payload_no_diff( notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) assert notifier.is_enabled() notifier.name @@ -957,6 +980,7 @@ def test_send_notification(self, sample_comparison, mocker, mock_repo_provider): notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) result = notifier.send_notification(sample_comparison, payload) assert result.notification_successful == True @@ -987,6 +1011,7 @@ def test_send_notification_annotations_paginations( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_calls = [ { @@ -1039,6 +1064,7 @@ def test_notify( notifier_yaml_settings={"paths": ["pathone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = sample_comparison.project_coverage_base.commit head_commit = sample_comparison.head.commit @@ -1068,6 +1094,7 @@ def test_notify_passing_empty_upload( notifier_yaml_settings={"paths": ["pathone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, decoration_type=Decoration.passing_empty_upload, ) result = notifier.notify(sample_comparison) @@ -1096,6 +1123,7 @@ def test_notify_failing_empty_upload( notifier_yaml_settings={"paths": ["pathone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, decoration_type=Decoration.failing_empty_upload, ) result = notifier.notify(sample_comparison) @@ -1121,6 +1149,7 @@ def test_notification_exception( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) # Test exception handling when there's a TorngitClientError @@ -1148,6 +1177,7 @@ def test_notification_exception_not_fit(self, sample_comparison, mocker): notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=None, ) mocker.patch.object( ChecksNotifier, "can_we_set_this_status", return_value=False @@ -1169,6 +1199,7 @@ def test_notification_exception_preexisting_commit_status( notifier_yaml_settings={"paths": ["file_1.go"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) mock_repo_provider.get_commit_statuses.return_value = Status( [ @@ -1210,6 +1241,7 @@ def test_checks_with_after_n_builds(self, sample_comparison, mocker): }, } ), + repository_service=None, ) mocker.patch.object(ChecksNotifier, "can_we_set_this_status", return_value=True) @@ -1232,6 +1264,7 @@ def test_build_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "state": "success", @@ -1257,6 +1290,7 @@ def test_build_upgrade_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, decoration_type=Decoration.upgrade, ) expected_result = { @@ -1286,6 +1320,7 @@ def test_build_payload_with_multiple_changes( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "state": "failure", @@ -1311,6 +1346,7 @@ def test_build_payload_without_base_report( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "state": "success", @@ -1335,6 +1371,7 @@ def test_build_failing_empty_upload_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, decoration_type=Decoration.failing_empty_upload, ) expected_result = { @@ -1382,6 +1419,7 @@ def test_analytics_url( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={"comment": {"layout": "files"}}, + repository_service=mock_repo_provider, ) result = notifier.send_notification(sample_comparison, payload) expected_result = { @@ -1419,6 +1457,7 @@ def test_build_flag_payload( notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) result = notifier.build_payload(sample_comparison) base_commit = sample_comparison.project_coverage_base.commit @@ -1445,6 +1484,7 @@ def test_build_upgrade_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, decoration_type=Decoration.upgrade, ) expected_result = { @@ -1470,6 +1510,7 @@ def test_build_passing_empty_upload_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, decoration_type=Decoration.passing_empty_upload, ) expected_result = { @@ -1492,6 +1533,7 @@ def test_build_default_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={"comment": {"layout": "files"}}, + repository_service=mock_repo_provider, ) result = notifier.build_payload(sample_comparison) repo = sample_comparison.head.commit.repository @@ -1538,6 +1580,7 @@ def test_build_default_payload_with_flags( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={"comment": {"layout": "files, flags"}}, + repository_service=mock_repo_provider, ) result = notifier.build_payload(sample_comparison) repo = sample_comparison.head.commit.repository @@ -1585,6 +1628,7 @@ def test_build_default_payload_with_flags_and_footer( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={"comment": {"layout": "files, flags, footer"}}, + repository_service=mock_repo_provider, ) result = notifier.build_payload(sample_comparison) repo = sample_comparison.head.commit.repository @@ -1637,6 +1681,7 @@ def test_build_default_payload_comment_off( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={"comment": False}, + repository_service=mock_repo_provider, ) result = notifier.build_payload(sample_comparison) repo = sample_comparison.head.commit.repository @@ -1660,6 +1705,7 @@ def test_build_default_payload_negative_change_comment_off( notifier_yaml_settings={"removed_code_behavior": "removals_only"}, notifier_site_settings=True, current_yaml={"comment": False}, + repository_service=mock_repo_provider, ) result = notifier.build_payload(sample_comparison_negative_change) repo = sample_comparison_negative_change.head.commit.repository @@ -1683,6 +1729,7 @@ def test_build_payload_not_auto( notifier_yaml_settings={"target": "57%", "flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) repo = sample_comparison.head.commit.repository expected_result = { @@ -1709,6 +1756,7 @@ def test_build_payload_no_base_report( notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) repo = sample_comparison_without_base_report.head.commit.repository expected_result = { @@ -1727,10 +1775,6 @@ def test_check_notify_no_path_match( mock_repo_provider.get_commit_statuses.return_value = Status([]) mock_configuration.params["setup"]["codecov_dashboard_url"] = "test.example.br" comparison = sample_comparison - payload = { - "state": "success", - "output": {"title": "Codecov Report", "summary": "Summary"}, - } mock_repo_provider.create_check_run.return_value = 2234563 mock_repo_provider.update_check_run.return_value = "success" @@ -1740,9 +1784,8 @@ def test_check_notify_no_path_match( notifier_yaml_settings={"paths": ["pathone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) - base_commit = sample_comparison.project_coverage_base.commit - head_commit = sample_comparison.head.commit result = notifier.notify(sample_comparison) assert result.notification_successful == True assert result.explanation is None @@ -1761,10 +1804,6 @@ def test_check_notify_single_path_match( mock_repo_provider.get_commit_statuses.return_value = Status([]) mock_configuration.params["setup"]["codecov_dashboard_url"] = "test.example.br" comparison = sample_comparison - payload = { - "state": "success", - "output": {"title": "Codecov Report", "summary": "Summary"}, - } mock_repo_provider.create_check_run.return_value = 2234563 mock_repo_provider.update_check_run.return_value = "success" @@ -1774,10 +1813,10 @@ def test_check_notify_single_path_match( notifier_yaml_settings={"paths": ["file_1.go"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = sample_comparison.project_coverage_base.commit - head_commit = sample_comparison.head.commit result = notifier.notify(sample_comparison) assert result.notification_successful is True assert result.explanation is None @@ -1802,10 +1841,6 @@ def test_check_notify_multiple_path_match( mock_repo_provider.get_commit_statuses.return_value = Status([]) mock_configuration.params["setup"]["codecov_dashboard_url"] = "test.example.br" comparison = sample_comparison - payload = { - "state": "success", - "output": {"title": "Codecov Report", "summary": "Summary"}, - } mock_repo_provider.create_check_run.return_value = 2234563 mock_repo_provider.update_check_run.return_value = "success" @@ -1815,10 +1850,10 @@ def test_check_notify_multiple_path_match( notifier_yaml_settings={"paths": ["file_2.py", "file_1.go"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = sample_comparison.project_coverage_base.commit - head_commit = sample_comparison.head.commit result = notifier.notify(sample_comparison) assert result.notification_successful == True assert result.explanation is None @@ -1850,9 +1885,9 @@ def test_check_notify_with_paths( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = sample_comparison.project_coverage_base.commit - head_commit = sample_comparison.head.commit result = notifier.notify(sample_comparison) assert result.notification_successful == True assert result.explanation is None @@ -1884,6 +1919,7 @@ def test_notify_pass_behavior_when_coverage_not_uploaded( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = ( sample_comparison_coverage_carriedforward.project_coverage_base.commit @@ -1930,6 +1966,7 @@ def test_notify_pass_behavior_when_coverage_uploaded( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = ( sample_comparison_coverage_carriedforward.project_coverage_base.commit @@ -1970,6 +2007,7 @@ def test_notify_include_behavior_when_coverage_not_uploaded( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = ( sample_comparison_coverage_carriedforward.project_coverage_base.commit @@ -2011,6 +2049,7 @@ def test_notify_exclude_behavior_when_coverage_not_uploaded( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = NotificationResult( notification_attempted=False, @@ -2041,6 +2080,7 @@ def test_notify_exclude_behavior_when_coverage_uploaded( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = ( sample_comparison_coverage_carriedforward.project_coverage_base.commit @@ -2086,6 +2126,7 @@ def test_notify_exclude_behavior_when_some_coverage_uploaded( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = ( sample_comparison_coverage_carriedforward.project_coverage_base.commit @@ -2127,6 +2168,7 @@ def test_notify_exclude_behavior_no_flags( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = ( sample_comparison_coverage_carriedforward.project_coverage_base.commit @@ -2166,6 +2208,7 @@ def test_build_payload_comments_true(self, sample_comparison, mock_configuration notifier_yaml_settings={}, notifier_site_settings={}, current_yaml={"comment": True}, + repository_service=None, ) res = notifier.build_payload(sample_comparison) assert res == { @@ -2186,6 +2229,7 @@ def test_build_payload_comments_false(self, sample_comparison, mock_configuratio notifier_yaml_settings={}, notifier_site_settings={}, current_yaml={"comment": False}, + repository_service=None, ) res = notifier.build_payload(sample_comparison) assert res == { diff --git a/services/notification/notifiers/tests/unit/test_codecov_slack_app.py b/services/notification/notifiers/tests/unit/test_codecov_slack_app.py index a53c8d2b3..cb6e6919e 100644 --- a/services/notification/notifiers/tests/unit/test_codecov_slack_app.py +++ b/services/notification/notifiers/tests/unit/test_codecov_slack_app.py @@ -12,6 +12,7 @@ def test_is_enabled(self, dbsession, mock_configuration, sample_comparison): notifier_yaml_settings={"enabled": True}, notifier_site_settings=True, current_yaml={"slack_app": {"enabled": True}}, + repository_service=None, ) assert notifier.is_enabled() == True @@ -22,6 +23,7 @@ def test_is_enable_false(self, dbsession, mock_configuration, sample_comparison) notifier_yaml_settings={"enabled": False}, notifier_site_settings=True, current_yaml={"slack_app": {"enabled": False}}, + repository_service=None, ) assert notifier.is_enabled() is False @@ -33,6 +35,7 @@ def test_notification_type(self, dbsession, mock_configuration, sample_compariso notifier_yaml_settings={"enabled": True}, notifier_site_settings=True, current_yaml={"slack_app": {"enabled": True}}, + repository_service=None, ) assert notifier.notification_type == Notification.codecov_slack_app @@ -47,6 +50,7 @@ def test_notify( notifier_yaml_settings={"enabled": True}, notifier_site_settings=True, current_yaml={"slack_app": {"enabled": True}}, + repository_service=None, ) result = notifier.notify(sample_comparison) assert result.notification_successful == True @@ -64,6 +68,7 @@ def test_notify_failure( notifier_yaml_settings={"enabled": True}, notifier_site_settings=True, current_yaml={"slack_app": {"enabled": True}}, + repository_service=None, ) result = notifier.notify(sample_comparison) assert result.notification_successful == False @@ -83,6 +88,7 @@ def test_notify_request_being_called( notifier_yaml_settings={"enabled": True}, notifier_site_settings=True, current_yaml={"slack_app": {"enabled": True}}, + repository_service=None, ) result = notifier.notify(sample_comparison) assert result.notification_successful == True diff --git a/services/notification/notifiers/tests/unit/test_comment.py b/services/notification/notifiers/tests/unit/test_comment.py index 088cf4e91..bbcbe606d 100644 --- a/services/notification/notifiers/tests/unit/test_comment.py +++ b/services/notification/notifiers/tests/unit/test_comment.py @@ -442,6 +442,7 @@ def test_is_enabled_settings_individual_settings_false(self, dbsession): notifier_yaml_settings=False, notifier_site_settings=None, current_yaml={}, + repository_service=None, ) assert not notifier.is_enabled() @@ -455,6 +456,7 @@ def test_is_enabled_settings_individual_settings_none(self, dbsession): notifier_yaml_settings=None, notifier_site_settings=None, current_yaml={}, + repository_service=None, ) assert not notifier.is_enabled() @@ -468,6 +470,7 @@ def test_is_enabled_settings_individual_settings_true(self, dbsession): notifier_yaml_settings=True, notifier_site_settings=None, current_yaml={}, + repository_service=None, ) assert not notifier.is_enabled() @@ -481,6 +484,7 @@ def test_is_enabled_settings_individual_settings_dict(self, dbsession): notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=None, current_yaml={}, + repository_service=None, ) assert notifier.is_enabled() @@ -616,6 +620,7 @@ def test_create_message_files_section( notifier_yaml_settings={"layout": "files"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) pull = comparison.pull repository = sample_comparison.head.commit.repository @@ -772,6 +777,7 @@ def test_create_message_files_section_with_critical_files( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) pull = comparison.pull repository = sample_comparison.head.commit.repository @@ -815,6 +821,7 @@ def test_create_message_with_github_app_comment( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) res = notifier.build_message(comparison) assert ( @@ -834,6 +841,7 @@ def test_build_message( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = sample_comparison.head.commit.repository result = notifier.build_message(comparison) @@ -911,6 +919,7 @@ def test_build_message_flags_empty_coverage( notifier_yaml_settings={"layout": "flags"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -961,6 +970,7 @@ def test_build_message_more_sections( notifier_yaml_settings={"layout": ",".join(all_sections)}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = sample_comparison.head.commit.repository result = notifier.build_message(comparison) @@ -1042,6 +1052,7 @@ def test_build_upgrade_message( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=None, decoration_type=Decoration.upgrade, ) result = notifier.build_message(comparison) @@ -1080,6 +1091,7 @@ def test_build_limited_upload_message( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=None, decoration_type=Decoration.upload_limit, ) result = notifier.build_message(comparison) @@ -1111,7 +1123,6 @@ def test_build_passing_empty_upload( "codecov_dashboard_url": "test.example.br", } comparison = sample_comparison - pull = comparison.enriched_pull.database_pull repository = sample_comparison.head.commit.repository notifier = CommentNotifier( repository=repository, @@ -1119,6 +1130,7 @@ def test_build_passing_empty_upload( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=None, decoration_type=Decoration.passing_empty_upload, ) result = notifier.build_message(comparison) @@ -1144,7 +1156,6 @@ def test_build_failing_empty_upload( "codecov_dashboard_url": "test.example.br", } comparison = sample_comparison - pull = comparison.enriched_pull.database_pull repository = sample_comparison.head.commit.repository notifier = CommentNotifier( repository=repository, @@ -1152,6 +1163,7 @@ def test_build_failing_empty_upload( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=None, decoration_type=Decoration.failing_empty_upload, ) result = notifier.build_message(comparison) @@ -1178,7 +1190,6 @@ def test_processing_upload( "codecov_dashboard_url": "test.example.br", } comparison = sample_comparison - pull = comparison.enriched_pull.database_pull repository = sample_comparison.head.commit.repository notifier = CommentNotifier( repository=repository, @@ -1186,6 +1197,7 @@ def test_processing_upload( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=None, decoration_type=Decoration.processing_upload, ) result = notifier.build_message(comparison) @@ -1222,6 +1234,7 @@ def test_build_upgrade_message_enterprise( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=None, decoration_type=Decoration.upgrade, ) result = notifier.build_message(comparison) @@ -1250,6 +1263,7 @@ def test_build_message_hide_complexity( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={"codecov": {"ui": {"hide_complexity": True}}}, + repository_service=mock_repo_provider, ) repository = sample_comparison.head.commit.repository result = notifier.build_message(comparison) @@ -1327,6 +1341,7 @@ def test_build_message_no_base_report( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -1401,6 +1416,7 @@ def test_build_message_no_base_commit( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -1476,6 +1492,7 @@ def test_build_message_no_change( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -1553,6 +1570,7 @@ def test_build_message_negative_change( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -1628,6 +1646,7 @@ def test_build_message_negative_change_tricky_rounding( notifier_yaml_settings={"layout": "diff"}, notifier_site_settings=True, current_yaml={"coverage": {"precision": 2, "round": "down"}}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository # Change the reports @@ -1694,6 +1713,7 @@ def test_build_message_negative_change_tricky_rounding_newheader( notifier_yaml_settings={"layout": "newheader"}, notifier_site_settings=True, current_yaml={"coverage": {"precision": 2, "round": "down"}}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository # Change the reports @@ -1744,6 +1764,7 @@ def test_build_message_show_carriedforward_flags_no_cf_coverage( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -1823,6 +1844,7 @@ def test_build_message_with_without_flags( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -1878,6 +1900,7 @@ def test_build_message_with_without_flags( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -1950,6 +1973,7 @@ def test_build_message_show_carriedforward_flags_has_cf_coverage( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -2022,6 +2046,7 @@ def test_build_message_hide_carriedforward_flags_has_cf_coverage( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -2085,6 +2110,7 @@ def test_send_actual_notification_spammy( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) data = {"message": ["message"], "commentid": "12345", "pullid": 98} mock_repo_provider.post_comment.return_value = {"id": 9865} @@ -2114,6 +2140,7 @@ def test_build_message_no_flags( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) sample_comparison.head.report = ReadOnlyReport.create_from_report( sample_report_without_flags @@ -2190,6 +2217,7 @@ def test_send_actual_notification_new_no_permissions( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) data = {"message": ["message"], "commentid": "12345", "pullid": 98} mock_repo_provider.post_comment.return_value = {"id": 9865} @@ -2218,6 +2246,7 @@ def test_send_actual_notification_new( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) data = {"message": ["message"], "commentid": "12345", "pullid": 98} mock_repo_provider.post_comment.return_value = {"id": 9865} @@ -2244,6 +2273,7 @@ def test_send_actual_notification_new_no_permissions_post( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) data = {"message": ["message"], "commentid": None, "pullid": 98} mock_repo_provider.post_comment.side_effect = TorngitClientError( @@ -2274,6 +2304,7 @@ def test_send_actual_notification_new_deleted_comment( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) data = {"message": ["message"], "commentid": "12345", "pullid": 98} mock_repo_provider.post_comment.return_value = {"id": 9865} @@ -2302,6 +2333,7 @@ def test_send_actual_notification_once_deleted_comment( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) data = {"message": ["message"], "commentid": "12345", "pullid": 98} mock_repo_provider.post_comment.return_value = {"id": 9865} @@ -2330,6 +2362,7 @@ def test_send_actual_notification_once_non_existing_comment( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) data = {"message": ["message"], "commentid": None, "pullid": 98} mock_repo_provider.post_comment.return_value = {"id": 9865} @@ -2358,6 +2391,7 @@ def test_send_actual_notification_once( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) data = {"message": ["message"], "commentid": "12345", "pullid": 98} mock_repo_provider.post_comment.return_value = {"id": 9865} @@ -2384,6 +2418,7 @@ def test_send_actual_notification_once_no_permissions( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) data = {"message": ["message"], "commentid": "12345", "pullid": 98} mock_repo_provider.post_comment.return_value = {"id": 9865} @@ -2412,6 +2447,7 @@ def test_send_actual_notification_default( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) data = {"message": ["message"], "commentid": "12345", "pullid": 98} mock_repo_provider.post_comment.return_value = {"id": 9865} @@ -2438,6 +2474,7 @@ def test_send_actual_notification_default_no_permissions_edit( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) data = {"message": ["message"], "commentid": "12345", "pullid": 98} mock_repo_provider.post_comment.return_value = {"id": 9865} @@ -2466,6 +2503,7 @@ def test_send_actual_notification_default_no_permissions_twice( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) data = {"message": ["message"], "commentid": "12345", "pullid": 98} mock_repo_provider.post_comment.side_effect = TorngitClientError( @@ -2496,6 +2534,7 @@ def test_send_actual_notification_default_comment_not_found( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) data = {"message": ["message"], "commentid": "12345", "pullid": 98} mock_repo_provider.post_comment.return_value = {"id": 9865} @@ -2522,6 +2561,7 @@ def test_notify_no_pull_request(self, dbsession, sample_comparison_without_pull) }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = notifier.notify(sample_comparison_without_pull) assert not result.notification_attempted @@ -2542,6 +2582,7 @@ def test_notify_pull_head_doesnt_match(self, dbsession, sample_comparison): }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = notifier.notify(sample_comparison) assert not result.notification_attempted @@ -2562,6 +2603,7 @@ def test_notify_pull_request_not_in_provider( }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = notifier.notify(sample_comparison_database_pull_without_provider) assert not result.notification_attempted @@ -2588,6 +2630,7 @@ def test_notify_server_unreachable(self, mocker, dbsession, sample_comparison): }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = notifier.notify(sample_comparison) assert result.notification_attempted @@ -2610,6 +2653,7 @@ def test_store_results(self, dbsession, sample_comparison): }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = NotificationResult( notification_attempted=True, @@ -2637,6 +2681,7 @@ def test_store_results_deleted_comment(self, dbsession, sample_comparison): }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = NotificationResult( notification_attempted=True, @@ -2662,6 +2707,7 @@ def test_store_results_no_succesfull_result(self, dbsession, sample_comparison): }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = NotificationResult( notification_attempted=True, @@ -2692,6 +2738,7 @@ def test_notify_unable_to_fetch_info(self, dbsession, mocker, sample_comparison) }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = notifier.notify(sample_comparison) assert not result.notification_attempted @@ -2711,6 +2758,7 @@ def test_notify_not_enough_builds(self, dbsession, sample_comparison): }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = notifier.notify(sample_comparison) assert not result.notification_attempted @@ -2724,9 +2772,6 @@ def test_notify_not_enough_builds(self, dbsession, sample_comparison): async def test_notify_with_enough_builds( self, dbsession, sample_comparison, mocker, pull_state ): - mocker.patch( - "services.notification.notifiers.comment.get_repo_provider_service", - ) build_message_mocker = mocker.patch.object( CommentNotifier, "build_message", @@ -2752,6 +2797,7 @@ async def test_notify_with_enough_builds( }, notifier_site_settings=True, current_yaml={}, + repository_service=mocker.MagicMock(), ) sample_comparison.pull.state = pull_state dbsession.flush() @@ -2819,6 +2865,7 @@ def test_notify_exact_same_report_diff_unrelated_report( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) res = notifier.notify(sample_comparison_no_change) assert res.notification_attempted is False @@ -2881,6 +2928,7 @@ def test_notify_exact_same_report_diff_unrelated_report_update_comment( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) res = notifier.notify(sample_comparison_no_change) assert res.notification_attempted is True @@ -2900,6 +2948,7 @@ def test_message_hide_details_github( notifier_yaml_settings={"layout": "reach", "hide_comment_details": True}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = sample_comparison.head.commit.repository result = notifier.build_message(comparison) @@ -2933,6 +2982,7 @@ def test_message_announcements_only( notifier_yaml_settings={"layout": "announcements"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = sample_comparison.head.commit.repository result = notifier.build_message(comparison) @@ -2969,6 +3019,7 @@ def test_message_hide_details_bitbucket( notifier_yaml_settings={"layout": "reach", "hide_comment_details": True}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = sample_comparison.head.commit.repository result = notifier.build_message(comparison) @@ -4039,6 +4090,7 @@ def test_create_message_files_section_with_critical_files_new_layout( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) pull = comparison.pull repository = sample_comparison.head.commit.repository @@ -4083,6 +4135,7 @@ def test_build_message_no_base_commit_new_layout( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -4152,6 +4205,7 @@ def test_build_message_no_base_report_new_layout( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -4224,6 +4278,7 @@ def test_build_message_no_project_coverage( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -4264,6 +4319,7 @@ def test_build_message_no_project_coverage_files( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -4310,6 +4366,7 @@ def test_build_message_no_project_coverage_condensed_yaml_configs( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -4350,6 +4407,7 @@ def test_build_message_head_and_pull_head_differ_new_layout( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -4426,6 +4484,7 @@ def test_build_message_head_and_pull_head_differ_with_components( ] } }, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -4510,6 +4569,7 @@ def test_build_message_team_plan_customer_missing_lines( ] } }, + repository_service=mock_repo_provider, ) pull = comparison.pull @@ -4551,6 +4611,7 @@ def test_build_message_team_plan_customer_all_lines_covered( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) pull = comparison.pull repository = sample_comparison_coverage_carriedforward.head.commit.repository @@ -4592,6 +4653,7 @@ def test_build_message_team_plan_customer_all_lines_covered_test_results_error( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) pull = comparison.pull repository = sample_comparison_coverage_carriedforward.head.commit.repository @@ -4633,6 +4695,7 @@ def test_build_message_team_plan_customer_all_lines_covered_no_third_line( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) pull = comparison.pull repository = sample_comparison_coverage_carriedforward.head.commit.repository @@ -4664,6 +4727,7 @@ def test_build_message_no_patch_or_proj_change( }, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) repository = comparison.head.commit.repository result = notifier.build_message(comparison) @@ -5021,6 +5085,7 @@ def test_build_message( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) result = notifier.build_message(sample_comparison) expected_result = [ @@ -5123,6 +5188,7 @@ def test_build_message_with_preexisting_bundle_pulls( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) result = notifier.build_message(comparison) @@ -5168,6 +5234,7 @@ def test_should_see_project_coverage_cta_public_repo( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) result = notifier.build_message(sample_comparison) assert PROJECT_COVERAGE_CTA not in result @@ -5188,6 +5255,7 @@ def test_should_see_project_coverage_cta_public_repo( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) result = notifier.build_message(sample_comparison) assert PROJECT_COVERAGE_CTA in result @@ -5220,6 +5288,7 @@ def test_should_see_project_coverage_cta_introduction_date( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) result = notifier.build_message(sample_comparison) assert PROJECT_COVERAGE_CTA not in result @@ -5240,6 +5309,7 @@ def test_should_see_project_coverage_cta_introduction_date( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) result = notifier.build_message(sample_comparison) assert PROJECT_COVERAGE_CTA in result @@ -5276,6 +5346,7 @@ def test_should_see_project_coverage_cta_team_plan( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) result = notifier.build_message(sample_comparison) assert PROJECT_COVERAGE_CTA not in result @@ -5293,6 +5364,7 @@ def test_should_see_project_coverage_cta_team_plan( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=mock_repo_provider, ) result = notifier.build_message(sample_comparison) assert PROJECT_COVERAGE_CTA in result diff --git a/services/notification/notifiers/tests/unit/test_comment_conditions.py b/services/notification/notifiers/tests/unit/test_comment_conditions.py index 9ad9e2c7b..1457c3b1f 100644 --- a/services/notification/notifiers/tests/unit/test_comment_conditions.py +++ b/services/notification/notifiers/tests/unit/test_comment_conditions.py @@ -14,7 +14,9 @@ def _get_notifier( - repository: Repository, required_changes: CoverageCommentRequiredChangesANDGroup + repository: Repository, + required_changes: CoverageCommentRequiredChangesANDGroup, + repo_provider, ): return CommentNotifier( repository=repository, @@ -22,6 +24,7 @@ def _get_notifier( notifier_yaml_settings={"require_changes": required_changes}, notifier_site_settings=True, current_yaml={}, + repository_service=repo_provider, ) @@ -105,7 +108,9 @@ def test_condition_different_comparisons_no_diff( # There's no diff between HEAD and BASE so we can't calculate unexpected coverage. # Any change then needs to be a coverage change mock_repo_provider.get_compare.return_value = {"diff": {"files": {}, "commits": []}} - notifier = _get_notifier(comparison.head.commit.repository, condition) + notifier = _get_notifier( + comparison.head.commit.repository, condition, mock_repo_provider + ) assert HasEnoughRequiredChanges.check_condition(notifier, comparison) == expected @@ -139,7 +144,9 @@ def test_condition_exact_same_report_coverage_not_affected_by_diff( "README.md", ["5", "8", "5", "9"] ) notifier = _get_notifier( - sample_comparison_no_change.head.commit.repository, condition + sample_comparison_no_change.head.commit.repository, + condition, + mock_repo_provider, ) assert ( HasEnoughRequiredChanges.check_condition(notifier, sample_comparison_no_change) @@ -177,7 +184,9 @@ def test_condition_exact_same_report_coverage_affected_by_diff( "file_1.go", ["4", "8", "4", "8"] ) notifier = _get_notifier( - sample_comparison_no_change.head.commit.repository, condition + sample_comparison_no_change.head.commit.repository, + condition, + mock_repo_provider, ) assert ( HasEnoughRequiredChanges.check_condition(notifier, sample_comparison_no_change) @@ -201,6 +210,7 @@ def test_uncovered_patch( notifier = _get_notifier( sample_comparison_no_change.head.commit.repository, [CoverageCommentRequiredChanges.uncovered_patch.value], + mock_repo_provider, ) assert ( HasEnoughRequiredChanges.check_condition(notifier, sample_comparison_no_change) @@ -240,5 +250,6 @@ def test_coverage_drop_with_different_project_configs( notifier = _get_notifier( comparison.head.commit.repository, [CoverageCommentRequiredChanges.coverage_drop.value], + None, ) assert HasEnoughRequiredChanges.check_condition(notifier, comparison) == expected diff --git a/services/notification/notifiers/tests/unit/test_generics.py b/services/notification/notifiers/tests/unit/test_generics.py index 29ade1103..556a77cef 100644 --- a/services/notification/notifiers/tests/unit/test_generics.py +++ b/services/notification/notifiers/tests/unit/test_generics.py @@ -32,6 +32,7 @@ def test_is_enabled_without_site_settings(self, dbsession): notifier_yaml_settings={}, notifier_site_settings=False, current_yaml={}, + repository_service=None, ) assert not notifier.is_enabled() @@ -47,6 +48,7 @@ def test_is_enabled_with_site_settings_no_special_config(self, dbsession): notifier_yaml_settings={"url": "https://example.com/myexample"}, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) assert notifier.is_enabled() @@ -62,6 +64,7 @@ def test_is_enabled_with_site_settings_no_url(self, dbsession): notifier_yaml_settings={"field_1": "something"}, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) assert not notifier.is_enabled() @@ -77,6 +80,7 @@ def test_is_enabled_with_site_settings_whitelisted_url(self, dbsession): notifier_yaml_settings={"url": "https://example.com/myexample"}, notifier_site_settings=["example.com"], current_yaml={}, + repository_service=None, ) assert notifier.is_enabled() @@ -92,6 +96,7 @@ def test_is_enabled_with_site_settings_not_whitelisted_url(self, dbsession): notifier_yaml_settings={"url": "https://example.com/myexample"}, notifier_site_settings=["badexample.com"], current_yaml={}, + repository_service=None, ) assert not notifier.is_enabled() @@ -102,6 +107,7 @@ def test_should_notify_comparison(self, sample_comparison): notifier_yaml_settings={"url": "https://example.com/myexample"}, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) assert notifier.should_notify_comparison(sample_comparison) @@ -115,6 +121,7 @@ def test_should_notify_comparison_bad_branch(self, sample_comparison): }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) assert not notifier.should_notify_comparison(sample_comparison) @@ -128,6 +135,7 @@ def test_should_notify_comparison_good_branch(self, sample_comparison): }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) assert notifier.should_notify_comparison(sample_comparison) @@ -141,6 +149,7 @@ def test_should_notify_comparison_not_above_threshold(self, sample_comparison): }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) assert not notifier.should_notify_comparison(sample_comparison) @@ -156,6 +165,7 @@ def test_should_notify_comparison_no_base( }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) assert not notifier.should_notify_comparison( sample_comparison_without_base_report @@ -171,6 +181,7 @@ def test_should_notify_comparison_is_above_threshold(self, sample_comparison): }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) assert notifier.should_notify_comparison(sample_comparison) @@ -189,6 +200,7 @@ def test_should_notify_comparison_is_above_threshold_no_coverage( }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) assert not notifier.should_notify_comparison(actual_comparison) @@ -202,6 +214,7 @@ def test_notify(self, sample_comparison): }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) res = notifier.notify(sample_comparison) assert res.notification_attempted @@ -220,6 +233,7 @@ def test_notify_should_not_notify(self, sample_comparison, mocker): }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) mocker.patch.object( SampleNotifierForTest, "should_notify_comparison", return_value=False @@ -245,6 +259,7 @@ def test_send_notification_exception(self, mocker, sample_comparison): }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) data = {} res = notifier.send_actual_notification(data) diff --git a/services/notification/notifiers/tests/unit/test_gitter.py b/services/notification/notifiers/tests/unit/test_gitter.py index e784e759a..e61446b2a 100644 --- a/services/notification/notifiers/tests/unit/test_gitter.py +++ b/services/notification/notifiers/tests/unit/test_gitter.py @@ -1,40 +1,39 @@ from decimal import Decimal from services.notification.notifiers.gitter import GitterNotifier +from services.repository import get_repo_provider_service -class TestGitterkNotifier(object): - def test_build_payload_without_special_config( - self, dbsession, mock_configuration, sample_comparison - ): - mock_configuration.params["setup"]["codecov_dashboard_url"] = "test.example.br" - comparison = sample_comparison - notifier = GitterNotifier( - repository=sample_comparison.head.commit.repository, - title="title", - notifier_yaml_settings={}, - notifier_site_settings=True, - current_yaml={}, - ) - result = notifier.build_payload(comparison) - commit = comparison.head.commit - repository = commit.repository - text = f"Coverage *increased* +10.00% on `new_branch` is `60.00000%` via test.example.br/gh/{repository.slug}/commit/{commit.commitid}" - expected_result = { - "message": text, - "branch": "new_branch", - "pr": comparison.pull.pullid, - "commit": commit.commitid, - "commit_short": commit.commitid[:7], - "text": "increased", - "commit_url": f"https://github.com/{repository.slug}/commit/{commit.commitid}", - "codecov_url": f"test.example.br/gh/{repository.slug}/commit/{commit.commitid}", - "coverage": "60.00000", - "coverage_change": Decimal("10.00"), - } - assert result["message"] == expected_result["message"] - assert result == expected_result - - def test_build_paylost_without_base_report(self): - # TODO (Thiago): Write - pass +def test_build_payload_without_special_config( + dbsession, mock_configuration, sample_comparison +): + mock_configuration.params["setup"]["codecov_dashboard_url"] = "test.example.br" + comparison = sample_comparison + notifier = GitterNotifier( + repository=sample_comparison.head.commit.repository, + title="title", + notifier_yaml_settings={}, + notifier_site_settings=True, + current_yaml={}, + repository_service=get_repo_provider_service( + sample_comparison.head.commit.repository + ), + ) + result = notifier.build_payload(comparison) + commit = comparison.head.commit + repository = commit.repository + text = f"Coverage *increased* +10.00% on `new_branch` is `60.00000%` via test.example.br/gh/{repository.slug}/commit/{commit.commitid}" + expected_result = { + "message": text, + "branch": "new_branch", + "pr": comparison.pull.pullid, + "commit": commit.commitid, + "commit_short": commit.commitid[:7], + "text": "increased", + "commit_url": f"https://github.com/{repository.slug}/commit/{commit.commitid}", + "codecov_url": f"test.example.br/gh/{repository.slug}/commit/{commit.commitid}", + "coverage": "60.00000", + "coverage_change": Decimal("10.00"), + } + assert result["message"] == expected_result["message"] + assert result == expected_result diff --git a/services/notification/notifiers/tests/unit/test_hipchat.py b/services/notification/notifiers/tests/unit/test_hipchat.py index 59945d52b..169bf101f 100644 --- a/services/notification/notifiers/tests/unit/test_hipchat.py +++ b/services/notification/notifiers/tests/unit/test_hipchat.py @@ -14,6 +14,7 @@ def test_build_payload_without_special_config( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = notifier.build_payload(comparison) commit = comparison.head.commit @@ -43,6 +44,7 @@ def test_build_payload_without_base_report( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = notifier.build_payload(comparison) commit = comparison.head.commit @@ -68,6 +70,7 @@ def test_build_payload_with_card(self, sample_comparison, mock_configuration): notifier_yaml_settings={"card": True}, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = notifier.build_payload(sample_comparison) commit = sample_comparison.head.commit diff --git a/services/notification/notifiers/tests/unit/test_irc.py b/services/notification/notifiers/tests/unit/test_irc.py index 9e8fc935b..9b11ef8b6 100644 --- a/services/notification/notifiers/tests/unit/test_irc.py +++ b/services/notification/notifiers/tests/unit/test_irc.py @@ -13,6 +13,7 @@ def test_build_payload(self, dbsession, mock_configuration, sample_comparison): notifier_yaml_settings={"server": ""}, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = notifier.build_payload(comparison) commit = comparison.head.commit @@ -60,6 +61,7 @@ def test_send_actual_notification( }, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) commit = comparison.head.commit repository = commit.repository diff --git a/services/notification/notifiers/tests/unit/test_slack.py b/services/notification/notifiers/tests/unit/test_slack.py index 97452ca07..d1c36f56d 100644 --- a/services/notification/notifiers/tests/unit/test_slack.py +++ b/services/notification/notifiers/tests/unit/test_slack.py @@ -13,10 +13,10 @@ def test_build_payload_without_attachments( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = notifier.build_payload(comparison) commit = comparison.head.commit - base_commit = comparison.project_coverage_base.commit repository = commit.repository text = f"Coverage for *increased* `` on `new_branch` is `60.00000%` via ``" expected_result = { @@ -39,10 +39,10 @@ def test_build_payload_with_attachments( notifier_yaml_settings={"attachments": ["sunburst"]}, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = notifier.build_payload(comparison) commit = comparison.head.commit - base_commit = comparison.project_coverage_base.commit repository = commit.repository text = f"Coverage for *increased* `` on `new_branch` is `60.00000%` via ``" expected_result = { @@ -73,6 +73,7 @@ def test_build_payload_with_message( notifier_yaml_settings={"message": "This is a sample"}, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = notifier.build_payload(comparison) commit = comparison.head.commit @@ -91,7 +92,6 @@ def test_build_payload_without_pull( mock_configuration.params["setup"]["codecov_dashboard_url"] = "test.example.br" comparison = sample_comparison_without_pull commit = sample_comparison_without_pull.head.commit - base_commit = comparison.project_coverage_base.commit repository = commit.repository notifier = SlackNotifier( repository=comparison.head.commit.repository, @@ -99,6 +99,7 @@ def test_build_payload_without_pull( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = notifier.build_payload(comparison) text = f"Coverage for *increased* `` on `new_branch` is `60.00000%` via ``" @@ -124,6 +125,7 @@ def test_build_payload_without_base( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={}, + repository_service=None, ) result = notifier.build_payload(comparison) text = f"Coverage for on `new_branch` is `60.00000%` via ``" diff --git a/services/notification/notifiers/tests/unit/test_status.py b/services/notification/notifiers/tests/unit/test_status.py index 0e2b723bd..1a0160742 100644 --- a/services/notification/notifiers/tests/unit/test_status.py +++ b/services/notification/notifiers/tests/unit/test_status.py @@ -37,6 +37,7 @@ def test_notification_type(mocker): mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock(), + mocker.MagicMock(), ).notification_type == Notification.status_project ) @@ -47,6 +48,7 @@ def test_notification_type(mocker): mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock(), + mocker.MagicMock(), ).notification_type == Notification.status_changes ) @@ -332,6 +334,7 @@ def test_can_we_set_this_status_no_pull(self, sample_comparison_without_pull): notifier_yaml_settings={"only_pulls": True}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) assert not only_pulls_notifier.can_we_set_this_status(comparison) wrong_branch_notifier = StatusNotifier( @@ -340,6 +343,7 @@ def test_can_we_set_this_status_no_pull(self, sample_comparison_without_pull): notifier_yaml_settings={"only_pulls": False, "branches": ["old.*"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) assert not wrong_branch_notifier.can_we_set_this_status(comparison) right_branch_notifier = StatusNotifier( @@ -348,6 +352,7 @@ def test_can_we_set_this_status_no_pull(self, sample_comparison_without_pull): notifier_yaml_settings={"only_pulls": False, "branches": ["new.*"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) assert right_branch_notifier.can_we_set_this_status(comparison) no_settings_notifier = StatusNotifier( @@ -356,6 +361,7 @@ def test_can_we_set_this_status_no_pull(self, sample_comparison_without_pull): notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) assert no_settings_notifier.can_we_set_this_status(comparison) exclude_branch_notifier = StatusNotifier( @@ -364,6 +370,7 @@ def test_can_we_set_this_status_no_pull(self, sample_comparison_without_pull): notifier_yaml_settings={"only_pulls": False, "branches": ["!new_branch"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) assert not exclude_branch_notifier.can_we_set_this_status(comparison) @@ -391,6 +398,7 @@ def test_notify_after_n_builds_flags(self, sample_comparison, mocker): }, } ), + repository_service={}, ) mocker.patch.object(StatusNotifier, "can_we_set_this_status", return_value=True) result = no_settings_notifier.notify(comparison) @@ -423,6 +431,7 @@ def test_notify_after_n_builds_flags2(self, sample_comparison, mocker): }, } ), + repository_service={}, ) mocker.patch.object(StatusNotifier, "can_we_set_this_status", return_value=True) result = no_settings_notifier.notify(comparison) @@ -440,6 +449,7 @@ def test_notify_cannot_set_status(self, sample_comparison, mocker): notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) mocker.patch.object( StatusNotifier, "can_we_set_this_status", return_value=False @@ -461,6 +471,7 @@ def test_notify_no_base( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) no_settings_notifier.context = "fake" mocker.patch.object(StatusNotifier, "can_we_set_this_status", return_value=True) @@ -516,6 +527,7 @@ def build_payload(self, comparison): notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) notifier.context = "fake" @@ -551,22 +563,20 @@ def status_already_exists( ) -> bool: return False + fake_repo_service = MagicMock( + name="fake_repo_provider", + set_commit_status=AsyncMock(side_effect=set_status_side_effect), + ) notifier = TestNotifier( repository=comparison.head.commit.repository, title="title", notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=fake_repo_service, ) notifier.context = "fake" - fake_repo_service = MagicMock( - name="fake_repo_provider", - set_commit_status=AsyncMock(side_effect=set_status_side_effect), - ) - mocker.patch.object( - TestNotifier, "repository_service", return_value=fake_repo_service - ) result = notifier.notify(comparison) assert result == NotificationResult( notification_attempted=True, @@ -605,6 +615,7 @@ def build_payload(self, comparison): notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) notifier.context = "fake" @@ -633,6 +644,7 @@ def test_send_notification(self, sample_comparison, mocker, mock_repo_provider): notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) no_settings_notifier.context = "fake" mocked_status_already_exists = mocker.patch.object( @@ -664,6 +676,7 @@ def test_notify_analytics(self, sample_comparison, mocker, mock_repo_provider): notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) no_settings_notifier.context = "fake" mocked_status_already_exists = mocker.patch.object( @@ -687,6 +700,7 @@ def test_notify_analytics_enterprise( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) no_settings_notifier.context = "fake" mocked_status_already_exists = mocker.patch.object( @@ -721,6 +735,7 @@ def test_determine_status_check_behavior_to_apply(self, sample_comparison): } } }, + repository_service={}, ) notifier.context = "fake" assert ( @@ -746,6 +761,7 @@ def test_determine_status_check_behavior_to_apply(self, sample_comparison): } } }, + repository_service={}, ) notifier.context = "fake" assert ( @@ -762,6 +778,7 @@ def test_determine_status_check_behavior_to_apply(self, sample_comparison): notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={"coverage": {"status": {"default_rules": {}, "project": {}}}}, + repository_service={}, ) notifier.context = "fake" assert ( @@ -781,6 +798,7 @@ def test_flag_coverage_was_uploaded_when_none_uploaded( notifier_yaml_settings={"flags": ["missing"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) notifier.context = "fake" assert notifier.flag_coverage_was_uploaded(comparison) is False @@ -795,6 +813,7 @@ def test_flag_coverage_was_uploaded_when_all_uploaded( notifier_yaml_settings={"flags": ["unit"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) notifier.context = "fake" assert notifier.flag_coverage_was_uploaded(comparison) is True @@ -809,6 +828,7 @@ def test_flag_coverage_was_uploaded_when_some_uploaded( notifier_yaml_settings={"flags": ["unit", "enterprise", "missing"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) notifier.context = "fake" assert notifier.flag_coverage_was_uploaded(comparison) is True @@ -823,6 +843,7 @@ def test_flag_coverage_was_uploaded_when_no_status_flags( notifier_yaml_settings={"flags": None}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) notifier.context = "fake" assert notifier.flag_coverage_was_uploaded(comparison) is True @@ -854,9 +875,9 @@ def test_get_github_app_used( notifier_yaml_settings={"flags": None}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=fake_torngit, ) notifier.context = "fake" - notifier._repository_service = fake_torngit assert notifier.get_github_app_used() == expected def test_get_github_app_used_no_repository_service( @@ -869,9 +890,9 @@ def test_get_github_app_used_no_repository_service( notifier_yaml_settings={"flags": None}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=None, ) notifier.context = "fake" - notifier._repository_service = None assert notifier.get_github_app_used() is None @@ -886,6 +907,7 @@ def test_build_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = sample_comparison.project_coverage_base.commit expected_result = { @@ -905,6 +927,7 @@ def test_build_payload_passing_empty_upload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, decoration_type=Decoration.passing_empty_upload, ) expected_result = { @@ -924,6 +947,7 @@ def test_build_payload_failing_empty_upload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, decoration_type=Decoration.failing_empty_upload, ) expected_result = { @@ -943,6 +967,7 @@ def test_build_upgrade_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, decoration_type=Decoration.upgrade, ) base_commit = sample_comparison.project_coverage_base.commit @@ -966,6 +991,7 @@ def test_build_payload_not_auto( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = {"message": "60.00% (target 57.00%)", "state": "success"} result = notifier.build_payload(sample_comparison) @@ -981,6 +1007,7 @@ def test_build_payload_not_auto_not_string( notifier_yaml_settings={"target": 57.0}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = {"message": "60.00% (target 57.00%)", "state": "success"} result = notifier.build_payload(sample_comparison) @@ -1000,9 +1027,8 @@ def test_build_payload_no_base_report( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) - base_commit = comparison.project_coverage_base.commit - head_commit = comparison.head.commit expected_result = { "message": "No report found to compare against", "state": "success", @@ -1022,6 +1048,7 @@ def test_notify_status_doesnt_exist( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = sample_comparison.project_coverage_base.commit expected_result = NotificationResult( @@ -1053,9 +1080,9 @@ def test_notify_client_side_exception( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) base_commit = sample_comparison.project_coverage_base.commit - head_commit = sample_comparison.head.commit repo = sample_comparison.head.commit.repository expected_result = NotificationResult( notification_attempted=True, @@ -1086,9 +1113,9 @@ def test_notify_server_side_exception( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) base_commit = sample_comparison.project_coverage_base.commit - head_commit = sample_comparison.head.commit repo = sample_comparison.head.commit.repository expected_result = NotificationResult( notification_attempted=True, @@ -1119,6 +1146,7 @@ def test_notify_pass_behavior_when_coverage_not_uploaded( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = ( sample_comparison_coverage_carriedforward.project_coverage_base.commit @@ -1151,6 +1179,7 @@ def test_notify_pass_behavior_when_coverage_uploaded( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = ( sample_comparison_coverage_carriedforward.project_coverage_base.commit @@ -1183,6 +1212,7 @@ def test_notify_include_behavior_when_coverage_not_uploaded( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = ( sample_comparison_coverage_carriedforward.project_coverage_base.commit @@ -1214,6 +1244,7 @@ def test_notify_exclude_behavior_when_coverage_not_uploaded( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = NotificationResult( notification_attempted=False, @@ -1239,6 +1270,7 @@ def test_notify_exclude_behavior_when_coverage_uploaded( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = ( sample_comparison_coverage_carriedforward.project_coverage_base.commit @@ -1275,6 +1307,7 @@ def test_notify_exclude_behavior_when_some_coverage_uploaded( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = ( sample_comparison_coverage_carriedforward.project_coverage_base.commit @@ -1307,6 +1340,7 @@ def test_notify_exclude_behavior_no_flags( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = ( sample_comparison_coverage_carriedforward.project_coverage_base.commit @@ -1339,6 +1373,7 @@ def test_notify_path_filter( notifier_yaml_settings={"paths": ["file_1.go"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = sample_comparison.project_coverage_base.commit expected_result = { @@ -1363,8 +1398,8 @@ def test_notify_path_and_flags_filter_nothing_on_base( notifier_yaml_settings={"paths": ["file_1.go"], "flags": ["unit"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) - base_commit = sample_comparison.project_coverage_base.commit expected_result = { # base report does not have unit flag, so there is no coverage there "message": "No coverage information found on base report", @@ -1392,6 +1427,7 @@ def test_notify_path_and_flags_filter_something_on_base( notifier_yaml_settings={"paths": ["file_1.go"], "flags": ["unit"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = sample_comparison_matching_flags.project_coverage_base.commit expected_result = { @@ -1442,6 +1478,7 @@ def test_notify_pass_via_removals_only_behavior( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) expected_result = { "message": "60.00% (target 80.00%), passed because this change only removed code", @@ -1555,6 +1592,7 @@ def test_adjust_base_behavior( notifier_yaml_settings=settings, notifier_site_settings={}, current_yaml=settings, + repository_service={}, ) result = status_mixin._apply_adjust_base_behavior(comparison) assert result == expected @@ -1593,6 +1631,7 @@ def test_notify_pass_adjust_base_behavior( notifier_yaml_settings={"removed_code_behavior": "adjust_base"}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) expected_result = { "message": f"50.00% (-10.00%) compared to {sample_comparison.project_coverage_base.commit.commitid[:7]}, passed because coverage increased by 0% when compared to adjusted base (50.00%)", @@ -1627,6 +1666,7 @@ def test_notify_removed_code_behavior_fail( }, ], }, + repository_service=None, ) mock_configuration.params["setup"]["codecov_dashboard_url"] = "test.example.br" notifier = ProjectStatusNotifier( @@ -1638,6 +1678,7 @@ def test_notify_removed_code_behavior_fail( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=None, ) expected_result = { "message": "60.00% (target 80.00%)", @@ -1681,6 +1722,7 @@ def test_notify_adjust_base_behavior_fail( notifier_yaml_settings={"removed_code_behavior": "adjust_base"}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) expected_result = { "message": f"50.00% (-10.00%) compared to {sample_comparison.project_coverage_base.commit.commitid[:7]}", @@ -1707,6 +1749,7 @@ def test_notify_adjust_base_behavior_skips_if_target_coverage_defined( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) expected_result = { "message": "50.00% (target 80.00%)", @@ -1729,6 +1772,7 @@ def test_notify_removed_code_behavior_unknown( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) expected_result = { "message": "60.00% (target 80.00%)", @@ -1780,6 +1824,7 @@ def test_notify_fully_covered_patch_behavior_fail( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "message": "50.00% (target 70.00%)", @@ -1832,6 +1877,7 @@ def test_notify_fully_covered_patch_behavior_success( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "message": "28.57% (target 70.00%), passed because patch was fully covered by tests, and no indirect coverage changes", @@ -1992,6 +2038,7 @@ def test_notify_fully_covered_patch_behavior_no_coverage_change( }, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service={}, ) expected_result = { "message": "60.00% (target 70.00%), passed because coverage was not affected by patch", @@ -2013,6 +2060,7 @@ def test_build_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "message": "66.67% of diff hit (target 50.00%)", @@ -2031,6 +2079,7 @@ def test_build_upgrade_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, decoration_type=Decoration.upgrade, ) expected_result = { @@ -2050,6 +2099,7 @@ def test_build_payload_target_coverage_failure( notifier_yaml_settings={"target": "70%"}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "message": "66.67% of diff hit (target 70.00%)", @@ -2068,6 +2118,7 @@ def test_build_payload_not_auto_not_string( notifier_yaml_settings={"target": 57.0}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "message": "66.67% of diff hit (target 57.00%)", @@ -2092,6 +2143,7 @@ def test_build_payload_target_coverage_failure_within_threshold( notifier_yaml_settings={"threshold": "5"}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "message": "66.67% of diff hit (within 5.00% threshold of 70.00%)", @@ -2116,6 +2168,7 @@ def test_get_patch_status_bad_threshold( notifier_yaml_settings={"threshold": None}, # invalid value for threshold notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "message": "66.67% of diff hit (target 70.00%)", @@ -2142,6 +2195,7 @@ def test_get_patch_status_bad_threshold_fixed( }, # invalid value for threshold, caught and fixed notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "message": "66.67% of diff hit (within 5.00% threshold of 70.00%)", @@ -2189,6 +2243,7 @@ def test_build_payload_no_diff( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) base_commit = sample_comparison.project_coverage_base.commit head_commit = sample_comparison.head.commit @@ -2242,6 +2297,7 @@ def test_build_payload_no_diff_no_base_report( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = {"message": "Coverage not affected", "state": "success"} result = notifier.build_payload(comparison) @@ -2261,6 +2317,7 @@ def test_build_payload_without_base_report( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "message": "No report found to compare against", @@ -2286,6 +2343,7 @@ def test_build_payload_with_multiple_changes( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "message": "50.00% of diff hit (target 76.92%)", @@ -2306,6 +2364,7 @@ def test_build_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "message": "No indirect coverage changes found", @@ -2324,6 +2383,7 @@ def test_build_upgrade_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, decoration_type=Decoration.upgrade, ) expected_result = { @@ -2350,6 +2410,7 @@ def test_build_payload_with_multiple_changes( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "message": "3 files have indirect coverage changes not visible in diff", @@ -2372,6 +2433,7 @@ def test_build_payload_without_base_report( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) expected_result = { "message": "Unable to determine changes, no report found at pull request base", @@ -2393,8 +2455,8 @@ def test_notify_path_filter( notifier_yaml_settings={"paths": ["file_1.go"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, ) - base_commit = sample_comparison.project_coverage_base.commit expected_result = { "message": "No indirect coverage changes found", "state": "success", @@ -2414,6 +2476,7 @@ def test_build_passing_empty_upload_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, decoration_type=Decoration.passing_empty_upload, ) expected_result = {"state": "success", "message": "Non-testable files changed."} diff --git a/services/notification/notifiers/tests/unit/test_webhook.py b/services/notification/notifiers/tests/unit/test_webhook.py index 0f8470003..f46b9820a 100644 --- a/services/notification/notifiers/tests/unit/test_webhook.py +++ b/services/notification/notifiers/tests/unit/test_webhook.py @@ -3,6 +3,7 @@ from database.tests.factories import CommitFactory, RepositoryFactory from services.comparison.types import FullCommit from services.notification.notifiers.webhook import WebhookNotifier +from services.repository import get_repo_provider_service class TestWebhookNotifier(object): @@ -28,6 +29,9 @@ def test_build_commit_payload( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={}, + repository_service=get_repo_provider_service( + sample_comparison.head.commit.repository + ), ) repository = base_commit.repository comparison = sample_comparison @@ -88,6 +92,9 @@ def test_build_commit_payload_gitlab( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={}, + repository_service=get_repo_provider_service( + sample_comparison.head.commit.repository + ), ) repository = base_commit.repository comparison = sample_comparison @@ -146,6 +153,7 @@ def test_build_commit_payload_no_author( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={}, + repository_service=get_repo_provider_service(repository), ) result = notifier.build_commit_payload(head_full_commit) expected_result = { @@ -194,6 +202,9 @@ def test_build_payload(self, dbsession, mock_configuration, sample_comparison): notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={}, + repository_service=get_repo_provider_service( + sample_comparison.head.commit.repository + ), ) repository = base_commit.repository comparison = sample_comparison @@ -317,6 +328,9 @@ def test_build_payload_higher_precision( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={"coverage": {"precision": 5, "round": "up"}}, + repository_service=get_repo_provider_service( + sample_comparison.head.commit.repository + ), ) repository = base_commit.repository comparison = sample_comparison @@ -433,6 +447,9 @@ def test_build_payload_without_pull( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={}, + repository_service=get_repo_provider_service( + comparison.head.commit.repository + ), ) result = notifier.build_payload(comparison) expected_result = { @@ -526,7 +543,9 @@ def test_build_payload_without_pull( assert result == expected_result def test_build_payload_without_base_report( - self, sample_comparison_without_base_report, mock_configuration + self, + sample_comparison_without_base_report, + mock_configuration, ): mock_configuration.params["setup"]["codecov_dashboard_url"] = "test.example.br" comparison = sample_comparison_without_base_report @@ -538,6 +557,9 @@ def test_build_payload_without_base_report( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={}, + repository_service=get_repo_provider_service( + comparison.head.commit.repository + ), ) result = notifier.build_payload(comparison) head_commit = comparison.head.commit @@ -627,7 +649,9 @@ def test_build_payload_without_base_report( assert result == expected_result def test_build_payload_without_base( - self, sample_comparison_without_base_with_pull, mock_configuration + self, + sample_comparison_without_base_with_pull, + mock_configuration, ): mock_configuration.params["setup"]["codecov_dashboard_url"] = "test.example.br" comparison = sample_comparison_without_base_with_pull @@ -639,6 +663,9 @@ def test_build_payload_without_base( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml={}, + repository_service=get_repo_provider_service( + comparison.head.commit.repository + ), ) result = notifier.build_payload(comparison) head_commit = comparison.head.commit diff --git a/services/notification/tests/unit/test_commit_notifications.py b/services/notification/tests/unit/test_commit_notifications.py index 9b91bea8b..5105b07e0 100644 --- a/services/notification/tests/unit/test_commit_notifications.py +++ b/services/notification/tests/unit/test_commit_notifications.py @@ -61,6 +61,7 @@ def test_create_or_update_commit_notification_not_yet_exists( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=None, decoration_type=Decoration.standard, ) notify_res = NotificationResult( @@ -94,6 +95,7 @@ def test_create_or_update_commit_notification_not_yet_exists_no_pull_but_ghapp_i notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=None, decoration_type=Decoration.standard, ) notify_res = NotificationResult( @@ -125,6 +127,7 @@ def test_create_or_update_commit_notification_no_result( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=None, decoration_type=Decoration.standard, ) result_dict = None @@ -157,6 +160,7 @@ def test_create_or_update_commit_notification_decoration_change( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=None, decoration_type=Decoration.standard, ) notify_res = NotificationResult( @@ -195,6 +199,7 @@ def test_create_or_update_commit_notification_now_successful( notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"}, notifier_site_settings=True, current_yaml={}, + repository_service=None, decoration_type=Decoration.standard, ) notify_res = NotificationResult( diff --git a/services/notification/tests/unit/test_notification_service.py b/services/notification/tests/unit/test_notification_service.py index 9a64bf15a..6a4f71a77 100644 --- a/services/notification/tests/unit/test_notification_service.py +++ b/services/notification/tests/unit/test_notification_service.py @@ -60,7 +60,7 @@ class TestNotificationService(object): def test_should_use_checks_notifier_yaml_field_false(self, dbsession): repository = RepositoryFactory.create() current_yaml = {"github_checks": False} - service = NotificationService(repository, current_yaml) + service = NotificationService(repository, current_yaml, None) assert ( service._should_use_checks_notifier(status_type=StatusType.PROJECT.value) == False @@ -109,7 +109,7 @@ def test_should_use_checks_notifier_deprecated_flow( repository = RepositoryFactory.create(**repo_data) current_yaml = {"github_checks": True} assert repository.owner.github_app_installations == [] - service = NotificationService(repository, current_yaml) + service = NotificationService(repository, current_yaml, None) assert ( service._should_use_checks_notifier(status_type=StatusType.PROJECT.value) == outcome @@ -127,7 +127,7 @@ def test_should_use_checks_notifier_ghapp_all_repos_covered(self, dbsession): dbsession.flush() current_yaml = {"github_checks": True} assert repository.owner.github_app_installations == [ghapp_installation] - service = NotificationService(repository, current_yaml) + service = NotificationService(repository, current_yaml, None) assert ( service._should_use_checks_notifier(status_type=StatusType.PROJECT.value) == True @@ -147,7 +147,7 @@ def test_use_checks_notifier_for_team_plan(self, dbsession): dbsession.flush() current_yaml = {"github_checks": True} assert repository.owner.github_app_installations == [ghapp_installation] - service = NotificationService(repository, current_yaml) + service = NotificationService(repository, current_yaml, None) assert ( service._should_use_checks_notifier(status_type=StatusType.PROJECT.value) == False @@ -175,7 +175,7 @@ def test_use_status_notifier_for_team_plan(self, dbsession): dbsession.flush() current_yaml = {"github_checks": True} assert repository.owner.github_app_installations == [ghapp_installation] - service = NotificationService(repository, current_yaml) + service = NotificationService(repository, current_yaml, None) assert ( service._should_use_status_notifier(status_type=StatusType.PROJECT.value) == False @@ -203,7 +203,7 @@ def test_use_status_notifier_for_non_team_plan(self, dbsession): dbsession.flush() current_yaml = {"github_checks": True} assert repository.owner.github_app_installations == [ghapp_installation] - service = NotificationService(repository, current_yaml) + service = NotificationService(repository, current_yaml, None) assert ( service._should_use_status_notifier(status_type=StatusType.PROJECT.value) == True @@ -239,13 +239,16 @@ def test_should_use_checks_notifier_ghapp_some_repos_covered( current_yaml = {"github_checks": True} assert repository.owner.github_app_installations == [ghapp_installation] service = NotificationService( - repository, current_yaml, gh_installation_name_to_use=gh_installation_name + repository, + current_yaml, + None, + gh_installation_name_to_use=gh_installation_name, ) assert ( service._should_use_checks_notifier(status_type=StatusType.PROJECT.value) == True ) - service = NotificationService(other_repo_same_owner, current_yaml) + service = NotificationService(other_repo_same_owner, current_yaml, None) assert ( service._should_use_checks_notifier(status_type=StatusType.PROJECT.value) == False @@ -268,7 +271,7 @@ def test_get_notifiers_instances_only_third_party( current_yaml = { "coverage": {"notify": {"slack": {"default": {"field": "1y ago"}}}} } - service = NotificationService(repository, current_yaml) + service = NotificationService(repository, current_yaml, None) instances = list(service.get_notifiers_instances()) assert len(instances) == 2 instance = instances[0] @@ -297,7 +300,7 @@ def test_get_notifiers_instances_checks( mocker.patch.dict( os.environ, {"CHECKS_WHITELISTED_OWNERS": f"0,{repository.owner.ownerid}"} ) - service = NotificationService(repository, current_yaml) + service = NotificationService(repository, current_yaml, None) instances = list(service.get_notifiers_instances()) names = sorted([instance.name for instance in instances]) assert names == [ @@ -335,7 +338,9 @@ def test_get_notifiers_instances_checks_percentage_whitelist( }, ) service = NotificationService( - repository, current_yaml, gh_installation_name_to_use=gh_installation_name + repository, + current_yaml, + gh_installation_name, ) instances = list(service.get_notifiers_instances()) # we don't need that for slack-app notifier @@ -348,15 +353,13 @@ def test_get_notifiers_instances_checks_percentage_whitelist( for instance in instances: if isinstance(instance, ChecksWithFallback): assert ( - instance._checks_notifier.gh_installation_name - == gh_installation_name + instance._checks_notifier.repository_service == gh_installation_name ) assert ( - instance._status_notifier.gh_installation_name - == gh_installation_name + instance._status_notifier.repository_service == gh_installation_name ) else: - assert instance.gh_installation_name == gh_installation_name + assert instance.repository_service == gh_installation_name @pytest.mark.parametrize( "gh_installation_name", @@ -377,11 +380,13 @@ def test_get_notifiers_instances_comment( dbsession.flush() current_yaml = {"comment": {"layout": "condensed_header"}, "slack_app": False} service = NotificationService( - repository, current_yaml, gh_installation_name_to_use=gh_installation_name + repository, + current_yaml, + gh_installation_name, ) instances = list(service.get_notifiers_instances()) assert len(instances) == 1 - assert instances[0].gh_installation_name == gh_installation_name + assert instances[0].repository_service == gh_installation_name def test_notify_general_exception(self, mocker, dbsession, sample_comparison): current_yaml = {} @@ -421,7 +426,9 @@ def test_notify_general_exception(self, mocker, dbsession, sample_comparison): "get_notifiers_instances", return_value=[bad_notifier, good_notifier, disabled_notifier], ) - notifications_service = NotificationService(commit.repository, current_yaml) + notifications_service = NotificationService( + commit.repository, current_yaml, None + ) expected_result = [ {"notifier": "bad_name", "title": "bad_notifier", "result": None}, { @@ -449,7 +456,9 @@ def test_notify_individual_notifier_timeout(self, mocker, sample_comparison): decoration_type=Decoration.standard, ) notifier.notify.side_effect = AsyncioTimeoutError - notifications_service = NotificationService(commit.repository, current_yaml) + notifications_service = NotificationService( + commit.repository, current_yaml, None + ) res = notifications_service.notify_individual_notifier( notifier, sample_comparison ) @@ -482,8 +491,11 @@ def test_notify_individual_checks_notifier( notifier_yaml_settings={"flags": ["flagone"]}, notifier_site_settings=True, current_yaml=UserYaml({}), + repository_service=mock_repo_provider, + ) + notifications_service = NotificationService( + commit.repository, current_yaml, mock_repo_provider ) - notifications_service = NotificationService(commit.repository, current_yaml) res = notifications_service.notify_individual_notifier( notifier, sample_comparison ) @@ -519,7 +531,9 @@ def test_notify_individual_notifier_timeout_notification_created( decoration_type=Decoration.standard, ) notifier.notify.side_effect = AsyncioTimeoutError - notifications_service = NotificationService(commit.repository, current_yaml) + notifications_service = NotificationService( + commit.repository, current_yaml, None + ) res = notifications_service.notify_individual_notifier( notifier, sample_comparison ) @@ -552,7 +566,9 @@ def test_notify_individual_notifier_notification_created_then_updated( ) # first attempt not successful notifier.notify.side_effect = AsyncioTimeoutError - notifications_service = NotificationService(commit.repository, current_yaml) + notifications_service = NotificationService( + commit.repository, current_yaml, None + ) res = notifications_service.notify_individual_notifier( notifier, sample_comparison ) @@ -596,7 +612,9 @@ def test_notify_individual_notifier_cancellation(self, mocker, sample_comparison decoration_type=Decoration.standard, ) notifier.notify.side_effect = CancelledError() - notifications_service = NotificationService(commit.repository, current_yaml) + notifications_service = NotificationService( + commit.repository, current_yaml, None + ) with pytest.raises(CancelledError): notifications_service.notify_individual_notifier( notifier, sample_comparison @@ -658,7 +676,9 @@ def test_notify_timeout_exception(self, mocker, dbsession, sample_comparison): no_attempt_notifier, ], ) - notifications_service = NotificationService(commit.repository, current_yaml) + notifications_service = NotificationService( + commit.repository, current_yaml, None + ) with pytest.raises(SoftTimeLimitExceeded): notifications_service.notify(sample_comparison) @@ -683,7 +703,9 @@ def test_not_licensed_enterprise(self, mocker, dbsession, sample_comparison): ) current_yaml = {} commit = sample_comparison.head.commit - notifications_service = NotificationService(commit.repository, current_yaml) + notifications_service = NotificationService( + commit.repository, current_yaml, None + ) expected_result = [] res = notifications_service.notify(sample_comparison) assert expected_result == res @@ -706,7 +728,7 @@ def test_get_statuses(self, mocker, dbsession, sample_comparison): } commit = sample_comparison.head.commit notifications_service = NotificationService( - commit.repository, UserYaml(current_yaml) + commit.repository, UserYaml(current_yaml), None ) expected_result = [ ("project", "default", {}), @@ -756,7 +778,7 @@ def test_get_component_statuses(self, mocker, dbsession, sample_comparison): } commit = sample_comparison.head.commit notifications_service = NotificationService( - commit.repository, UserYaml(current_yaml) + commit.repository, UserYaml(current_yaml), None ) expected_result = [ ( diff --git a/tasks/save_report_results.py b/tasks/save_report_results.py index b66148ce9..38630f27b 100644 --- a/tasks/save_report_results.py +++ b/tasks/save_report_results.py @@ -10,7 +10,7 @@ from database.models.reports import CommitReport, ReportResults from helpers.exceptions import RepositoryWithoutValidBotError from helpers.github_installation import get_installation_name_for_owner_for_task -from services.comparison import ComparisonProxy +from services.comparison import ComparisonContext, ComparisonProxy from services.comparison.types import Comparison, FullCommit from services.notification.notifiers.status.patch import PatchStatusNotifier from services.report import ReportService @@ -74,7 +74,8 @@ def run_impl( commit=base_commit, report=base_report ), patch_coverage_base_commitid=patch_coverage_base_commitid, - ) + ), + ComparisonContext(repository_service=repository_service), ) notifier = PatchStatusNotifier( @@ -83,6 +84,7 @@ def run_impl( notifier_yaml_settings={}, notifier_site_settings=True, current_yaml=current_yaml, + repository_service=repository_service, ) result = notifier.build_payload(comparison) report = self.fetch_report(commit, report_code) diff --git a/tasks/tests/unit/test_notify_task.py b/tasks/tests/unit/test_notify_task.py index 261c57771..f4de6735a 100644 --- a/tasks/tests/unit/test_notify_task.py +++ b/tasks/tests/unit/test_notify_task.py @@ -43,7 +43,11 @@ ) from services.report import ReportService from services.repository import EnrichedPull -from tasks.notify import NotifyTask +from tasks.notify import ( + NotifyTask, + _possibly_pin_commit_to_github_app, + _possibly_refresh_previous_selection, +) def _create_checkpoint_logger(mocker): @@ -295,8 +299,7 @@ def test__possibly_refresh_previous_selection( "tasks.notify.set_github_app_for_commit" ) mocker.patch("tasks.notify.get_github_app_for_commit", return_value=cached_id) - task = NotifyTask() - assert task._possibly_refresh_previous_selection(commit) == True + assert _possibly_refresh_previous_selection(commit) == True mock_set_gh_app_for_commit.assert_called_with(app_to_save, commit) def test__possibly_refresh_previous_selection_false(self, mocker, dbsession): @@ -307,8 +310,7 @@ def test__possibly_refresh_previous_selection_false(self, mocker, dbsession): mock_set_gh_app_for_commit = mocker.patch( "tasks.notify.set_github_app_for_commit" ) - task = NotifyTask() - assert task._possibly_refresh_previous_selection(commit) == False + assert _possibly_refresh_previous_selection(commit) == False mock_set_gh_app_for_commit.assert_not_called() def test_possibly_pin_commit_to_github_app_not_github_or_no_installation( @@ -318,28 +320,27 @@ def test_possibly_pin_commit_to_github_app_not_github_or_no_installation( commit_from_gh = CommitFactory(repository__owner__service="github") dbsession.add_all([commit, commit_from_gh]) dbsession.flush() - mock_refresh_selection = mocker.patch.object( - NotifyTask, "_possibly_refresh_previous_selection", return_value=None + mock_refresh_selection = mocker.patch( + "tasks.notify._possibly_refresh_previous_selection", return_value=None ) torngit = MagicMock(data=TorngitInstanceData()) torngit_with_installation = MagicMock( data=TorngitInstanceData(installation=GithubInstallationInfo(id=12)) ) - task = NotifyTask() assert ( - task._possibly_pin_commit_to_github_app(commit, torngit_with_installation) + _possibly_pin_commit_to_github_app(commit, torngit_with_installation) is None ) mock_refresh_selection.assert_not_called() - assert task._possibly_pin_commit_to_github_app(commit_from_gh, torngit) is None + assert _possibly_pin_commit_to_github_app(commit_from_gh, torngit) is None mock_refresh_selection.assert_called_with(commit_from_gh) def test_possibly_pin_commit_to_github_app_new_selection(self, mocker, dbsession): commit = CommitFactory(repository__owner__service="github") dbsession.add(commit) dbsession.flush() - mock_refresh_selection = mocker.patch.object( - NotifyTask, "_possibly_refresh_previous_selection", return_value=None + mock_refresh_selection = mocker.patch( + "tasks.notify._possibly_refresh_previous_selection", return_value=None ) mock_set_gh_app_for_commit = mocker.patch( "tasks.notify.set_github_app_for_commit" @@ -347,8 +348,7 @@ def test_possibly_pin_commit_to_github_app_new_selection(self, mocker, dbsession torngit = MagicMock( data=TorngitInstanceData(installation=GithubInstallationInfo(id=12)) ) - task = NotifyTask() - assert task._possibly_pin_commit_to_github_app(commit, torngit) == 12 + assert _possibly_pin_commit_to_github_app(commit, torngit) == 12 mock_refresh_selection.assert_called_with(commit) mock_set_gh_app_for_commit.assert_called_with(12, commit) @@ -1013,6 +1013,7 @@ def test_submit_third_party_notifications_exception(self, mocker, dbsession): base_report, head_report, enrichedPull, + mocker.MagicMock(), ) assert expected_result == res @@ -1160,6 +1161,7 @@ def test_checkpoints_not_logged_outside_upload_flow( mock_configuration.params["setup"]["codecov_dashboard_url"] = ( "https://codecov.io" ) + mocker.patch("tasks.notify.get_repo_provider_service_for_specific_commit") mocker.patch.object(NotifyTask, "app") mocker.patch.object(NotifyTask, "save_patch_totals") mocker.patch.object(NotifyTask, "should_send_notifications", return_value=True)