Skip to content

Commit

Permalink
0.6.1 Fixes (#14)
Browse files Browse the repository at this point in the history
- github: Fix issue with RepoFinder apply
- gh-pr: Show which repo it's working on
  • Loading branch information
pmatseykanets authored Jan 10, 2021
1 parent 3821513 commit a56ef2b
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 7 deletions.
12 changes: 9 additions & 3 deletions cmd/gh-pr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func usage() {
usage := `Automate PR creation across multiple GitHub repositories
usage := `Automate PR creation across GitHub repositories
Usage: gh-pr [flags] [owner][/repo]
owner Repository owner (user or organization)
Expand Down Expand Up @@ -320,12 +320,15 @@ func (p *prmaker) create(ctx context.Context) error {
}
}

fmt.Fprint(p.stderr, repo.GetFullName())

err = p.apply(ctx, repo, scriptFile.Name())
if err != nil {
if err == errNoChanges {
fmt.Fprintln(p.stdout, repo.GetFullName(), "no changes")
fmt.Fprintln(p.stdout, " no changes")
continue
}
fmt.Fprintln(p.stdout)
return err
}

Expand All @@ -336,6 +339,7 @@ func (p *prmaker) create(ctx context.Context) error {
Body: &p.config.desc,
})
if err != nil {
fmt.Fprintln(p.stdout)
return fmt.Errorf("%s: error creating a PR: %s", repo.GetFullName(), err)
}

Expand All @@ -345,18 +349,20 @@ func (p *prmaker) create(ctx context.Context) error {
Reviewers: p.config.reviewers,
})
if err != nil {
fmt.Fprintln(p.stdout)
fmt.Fprintf(p.stderr, "%s: error requesting a PR review: %s\n", repo.GetFullName(), err)
}
}

if len(p.config.assignees) > 0 {
_, _, err = p.gh.Issues.AddAssignees(ctx, p.config.owner, repo.GetName(), prNo, p.config.assignees)
if err != nil {
fmt.Fprintln(p.stdout)
fmt.Fprintf(p.stderr, "%s: error assigning the PR: %s\n", repo.GetFullName(), err)
}
}

fmt.Println(repo.GetFullName(), pr.GetHTMLURL())
fmt.Println("", pr.GetHTMLURL())
}

return nil
Expand Down
14 changes: 11 additions & 3 deletions github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ func (f *RepoFinder) orgRepos(ctx context.Context, filter RepoFilter) ([]*github
}

func apply(repos []*github.Repository, filter RepoFilter) []*github.Repository {
filtered := make([]*github.Repository, len(repos))
var (
filtered = make([]*github.Repository, len(repos))
n int
)
for _, repo := range repos {
if repo.GetArchived() && !filter.Archived {
continue
Expand All @@ -140,8 +143,13 @@ func apply(repos []*github.Repository, filter RepoFilter) []*github.Repository {
continue
}

filtered = append(filtered, repo)
filtered[n] = repo
n++
}

if n == 0 {
return nil
}

return filtered
return filtered[:n]
}
141 changes: 141 additions & 0 deletions github/repo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package github

import (
"reflect"
"regexp"
"testing"

"github.com/google/go-github/v32/github"
)

func boolp(v bool) *bool {
return &v
}

func stringp(v string) *string {
return &v
}

func TestApply(t *testing.T) {
tests := []struct {
desc string
in []*github.Repository
filter RepoFilter
out []*github.Repository
}{
{
desc: "all matched",
in: []*github.Repository{
{Name: stringp("foo")},
{Name: stringp("bar")},
},
filter: RepoFilter{},
out: []*github.Repository{
{Name: stringp("foo")},
{Name: stringp("bar")},
},
},
{
desc: "foo matched",
in: []*github.Repository{
{Name: stringp("foo")},
{Name: stringp("bar")},
},
filter: RepoFilter{
RepoRegexp: regexp.MustCompile("foo"),
},
out: []*github.Repository{
{Name: stringp("foo")},
},
},
{
desc: "no private",
in: []*github.Repository{
{Name: stringp("foo")},
{Name: stringp("bar"), Private: boolp(true)},
},
filter: RepoFilter{
NoPrivate: true,
},
out: []*github.Repository{
{Name: stringp("foo")},
},
},
{
desc: "no public",
in: []*github.Repository{
{Name: stringp("foo")},
{Name: stringp("bar"), Private: boolp(true)},
},
filter: RepoFilter{
NoPublic: true,
},
out: []*github.Repository{
{Name: stringp("bar"), Private: boolp(true)},
},
},
{
desc: "no fork",
in: []*github.Repository{
{Name: stringp("foo")},
{Name: stringp("bar"), Fork: boolp(true)},
},
filter: RepoFilter{
NoFork: true,
},
out: []*github.Repository{
{Name: stringp("foo")},
},
},
{
desc: "no archived by default",
in: []*github.Repository{
{Name: stringp("foo")},
{Name: stringp("bar"), Archived: boolp(true)},
},
filter: RepoFilter{},
out: []*github.Repository{
{Name: stringp("foo")},
},
},
{
desc: "include archived",
in: []*github.Repository{
{Name: stringp("foo")},
{Name: stringp("bar"), Archived: boolp(true)},
},
filter: RepoFilter{
Archived: true,
},
out: []*github.Repository{
{Name: stringp("foo")},
{Name: stringp("bar"), Archived: boolp(true)},
},
},
{
desc: "no matches",
in: []*github.Repository{
{Name: stringp("foo")},
{Name: stringp("bar")},
},
filter: RepoFilter{
RepoRegexp: regexp.MustCompile("baz"),
},
},
{
desc: "empty input",
filter: RepoFilter{},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()

if want, got := tt.out, apply(tt.in, tt.filter); !reflect.DeepEqual(want, got) {
t.Errorf("Expected\n%+v\ngot\n%+v", want, got)
}
})
}
}
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.0"
var Version = "0.6.1"

0 comments on commit a56ef2b

Please sign in to comment.