diff --git a/services/notification/notifiers/mixins/status.py b/services/notification/notifiers/mixins/status.py index 737933466..064c3440b 100644 --- a/services/notification/notifiers/mixins/status.py +++ b/services/notification/notifiers/mixins/status.py @@ -303,6 +303,7 @@ def _apply_adjust_base_behavior( base_adjusted_coverage = ( Decimal(base_adjusted_hits / base_adjusted_totals) * 100 ) + threshold = self._get_threshold() head_coverage = Decimal(comparison.head.report.totals.coverage) log.info( @@ -310,12 +311,15 @@ def _apply_adjust_base_behavior( extra=dict( commit=comparison.head.commit.commitid, base_adjusted_coverage=base_adjusted_coverage, + threshold=threshold, head_coverage=head_coverage, hits_removed=hits_removed, misses_removed=misses_removed, partials_removed=partials_removed, ), ) + base_adjusted_coverage = base_adjusted_coverage - threshold + # the head coverage is rounded to five digits after the dot, using shared.helpers.numeric.ratio # so we should round the base adjusted coverage to the same amount of digits after the dot # Decimal.quantize: https://docs.python.org/3/library/decimal.html#decimal.Decimal.quantize @@ -331,13 +335,15 @@ def _apply_adjust_base_behavior( 0, round_number(self.current_yaml, head_coverage - base_adjusted_coverage), ) + message = f", passed because coverage increased by {rounded_difference}% when compared to adjusted base ({rounded_base_adjusted_coverage}%)" return ( ( StatusState.success.value, - f", passed because coverage increased by {rounded_difference}% when compared to adjusted base ({rounded_base_adjusted_coverage}%)", + message, ), helper_text, ) + # use rounded numbers for messages coverage_rounded = round_number(self.current_yaml, head_coverage) diff --git a/services/notification/notifiers/tests/unit/test_status.py b/services/notification/notifiers/tests/unit/test_status.py index b29d3b5f7..3bba7f05a 100644 --- a/services/notification/notifiers/tests/unit/test_status.py +++ b/services/notification/notifiers/tests/unit/test_status.py @@ -1639,7 +1639,7 @@ def test_adjust_base_behavior( ), head=FullCommit(commit=CommitFactory(), report=Report(totals=head_totals)), ) - settings = {"target": "auto"} + settings = {"target": "auto", "threshold": "0"} status_mixin = ProjectStatusNotifier( repository="repo", title="fake-notifier", @@ -1698,6 +1698,54 @@ def test_notify_pass_adjust_base_behavior( assert result == expected_result mock_get_impacted_files.assert_called() + def test_notify_pass_adjust_base_behavior_with_threshold( + self, mock_configuration, sample_comparison_negative_change, mocker + ): + sample_comparison = sample_comparison_negative_change + mock_get_impacted_files = mocker.patch.object( + ComparisonProxy, + "get_impacted_files", + return_value={ + "files": [ + { + "base_name": "tests/file1.py", + "head_name": "tests/file1.py", + # Not complete, but we only care about these fields + "removed_diff_coverage": [[1, "h"], [3, "h"], [4, "m"]], + "added_diff_coverage": [], + "unexpected_line_changes": [], + }, + { + "base_name": "tests/file2.go", + "head_name": "tests/file2.go", + "removed_diff_coverage": [[1, "h"]], + "added_diff_coverage": [], + "unexpected_line_changes": [], + }, + ], + }, + ) + mock_configuration.params["setup"]["codecov_dashboard_url"] = "test.example.br" + notifier = ProjectStatusNotifier( + repository=sample_comparison.head.commit.repository, + title="title", + notifier_yaml_settings={ + "removed_code_behavior": "adjust_base", + "threshold": "5", + }, + 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 5.00% when compared to adjusted base (45.00%)", + "state": "success", + "included_helper_text": {}, + } + result = notifier.build_payload(sample_comparison) + assert result == expected_result + mock_get_impacted_files.assert_called() + def test_notify_removed_code_behavior_fail( self, mock_configuration, sample_comparison, mocker ):