-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
feat(code_review): Forward pull_request events to Seer #105740
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -801,7 +801,10 @@ class PullRequestEventWebhook(GitHubWebhook): | |||||
| """https://developer.github.com/v3/activity/events/types/#pullrequestevent""" | ||||||
|
|
||||||
| EVENT_TYPE = IntegrationWebhookEventType.MERGE_REQUEST | ||||||
| WEBHOOK_EVENT_PROCESSORS = (_handle_pr_webhook_for_autofix_processor,) | ||||||
| WEBHOOK_EVENT_PROCESSORS = ( | ||||||
| _handle_pr_webhook_for_autofix_processor, | ||||||
| code_review_handle_webhook_event, | ||||||
|
Member
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. Adding this to the PullRequestEventWebhook class which handles sentry/src/sentry/integrations/github/webhook.py Line 1041 in 67db771
|
||||||
| ) | ||||||
|
|
||||||
| def _handle( | ||||||
| self, | ||||||
|
|
@@ -896,8 +899,6 @@ def _handle( | |||||
| "github.webhook.pull_request.created", | ||||||
| sample_rate=1.0, | ||||||
| tags={ | ||||||
| "organization_id": organization.id, | ||||||
| "repository_id": repo.id, | ||||||
|
Member
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. If you run the tests with verbosity, you can see that these calls to
Member
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. @ajay-sentry @srest2021 heads up ^ in case you were still using that metric to track billing related stuff for GA
Member
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. Thanks for catching this!! |
||||||
| "is_private": pr_repo_private, | ||||||
| }, | ||||||
| ) | ||||||
|
|
@@ -929,10 +930,6 @@ def _handle( | |||||
| metrics.incr( | ||||||
| "github.webhook.organization_contributor.should_create", | ||||||
| sample_rate=1.0, | ||||||
| tags={ | ||||||
| "organization_id": organization.id, | ||||||
| "repository_id": repo.id, | ||||||
| }, | ||||||
| ) | ||||||
|
|
||||||
| locked_contributor = None | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| """ | ||
| Handler for GitHub pull_request webhook events. | ||
| https://docs.github.com/en/webhooks/webhook-events-and-payloads#pull_request | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import enum | ||
| import logging | ||
| from collections.abc import Mapping | ||
| from typing import Any | ||
|
|
||
| from sentry import options | ||
| from sentry.integrations.github.webhook_types import GithubWebhookType | ||
| from sentry.integrations.services.integration import RpcIntegration | ||
| from sentry.models.organization import Organization | ||
| from sentry.models.repository import Repository | ||
| from sentry.models.repositorysettings import CodeReviewTrigger | ||
| from sentry.utils import metrics | ||
|
|
||
| from ..utils import _get_target_commit_sha | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class ErrorStatus(enum.StrEnum): | ||
| MISSING_PULL_REQUEST = "missing_pull_request" | ||
| MISSING_ACTION = "missing_action" | ||
| UNSUPPORTED_ACTION = "unsupported_action" | ||
| DRAFT_PR = "draft_pr" | ||
|
|
||
|
|
||
| class Log(enum.StrEnum): | ||
| MISSING_PULL_REQUEST = "github.webhook.pull_request.missing-pull-request" | ||
| MISSING_ACTION = "github.webhook.pull_request.missing-action" | ||
| UNSUPPORTED_ACTION = "github.webhook.pull_request.unsupported-action" | ||
| DRAFT_PR = "github.webhook.pull_request.draft-pr" | ||
|
|
||
|
|
||
| class Metrics(enum.StrEnum): | ||
| ERROR = "seer.code_review.webhook.pull_request.error" | ||
| OUTCOME = "seer.code_review.webhook.pull_request.outcome" | ||
|
|
||
|
|
||
| class PullRequestAction(enum.StrEnum): | ||
| """ | ||
| GitHub pull_request webhook actions. | ||
| https://docs.github.com/en/webhooks/webhook-events-and-payloads#pull_request | ||
| """ | ||
|
|
||
| ASSIGNED = "assigned" | ||
| AUTO_MERGE_DISABLED = "auto_merge_disabled" | ||
| AUTO_MERGE_ENABLED = "auto_merge_enabled" | ||
| CLOSED = "closed" | ||
| CONVERTED_TO_DRAFT = "converted_to_draft" | ||
| DEMILESTONED = "demilestoned" | ||
| DEQUEUED = "dequeued" | ||
| EDITED = "edited" | ||
| ENQUEUED = "enqueued" | ||
| LABELED = "labeled" | ||
| LOCKED = "locked" | ||
| MILESTONED = "milestoned" | ||
| OPENED = "opened" | ||
| READY_FOR_REVIEW = "ready_for_review" | ||
| REOPENED = "reopened" | ||
| REVIEW_REQUEST_REMOVED = "review_request_removed" | ||
| REVIEW_REQUESTED = "review_requested" | ||
| # New commits are pushed to the PR | ||
| SYNCHRONIZE = "synchronize" | ||
| UNASSIGNED = "unassigned" | ||
| UNLABELED = "unlabeled" | ||
| UNLOCKED = "unlocked" | ||
|
|
||
|
|
||
| WHITELISTED_ACTIONS = { | ||
| PullRequestAction.OPENED, | ||
| PullRequestAction.READY_FOR_REVIEW, | ||
| PullRequestAction.SYNCHRONIZE, | ||
| } | ||
|
|
||
|
|
||
| def _get_trigger_for_action(action: PullRequestAction) -> CodeReviewTrigger: | ||
| match action: | ||
| case PullRequestAction.READY_FOR_REVIEW: | ||
| return CodeReviewTrigger.ON_READY_FOR_REVIEW | ||
| case PullRequestAction.SYNCHRONIZE: | ||
| return CodeReviewTrigger.ON_NEW_COMMIT | ||
| case _: | ||
| raise ValueError(f"Unsupported pull request action: {action}") | ||
armenzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def _warn_and_increment_metric( | ||
| error_status: ErrorStatus, | ||
| extra: Mapping[str, Any], | ||
| action: PullRequestAction | str | None = None, | ||
| ) -> None: | ||
| """ | ||
| Warn and increment metric for a given error status and action. | ||
| """ | ||
| logger.warning(Log[error_status.name].value, extra=extra) | ||
| tags = {"error_status": error_status.value} | ||
| if action: | ||
| tags["action"] = action | ||
| metrics.incr(Metrics.ERROR.value, tags=tags) | ||
|
|
||
|
|
||
| def handle_pull_request_event( | ||
| *, | ||
| github_event: GithubWebhookType, | ||
| event: Mapping[str, Any], | ||
| organization: Organization, | ||
| repo: Repository, | ||
| integration: RpcIntegration | None = None, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| """ | ||
| Handle pull_request webhook events for code review. | ||
| This handler processes PR events and sends them directly to Seer | ||
| """ | ||
| extra = {"organization_id": organization.id, "repo": repo.name} | ||
| pull_request = event.get("pull_request") | ||
| if not pull_request: | ||
| _warn_and_increment_metric(ErrorStatus.MISSING_PULL_REQUEST, extra=extra) | ||
| return | ||
|
|
||
| action_value = event.get("action") | ||
| if not action_value or not isinstance(action_value, str): | ||
| _warn_and_increment_metric(ErrorStatus.MISSING_ACTION, extra=extra) | ||
| return | ||
|
|
||
| try: | ||
| action = PullRequestAction(action_value) | ||
| except ValueError: | ||
| _warn_and_increment_metric(ErrorStatus.UNSUPPORTED_ACTION, action=action_value, extra=extra) | ||
| return | ||
|
|
||
| if action not in WHITELISTED_ACTIONS: | ||
| return | ||
|
|
||
| if pull_request.get("draft") is True: | ||
|
Member
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. Later I'm guessing we're going to want to start tracking this metric later too to inform whether we should allow our feature on drafts or not
Member
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. I'm not certain if this is necessary since we check the actions and there's a draft action. |
||
| return | ||
|
|
||
| if not options.get("github.webhook.pr"): | ||
| from .task import schedule_task | ||
|
|
||
| schedule_task( | ||
| github_event=github_event, | ||
| event=event, | ||
| organization=organization, | ||
| repo=repo, | ||
| target_commit_sha=_get_target_commit_sha(github_event, event, repo, integration), | ||
| trigger=_get_trigger_for_action(action), | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -138,19 +138,21 @@ def send_github_webhook_event( | |
|
|
||
| class GitHubWebhookCodeReviewTestCase(GitHubWebhookTestCase): | ||
| CODE_REVIEW_FEATURES = {"organizations:gen-ai-features", "organizations:code-review-beta"} | ||
| OPTIONS_TO_SET: dict[str, bool] = {} | ||
|
|
||
| @contextmanager | ||
| def code_review_setup( | ||
| self, | ||
| features: Collection[str] | Mapping[str, Any] | None = None, | ||
| forward_to_overwatch: bool = False, | ||
| options: dict[str, bool] | None = None, | ||
| ) -> Generator[None]: | ||
| """Helper to set up code review test context.""" | ||
| self.organization.update_option("sentry:enable_pr_review_test_generation", True) | ||
| features_to_enable = self.CODE_REVIEW_FEATURES if features is None else features | ||
| options_to_set = dict(self.OPTIONS_TO_SET) | (options or {}) | ||
|
Member
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. this is nice
Member
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. Thank you! |
||
| with ( | ||
| self.feature(features_to_enable), | ||
| self.options({"github.webhook.issue-comment": forward_to_overwatch}), | ||
| self.options(options_to_set), | ||
| ): | ||
| yield | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for my own mental model, these 2 processors run serially right?
sentry/src/sentry/integrations/github/webhook.py
Line 176 in 4ba9a5a
With a failure in 1 does not block the next, and each processor runs synchronous for most of its logic except for the call to seer in
schedule_taskwhich is async?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is correct.
Once the code review processors executes, the work is in the task broker queue and will be handled async.