Skip to content

Commit

Permalink
fix: filters prs by labels when executing template
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszmajsak committed Dec 20, 2019
1 parent 1766fd8 commit 95450f5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 43 deletions.
49 changes: 35 additions & 14 deletions pkg/cmd/generate/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,28 @@ func NewCmd() *cobra.Command {
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error { //nolint[:unparam]
pullRequests := fetchPRsSinceLastRelease(repo)
simplifyDepsPRs(pullRequests)
dependencies, otherPRs := extractDepPRs(pullRequests)
dependencies = simplifyDepsPRs(dependencies)
tpl := Default
if cmd.Flag("format").Value.String() == "adoc" {
tpl = DefaultAdoc
}
t, err := template.New("changelog").Parse(tpl)
t, err := template.New("changelog").Funcs(map[string]interface{}{
"withLabel": func(prs []github.PullRequest, label string) []github.PullRequest {
prsWithLabel := make([]github.PullRequest, 0)
for i := range prs {
pr := &prs[i]
if Contains(pr.Labels, label) {
prsWithLabel = append(prsWithLabel, *pr)
}
}
return prsWithLabel
},
}).Parse(tpl)
if err != nil {
return err
}
if err := t.Execute(os.Stdout, &Changelog{Release: tag, PullRequests: pullRequests}); err != nil {
if err := t.Execute(os.Stdout, &Changelog{Release: tag, PullRequests: append(otherPRs, dependencies...)}); err != nil {
return err
}
return nil
Expand All @@ -49,28 +61,24 @@ func NewCmd() *cobra.Command {
return generateCmd
}

func fetchPRsSinceLastRelease(repoName string) map[string][]github.PullRequest {
func fetchPRsSinceLastRelease(repoName string) []github.PullRequest {
check.RepoFormat(repoName)
repo := strings.Split(repoName, "/")
client := github.CreateClient()
previousRelease, _ := github.LatestReleaseOf(repo[0], repo[1])
matchingCommit := github.FindMatchingCommit(client, repo, previousRelease)
prs := github.FindAssociatedPRs(client, repo, matchingCommit)

prsByLabels := make(map[string][]github.PullRequest)
filteredPRs := make([]github.PullRequest, 0)
for i := range prs {
pr := prs[i]
label := "misc"
if shouldSkipInChangelog(pr.Labels) {
continue
}
if len(pr.Labels) > 0 {
label = pr.Labels[0]
}
prsByLabels[label] = append(prsByLabels[label], pr)
filteredPRs = append(filteredPRs, pr)
}

return prsByLabels
return filteredPRs
}

const skipLabel = "skip-changelog"
Expand All @@ -86,8 +94,7 @@ func shouldSkipInChangelog(labels []string) bool {

const dependabotPrefix = "build(deps): bump "

func simplifyDepsPRs(prsByLabels map[string][]github.PullRequest) {
dependencies := prsByLabels["dependencies"]
func simplifyDepsPRs(dependencies []github.PullRequest) []github.PullRequest {
sort.SliceStable(dependencies, func(i, j int) bool {
return strings.Compare(dependencies[i].Title, dependencies[j].Title) < 0
})
Expand All @@ -114,5 +121,19 @@ func simplifyDepsPRs(prsByLabels map[string][]github.PullRequest) {
return strings.Compare(latestPrs[i].Title, latestPrs[j].Title) < 0
})

prsByLabels["dependencies"] = latestPrs
return latestPrs
}

func extractDepPRs(prs []github.PullRequest) (depPRs, otherPRs []github.PullRequest) {
depPRs = make([]github.PullRequest, 0)
otherPRs = make([]github.PullRequest, 0)
for i := range prs {
pr := &prs[i]
if Contains(pr.Labels, "dependencies") {
depPRs = append(depPRs, prs[i])
} else {
otherPRs = append(otherPRs, prs[i])
}
}
return
}
49 changes: 20 additions & 29 deletions pkg/cmd/generate/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ import "github.com/bartoszmajsak/github-changelog-generator/pkg/github"

type Changelog struct {
Release string
PullRequests map[string][]github.PullRequest
PullRequests []github.PullRequest
}

func Contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

const Default = `
{{- with $prs := (index .PullRequests "enhancement") -}}
{{- with $prs := (withLabel .PullRequests "kind/enhancement") -}}
{{ if $prs }}
### New features
{{range $pr := $prs }}
Expand All @@ -17,7 +26,7 @@ const Default = `
{{ end }}
{{ end }}
{{- with $prs := (index .PullRequests "bug") -}}
{{- with $prs := (withLabel .PullRequests "kind/bug") -}}
{{ if $prs }}
### Bug fixes
{{range $pr := $prs }}
Expand All @@ -26,7 +35,7 @@ const Default = `
{{ end }}
{{ end }}
{{- with $prs := (index .PullRequests "dependencies") -}}
{{- with $prs := (withLabel .PullRequests "dependencies") -}}
{{ if $prs }}
### Latest dependencies update
{{range $pr := $prs }}
Expand All @@ -35,7 +44,7 @@ const Default = `
{{ end }}
{{ end }}
{{- with $prs := (index .PullRequests "infra") -}}
{{- with $prs := (withLabel .PullRequests "internal/infra") -}}
{{ if $prs }}
### Project infrastructure
{{range $pr := $prs }}
Expand All @@ -44,27 +53,18 @@ const Default = `
{{ end }}
{{ end }}
{{- with $prs := (index .PullRequests "test-infra") -}}
{{- with $prs := (withLabel .PullRequests "internal/test-infra") -}}
{{ if $prs }}
### Testing
{{range $pr := $prs }}
* {{$pr.Title}} ([#{{$pr.Number}}]({{$pr.Permalink}})), by [@{{$pr.Author}}](https://github.com/{{$pr.Author}})
{{- end -}}
{{ end }}
{{ end }}
{{- with $prs := (index .PullRequests "misc") -}}
{{ if $prs }}
### Misc
{{range $pr := $prs }}
* {{$pr.Title}} ([#{{$pr.Number}}]({{$pr.Permalink}})), by [@{{$pr.Author}}](https://github.com/{{$pr.Author}})
{{- end -}}
{{ end }}
{{ end }}
`

const DefaultAdoc = `
{{- with $prs := (index .PullRequests "enhancement") -}}
{{- with $prs := (withLabel .PullRequests "kind/enhancement") -}}
{{ if $prs }}
=== New features
{{range $pr := $prs }}
Expand All @@ -73,7 +73,7 @@ const DefaultAdoc = `
{{ end }}
{{ end }}
{{- with $prs := (index .PullRequests "bug") -}}
{{- with $prs := (withLabel .PullRequests "kind/bug") -}}
{{ if $prs }}
=== Bug fixes
{{range $pr := $prs }}
Expand All @@ -82,7 +82,7 @@ const DefaultAdoc = `
{{ end }}
{{ end }}
{{- with $prs := (index .PullRequests "dependencies") -}}
{{- with $prs := (withLabel .PullRequests "dependencies") -}}
{{ if $prs }}
=== Latest dependencies update
{{range $pr := $prs }}
Expand All @@ -91,7 +91,7 @@ const DefaultAdoc = `
{{ end }}
{{ end }}
{{- with $prs := (index .PullRequests "infra") -}}
{{- with $prs := (withLabel .PullRequests "internal/infra") -}}
{{ if $prs }}
=== Project infrastructure
{{range $pr := $prs }}
Expand All @@ -100,21 +100,12 @@ const DefaultAdoc = `
{{ end }}
{{ end }}
{{- with $prs := (index .PullRequests "test-infra") -}}
{{- with $prs := (withLabel .PullRequests "internal/test-infra") -}}
{{ if $prs }}
=== Testing
{{range $pr := $prs }}
* {{$pr.Title}} ({{$pr.Permalink}}[#{{$pr.Number}}]), by https://github.com/{{$pr.Author}}[@{{$pr.Author}}]
{{- end -}}
{{ end }}
{{ end }}
{{- with $prs := (index .PullRequests "misc") -}}
{{ if $prs }}
=== Misc
{{range $pr := $prs }}
* {{$pr.Title}} ([#{{$pr.Number}}]({{$pr.Permalink}})), by [@{{$pr.Author}}](https://github.com/{{$pr.Author}})
{{- end -}}
{{ end }}
{{ end }}
`

0 comments on commit 95450f5

Please sign in to comment.