Skip to content

Commit

Permalink
feat: generates changelog grouped by area and kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszmajsak committed Feb 4, 2021
1 parent 7b1397a commit e734771
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 88 deletions.
48 changes: 38 additions & 10 deletions pkg/cmd/generate/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,47 @@ func NewCmd() *cobra.Command {
pullRequests := fetchPRsSinceLastRelease(repo)
dependencies, otherPRs := extractDepPRs(pullRequests)
dependencies = simplifyDepsPRs(dependencies)
tpl := Default
if cmd.Flag("format").Value.String() == "adoc" {
tpl = DefaultAdoc
}

t, err := template.New("changelog").Funcs(map[string]interface{}{
"withLabel": func(prs []github.PullRequest, label string) []github.PullRequest {
"withLabels": func(prs []github.PullRequest, labels ...string) []github.PullRequest {
prsWithLabel := make([]github.PullRequest, 0)
for i := range prs {
pr := &prs[i]
if Contains(pr.Labels, label) {
if Contains(pr.Labels, labels...) {
prsWithLabel = append(prsWithLabel, *pr)
}
}
return prsWithLabel
},
}).Parse(tpl)
"combine": func(prs []github.PullRequest, title string) ChangeGroup {
return ChangeGroup{
Title: title,
PullRequests: prs,
}
},
}).Parse(formats["changelog."+format])
if err != nil {
return err
}

t, err = t.New("section").Parse(formats["section."+format])
if err != nil {
return err
}
if err := t.Execute(os.Stdout, &Changelog{Release: tag, PullRequests: append(otherPRs, dependencies...)}); err != nil {

if err := t.ExecuteTemplate(os.Stdout, "changelog", &Changelog{
Release: tag,
PullRequests: append(otherPRs, dependencies...),
Areas: map[string]string{
"Command line": "component/cli",
"Operator": "component/operator",
"Build System Integration": "component/ci",
"IDE integration": "component/ide",
"Documentation": "component/docs",
"Project infrastructure": "internal/infra",
"Testing": "internal/test-infra",
},
}); err != nil {
return err
}

Expand All @@ -76,8 +97,8 @@ func NewCmd() *cobra.Command {
}

generateCmd.Flags().StringVarP(&tag, "tag", "t", "UNRELEASED", "tag used for current release")
generateCmd.Flags().StringVar(&format, "format", "md", "format of generated release notes")
generateCmd.Flags().StringVarP(&repo, "repository", "r", "", "repository URL")
generateCmd.Flags().StringVarP(&format, "format", "f", "md", "format of generated release notes [md or adoc]")
generateCmd.Flags().StringVarP(&repo, "repository", "r", "", "repository (org/repoName)")

_ = generateCmd.MarkFlagRequired("repository")
return generateCmd
Expand All @@ -103,6 +124,13 @@ func fetchPRsSinceLastRelease(repoName string) []github.PullRequest {
return filteredPRs
}

var formats = map[string]string{
"changelog.md": Default,
"changelog.adoc": DefaultAdoc,
"section.md": ChangeSection,
"section.adoc": ChangeSectionAdoc,
}

const skipLabel = "skip-changelog"

func shouldSkipInChangelog(labels []string) bool {
Expand Down
136 changes: 58 additions & 78 deletions pkg/cmd/generate/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,108 +4,88 @@ import "github.com/bartoszmajsak/github-changelog-generator/pkg/github"

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

func Contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
type ChangeGroup struct {
Title string
PullRequests []github.PullRequest
}

// TODO withLabels moved here

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

const Default = `
{{- with $prs := (withLabel .PullRequests "kind/enhancement") -}}
{{ if $prs }}
### New features
{{range $pr := $prs }}
const ChangeSection = `{{- if .PullRequests -}}
#### {{ .Title }}
{{range $pr := .PullRequests -}}
* {{$pr.Title}} ([#{{$pr.Number}}]({{$pr.Permalink}})), by [@{{$pr.Author}}](https://github.com/{{$pr.Author}})
{{- end -}}
{{ end }}
{{ end }}
{{ end -}}
{{- end -}}`

{{- with $prs := (withLabel .PullRequests "kind/bug") -}}
{{ if $prs }}
### Bug fixes
{{range $pr := $prs }}
* {{$pr.Title}} ([#{{$pr.Number}}]({{$pr.Permalink}})), by [@{{$pr.Author}}](https://github.com/{{$pr.Author}})
{{- end -}}
{{ end }}
{{ end }}
const ChangeSectionAdoc = `{{- if .PullRequests -}}
==== {{ .Title }}
{{range $pr := .PullRequests -}}
* {{$pr.Title}} ({{$pr.Permalink}}[#{{$pr.Number}}]), by https://github.com/{{$pr.Author}}[@{{$pr.Author}}]
{{ end -}}
{{- end -}}`

{{- with $prs := (withLabel .PullRequests "dependencies") -}}
{{ if $prs }}
### Latest dependencies update
{{range $pr := $prs }}
* {{$pr.Title}} ([#{{$pr.Number}}]({{$pr.Permalink}}))
{{- end -}}
{{ end }}
{{ end }}
const Default = `
{{- range $areaName, $areaLabel := .Areas -}}
{{- $bugs := (withLabels $.PullRequests "kind/bug" $areaLabel) -}}
{{- $features := (withLabels $.PullRequests "kind/enhancement" $areaLabel) -}}
{{- if or $bugs $features -}}
{{- with $prs := (withLabel .PullRequests "internal/infra") -}}
{{ if $prs }}
### Project infrastructure
{{range $pr := $prs }}
* {{$pr.Title}} ([#{{$pr.Number}}]({{$pr.Permalink}})), by [@{{$pr.Author}}](https://github.com/{{$pr.Author}})
### {{ $areaName }}
{{ template "section" (combine $features "New features") }}
{{ template "section" (combine $bugs "Bugs") }}
{{ end -}}
{{- end -}}
{{ end }}
{{ end }}
{{- with $prs := (withLabel .PullRequests "internal/test-infra") -}}
{{ if $prs }}
### Testing
{{- with $prs := (withLabels .PullRequests "dependencies") -}}
{{- if $prs -}}
## Latest dependencies update
{{range $pr := $prs }}
* {{$pr.Title}} ([#{{$pr.Number}}]({{$pr.Permalink}})), by [@{{$pr.Author}}](https://github.com/{{$pr.Author}})
* {{$pr.Title}} ([#{{$pr.Number}}]({{$pr.Permalink}}))
{{- end -}}
{{- end -}}
{{ end }}
{{ end }}
{{- end }}
`

const DefaultAdoc = `
{{- with $prs := (withLabel .PullRequests "kind/enhancement") -}}
{{ if $prs }}
==== New features
{{range $pr := $prs }}
* {{$pr.Title}} ({{$pr.Permalink}}[#{{$pr.Number}}]), by https://github.com/{{$pr.Author}}[@{{$pr.Author}}]
{{- end -}}
{{ end }}
{{ end }}
{{- range $areaName, $areaLabel := .Areas -}}
{{- $bugs := (withLabels $.PullRequests "kind/bug" $areaLabel) -}}
{{- $features := (withLabels $.PullRequests "kind/enhancement" $areaLabel) -}}
{{- if or $bugs $features -}}
{{- with $prs := (withLabel .PullRequests "kind/bug") -}}
{{ if $prs }}
==== Bug fixes
{{range $pr := $prs }}
* {{$pr.Title}} ({{$pr.Permalink}}[#{{$pr.Number}}]), by https://github.com/{{$pr.Author}}[@{{$pr.Author}}]
{{- end -}}
{{ end }}
{{ end }}
=== {{ $areaName }}
{{- with $prs := (withLabel .PullRequests "dependencies") -}}
{{ if $prs }}
==== Latest dependencies update
{{range $pr := $prs }}
* {{$pr.Title}} ({{$pr.Permalink}}[#{{$pr.Number}}])
{{ template "section" (combine $features "New features") }}
{{ template "section" (combine $bugs "Bugs") }}
{{ end -}}
{{- end -}}
{{ end }}
{{ end }}
{{- with $prs := (withLabel .PullRequests "internal/infra") -}}
{{ if $prs }}
==== Project infrastructure
{{- with $prs := (withLabels .PullRequests "dependencies") -}}
{{- if $prs -}}
== Latest dependencies update
{{range $pr := $prs }}
* {{$pr.Title}} ({{$pr.Permalink}}[#{{$pr.Number}}]), by https://github.com/{{$pr.Author}}[@{{$pr.Author}}]
{{- end -}}
{{ end }}
{{ end }}
{{- 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 }}
{{- end }}
`

0 comments on commit e734771

Please sign in to comment.