-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(preprod): Add snapshot PR comment task and wiring #112360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b7ff5d8
feat(preprod): Add snapshot PR comment task and wiring (EME-999)
runningcode 30f425a
fix(preprod): Fix mypy type error for sentinel default argument
runningcode a854456
ref(preprod): Extract shared PR comment save/find helpers
runningcode 89cd881
feat(preprod): Add trigger script for snapshot PR comments
runningcode 6a9ab12
ref(preprod): Inline wrapper functions and extract build_changes_map
runningcode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from collections.abc import Sequence | ||
| from typing import Any | ||
|
|
||
| from django.db import router, transaction | ||
| from taskbroker_client.retry import Retry | ||
|
|
||
| from sentry import features | ||
| from sentry.models.commitcomparison import CommitComparison | ||
| from sentry.preprod.integration_utils import get_commit_context_client | ||
| from sentry.preprod.models import PreprodArtifact, PreprodComparisonApproval | ||
| from sentry.preprod.snapshots.models import PreprodSnapshotComparison, PreprodSnapshotMetrics | ||
| from sentry.preprod.vcs.pr_comments.snapshot_templates import format_snapshot_pr_comment | ||
| from sentry.preprod.vcs.pr_comments.tasks import _get_error_type | ||
| from sentry.preprod.vcs.status_checks.snapshots.tasks import ( | ||
| FAIL_ON_ADDED_OPTION_KEY, | ||
| FAIL_ON_REMOVED_OPTION_KEY, | ||
| _build_changes_map, | ||
| ) | ||
| from sentry.shared_integrations.exceptions import ApiError | ||
| from sentry.silo.base import SiloMode | ||
| from sentry.tasks.base import instrumented_task | ||
| from sentry.taskworker.namespaces import preprod_tasks | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| ENABLED_OPTION_KEY = "sentry:preprod_snapshot_pr_comments_enabled" | ||
| FEATURE_FLAG = "organizations:preprod-snapshot-pr-comments" | ||
|
|
||
|
|
||
| @instrumented_task( | ||
| name="sentry.preprod.tasks.create_preprod_snapshot_pr_comment", | ||
| namespace=preprod_tasks, | ||
| processing_deadline_duration=30, | ||
| silo_mode=SiloMode.CELL, | ||
| retry=Retry(times=5, delay=60 * 5), | ||
| ) | ||
| def create_preprod_snapshot_pr_comment_task( | ||
| preprod_artifact_id: int, caller: str | None = None, **kwargs: Any | ||
| ) -> None: | ||
| try: | ||
| artifact = PreprodArtifact.objects.select_related( | ||
| "mobile_app_info", | ||
| "commit_comparison", | ||
| "project", | ||
| "project__organization", | ||
| ).get(id=preprod_artifact_id) | ||
| except PreprodArtifact.DoesNotExist: | ||
| logger.exception( | ||
| "preprod.snapshot_pr_comments.create.artifact_not_found", | ||
| extra={"artifact_id": preprod_artifact_id, "caller": caller}, | ||
| ) | ||
| return | ||
|
|
||
| if not artifact.commit_comparison: | ||
| logger.info( | ||
| "preprod.snapshot_pr_comments.create.no_commit_comparison", | ||
| extra={"artifact_id": artifact.id}, | ||
| ) | ||
| return | ||
|
|
||
| commit_comparison = artifact.commit_comparison | ||
| if ( | ||
| not commit_comparison.pr_number | ||
| or not commit_comparison.head_repo_name | ||
| or not commit_comparison.provider | ||
| ): | ||
| logger.info( | ||
| "preprod.snapshot_pr_comments.create.no_pr_info", | ||
| extra={ | ||
| "artifact_id": artifact.id, | ||
| "pr_number": commit_comparison.pr_number, | ||
| "head_repo_name": commit_comparison.head_repo_name, | ||
| }, | ||
| ) | ||
| return | ||
|
|
||
| if not artifact.project.get_option(ENABLED_OPTION_KEY, default=True): | ||
| logger.info( | ||
| "preprod.snapshot_pr_comments.create.project_disabled", | ||
| extra={"artifact_id": artifact.id, "project_id": artifact.project.id}, | ||
| ) | ||
| return | ||
|
|
||
| organization = artifact.project.organization | ||
| if not features.has(FEATURE_FLAG, organization): | ||
| logger.info( | ||
| "preprod.snapshot_pr_comments.create.feature_disabled", | ||
| extra={"artifact_id": artifact.id, "organization_id": organization.id}, | ||
| ) | ||
| return | ||
|
|
||
| client = get_commit_context_client( | ||
| organization, commit_comparison.head_repo_name, commit_comparison.provider | ||
| ) | ||
| if not client: | ||
| logger.info( | ||
| "preprod.snapshot_pr_comments.create.no_client", | ||
| extra={"artifact_id": artifact.id}, | ||
| ) | ||
| return | ||
|
|
||
| api_error: Exception | None = None | ||
|
|
||
| with transaction.atomic(router.db_for_write(CommitComparison)): | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we lock here on the commit comparison. this is the same as what we do for build distirbution comments. |
||
| all_for_pr = list( | ||
| CommitComparison.objects.select_for_update() | ||
| .filter( | ||
| organization_id=commit_comparison.organization_id, | ||
| head_repo_name=commit_comparison.head_repo_name, | ||
| pr_number=commit_comparison.pr_number, | ||
| ) | ||
| .order_by("id") | ||
| ) | ||
|
|
||
| try: | ||
| cc = next(c for c in all_for_pr if c.id == commit_comparison.id) | ||
| except StopIteration: | ||
| raise CommitComparison.DoesNotExist( | ||
| f"CommitComparison {commit_comparison.id} was deleted before lock acquisition" | ||
| ) | ||
|
|
||
| # Gather snapshot data | ||
| all_artifacts = list(artifact.get_sibling_artifacts_for_commit()) | ||
|
|
||
| artifact_ids = [a.id for a in all_artifacts] | ||
| snapshot_metrics_qs = PreprodSnapshotMetrics.objects.filter( | ||
| preprod_artifact_id__in=artifact_ids, | ||
| ) | ||
| snapshot_metrics_map: dict[int, PreprodSnapshotMetrics] = { | ||
| m.preprod_artifact_id: m for m in snapshot_metrics_qs | ||
| } | ||
|
|
||
| all_artifacts = [a for a in all_artifacts if a.id in snapshot_metrics_map] | ||
| if not all_artifacts: | ||
| return | ||
|
|
||
| metrics_ids = [m.id for m in snapshot_metrics_map.values()] | ||
| comparisons_qs = PreprodSnapshotComparison.objects.filter( | ||
| head_snapshot_metrics_id__in=metrics_ids, | ||
| ) | ||
| comparisons_map: dict[int, PreprodSnapshotComparison] = { | ||
| c.head_snapshot_metrics_id: c for c in comparisons_qs | ||
| } | ||
|
|
||
| approvals_map: dict[int, PreprodComparisonApproval] = {} | ||
| approval_qs = PreprodComparisonApproval.objects.filter( | ||
| preprod_artifact_id__in=artifact_ids, | ||
| preprod_feature_type=PreprodComparisonApproval.FeatureType.SNAPSHOTS, | ||
| approval_status=PreprodComparisonApproval.ApprovalStatus.APPROVED, | ||
| ) | ||
| for approval in approval_qs: | ||
| approvals_map[approval.preprod_artifact_id] = approval | ||
|
|
||
| base_artifact_map = PreprodArtifact.get_base_artifacts_for_commit(all_artifacts) | ||
|
|
||
| fail_on_added = artifact.project.get_option(FAIL_ON_ADDED_OPTION_KEY, default=False) | ||
| fail_on_removed = artifact.project.get_option(FAIL_ON_REMOVED_OPTION_KEY, default=True) | ||
| changes_map = _build_changes_map( | ||
| all_artifacts, | ||
| snapshot_metrics_map, | ||
| comparisons_map, | ||
| fail_on_added=fail_on_added, | ||
| fail_on_removed=fail_on_removed, | ||
| ) | ||
|
|
||
| comment_body = format_snapshot_pr_comment( | ||
| all_artifacts, | ||
| snapshot_metrics_map, | ||
| comparisons_map, | ||
| base_artifact_map, | ||
| changes_map, | ||
| approvals_map=approvals_map, | ||
| ) | ||
|
|
||
| existing_comment_id = _find_existing_comment_id(all_for_pr) | ||
|
|
||
| try: | ||
| if existing_comment_id: | ||
| client.update_comment( | ||
| repo=cc.head_repo_name, | ||
| issue_id=str(cc.pr_number), | ||
| comment_id=str(existing_comment_id), | ||
| data={"body": comment_body}, | ||
| ) | ||
| comment_id = existing_comment_id | ||
| logger.info( | ||
| "preprod.snapshot_pr_comments.create.updated", | ||
| extra={"artifact_id": artifact.id, "comment_id": comment_id}, | ||
| ) | ||
| else: | ||
| resp = client.create_comment( | ||
| repo=cc.head_repo_name, | ||
| issue_id=str(cc.pr_number), | ||
| data={"body": comment_body}, | ||
| ) | ||
| comment_id = str(resp["id"]) | ||
| logger.info( | ||
| "preprod.snapshot_pr_comments.create.created", | ||
| extra={"artifact_id": artifact.id, "comment_id": comment_id}, | ||
| ) | ||
| except Exception as e: | ||
| extra: dict[str, Any] = { | ||
| "artifact_id": artifact.id, | ||
| "organization_id": organization.id, | ||
| "error_type": type(e).__name__, | ||
| } | ||
| if isinstance(e, ApiError): | ||
| extra["status_code"] = e.code | ||
| logger.exception("preprod.snapshot_pr_comments.create.failed", extra=extra) | ||
| _save_pr_comment_result(cc, success=False, error=e) | ||
| api_error = e | ||
| else: | ||
| _save_pr_comment_result(cc, success=True, comment_id=comment_id) | ||
|
|
||
| if api_error is not None: | ||
| raise api_error | ||
|
|
||
|
|
||
| def _find_existing_comment_id( | ||
| comparisons: Sequence[CommitComparison], | ||
| ) -> str | None: | ||
| for cc in comparisons: | ||
| extras = cc.extras or {} | ||
| comment_id = extras.get("pr_comments", {}).get("snapshots", {}).get("comment_id") | ||
| if comment_id: | ||
| return str(comment_id) | ||
| return None | ||
|
|
||
|
|
||
| def _save_pr_comment_result( | ||
| commit_comparison: CommitComparison, | ||
| success: bool, | ||
| comment_id: str | None = None, | ||
| error: Exception | None = None, | ||
| ) -> None: | ||
| extras = commit_comparison.extras or {} | ||
|
|
||
| # Preserve the existing comment_id on failure so retries use | ||
| # update_comment instead of creating a duplicate. | ||
| if not comment_id: | ||
| existing = extras.get("pr_comments", {}).get("snapshots", {}) | ||
| comment_id = existing.get("comment_id") | ||
|
|
||
| result: dict[str, Any] = {"success": success} | ||
| if comment_id: | ||
| result["comment_id"] = comment_id | ||
| if not success: | ||
| result["error_type"] = _get_error_type(error) | ||
|
|
||
| pr_comments = extras.setdefault("pr_comments", {}) | ||
| pr_comments["snapshots"] = result | ||
| commit_comparison.extras = extras | ||
| commit_comparison.save(update_fields=["extras"]) | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.