Repository navigation
feat(cursor-origin): Track pull requests #125143
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from sentry.models.commitauthor import CommitAuthor | ||
|
|
||
| MAX_AUTHOR_EMAIL_LENGTH = 75 | ||
|
|
||
|
|
||
| def get_or_create_commit_author(organization_id: int, email: str, name: str) -> CommitAuthor | None: | ||
| if not email or len(email) > MAX_AUTHOR_EMAIL_LENGTH: | ||
| return None | ||
|
|
||
| author, _ = CommitAuthor.objects.get_or_create( | ||
| organization_id=organization_id, | ||
| email=email, | ||
| defaults={"name": name[:128]}, | ||
| ) | ||
| return author |
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,96 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from collections.abc import Mapping, Sequence | ||
| from typing import Any | ||
|
|
||
| from sentry.integrations.cursor_origin.authors import get_or_create_commit_author | ||
| from sentry.integrations.cursor_origin.handlers import WebhookEventHandler | ||
| from sentry.integrations.cursor_origin.repository import active_repositories | ||
| from sentry.integrations.cursor_origin.webhook_types import PullRequestEvent | ||
| from sentry.integrations.services.integration.model import ( | ||
| RpcIntegration, | ||
| RpcOrganizationIntegration, | ||
| ) | ||
| from sentry.integrations.types import IntegrationProviderSlug | ||
| from sentry.integrations.utils.metrics import IntegrationWebhookEventType | ||
| from sentry.models.pullrequest import PullRequestLifecycleState | ||
| from sentry.models.repository import Repository | ||
| from sentry.pr_metrics.lifecycle_mapping import update_pull_request_from_scm_snapshot | ||
| from sentry.utils import metrics | ||
|
|
||
| logger = logging.getLogger("sentry.integrations.cursor_origin") | ||
|
|
||
|
|
||
| def lifecycle_state(event: PullRequestEvent) -> str: | ||
| if event.pull_request.merged: | ||
| return PullRequestLifecycleState.MERGED | ||
| if event.pull_request.state == "closed": | ||
| return PullRequestLifecycleState.CLOSED | ||
| return PullRequestLifecycleState.OPEN | ||
|
|
||
|
|
||
| class PullRequestLifecycleHandler(WebhookEventHandler): | ||
| """Record a pull request from any of Origin's snapshot events.""" | ||
|
|
||
| EVENT_TYPE = IntegrationWebhookEventType.MERGE_REQUEST | ||
|
|
||
| def __call__( | ||
| self, | ||
| payload: Mapping[str, Any], | ||
| delivery_id: str, | ||
| integration: RpcIntegration, | ||
| org_integrations: Sequence[RpcOrganizationIntegration], | ||
| ) -> None: | ||
| event = PullRequestEvent.from_payload(payload) | ||
|
|
||
| repositories = active_repositories(event.repository_id, org_integrations) | ||
| if not repositories: | ||
| logger.info( | ||
| "cursor_origin.pull_request.unknown_repository", | ||
| extra={"delivery_id": delivery_id, "external_id": event.repository_id}, | ||
| ) | ||
| metrics.incr("cursor_origin.pull_request.unknown_repository", sample_rate=1.0) | ||
| return | ||
|
|
||
| for repo in repositories: | ||
| self._record(repo, event, delivery_id) | ||
|
|
||
| def _record(self, repo: Repository, event: PullRequestEvent, delivery_id: str) -> None: | ||
| pull_request = event.pull_request | ||
| state = lifecycle_state(event) | ||
| email, name = pull_request.author.email_and_name() | ||
| author = get_or_create_commit_author(repo.organization_id, email, name) | ||
|
Check warning on line 63 in src/sentry/integrations/cursor_origin/pull_request.py
|
||
|
|
||
| _, created = update_pull_request_from_scm_snapshot( | ||
| provider=IntegrationProviderSlug.CURSOR_ORIGIN.value, | ||
| organization_id=repo.organization_id, | ||
| repository_id=repo.id, | ||
| key=pull_request.number, | ||
| defaults={ | ||
| "organization_id": repo.organization_id, | ||
| "title": pull_request.title, | ||
| "author": author, | ||
| "message": pull_request.body, | ||
| "merge_commit_sha": pull_request.merge_commit_sha or None, | ||
| "head_commit_sha": pull_request.head.sha, | ||
| "opened_at": pull_request.created_at, | ||
| "closed_at": pull_request.closed_at, | ||
| "merged_at": pull_request.merged_at, | ||
| "provider_updated_at": pull_request.updated_at, | ||
| "state": state, | ||
| "draft": pull_request.draft, | ||
| }, | ||
| event_state=state, | ||
| event_updated_at=pull_request.updated_at, | ||
| ) | ||
|
|
||
| if created: | ||
| logger.info( | ||
| "cursor_origin.pull_request.created", | ||
| extra={ | ||
| "delivery_id": delivery_id, | ||
| "repository_id": repo.id, | ||
| "key": pull_request.number, | ||
| }, | ||
| ) | ||
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
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.
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.
Is there a way to detect if it's a user, app or service account? Like a "type" field or something?
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.
There's no type field explicitly, but the response does have
author.user,author.app,author.serviceAccountto differentiate.Modifying this to include these options in the author too