From ff8783bc11bc4a4c770b438e536a53c5d9264fd6 Mon Sep 17 00:00:00 2001 From: Bartosz Majsak Date: Thu, 16 Apr 2020 16:56:16 +0200 Subject: [PATCH] fix: returns non-zero code when unlabeled PRs found --- Gopkg.lock | 9 +++++++++ pkg/cmd/generate/cmd.go | 27 ++++++++++++++++++++++++--- pkg/github/client.go | 21 +++------------------ pkg/github/pr.go | 2 +- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 0641e13..6f60342 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -154,6 +154,14 @@ revision = "728039f679cbcd4f6a54e080d2219a4c4928c546" version = "v1.4.0" +[[projects]] + digest = "1:4047c378584616813d610c9f993bf90dd0d07aed8d94bd3bc299cd35ececdcba" + name = "github.com/pkg/errors" + packages = ["."] + pruneopts = "NT" + revision = "614d223910a179a466c1767a985424175c39b465" + version = "v0.9.1" + [[projects]] digest = "1:d476ba4b13770e960d8e1b89d33ba00b3c028c80f4f33191b6f1766defc76b7e" name = "github.com/shurcooL/githubv4" @@ -380,6 +388,7 @@ "github.com/onsi/ginkgo", "github.com/onsi/ginkgo/reporters", "github.com/onsi/gomega", + "github.com/pkg/errors", "github.com/shurcooL/githubv4", "github.com/spf13/cobra", "github.com/spf13/pflag", diff --git a/pkg/cmd/generate/cmd.go b/pkg/cmd/generate/cmd.go index e0b6fea..9bf0fe0 100644 --- a/pkg/cmd/generate/cmd.go +++ b/pkg/cmd/generate/cmd.go @@ -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" @@ -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) @@ -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 }, } diff --git a/pkg/github/client.go b/pkg/github/client.go index 60209b4..d81b0e7 100644 --- a/pkg/github/client.go +++ b/pkg/github/client.go @@ -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, @@ -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) } diff --git a/pkg/github/pr.go b/pkg/github/pr.go index 536a2f3..8d92d55 100644 --- a/pkg/github/pr.go +++ b/pkg/github/pr.go @@ -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