Skip to content

Commit

Permalink
fix: returns non-zero code when unlabeled PRs found
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszmajsak committed Apr 16, 2020
1 parent 75641c3 commit ff8783b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 24 additions & 3 deletions pkg/cmd/generate/cmd.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package generate

import (
"fmt"
"os"
"regexp"
"sort"
"strings"
"text/template"

"github.com/pkg/errors"

"github.com/bartoszmajsak/github-changelog-generator/pkg/github"

"github.com/bartoszmajsak/github-changelog-generator/pkg/check"
Expand All @@ -21,9 +24,10 @@ func NewCmd() *cobra.Command {
tag string
)
generateCmd := &cobra.Command{
Use: "generate",
Short: "Generates changelog for a given from",
SilenceUsage: true,
Use: "generate",
Short: "Generates changelog for a given from",
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error { //nolint[:unparam]
pullRequests := fetchPRsSinceLastRelease(repo)
dependencies, otherPRs := extractDepPRs(pullRequests)
Expand All @@ -50,6 +54,23 @@ func NewCmd() *cobra.Command {
if err := t.Execute(os.Stdout, &Changelog{Release: tag, PullRequests: append(otherPRs, dependencies...)}); err != nil {
return err
}

unlabeledPRs := 0
for i := range pullRequests {
pr := pullRequests[i]

if len(pr.Labels) == 0 {
unlabeledPRs++
if unlabeledPRs == 1 {
_, _ = fmt.Fprint(os.Stderr, "#### Found unlabeled PRs\n\n")
}
_, _ = fmt.Fprintf(os.Stderr, "* %s\n", pr.Permalink)
}
}

if unlabeledPRs > 0 {
return errors.Errorf("found %d unlabeled PRs", unlabeledPRs)
}
return nil
},
}
Expand Down
21 changes: 3 additions & 18 deletions pkg/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,11 @@ func FindAssociatedPRs(client *githubv4.Client, repo []string, matchingCommit Ma
})
check.IfError(err)

var (
prs []PullRequest
unlabeledPRs bool
)
var prs []PullRequest

for _, node := range associatedPRs.Repository.DefaultBranch.Target.Commit.History.Nodes {
if node.Oid != matchingCommit.Repository.Object.Commit.Oid && node.MessageHeadline != "release: next iteration" {
for _, pr := range node.AssociatedPullRequests.Nodes {
if len(pr.Labels.Nodes) == 0 {
unlabeledPRs = true
}
prs = append(prs, PullRequest{
RelatedCommit: Commit{
Hash: node.Oid,
Expand All @@ -122,15 +117,5 @@ func FindAssociatedPRs(client *githubv4.Client, repo []string, matchingCommit Ma
}
}
}
withoutDuplicates := removeDuplicates(prs)
if unlabeledPRs {
_, _ = fmt.Fprint(os.Stderr, "#### Found unlabeled PRs\n\n")
for i := range withoutDuplicates {
pr := withoutDuplicates[i]
if len(pr.Labels) == 0 {
_, _ = fmt.Fprintf(os.Stderr, "* %s\n", pr.Permalink)
}
}
}
return withoutDuplicates
return withoutDuplicates(prs)
}
2 changes: 1 addition & 1 deletion pkg/github/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func extractLabels(nodes []struct{ Name string }) []string {
return labels
}

func removeDuplicates(elements []PullRequest) []PullRequest {
func withoutDuplicates(elements []PullRequest) []PullRequest {
found := map[int]bool{}
var result []PullRequest

Expand Down

0 comments on commit ff8783b

Please sign in to comment.