Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions fixtures/github.py
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just adding some missing data in these fixtures.

Original file line number Diff line number Diff line change
Expand Up @@ -3553,12 +3553,17 @@
"installation": {"id": 12345},
"repository": {
"id": 35129377,
"name": "sentry",
"full_name": "getsentry/sentry",
"html_url": "https://github.com/getsentry/sentry"
"html_url": "https://github.com/getsentry/sentry",
"owner": {
"login": "getsentry",
"id": 1396951
}
},
"check_run": {
"external_id": "4663713",
"html_url": "https://github.com/test/repo/runs/4"
"html_url": "https://github.com/getsentry/sentry/runs/4"
},
"sender": {
"id": 12345678,
Expand All @@ -3571,8 +3576,18 @@
"installation": {"id": 12345},
"repository": {
"id": 35129377,
"name": "sentry",
"full_name": "getsentry/sentry",
"html_url": "https://github.com/getsentry/sentry"
"html_url": "https://github.com/getsentry/sentry",
"owner": {
"login": "getsentry",
"id": 1396951
}
},
"check_run": {
"id": 9876543,
"status": "completed",
"conclusion": "success"
},
"sender": {
"id": 12345678,
Expand Down
61 changes: 61 additions & 0 deletions src/sentry/seer/code_review/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,64 @@ def get_pr_author_id(event: Mapping[str, Any]) -> str | None:
return str(user_id)

return None


def extract_github_info(
event: Mapping[str, Any], github_event: str | None = None
) -> dict[str, str | None]:
"""
Extract GitHub-related information from a webhook event payload.

Args:
event: The GitHub webhook event payload
github_event: The GitHub event type (e.g., "pull_request", "check_run", "issue_comment")

Returns:
Dictionary containing:
- github_owner: The repository owner/organization name
- github_repo_name: The repository name
- github_repo_full_name: The repository full name (owner/repo)
- github_event_url: URL to the specific event (check_run, pull_request, or comment)
- github_event: The GitHub event type
- github_event_action: The event action (e.g., "opened", "closed", "created")
"""
result: dict[str, str | None] = {
"github_owner": None,
"github_repo_name": None,
"github_repo_full_name": None,
"github_event_url": None,
"github_event": github_event,
"github_event_action": None,
}

repository = event.get("repository", {})
if repository:
if owner := repository.get("owner", {}).get("login"):
result["github_owner"] = owner
if repo_name := repository.get("name"):
result["github_repo_name"] = repo_name
if owner_repo_name := repository.get("full_name"):
result["github_repo_full_name"] = owner_repo_name

if action := event.get("action"):
result["github_event_action"] = action

if pull_request := event.get("pull_request"):
if html_url := pull_request.get("html_url"):
result["github_event_url"] = html_url

if check_run := event.get("check_run"):
if html_url := check_run.get("html_url"):
result["github_event_url"] = html_url

if comment := event.get("comment"):
if html_url := comment.get("html_url"):
result["github_event_url"] = html_url

if issue := event.get("issue"):
if pull_request_data := issue.get("pull_request"):
if html_url := pull_request_data.get("html_url"):
if result["github_event_url"] is None:
result["github_event_url"] = html_url

return result
3 changes: 1 addition & 2 deletions src/sentry/seer/code_review/webhooks/check_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def handle_check_run_event(
*,
github_event: GithubWebhookType,
event: Mapping[str, Any],
extra: Mapping[str, str | None],
**kwargs: Any,
) -> None:
"""
Expand All @@ -87,8 +88,6 @@ def handle_check_run_event(
return

action = event.get("action")
# We can use html_url to search through the logs for this event.
extra = {"html_url": event.get("check_run", {}).get("html_url"), "action": action}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra is already being used by the logger, thus, no more changes are needed in this file.


if action is None:
logger.error(Log.MISSING_ACTION.value, extra=extra)
Expand Down
11 changes: 7 additions & 4 deletions src/sentry/seer/code_review/webhooks/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from ..metrics import record_webhook_filtered
from ..preflight import CodeReviewPreflightService
from ..utils import extract_github_info
from .check_run import handle_check_run_event
from .issue_comment import handle_issue_comment_event
from .pull_request import handle_pull_request_event
Expand Down Expand Up @@ -46,16 +47,17 @@ def handle_webhook_event(
integration: The GitHub integration
**kwargs: Additional keyword arguments
"""
# The extracted important key values are used for debugging with logs
extra = extract_github_info(event, github_event=github_event.value)
extra["organization_slug"] = organization.slug
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, we only have a couple of keys to filter-by.

Here's a screenshot using these kind of keys on Sentry Logs (link):

Image


# Skip GitHub Enterprise on-prem - code review is only supported for GitHub Cloud
if integration and integration.provider == IntegrationProviderSlug.GITHUB_ENTERPRISE:
return

handler = EVENT_TYPE_TO_HANDLER.get(github_event)
if handler is None:
logger.warning(
"github.webhook.handler.not_found",
extra={"github_event": github_event.value},
)
logger.warning("github.webhook.handler.not_found", extra=extra)
return

from ..utils import get_pr_author_id
Expand Down Expand Up @@ -83,4 +85,5 @@ def handle_webhook_event(
repo=repo,
integration=integration,
org_code_review_settings=preflight.settings,
extra=extra,
)
16 changes: 3 additions & 13 deletions src/sentry/seer/code_review/webhooks/issue_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,9 @@ def _add_eyes_reaction_to_comment(
organization: Organization,
repo: Repository,
comment_id: str,
extra: Mapping[str, str | None],
) -> None:
"""Add 👀 reaction to acknowledge a review command. Errors are logged/added to metrics but not raised."""
extra = {
"organization_id": organization.id,
"repo": repo.name,
"comment_id": comment_id,
"github_event": github_event,
"github_event_action": github_event_action.value,
}

if integration is None:
record_webhook_handler_error(
Expand Down Expand Up @@ -95,18 +89,13 @@ def handle_issue_comment_event(
organization: Organization,
repo: Repository,
integration: RpcIntegration | None = None,
extra: Mapping[str, str | None],
**kwargs: Any,
) -> None:
"""
Handle issue_comment webhook events for PR review commands.
"""
github_event_action = event.get("action", "")
extra = {
"organization_id": organization.id,
"repo": repo.name,
"github_event": github_event,
"github_event_action": github_event_action,
}
record_webhook_received(github_event, github_event_action)

if github_event_action != GitHubIssueCommentAction.CREATED:
Expand Down Expand Up @@ -135,6 +124,7 @@ def handle_issue_comment_event(
organization,
repo,
str(comment_id),
extra,
)

target_commit_sha = _get_target_commit_sha(github_event, event, repo, integration)
Expand Down
4 changes: 1 addition & 3 deletions src/sentry/seer/code_review/webhooks/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,14 @@ def handle_pull_request_event(
repo: Repository,
integration: RpcIntegration | None = None,
org_code_review_settings: CodeReviewSettings | None = None,
extra: Mapping[str, str | 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:
logger.warning(Log.MISSING_PULL_REQUEST.value, extra=extra)
Expand All @@ -109,7 +108,6 @@ def handle_pull_request_event(
logger.warning(Log.MISSING_ACTION.value, extra=extra)
record_webhook_handler_error(github_event, "unknown", CodeReviewErrorType.MISSING_ACTION)
return
extra["action"] = action_value

record_webhook_received(github_event, action_value)

Expand Down
Loading
Loading