Skip to content

Commit a379de0

Browse files
committed
convert constants to enums
1 parent cc21bb5 commit a379de0

File tree

4 files changed

+69
-63
lines changed

4 files changed

+69
-63
lines changed

services/notification/notifiers/mixins/status.py

+43-33
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,37 @@ class StatusResult(TypedDict):
2525
included_helper_text: dict[str, str]
2626

2727

28-
CUSTOM_TARGET_TEXT_PATCH_KEY = "custom_target_helper_text_patch"
29-
CUSTOM_TARGET_TEXT_PROJECT_KEY = "custom_target_helper_text_project"
30-
CUSTOM_RCB_INDIRECT_CHANGES_KEY = "custom_rcb_indirect_changes_helper_text"
31-
CUSTOM_RCB_ADJUST_BASE_KEY = "custom_rcb_adjust_base_helper_text"
32-
CUSTOM_TARGET_TEXT_VALUE = (
33-
"Your {context} {notification_type} has failed because the {point_of_comparison} coverage ({coverage}%) is below the target coverage ({target}%). "
34-
"You can increase the {point_of_comparison} coverage or adjust the "
35-
"[target](https://docs.codecov.com/docs/commit-status#target) coverage."
36-
)
37-
CUSTOM_RCB_INDIRECT_CHANGES_VALUE = (
38-
"Your {context} {notification_type} has failed because you have indirect coverage changes. "
39-
"Learn more about [Unexpected Coverage Changes](https://docs.codecov.com/docs/unexpected-coverage-changes) "
40-
"and [reasons for indirect coverage changes](https://docs.codecov.com/docs/unexpected-coverage-changes#reasons-for-indirect-changes)."
41-
)
42-
CUSTOM_RCB_TEXT_VALUE = (
43-
"Your project {notification_type} has failed because the head coverage ({coverage}%) "
44-
"is below the [adjusted base coverage](https://docs.codecov.com/docs/removed-code-behavior#option-3-default-adjust_base) ({adjusted_base_cov}%). "
45-
"You can increase the head coverage or adjust the "
46-
"[Removed Code Behavior](https://docs.codecov.com/docs/removed-code-behavior)."
47-
)
28+
class HelperTextKey(str, Enum):
29+
CUSTOM_TARGET_PATCH = "custom_target_helper_text_patch"
30+
CUSTOM_TARGET_PROJECT = "custom_target_helper_text_project"
31+
RCB_INDIRECT_CHANGES = "rcb_indirect_changes_helper_text"
32+
RCB_ADJUST_BASE = "rcb_adjust_base_helper_text"
33+
34+
35+
class HelperTextTemplate(str, Enum):
36+
CUSTOM_TARGET = (
37+
"Your {context} {notification_type} has failed because the {point_of_comparison} coverage ({coverage}%) is below the target coverage ({target}%). "
38+
"You can increase the {point_of_comparison} coverage or adjust the "
39+
"[target](https://docs.codecov.com/docs/commit-status#target) coverage."
40+
)
41+
INDIRECT_CHANGES = (
42+
"Your {context} {notification_type} has failed because you have indirect coverage changes. "
43+
"Learn more about [Unexpected Coverage Changes](https://docs.codecov.com/docs/unexpected-coverage-changes) "
44+
"and [reasons for indirect coverage changes](https://docs.codecov.com/docs/unexpected-coverage-changes#reasons-for-indirect-changes)."
45+
)
46+
RCB_ADJUST_BASE = (
47+
"Your project {notification_type} has failed because the head coverage ({coverage}%) "
48+
"is below the [adjusted base coverage](https://docs.codecov.com/docs/removed-code-behavior#option-3-default-adjust_base) ({adjusted_base_cov}%). "
49+
"You can increase the head coverage or adjust the "
50+
"[Removed Code Behavior](https://docs.codecov.com/docs/removed-code-behavior)."
51+
)
4852

4953

5054
HELPER_TEXT_MAP = {
51-
CUSTOM_TARGET_TEXT_PATCH_KEY: CUSTOM_TARGET_TEXT_VALUE,
52-
CUSTOM_TARGET_TEXT_PROJECT_KEY: CUSTOM_TARGET_TEXT_VALUE,
53-
CUSTOM_RCB_INDIRECT_CHANGES_KEY: CUSTOM_RCB_INDIRECT_CHANGES_VALUE,
54-
CUSTOM_RCB_ADJUST_BASE_KEY: CUSTOM_RCB_TEXT_VALUE,
55+
HelperTextKey.CUSTOM_TARGET_PATCH: HelperTextTemplate.CUSTOM_TARGET,
56+
HelperTextKey.CUSTOM_TARGET_PROJECT: HelperTextTemplate.CUSTOM_TARGET,
57+
HelperTextKey.RCB_INDIRECT_CHANGES: HelperTextTemplate.INDIRECT_CHANGES,
58+
HelperTextKey.RCB_ADJUST_BASE: HelperTextTemplate.RCB_ADJUST_BASE,
5559
}
5660

5761

@@ -130,14 +134,18 @@ def get_patch_status(
130134
f"{coverage_rounded}% of diff hit (target {target_rounded}%)"
131135
)
132136
if state == StatusState.failure.value and is_custom_target:
133-
helper_text = HELPER_TEXT_MAP[CUSTOM_TARGET_TEXT_PATCH_KEY].format(
137+
helper_text = HELPER_TEXT_MAP[
138+
HelperTextKey.CUSTOM_TARGET_PATCH
139+
].value.format(
134140
context=self.context,
135141
notification_type=notification_type,
136142
point_of_comparison=self.context,
137143
coverage=coverage_rounded,
138144
target=target_rounded,
139145
)
140-
included_helper_text[CUSTOM_TARGET_TEXT_PATCH_KEY] = helper_text
146+
included_helper_text[HelperTextKey.CUSTOM_TARGET_PATCH.value] = (
147+
helper_text
148+
)
141149
return StatusResult(
142150
state=state, message=message, included_helper_text=included_helper_text
143151
)
@@ -334,9 +342,9 @@ def _apply_adjust_base_behavior(
334342
coverage_rounded = round_number(self.current_yaml, head_coverage)
335343

336344
# their comparison failed despite the adjusted base, give them helper text about it
337-
helper_text[CUSTOM_RCB_ADJUST_BASE_KEY] = HELPER_TEXT_MAP[
338-
CUSTOM_RCB_ADJUST_BASE_KEY
339-
].format(
345+
helper_text[HelperTextKey.RCB_ADJUST_BASE.value] = HELPER_TEXT_MAP[
346+
HelperTextKey.RCB_ADJUST_BASE
347+
].value.format(
340348
notification_type=notification_type,
341349
coverage=coverage_rounded,
342350
adjusted_base_cov=rounded_base_adjusted_coverage,
@@ -370,8 +378,8 @@ def _apply_fully_covered_patch_behavior(
370378
)
371379

372380
# their comparison failed because of unexpected/indirect changes, give them helper text about it
373-
helper_text[CUSTOM_RCB_INDIRECT_CHANGES_KEY] = HELPER_TEXT_MAP[
374-
CUSTOM_RCB_INDIRECT_CHANGES_KEY
381+
helper_text[HelperTextKey.RCB_INDIRECT_CHANGES] = HELPER_TEXT_MAP[
382+
HelperTextKey.RCB_INDIRECT_CHANGES
375383
].format(
376384
context=self.context,
377385
notification_type=notification_type,
@@ -550,14 +558,16 @@ def _get_project_status(
550558
target_rounded = round_number(self.current_yaml, target_coverage)
551559
message = f"{head_coverage_rounded}% (target {target_rounded}%)"
552560
if state == StatusState.failure.value:
553-
helper_text = HELPER_TEXT_MAP[CUSTOM_TARGET_TEXT_PROJECT_KEY].format(
561+
helper_text = HELPER_TEXT_MAP[
562+
HelperTextKey.CUSTOM_TARGET_PROJECT
563+
].value.format(
554564
context=self.context,
555565
notification_type=notification_type,
556566
point_of_comparison=self.point_of_comparison,
557567
coverage=head_coverage_rounded,
558568
target=target_rounded,
559569
)
560-
included_helper_text[CUSTOM_TARGET_TEXT_PROJECT_KEY] = helper_text
570+
included_helper_text[HelperTextKey.CUSTOM_TARGET_PROJECT] = helper_text
561571
return StatusResult(
562572
state=state, message=message, included_helper_text=included_helper_text
563573
)

services/notification/notifiers/tests/unit/test_checks.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
ChecksWithFallback,
2222
)
2323
from services.notification.notifiers.mixins.status import (
24-
CUSTOM_TARGET_TEXT_PATCH_KEY,
25-
CUSTOM_TARGET_TEXT_VALUE,
24+
HelperTextKey,
25+
HelperTextTemplate,
2626
)
2727
from services.notification.notifiers.status import PatchStatusNotifier
2828
from tests.helpers import mock_all_plans_and_tiers
@@ -798,7 +798,7 @@ def test_build_payload_target_coverage_failure(
798798
"annotations": [],
799799
},
800800
"included_helper_text": {
801-
CUSTOM_TARGET_TEXT_PATCH_KEY: CUSTOM_TARGET_TEXT_VALUE.format(
801+
HelperTextKey.CUSTOM_TARGET_PATCH: HelperTextTemplate.CUSTOM_TARGET.format(
802802
context="patch",
803803
notification_type="check",
804804
point_of_comparison="patch",

services/notification/notifiers/tests/unit/test_status.py

+17-20
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@
2121
from services.decoration import Decoration
2222
from services.notification.notifiers.base import NotificationResult
2323
from services.notification.notifiers.mixins.status import (
24-
CUSTOM_RCB_INDIRECT_CHANGES_KEY,
25-
CUSTOM_RCB_ADJUST_BASE_KEY,
26-
CUSTOM_TARGET_TEXT_PATCH_KEY,
27-
CUSTOM_TARGET_TEXT_PROJECT_KEY,
28-
CUSTOM_TARGET_TEXT_VALUE,
2924
HELPER_TEXT_MAP,
25+
HelperTextKey,
26+
HelperTextTemplate,
3027
)
3128
from services.notification.notifiers.status import (
3229
ChangesStatusNotifier,
@@ -1611,9 +1608,9 @@ def test_notify_pass_via_removals_only_behavior(
16111608
(
16121609
None,
16131610
{
1614-
CUSTOM_RCB_ADJUST_BASE_KEY: HELPER_TEXT_MAP[
1615-
CUSTOM_RCB_ADJUST_BASE_KEY
1616-
].format(
1611+
HelperTextKey.RCB_ADJUST_BASE.value: HELPER_TEXT_MAP[
1612+
HelperTextKey.RCB_ADJUST_BASE
1613+
].value.format(
16171614
notification_type="status",
16181615
coverage=94.27,
16191616
adjusted_base_cov=94.28,
@@ -1744,7 +1741,7 @@ def test_notify_removed_code_behavior_fail(
17441741
"message": "60.00% (target 80.00%)",
17451742
"state": "failure",
17461743
"included_helper_text": {
1747-
CUSTOM_TARGET_TEXT_PROJECT_KEY: CUSTOM_TARGET_TEXT_VALUE.format(
1744+
HelperTextKey.CUSTOM_TARGET_PROJECT: HelperTextTemplate.CUSTOM_TARGET.format(
17481745
context="project",
17491746
notification_type="status",
17501747
point_of_comparison="head",
@@ -1798,9 +1795,9 @@ def test_notify_adjust_base_behavior_fail(
17981795
"message": f"50.00% (-10.00%) compared to {sample_comparison.project_coverage_base.commit.commitid[:7]}",
17991796
"state": "failure",
18001797
"included_helper_text": {
1801-
CUSTOM_RCB_ADJUST_BASE_KEY: HELPER_TEXT_MAP[
1802-
CUSTOM_RCB_ADJUST_BASE_KEY
1803-
].format(
1798+
HelperTextKey.RCB_ADJUST_BASE.value: HELPER_TEXT_MAP[
1799+
HelperTextKey.RCB_ADJUST_BASE
1800+
].value.format(
18041801
notification_type="status",
18051802
coverage="50.00",
18061803
adjusted_base_cov=71.43,
@@ -1880,7 +1877,7 @@ def test_notify_adjust_base_behavior_skips_if_target_coverage_defined(
18801877
"message": "50.00% (target 80.00%)",
18811878
"state": "failure",
18821879
"included_helper_text": {
1883-
CUSTOM_TARGET_TEXT_PROJECT_KEY: CUSTOM_TARGET_TEXT_VALUE.format(
1880+
HelperTextKey.CUSTOM_TARGET_PROJECT: HelperTextTemplate.CUSTOM_TARGET.format(
18841881
context="project",
18851882
notification_type="status",
18861883
point_of_comparison="head",
@@ -1912,7 +1909,7 @@ def test_notify_removed_code_behavior_unknown(
19121909
"message": "60.00% (target 80.00%)",
19131910
"state": "failure",
19141911
"included_helper_text": {
1915-
CUSTOM_TARGET_TEXT_PROJECT_KEY: CUSTOM_TARGET_TEXT_VALUE.format(
1912+
HelperTextKey.CUSTOM_TARGET_PROJECT: HelperTextTemplate.CUSTOM_TARGET.format(
19161913
context="project",
19171914
notification_type="status",
19181915
point_of_comparison="head",
@@ -1973,7 +1970,7 @@ def test_notify_fully_covered_patch_behavior_fail(
19731970
"message": "50.00% (target 70.00%)",
19741971
"state": "failure",
19751972
"included_helper_text": {
1976-
CUSTOM_TARGET_TEXT_PROJECT_KEY: CUSTOM_TARGET_TEXT_VALUE.format(
1973+
HelperTextKey.CUSTOM_TARGET_PROJECT: HelperTextTemplate.CUSTOM_TARGET.format(
19771974
context="project",
19781975
notification_type="status",
19791976
point_of_comparison="head",
@@ -2035,16 +2032,16 @@ def test_notify_fully_covered_patch_behavior_fail_indirect_changes(
20352032
"message": "50.00% (target 70.00%)",
20362033
"state": "failure",
20372034
"included_helper_text": {
2038-
CUSTOM_TARGET_TEXT_PROJECT_KEY: CUSTOM_TARGET_TEXT_VALUE.format(
2035+
HelperTextKey.CUSTOM_TARGET_PROJECT.value: HelperTextTemplate.CUSTOM_TARGET.value.format(
20392036
context="project",
20402037
notification_type="status",
20412038
point_of_comparison="head",
20422039
coverage="50.00",
20432040
target="70.00",
20442041
),
2045-
CUSTOM_RCB_INDIRECT_CHANGES_KEY: HELPER_TEXT_MAP[
2046-
CUSTOM_RCB_INDIRECT_CHANGES_KEY
2047-
].format(
2042+
HelperTextKey.RCB_INDIRECT_CHANGES.value: HELPER_TEXT_MAP[
2043+
HelperTextKey.RCB_INDIRECT_CHANGES
2044+
].value.format(
20482045
context="project",
20492046
notification_type="status",
20502047
),
@@ -2329,7 +2326,7 @@ def test_build_payload_target_coverage_failure(
23292326
"message": "66.67% of diff hit (target 70.00%)",
23302327
"state": "failure",
23312328
"included_helper_text": {
2332-
CUSTOM_TARGET_TEXT_PATCH_KEY: CUSTOM_TARGET_TEXT_VALUE.format(
2329+
HelperTextKey.CUSTOM_TARGET_PATCH: HelperTextTemplate.CUSTOM_TARGET.format(
23332330
context="patch",
23342331
notification_type="status",
23352332
point_of_comparison="patch",

services/notification/tests/unit/test_notification_service.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@
3131
ChecksWithFallback,
3232
)
3333
from services.notification.notifiers.mixins.status import (
34-
CUSTOM_TARGET_TEXT_PATCH_KEY,
35-
CUSTOM_TARGET_TEXT_PROJECT_KEY,
36-
CUSTOM_TARGET_TEXT_VALUE,
34+
HelperTextKey,
35+
HelperTextTemplate,
3736
)
3837
from tests.helpers import mock_all_plans_and_tiers
3938

@@ -774,7 +773,7 @@ def test_notify_individual_checks_patch_and_project_notifier_included_helper_tex
774773
"annotations": [],
775774
},
776775
"included_helper_text": {
777-
CUSTOM_TARGET_TEXT_PATCH_KEY: CUSTOM_TARGET_TEXT_VALUE.format(
776+
HelperTextKey.CUSTOM_TARGET_PATCH: HelperTextTemplate.CUSTOM_TARGET.format(
778777
context="patch",
779778
notification_type="check",
780779
point_of_comparison="patch",
@@ -797,7 +796,7 @@ def test_notify_individual_checks_patch_and_project_notifier_included_helper_tex
797796
"annotations": [],
798797
},
799798
"included_helper_text": {
800-
CUSTOM_TARGET_TEXT_PROJECT_KEY: CUSTOM_TARGET_TEXT_VALUE.format(
799+
HelperTextKey.CUSTOM_TARGET_PROJECT: HelperTextTemplate.CUSTOM_TARGET.format(
801800
context="project",
802801
notification_type="check",
803802
point_of_comparison="head",
@@ -866,11 +865,11 @@ def test_notify_individual_checks_patch_and_project_notifier_included_helper_tex
866865
assert (
867866
":x: "
868867
+ checks_patch_result["included_helper_text"][
869-
CUSTOM_TARGET_TEXT_PATCH_KEY
868+
HelperTextKey.CUSTOM_TARGET_PATCH
870869
]
871870
and ":x: "
872871
+ checks_proj_result["included_helper_text"][
873-
CUSTOM_TARGET_TEXT_PROJECT_KEY
872+
HelperTextKey.CUSTOM_TARGET_PROJECT
874873
]
875874
in r["result"].data_sent["message"]
876875
)

0 commit comments

Comments
 (0)