Skip to content
Merged
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
5 changes: 4 additions & 1 deletion src/sentry/pr_metrics/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ def handle_attribution(
if pr is None:
return

if action == "opened":
# SENTRY_APP author attribution is recorded on open and re-checked on close.
# This is for the unlikely event that we missed the open webhook or for cases
# where the PR open and closes super fast and the webhooks might be out of order
if action in ("opened", "closed"):
pr_url = (pull_request or {}).get("html_url") or None
_write_author_attribution(pr, github_user, pr_url=pr_url, group_ids=resolved_group_ids(pr))
if features.has("organizations:mcp-issue-view-attribution", organization):
Expand Down
26 changes: 24 additions & 2 deletions tests/sentry/pr_metrics/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_unknown_user_no_attribution_created(self) -> None:

assert not PullRequestAttribution.objects.filter(pull_request=self.pr).exists()

def test_app_attribution_only_written_on_opened(self) -> None:
def test_app_attribution_not_written_on_non_terminal_actions(self) -> None:
self._call(action="synchronize", user_id=settings.SEER_AUTOFIX_GITHUB_APP_USER_ID)
self._call(action="labeled", user_id=settings.SEER_AUTOFIX_GITHUB_APP_USER_ID)

Expand All @@ -111,12 +111,34 @@ def test_app_attribution_only_written_on_opened(self) -> None:
signal_type=PullRequestAttributionSignalType.SENTRY_APP,
).exists()

def test_app_attribution_written_on_closed(self) -> None:
# A Sentry-authored PR that never got a row at open (e.g. opened before the
# flag, or resolving no issues) is still attributed at the terminal close
# event so it can be tracked for emission.
self._call(action="closed", user_id=settings.SENTRY_GITHUB_APP_USER_ID)

attr = PullRequestAttribution.objects.get(pull_request=self.pr)
assert attr.signal_type == PullRequestAttributionSignalType.SENTRY_APP
assert attr.source == PullRequestAttributionSource.WEBHOOK_DATA
assert attr.is_valid is True

def test_app_attribution_idempotent_across_open_then_close(self) -> None:
self._call(action="opened", user_id=settings.SENTRY_GITHUB_APP_USER_ID)
self._call(action="closed", user_id=settings.SENTRY_GITHUB_APP_USER_ID)

assert (
PullRequestAttribution.objects.filter(
pull_request=self.pr,
signal_type=PullRequestAttributionSignalType.SENTRY_APP,
).count()
== 1
)

# --- Action gate ---

def test_irrelevant_actions_skipped(self) -> None:
for action in (
"synchronize",
"closed",
"labeled",
"assigned",
):
Expand Down
Loading