Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions git-open-pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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)
}
Expand Down