Skip to content

Commit 36a794c

Browse files
committed
Include in-order grouped template by default
Changelog now includes flat and grouped templates by default, choosing to group templates according to the Groupings array in external config. Grouped output now ensures the order of groupings based on the order they're declared in external config. Adds simple tests for changelog write outputs.
1 parent 9990aad commit 36a794c

File tree

4 files changed

+306
-66
lines changed

4 files changed

+306
-66
lines changed

README.md

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ Help Options:
3434

3535
The changelog output is written to standard output and can be redirected to overwrite or append to a file.
3636

37+
### Limitations
38+
39+
As this tool uses GitHub's comparison API for details, there are a few limitations to output:
40+
41+
* Limited to 250 commits
42+
* Limited to 5000 API requests per hour
43+
44+
See the [GitHub Commits API](https://developer.github.com/v3/repos/commits/#compare-two-commits) for additional details.
45+
3746
### Basic
3847

3948
The changelog generator doesn't assume a start or end tag, and doesn't evaluate existing tags to determine tag order. If `from` and `to` options are not provided, your changelog will result in the single latest commit on `master`.
@@ -70,22 +79,56 @@ You may specify `GITHUB_OWNER` and `GITHUB_REPO` as environment variables for us
7079
<em>For more details, see <a href="https://github.com/jimschubert/kopper/compare/v0.0.2...v0.0.3">v0.0.2..v0.0.3</a></em>
7180
```
7281

73-
#### Default Template
82+
#### Templating
83+
84+
The default template used in basic usage will output Markdown in flatten or grouped display (see later for configuration options). The template is defined as:
7485

75-
The default template used in basic usage will output Markdown. The template is defined as:
86+
```gotemplate
87+
{{define "GroupTemplate" -}}
88+
{{- range .Grouped}}
89+
### {{ .Name }}
7690
91+
{{range .Items -}}
92+
* [{{.CommitHashShort}}]({{.CommitURL}}) {{.Title}} ({{if .IsPull}}[contributed]({{.PullURL}}) by {{end}}[{{.Author}}]({{.AuthorURL}}))
93+
{{end -}}
94+
{{end -}}
95+
{{end -}}
96+
{{define "FlatTemplate" -}}
97+
{{range .Items -}}
98+
* [{{.CommitHashShort}}]({{.CommitURL}}) {{.Title}} ({{if .IsPull}}[contributed]({{.PullURL}}) by {{end}}[{{.Author}}]({{.AuthorURL}}))
99+
{{end -}}
100+
{{end -}}
101+
{{define "DefaultTemplate" -}}
102+
## {{.Version}}
103+
{{if len .Grouped -}}
104+
{{template "GroupTemplate" . -}}
105+
{{- else}}
106+
{{template "FlatTemplate" . -}}
107+
{{end}}
108+
<em>For more details, see <a href="{{.CompareURL}}">{{.PreviousVersion}}..{{.Version}}</a></em>
109+
{{end -}}
110+
{{template "DefaultTemplate" . -}}
77111
```
112+
113+
Groupings will be displayed in the order they're defined in your external configuration.
114+
115+
You must define an external JSON configuration file to override the default template. For example, suppose you want to display flat commit history and link to diff, patch, and compare URLs. You could define a template like so:
116+
117+
```gotemplate
78118
## {{.Version}}
79119
80120
{{range .Items -}}
81121
* [{{.CommitHashShort}}]({{.CommitURL}}) {{.Title}} ({{if .IsPull}}[contributed]({{.PullURL}}) by {{end}}[{{.Author}}]({{.AuthorURL}}))
82122
{{end}}
83123
84-
<em>For more details, see <a href="{{.CompareURL}}">{{.PreviousVersion}}..{{.Version}}</a></em>
124+
### Links
125+
<ul>
126+
<li><a href="{{.CompareURL}}">Compare {{.PreviousVersion}}..{{.Version}}</a></li>
127+
<li><a href="{{.DiffURL}}">Diff {{.PreviousVersion}}..{{.Version}}</a></li>
128+
<li><a href="{{.PatchURL}}">Patch {{.PreviousVersion}}..{{.Version}}</a></li>
129+
</ul>
85130
```
86131

87-
Currently, you must define an external JSON configuration file to override the default template.
88-
89132
## Install
90133

91134
Latest binary releases are available via [GitHub Releases](https://github.com/jimschubert/changelog/releases).
@@ -140,29 +183,30 @@ More advanced scenarios require an external JSON configuration object which can
140183
}
141184
```
142185

143-
### Grouping
186+
### Custom templating
144187

145-
Grouping is done by the `name` property of the groupings array objects. You will want to provide a custom template to display groupings.
188+
Grouping is done by the `name` property of the groupings array objects, in the order in which groupings are declared.
189+
Groupings are displayed by default, but suppose you want to provide a custom template to display grouping differently. In this example, we'll only display the author name if the commit comes from a pull request.
146190

147-
For example, create a directory at `/tmp/changelog` to contain a sample JSON and template.
191+
First, create a directory at `/tmp/changelog` to contain a sample JSON and template.
148192

149193
Save the follow **template** as `template.tmpl`:
150194

151195
```gotemplate
152196
## {{.Version}}
153197
154-
{{range $key, $value := .Grouped -}}
155-
### {{ $key }}
198+
{{range .Grouped -}}
199+
### {{ .Name }}
156200
157-
{{range $value -}}
158-
* [{{.CommitHashShort}}]({{.CommitURL}}) {{.Title}} ({{if .IsPull}}[contributed]({{.PullURL}}) by {{end}}[{{.Author}}]({{.AuthorURL}}))
201+
{{range .Items -}}
202+
* [{{.CommitHashShort}}]({{.CommitURL}}) {{.Title}}{{if .IsPull}} ([contributed]({{.PullURL}}) by [{{.Author}}]({{.AuthorURL}})){{end}}
159203
{{end}}
160204
{{end}}
161205
162206
<em>For more details, see <a href="{{.CompareURL}}">{{.PreviousVersion}}..{{.Version}}</a></em>
163207
```
164208

165-
And save the following as `config.json` (note: template currently requires a full path to the template file):
209+
Save the following as `config.json` (note: template currently requires a full path to the template file):
166210

167211
```json
168212
{
@@ -232,22 +276,24 @@ This changelog output in `/tmp/changelog/CHANGELOG.md` should look like this:
232276
233277
### Fixes
234278
235-
* [4c3e498021](https://github.com/cli/cli/commit/4c3e498021997b40d3c78f8c858ed734f819b064) Fix column alignment and truncation for Eastern Asian languages ([mislav](https://github.com/mislav))
236-
* [4ee995dafd](https://github.com/cli/cli/commit/4ee995dafdf98730c292c63c1b8a0fab5f2198d1) fix(486): Getting issue list on no remotes specified ([yashLadha](https://github.com/yashLadha))
237-
* [f9649ebddd](https://github.com/cli/cli/commit/f9649ebddd1b6a9731046c98cd8019a245c82fde) Merge pull request #521 from yashLadha/bug/issue_list_on_no_remote ([mislav](https://github.com/mislav))
279+
* [f9649ebddd](https://github.com/cli/cli/commit/f9649ebddd1b6a9731046c98cd8019a245c82fde) Merge pull request #521 from yashLadha/bug/issue_list_on_no_remote ([contributed](https://github.com/cli/cli/pull/521) by [mislav](https://github.com/mislav))
280+
* [4ee995dafd](https://github.com/cli/cli/commit/4ee995dafdf98730c292c63c1b8a0fab5f2198d1) fix(486): Getting issue list on no remotes specified
281+
* [4c3e498021](https://github.com/cli/cli/commit/4c3e498021997b40d3c78f8c858ed734f819b064) Fix column alignment and truncation for Eastern Asian languages
238282
239283
### Other Contributions
240284
241-
* [4727fc4659](https://github.com/cli/cli/commit/4727fc465982d3029324fc5b77ee37e28c29a2b3) Ensure descriptive error when no github.com remotes found ([mislav](https://github.com/mislav))
242-
* [69304ce9af](https://github.com/cli/cli/commit/69304ce9af6100e49bb6a128a81639d48ac590ec) Merge pull request #518 from cli/eastern-asian ([mislav](https://github.com/mislav))
243-
* [1a82e39ba9](https://github.com/cli/cli/commit/1a82e39ba9627654aca22e9608d5b81589855d41) Respect title & body from arguments to `pr create -w` ([mislav](https://github.com/mislav))
244-
* [b5d0b7c640](https://github.com/cli/cli/commit/b5d0b7c640ad897f395a72074a0f4b31787e5826) Merge pull request #523 from cli/title-body-web ([mislav](https://github.com/mislav))
285+
* [b5d0b7c640](https://github.com/cli/cli/commit/b5d0b7c640ad897f395a72074a0f4b31787e5826) Merge pull request #523 from cli/title-body-web ([contributed](https://github.com/cli/cli/pull/523) by [mislav](https://github.com/mislav))
286+
* [1a82e39ba9](https://github.com/cli/cli/commit/1a82e39ba9627654aca22e9608d5b81589855d41) Respect title & body from arguments to `pr create -w`
287+
* [69304ce9af](https://github.com/cli/cli/commit/69304ce9af6100e49bb6a128a81639d48ac590ec) Merge pull request #518 from cli/eastern-asian ([contributed](https://github.com/cli/cli/pull/518) by [mislav](https://github.com/mislav))
288+
* [4727fc4659](https://github.com/cli/cli/commit/4727fc465982d3029324fc5b77ee37e28c29a2b3) Ensure descriptive error when no github.com remotes found
245289
246290
247291
248292
<em>For more details, see <a href="https://github.com/cli/cli/compare/v0.5.6...v0.5.7">v0.5.6..v0.5.7</a></em>
249293
```
250294

295+
Notice that this differs from the default in that it removes the committer name from the two commits in each section which were not pull requests.
296+
251297
### Debugging
252298

253299
You may debug select operations such as groupings and exclusions by exporting `LOG_LEVEL=debug`.

changelog.go

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,30 @@ import (
2323

2424
const emptyTree = "master~1"
2525
const defaultEnd = "master"
26-
const defaultTemplate = `
27-
## {{.Version}}
26+
const defaultTemplate = `{{define "GroupTemplate" -}}
27+
{{- range .Grouped}}
28+
### {{ .Name }}
2829
2930
{{range .Items -}}
3031
* [{{.CommitHashShort}}]({{.CommitURL}}) {{.Title}} ({{if .IsPull}}[contributed]({{.PullURL}}) by {{end}}[{{.Author}}]({{.AuthorURL}}))
32+
{{end -}}
33+
{{end -}}
34+
{{end -}}
35+
{{define "FlatTemplate" -}}
36+
{{range .Items -}}
37+
* [{{.CommitHashShort}}]({{.CommitURL}}) {{.Title}} ({{if .IsPull}}[contributed]({{.PullURL}}) by {{end}}[{{.Author}}]({{.AuthorURL}}))
38+
{{end -}}
39+
{{end -}}
40+
{{define "DefaultTemplate" -}}
41+
## {{.Version}}
42+
{{if len .Grouped -}}
43+
{{template "GroupTemplate" . -}}
44+
{{- else}}
45+
{{template "FlatTemplate" . -}}
3146
{{end}}
32-
3347
<em>For more details, see <a href="{{.CompareURL}}">{{.PreviousVersion}}..{{.Version}}</a></em>
48+
{{end -}}
49+
{{template "DefaultTemplate" . -}}
3450
`
3551

3652
// Changelog holds the information required to define the bounds for the changelog
@@ -165,7 +181,29 @@ func (c *Changelog) writeChangelog(all []model.ChangeItem, comparison *github.Co
165181

166182
grouped := make(map[string][]model.ChangeItem)
167183
for _, item := range all {
168-
grouped[item.Group()] = append(grouped[item.Group()], item)
184+
g := item.Group()
185+
if len(g) > 0 {
186+
grouped[g] = append(grouped[g], item)
187+
}
188+
}
189+
190+
templateGroups := make([]model.TemplateGroup, 0)
191+
192+
if c.Groupings != nil {
193+
for _, grouping := range *c.Groupings {
194+
if grouping.Name != "" {
195+
if items, ok := grouped[grouping.Name]; ok && len(items) > 0 {
196+
log.WithFields(log.Fields{
197+
"name": grouping.Name,
198+
"count": len(items),
199+
}).Debug("found template grouping data")
200+
templateGroups = append(templateGroups, model.TemplateGroup{
201+
Name: grouping.Name,
202+
Items: items,
203+
})
204+
}
205+
}
206+
}
169207
}
170208

171209
d := &model.TemplateData{
@@ -175,7 +213,7 @@ func (c *Changelog) writeChangelog(all []model.ChangeItem, comparison *github.Co
175213
CompareURL: compareURL,
176214
DiffURL: diffURL,
177215
PatchURL: patchURL,
178-
Grouped: grouped,
216+
Grouped: templateGroups,
179217
}
180218

181219
var tpl = defaultTemplate
@@ -297,7 +335,7 @@ func (c *Changelog) shouldExcludeByText(text *string) bool {
297335

298336
func (c *Changelog) findGroup(commit *github.RepositoryCommit) *string {
299337
var grouping *string
300-
if c.Groupings != nil {
338+
if c.Groupings != nil && len(*c.Groupings) > 0 {
301339
title := strings.Split(commit.GetCommit().GetMessage(), "\n")[0]
302340
for _, g := range *c.Groupings {
303341
for _, pattern := range g.Patterns {

0 commit comments

Comments
 (0)