Skip to content

Commit

Permalink
fix getting pull requests that come from forks
Browse files Browse the repository at this point in the history
  • Loading branch information
alankpatel committed Nov 8, 2024
1 parent 5701087 commit 3f90bda
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
35 changes: 23 additions & 12 deletions pull/pull_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,33 @@ import (
// ListOpenPullRequestsForSHA returns all pull requests where the HEAD of the source branch
// in the pull request matches the given SHA.
func ListOpenPullRequestsForSHA(ctx context.Context, client *github.Client, owner, repoName, SHA string) ([]*github.PullRequest, error) {
prs, _, err := client.PullRequests.ListPullRequestsWithCommit(ctx, owner, repoName, SHA, &github.ListOptions{
// In practice, there should be at most 1-3 PRs for a given commit. In
// exceptional cases, if there are more than 100 PRs, we'll only
// consider the first 100 to avoid paging.
PerPage: 100,
})
if err != nil {
return nil, errors.Wrapf(err, "failed to list pull requests for repository %s/%s", owner, repoName)
var results []*github.PullRequest
logger := zerolog.Ctx(ctx)

opts := &github.PullRequestListOptions{
State: "open",
ListOptions: github.ListOptions{
PerPage: 100,
},
}

var results []*github.PullRequest
for _, pr := range prs {
if pr.GetState() == "open" && pr.GetHead().GetSHA() == SHA {
results = append(results, pr)
for {
prs, resp, err := client.PullRequests.List(ctx, owner, repoName, opts)
if err != nil {
return results, errors.Wrapf(err, "failed to list pull requests for repository %s/%s", owner, repoName)
}
for _, pr := range prs {
if pr.Head.GetSHA() == SHA {
logger.Debug().Msgf("found open pull request with sha %s", pr.Head.GetSHA())
results = append(results, pr)
}
}
if resp.NextPage == 0 {
break
}
opts.ListOptions.Page = resp.NextPage
}

return results, nil
}

Expand Down
9 changes: 8 additions & 1 deletion server/handler/check_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func (h *CheckRun) Handle(ctx context.Context, eventType, deliveryID string, pay
}

repo := event.GetRepo()
owner := repo.GetOwner().GetLogin()
repoName := repo.GetName()

installationID := githubapp.GetInstallationIDFromEvent(&event)

ctx, logger := githubapp.PrepareRepoContext(ctx, installationID, repo)
Expand All @@ -54,7 +57,11 @@ func (h *CheckRun) Handle(ctx context.Context, eventType, deliveryID string, pay
return errors.Wrap(err, "failed to instantiate github client")
}

prs := event.GetCheckRun().PullRequests
prs, err := pull.ListOpenPullRequestsForSHA(ctx, client, owner, repoName, event.GetCheckRun().GetHeadSHA())
if err != nil {
return errors.Wrap(err, "failed to determine open pull requests matching the status context change")
}

if len(prs) == 0 {
logger.Debug().Msg("Doing nothing since status change event affects no open pull requests")
return nil
Expand Down

0 comments on commit 3f90bda

Please sign in to comment.