Skip to content

Commit

Permalink
Filter pull requests pulled by Bulldozer (#559)
Browse files Browse the repository at this point in the history
* Filter pull requests pulled by Bulldozer

* remove prefix check

---------

Co-authored-by: alanpatel <[email protected]>
  • Loading branch information
alankpatel and alanpatel authored Oct 30, 2024
1 parent 00e56cb commit f35f98d
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions pull/pull_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package pull

import (
"context"
"fmt"
"strings"

"github.com/google/go-github/v65/github"
"github.com/pkg/errors"
Expand All @@ -26,49 +26,34 @@ 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) {
var results []*github.PullRequest

openPRs, err := ListOpenPullRequests(ctx, client, owner, repoName)

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, err
return nil, errors.Wrapf(err, "failed to list pull requests for repository %s/%s", owner, repoName)
}

for _, openPR := range openPRs {
if openPR.Head.GetSHA() == SHA {
results = append(results, openPR)
var results []*github.PullRequest
for _, pr := range prs {
if pr.GetState() == "open" && pr.GetHead().GetSHA() == SHA {
results = append(results, pr)
}
}

return results, nil
}

func ListOpenPullRequestsForRef(ctx context.Context, client *github.Client, owner, repoName, ref string) ([]*github.PullRequest, error) {
var results []*github.PullRequest
logger := zerolog.Ctx(ctx)

openPRs, err := ListOpenPullRequests(ctx, client, owner, repoName)

if err != nil {
return nil, err
}

for _, openPR := range openPRs {
formattedRef := fmt.Sprintf("refs/heads/%s", openPR.GetBase().GetRef())
logger.Debug().Msgf("found open pull request with base ref %s", formattedRef)
if formattedRef == ref {
results = append(results, openPR)
}
}

return results, nil
}

func ListOpenPullRequests(ctx context.Context, client *github.Client, owner, repoName string) ([]*github.PullRequest, error) {
var results []*github.PullRequest
ref = strings.TrimPrefix(ref, "refs/heads/")

opts := &github.PullRequestListOptions{
State: "open",
Base: ref, // Filter by base branch name
ListOptions: github.ListOptions{
PerPage: 100,
},
Expand All @@ -77,16 +62,18 @@ func ListOpenPullRequests(ctx context.Context, client *github.Client, owner, rep
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)
return nil, errors.Wrapf(err, "failed to list pull requests for repository %s/%s", owner, repoName)
}
for _, pr := range prs {
logger.Debug().Msgf("found open pull request with base ref %s", pr.GetBase().GetRef())
results = append(results, pr)
}
if resp.NextPage == 0 {
break
}
opts.ListOptions.Page = resp.NextPage
opts.Page = resp.NextPage
}

return results, nil

}

0 comments on commit f35f98d

Please sign in to comment.