Skip to content

ref(commits): Simplify main loop, extra logging & CODEOWNERS update#113418

Merged
armenzg merged 3 commits intomasterfrom
armenzg/ref/extract-resolve-ref-helper
Apr 20, 2026
Merged

ref(commits): Simplify main loop, extra logging & CODEOWNERS update#113418
armenzg merged 3 commits intomasterfrom
armenzg/ref/extract-resolve-ref-helper

Conversation

@armenzg
Copy link
Copy Markdown
Member

@armenzg armenzg commented Apr 20, 2026

Follow-up refactor to #113293.

It adds some context missing from some of the logging lines and makes the main for loop shorter and easier to read.

This also makes the task be owned by @getsentry/coding-workflows's backend team.

Reshapes the fetch_commits task body around two ideas:

  1. A new resolve_ref helper
  2. Concerns now live with the code that consumes them:
    • The compare-commits cache gating (provider allowlist + feature flag) moved into fetch_commits_for_compare_range, next to the cache.get/cache.set and the compare_commits_cache_enabled span tag. The task just passes the flag through.
    • fetch_commits.loop.start / fetch_commits.loop.complete logs moved into fetch_commits_for_ref_with_lifecycle, which now owns its loop_extra construction from a task-level task_extra mapping. The complete log runs in a finally so it still fires on every swallowed-error path (NotImplementedError, IntegrationResourceNotFoundError, the handled Exception branches).

No behavior change. All existing tests in tests/sentry/tasks/test_commits.py pass unmodified.

Made with Cursor

Fold the per-ref resolution steps (repo lookup, provider binding, start
sha derivation, end sha extraction) into a single resolve_ref helper
returning a ResolvedRef NamedTuple. The task loop becomes resolve →
skip-or-fetch → collect, and refs that can never succeed still short-
circuit before an SCM lifecycle event is opened.

Push the compare-commits cache gating from the task body down into
fetch_commits_for_compare_range where it is actually consumed. The
feature flag flows through untouched; the provider/feature-flag check
lives next to the cache read/write and the compare_commits_cache_enabled
span tag, so the three concerns move together.

Move the fetch_commits.loop.start / fetch_commits.loop.complete logs
into fetch_commits_for_ref_with_lifecycle. The helper owns its own
loop_extra construction from a task-level task_extra mapping, and the
complete log is emitted from a finally so it still fires on every
swallowed-error path. No behavior change.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Made-with: Cursor
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Apr 20, 2026
@armenzg armenzg self-assigned this Apr 20, 2026
end_sha: str,
user: RpcUser | None,
) -> list[dict[str, Any]]:
cache_enabled = (
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is part of cleaning up the for loop in the main task.

release=release,
repo=repo,
prev_release=prev_release,
)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

All of the above logic comes from the task's main for loop.

)


def fetch_commits_for_ref_with_lifecycle(
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If you hide whitespaces for the view, you will more clearly see what's happening in this function.

It's also easier if you switch to the split view.

logger.info(
"fetch_commits.loop.complete",
extra={**loop_extra, "num_commits": len(repo_commits or [])},
)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This logging line also comes from the main loop.


release = Release.objects.get(id=release_id)
logger.info("fetch_commits.start", extra={"organization_slug": release.organization.slug})
set_tag("organization.slug", release.organization.slug)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm moving some of this logging a few lines further down to include more context.

"user_id": user_id,
"refs": refs,
"num_refs": len(refs),
"prev_release_id": prev_release_id,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Including a bit more context to fetch_commits.start to help with debugging.


provider_values = get_provider_for_repo(repo=repo)
if provider_values is None:
resolved = resolve_ref(ref=ref, release=release, prev_release=prev_release, user_id=user_id)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This last refactoring makes the for loop short enough.

"user_id": user_id,
},
)
logger.info("fetch_commits.duplicate", extra=extra)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This logging line now has enough info to help me see which orgs are seeing the most impact.

@armenzg armenzg changed the title ref(commits): Collapse fetch_commits ref loop into resolver + lifecycle ref(commits): Simplify main for loop + extra logging context Apr 20, 2026
@armenzg armenzg requested a review from a team April 20, 2026 13:48
@armenzg armenzg changed the title ref(commits): Simplify main for loop + extra logging context ref(commits): Simplify main loop, extra logging & CODEOWNERS update Apr 20, 2026
@armenzg armenzg marked this pull request as ready for review April 20, 2026 13:50
@armenzg armenzg requested review from a team as code owners April 20, 2026 13:50
Copy link
Copy Markdown
Contributor

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Dropped release_id from shared logging extra dict
    • Added release_id to the shared extra dict at line 413, ensuring all log statements now include this critical identifier for debugging.

Create PR

Or push these changes by commenting:

@cursor push cc3dca3fe7
Preview (cc3dca3fe7)
diff --git a/src/sentry/tasks/commits.py b/src/sentry/tasks/commits.py
--- a/src/sentry/tasks/commits.py
+++ b/src/sentry/tasks/commits.py
@@ -410,6 +410,7 @@
     )
     set_tag("organization.slug", release.organization.slug)
     extra = {
+        "release_id": release.id,
         "organization_id": release.organization_id,
         "user_id": user_id,
         "refs": refs,

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 15d8dc3. Configure here.

"organization_id": release.organization_id,
"user_id": user_id,
},
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Dropped release_id from shared logging extra dict

Low Severity

The shared extra dict is missing release_id (release.id). The old fetch_commits.duplicate log explicitly included "release_id": release.id, but the new extra dict that replaced it doesn't contain this key. Since release_id is the primary identifier for the task's target entity, its absence from all log lines (fetch_commits.start, fetch_commits.duplicate, fetch_commits.complete, and the loop logs via task_extra) reduces debuggability.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 15d8dc3. Configure here.

@armenzg armenzg merged commit 910a2c8 into master Apr 20, 2026
59 checks passed
@armenzg armenzg deleted the armenzg/ref/extract-resolve-ref-helper branch April 20, 2026 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants