Skip to content

fix: recover fork PR re-runs when GitHub omits PR metadata#2655

Draft
chmouel wants to merge 1 commit intotektoncd:mainfrom
chmouel:fix-check-run-rerequest-fork-pr
Draft

fix: recover fork PR re-runs when GitHub omits PR metadata#2655
chmouel wants to merge 1 commit intotektoncd:mainfrom
chmouel:fix-check-run-rerequest-fork-pr

Conversation

@chmouel
Copy link
Copy Markdown
Member

@chmouel chmouel commented Apr 8, 2026

When GitHub receives a Re-run request for a Pipelines as Code check tied to a pull request from a fork, the webhook payload can omit the usual pull request metadata. PAC could already recover by looking up pull requests for the head SHA, but fork PRs still failed when GitHub's commits/{sha}/pulls API returned no matches.

This change makes rerequest handling more resilient and keeps the selection rules safe:

  • check_run rerequests now honor pull request data attached directly to the event when GitHub provides it
  • if the commit lookup API returns no open PR, PAC falls back to listing open pull requests and matching by head SHA so fork PR rerequests can still resolve
  • the SHA fallback now preserves the existing ambiguity guard and fails if more than one open PR shares the same head SHA instead of picking one arbitrarily
  • tests now cover the direct-event path, fork fallback success, missing-PR failures, and the ambiguous fallback regression

📝 Description of the Change

Improve GitHub rerequest PR resolution so fork pull requests can be recovered safely even when GitHub omits PR metadata or the commit lookup API returns no match.

🔗 Linked GitHub Issue

Not linked.

🧪 Testing Strategy

  • Unit tests
  • Integration tests
  • End-to-end tests
  • Manual testing
  • Not Applicable

Ran:

  • go test ./pkg/provider/github

🤖 AI Assistance

  • I have not used any AI assistance for this PR.
  • I have used AI assistance for this PR.

AI assistance was used to help draft and refine the code change, tests, and PR description. The resulting changes were reviewed and validated with the package test run above.

✅ Submitter Checklist

  • 📝 My commit messages are clear, informative, and follow the project's How to write a git commit message guide. The Gitlint linter ensures in CI it's properly validated
  • ✨ I have ensured my commit message prefix (e.g., fix:, feat:) matches the "Type of Change" I selected above.
  • ♽ I have run make test and make lint locally to check for and fix any
    issues. For an efficient workflow, I have considered installing
    pre-commit and running pre-commit install to
    automate these checks.
  • 📖 I have added or updated documentation for any user-facing changes.
  • 🧪 I have added sufficient unit tests for my code changes.
  • 🎁 I have added end-to-end tests where feasible. See README for more details.
  • 🔎 I have addressed any CI test flakiness or provided a clear reason to bypass it.
  • If adding a provider feature, I have filled in the following and updated the provider documentation:
    • GitHub App
    • GitHub Webhook
    • Gitea/Forgejo
    • GitLab
    • Bitbucket Cloud
    • Bitbucket Data Center

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 8, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 96.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.94%. Comparing base (c9be9d6) to head (6ecab48).

Files with missing lines Patch % Lines
pkg/provider/github/parse_payload.go 96.00% 2 Missing and 1 partial ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2655      +/-   ##
==========================================
+ Coverage   58.85%   58.94%   +0.08%     
==========================================
  Files         204      204              
  Lines       20149    20193      +44     
==========================================
+ Hits        11859    11903      +44     
  Misses       7525     7525              
  Partials      765      765              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a fallback mechanism to resolve pull requests by SHA when the standard commit API fails, which is particularly useful for fork PRs. It also adds logic to extract pull request information directly from the check_run event. Feedback focuses on ensuring consistency and handling ambiguity when multiple pull requests might match a single SHA or check run, suggesting the use of existing helper functions to validate that only a single open pull request is processed.

Comment thread pkg/provider/github/parse_payload.go
Comment thread pkg/provider/github/parse_payload.go Outdated
@chmouel chmouel marked this pull request as draft April 8, 2026 09:11
@chmouel chmouel force-pushed the fix-check-run-rerequest-fork-pr branch from 3282631 to 212adf3 Compare April 8, 2026 09:17
@chmouel chmouel marked this pull request as ready for review April 8, 2026 11:07
@chmouel
Copy link
Copy Markdown
Member Author

chmouel commented Apr 8, 2026

tested on dogfooding as working

@chmouel chmouel force-pushed the fix-check-run-rerequest-fork-pr branch from 5f94900 to 8a5873e Compare April 8, 2026 14:06
Comment on lines +362 to +365

// ListPullRequestsWithCommit may return no matches for fork PR commits.
v.Logger.Infof("No PR found via commits API for SHA %s, falling back to open PR listing", runevent.SHA)
return v.findOpenPullRequestBySHA(ctx, runevent.Organization, runevent.Repository, runevent.SHA)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can we call the findOpenPullRequestBySHA only if its fork because if its not fork and there is no matching PRs as well then it would be just redundant API calls! (hint: event.pull_request.head.fork)

Comment thread pkg/provider/github/parse_payload.go Outdated
// If we don't have a pull_request in this it probably mean a push
if len(event.GetCheckRun().GetCheckSuite().PullRequests) == 0 {
if len(event.GetCheckRun().PullRequests) > 0 {
runevent.PullRequestNumber = event.GetCheckRun().PullRequests[0].GetNumber()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is it certain here that first PR would be the exact PR where rerun is made?

Copy link
Copy Markdown
Member Author

@chmouel chmouel Apr 14, 2026

Choose a reason for hiding this comment

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

there is no guarantee enough to keep using PullRequests[0] blindly, so I added a small guard for this path.

check_run.pull_requests now fails fast if GitHub gives us more than one associated PR in the webhook payload instead of guessing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

check_run.pull_requests now fails fast if GitHub gives us more than one associated PR in the webhook payload instead of guessing.

you're going to add or you did it? I don't see anything?

@chmouel chmouel marked this pull request as draft April 14, 2026 13:25
@chmouel chmouel force-pushed the fix-check-run-rerequest-fork-pr branch from 8a5873e to c6a4c49 Compare April 14, 2026 15:30
When someone clicks Re-run on a Pipelines as Code check for a pull
request coming from a fork, GitHub can leave out the usual pull request
details in the webhook payload. That left PAC with no obvious way to
tell which pull request the check belonged to, so the re-run stopped
with an error instead of starting again.

This change teaches PAC to keep looking in a more practical way. If the
first GitHub API says it cannot find the pull request for the commit,
PAC now lists open pull requests in the repository and matches the one
whose head commit SHA is the same. In simple terms: when GitHub does not
hand us the answer directly, we now look through the open pull requests
and find the one built from that exact commit.

The change also checks pull request data attached directly to the
check_run event before falling back to SHA-based lookup, and the tests
now cover both the new fork pull request path and the empty-result error
cases.

Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>
@chmouel chmouel force-pushed the fix-check-run-rerequest-fork-pr branch from c6a4c49 to 6ecab48 Compare April 14, 2026 15:30
Comment on lines +547 to +552
if len(checkSuite.PullRequests) > 0 {
runevent.PullRequestNumber = checkSuite.PullRequests[0].GetNumber()
runevent.TriggerTarget = triggertype.PullRequest
v.Logger.Infof("Recheck of PR %s/%s#%d has been requested", runevent.Organization, runevent.Repository, runevent.PullRequestNumber)
return v.getPullRequest(ctx, runevent)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if len(checkSuite.PullRequests) > 0 {
runevent.PullRequestNumber = checkSuite.PullRequests[0].GetNumber()
runevent.TriggerTarget = triggertype.PullRequest
v.Logger.Infof("Recheck of PR %s/%s#%d has been requested", runevent.Organization, runevent.Repository, runevent.PullRequestNumber)
return v.getPullRequest(ctx, runevent)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

you can delete this as you're already doing this on line 557 below

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants