From 20dfe0d44728a813027e9f4496beb4f9f98f0557 Mon Sep 17 00:00:00 2001 From: Faizudeen Kajogbola Date: Mon, 7 Sep 2026 07:20:46 +0200 Subject: [PATCH] fix: fetch all pages when checking fork branches The branch existence check inspected only the first page of results from the GitHub branches API, so a fork with more than 100 branches could report the freshly pushed branch as missing and abort before the pull request was opened. Extract the lookup into ForkBranches and follow Response.NextPage until GitHub stops advertising a next page. The branches endpoint uses page-number pagination, so NextPage is the correct cursor to follow. --- git-open-pull.go | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/git-open-pull.go b/git-open-pull.go index cd75e08..688d57f 100644 --- a/git-open-pull.go +++ b/git-open-pull.go @@ -78,6 +78,28 @@ func GetIssueNumber(ctx context.Context, client *github.Client, settings *Settin return detected, nil } +// ForkBranches returns the names of all of the branches in the user's fork +func ForkBranches(ctx context.Context, client *github.Client, settings *Settings) ([]string, error) { + opts := &github.BranchListOptions{ListOptions: github.ListOptions{PerPage: 100}} + var o []string + for { + branches, resp, err := client.Repositories.ListBranches(ctx, settings.User, settings.BaseRepo, opts) + if err != nil { + return nil, err + } + for _, b := range branches { + if b.Name != nil { + o = append(o, *b.Name) + } + } + if resp.NextPage == 0 { + break + } + opts.Page = resp.NextPage + } + return o, nil +} + func printUsage(settings *Settings) { out := flag.CommandLine.Output() fmt.Fprintln(out, "git-open-pull creates an issue, renames the local branch to include that issue number, pushes the renamed branch and finally converts the issue into a pull request against the renamed branch.") @@ -263,26 +285,20 @@ func main() { time.Sleep(2 * time.Second) // check branch exists on remote - branches, _, err := client.Repositories.ListBranches(ctx, settings.User, settings.BaseRepo, &github.BranchListOptions{ListOptions: github.ListOptions{PerPage: 100}}) + branches, err := ForkBranches(ctx, client, settings) if err != nil { log.Fatal(err) } var foundBranch bool for _, b := range branches { - if *b.Name == branch { + if b == branch { foundBranch = true } } if !foundBranch { fmt.Printf("Error: branch %s does not exist in %s/%s\n", branch, settings.User, settings.BaseRepo) if len(branches) > 1 { - fmt.Printf("valid branches are:") - for i, b := range branches { - if i > 0 { - fmt.Print(", ") - } - fmt.Printf("%s", *b.Name) - } + fmt.Printf("valid branches are:%s", strings.Join(branches, ", ")) } os.Exit(1) }