Skip to content

Commit

Permalink
gh-pr: Output PR url when the remote branch exists (#15)
Browse files Browse the repository at this point in the history
Additionally:

- `RepoFinder`: fetch max possible repos per request
  • Loading branch information
pmatseykanets authored Jan 11, 2021
1 parent a56ef2b commit 5913edb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
37 changes: 35 additions & 2 deletions cmd/gh-pr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,12 @@ func (p *prmaker) create(ctx context.Context) error {
_, resp, err := p.gh.Repositories.GetBranch(ctx, p.config.owner, repo.GetName(), p.config.branch)
switch err {
case nil:
fmt.Fprintln(p.stderr, repo.GetFullName(), "The remote branch already exists")
pullURL := ""
pull, err := p.getPullForBranch(ctx, repo, p.config.branch)
if err == nil {
pullURL = pull.GetHTMLURL()
}
fmt.Fprintln(p.stderr, repo.GetFullName(), "The remote branch already exists", pullURL)
continue
default:
if resp != nil && resp.StatusCode != http.StatusNotFound {
Expand Down Expand Up @@ -368,7 +373,35 @@ func (p *prmaker) create(ctx context.Context) error {
return nil
}

var errNoChanges = fmt.Errorf("no changes")
func (p *prmaker) getPullForBranch(ctx context.Context, repo *github.Repository, branch string) (*github.PullRequest, error) {
var (
pulls []*github.PullRequest
resp *github.Response
err error
opts = &github.PullRequestListOptions{ListOptions: github.ListOptions{PerPage: 100}}
)
for {
pulls, resp, err = p.gh.PullRequests.List(ctx, p.config.owner, repo.GetName(), opts)
if err != nil {
return nil, fmt.Errorf("%s: can't read pull requests: %s", repo.GetName(), err)
}

for _, pull := range pulls {
if pull.GetHead().GetRef() == branch {
return pull, nil
}
}

if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}

return nil, nil
}

var errNoChanges = fmt.Errorf("no changes were made")

func (p *prmaker) apply(ctx context.Context, repo *github.Repository, scriptPath string) error {
dir, err := ioutil.TempDir("", "gh-pr")
Expand Down
18 changes: 9 additions & 9 deletions github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ func (f *RepoFinder) Find(ctx context.Context, filter RepoFilter) ([]*github.Rep
return repos, err
}

var listOptions = github.ListOptions{PerPage: 100}

func (f *RepoFinder) userRepos(ctx context.Context, filter RepoFilter) ([]*github.Repository, error) {
opt := &github.RepositoryListOptions{
ListOptions: github.ListOptions{PerPage: 30},
opts := &github.RepositoryListOptions{
ListOptions: listOptions,
Affiliation: "owner",
}
var (
Expand All @@ -73,7 +75,7 @@ func (f *RepoFinder) userRepos(ctx context.Context, filter RepoFilter) ([]*githu
err error
)
for {
repos, resp, err = f.Client.Repositories.List(ctx, filter.Owner, opt)
repos, resp, err = f.Client.Repositories.List(ctx, filter.Owner, opts)
if err != nil {
return nil, fmt.Errorf("can't read repositories: %s", err)
}
Expand All @@ -83,23 +85,21 @@ func (f *RepoFinder) userRepos(ctx context.Context, filter RepoFilter) ([]*githu
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
opts.Page = resp.NextPage
}

return filtered, nil
}

func (f *RepoFinder) orgRepos(ctx context.Context, filter RepoFilter) ([]*github.Repository, error) {
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 30},
}
opts := &github.RepositoryListByOrgOptions{ListOptions: listOptions}
var (
filtered, repos []*github.Repository
resp *github.Response
err error
)
for {
repos, resp, err = f.Client.Repositories.ListByOrg(ctx, filter.Owner, opt)
repos, resp, err = f.Client.Repositories.ListByOrg(ctx, filter.Owner, opts)
if err != nil {
return nil, fmt.Errorf("can't read repositories: %s", err)
}
Expand All @@ -109,7 +109,7 @@ func (f *RepoFinder) orgRepos(ctx context.Context, filter RepoFilter) ([]*github
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
opts.Page = resp.NextPage
}

return filtered, nil
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package version

var Version = "0.6.1"
var Version = "0.6.2"

0 comments on commit 5913edb

Please sign in to comment.