Skip to content

Commit 6a9ab12

Browse files
committed
ref(preprod): Inline wrapper functions and extract build_changes_map
Remove single-line wrapper functions _find_existing_comment_id and _save_pr_comment_result, inlining the calls with the hardcoded "build_distribution" argument directly at the call sites. Rename _build_changes_map to build_changes_map and move it (along with its helper _comparison_has_changes) from the status_checks snapshots tasks module to sentry.preprod.snapshots.utils, since it is now shared across multiple production modules.
1 parent 89cd881 commit 6a9ab12

File tree

5 files changed

+52
-63
lines changed

5 files changed

+52
-63
lines changed

src/sentry/preprod/snapshots/utils.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from __future__ import annotations
22

33
from sentry.preprod.models import PreprodArtifact, PreprodBuildConfiguration
4-
from sentry.preprod.snapshots.models import PreprodSnapshotComparison
4+
from sentry.preprod.snapshots.models import (
5+
PreprodSnapshotComparison,
6+
PreprodSnapshotMetrics,
7+
)
58

69

710
def find_base_snapshot_artifact(
@@ -59,3 +62,37 @@ def find_head_snapshot_artifacts_awaiting_base(
5962
.select_related("preprodsnapshotmetrics")
6063
.order_by("-date_added")
6164
)
65+
66+
67+
def _comparison_has_changes(
68+
comparison: PreprodSnapshotComparison,
69+
fail_on_added: bool = False,
70+
fail_on_removed: bool = True,
71+
) -> bool:
72+
return (
73+
comparison.images_changed > 0
74+
or comparison.images_renamed > 0
75+
or (fail_on_added and comparison.images_added > 0)
76+
or (fail_on_removed and comparison.images_removed > 0)
77+
)
78+
79+
80+
def build_changes_map(
81+
artifacts: list[PreprodArtifact],
82+
snapshot_metrics_map: dict[int, PreprodSnapshotMetrics],
83+
comparisons_map: dict[int, PreprodSnapshotComparison],
84+
fail_on_added: bool = False,
85+
fail_on_removed: bool = True,
86+
) -> dict[int, bool]:
87+
changes_map: dict[int, bool] = {}
88+
for artifact in artifacts:
89+
metrics = snapshot_metrics_map.get(artifact.id)
90+
if not metrics:
91+
continue
92+
comparison = comparisons_map.get(metrics.id)
93+
if not comparison or comparison.state != PreprodSnapshotComparison.State.SUCCESS:
94+
continue
95+
changes_map[artifact.id] = _comparison_has_changes(
96+
comparison, fail_on_added, fail_on_removed
97+
)
98+
return changes_map

src/sentry/preprod/vcs/pr_comments/snapshot_tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
from sentry.preprod.integration_utils import get_commit_context_client
1212
from sentry.preprod.models import PreprodArtifact, PreprodComparisonApproval
1313
from sentry.preprod.snapshots.models import PreprodSnapshotComparison, PreprodSnapshotMetrics
14+
from sentry.preprod.snapshots.utils import build_changes_map
1415
from sentry.preprod.vcs.pr_comments.snapshot_templates import format_snapshot_pr_comment
1516
from sentry.preprod.vcs.pr_comments.tasks import find_existing_comment_id, save_pr_comment_result
1617
from sentry.preprod.vcs.status_checks.snapshots.tasks import (
1718
FAIL_ON_ADDED_OPTION_KEY,
1819
FAIL_ON_REMOVED_OPTION_KEY,
19-
_build_changes_map,
2020
)
2121
from sentry.shared_integrations.exceptions import ApiError
2222
from sentry.silo.base import SiloMode
@@ -157,7 +157,7 @@ def create_preprod_snapshot_pr_comment_task(
157157

158158
fail_on_added = artifact.project.get_option(FAIL_ON_ADDED_OPTION_KEY, default=False)
159159
fail_on_removed = artifact.project.get_option(FAIL_ON_REMOVED_OPTION_KEY, default=True)
160-
changes_map = _build_changes_map(
160+
changes_map = build_changes_map(
161161
all_artifacts,
162162
snapshot_metrics_map,
163163
comparisons_map,

src/sentry/preprod/vcs/pr_comments/tasks.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def create_preprod_pr_comment_task(
129129
if not installable_siblings:
130130
return
131131

132-
existing_comment_id = _find_existing_comment_id(all_for_pr)
132+
existing_comment_id = find_existing_comment_id(all_for_pr, "build_distribution")
133133
comment_body = format_pr_comment(installable_siblings, project=artifact.project)
134134

135135
try:
@@ -165,30 +165,15 @@ def create_preprod_pr_comment_task(
165165
if isinstance(e, ApiError):
166166
extra["status_code"] = e.code
167167
logger.exception("preprod.pr_comments.create.failed", extra=extra)
168-
_save_pr_comment_result(cc, success=False, error=e)
168+
save_pr_comment_result(cc, "build_distribution", success=False, error=e)
169169
api_error = e
170170
else:
171-
_save_pr_comment_result(cc, success=True, comment_id=comment_id)
171+
save_pr_comment_result(cc, "build_distribution", success=True, comment_id=comment_id)
172172

173173
if api_error is not None:
174174
raise api_error
175175

176176

177-
def _find_existing_comment_id(
178-
comparisons: Sequence[CommitComparison],
179-
) -> str | None:
180-
return find_existing_comment_id(comparisons, "build_distribution")
181-
182-
183-
def _save_pr_comment_result(
184-
commit_comparison: CommitComparison,
185-
success: bool,
186-
comment_id: str | None = None,
187-
error: Exception | None = None,
188-
) -> None:
189-
save_pr_comment_result(commit_comparison, "build_distribution", success, comment_id, error)
190-
191-
192177
def find_existing_comment_id(
193178
comparisons: Sequence[CommitComparison],
194179
comment_type: str,

src/sentry/preprod/vcs/status_checks/snapshots/tasks.py

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
PreprodComparisonApproval,
1313
)
1414
from sentry.preprod.snapshots.models import PreprodSnapshotComparison, PreprodSnapshotMetrics
15+
from sentry.preprod.snapshots.utils import build_changes_map
1516
from sentry.preprod.url_utils import get_preprod_artifact_url
1617
from sentry.preprod.vcs.status_checks.size.tasks import (
1718
GITHUB_STATUS_CHECK_STATUS_MAPPING,
@@ -147,7 +148,7 @@ def create_preprod_snapshot_status_check_task(
147148
is_solo = not base_artifact_map
148149

149150
if not is_solo:
150-
changes_map = _build_changes_map(
151+
changes_map = build_changes_map(
151152
all_artifacts,
152153
snapshot_metrics_map,
153154
comparisons_map,
@@ -315,40 +316,6 @@ def create_preprod_snapshot_status_check_task(
315316
)
316317

317318

318-
def _comparison_has_changes(
319-
comparison: PreprodSnapshotComparison,
320-
fail_on_added: bool = False,
321-
fail_on_removed: bool = True,
322-
) -> bool:
323-
return (
324-
comparison.images_changed > 0
325-
or comparison.images_renamed > 0
326-
or (fail_on_added and comparison.images_added > 0)
327-
or (fail_on_removed and comparison.images_removed > 0)
328-
)
329-
330-
331-
def _build_changes_map(
332-
artifacts: list[PreprodArtifact],
333-
snapshot_metrics_map: dict[int, PreprodSnapshotMetrics],
334-
comparisons_map: dict[int, PreprodSnapshotComparison],
335-
fail_on_added: bool = False,
336-
fail_on_removed: bool = True,
337-
) -> dict[int, bool]:
338-
changes_map: dict[int, bool] = {}
339-
for artifact in artifacts:
340-
metrics = snapshot_metrics_map.get(artifact.id)
341-
if not metrics:
342-
continue
343-
comparison = comparisons_map.get(metrics.id)
344-
if not comparison or comparison.state != PreprodSnapshotComparison.State.SUCCESS:
345-
continue
346-
changes_map[artifact.id] = _comparison_has_changes(
347-
comparison, fail_on_added, fail_on_removed
348-
)
349-
return changes_map
350-
351-
352319
def _compute_snapshot_status(
353320
artifacts: list[PreprodArtifact],
354321
snapshot_metrics_map: dict[int, PreprodSnapshotMetrics],

tests/sentry/preprod/vcs/status_checks/snapshots/test_tasks.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from sentry.integrations.source_code_management.status_check import StatusCheckStatus
44
from sentry.preprod.snapshots.models import PreprodSnapshotComparison, PreprodSnapshotMetrics
5+
from sentry.preprod.snapshots.utils import build_changes_map
56
from sentry.preprod.vcs.status_checks.snapshots.tasks import (
6-
_build_changes_map,
77
_compute_snapshot_status,
88
)
99
from sentry.testutils.cases import TestCase
@@ -58,7 +58,7 @@ def _status_with_changes_map(self, artifact, metrics, approvals=None, **flags):
5858
artifacts = [artifact]
5959
metrics_map = {artifact.id: metrics}
6060
comparisons_map = {metrics.id: _get_comparison(metrics)}
61-
changes_map = _build_changes_map(artifacts, metrics_map, comparisons_map, **flags)
61+
changes_map = build_changes_map(artifacts, metrics_map, comparisons_map, **flags)
6262
return _compute_snapshot_status(
6363
artifacts, metrics_map, comparisons_map, approvals or {}, changes_map
6464
)
@@ -102,14 +102,14 @@ def test_no_changes_succeeds(self):
102102
class BuildChangesMapTest(SnapshotTasksTestBase):
103103
def test_added_ignored_when_flag_off(self):
104104
artifact, metrics, _ = self._make_artifact_with_comparison(images_added=5)
105-
changes_map = _build_changes_map(
105+
changes_map = build_changes_map(
106106
[artifact], {artifact.id: metrics}, {metrics.id: _get_comparison(metrics)}
107107
)
108108
assert not any(changes_map.values())
109109

110110
def test_added_detected_when_flag_on(self):
111111
artifact, metrics, _ = self._make_artifact_with_comparison(images_added=5)
112-
changes_map = _build_changes_map(
112+
changes_map = build_changes_map(
113113
[artifact],
114114
{artifact.id: metrics},
115115
{metrics.id: _get_comparison(metrics)},
@@ -119,7 +119,7 @@ def test_added_detected_when_flag_on(self):
119119

120120
def test_removed_ignored_when_flag_off(self):
121121
artifact, metrics, _ = self._make_artifact_with_comparison(images_removed=3)
122-
changes_map = _build_changes_map(
122+
changes_map = build_changes_map(
123123
[artifact],
124124
{artifact.id: metrics},
125125
{metrics.id: _get_comparison(metrics)},
@@ -129,14 +129,14 @@ def test_removed_ignored_when_flag_off(self):
129129

130130
def test_removed_detected_by_default(self):
131131
artifact, metrics, _ = self._make_artifact_with_comparison(images_removed=3)
132-
changes_map = _build_changes_map(
132+
changes_map = build_changes_map(
133133
[artifact], {artifact.id: metrics}, {metrics.id: _get_comparison(metrics)}
134134
)
135135
assert changes_map[artifact.id] is True
136136

137137
def test_changed_always_detected(self):
138138
artifact, metrics, _ = self._make_artifact_with_comparison(images_changed=2)
139-
changes_map = _build_changes_map(
139+
changes_map = build_changes_map(
140140
[artifact], {artifact.id: metrics}, {metrics.id: _get_comparison(metrics)}
141141
)
142142
assert changes_map[artifact.id] is True

0 commit comments

Comments
 (0)