8000 feat(changelog): custom commit format (#4802) · goreleaser/goreleaser@39bf666 · GitHub
[go: up one dir, main page]

Skip to content

Commit 39bf666

Browse files
authored
feat(changelog): custom commit format (#4802)
This allows to use templates for commit messages in the changelog when using `github`, `gitea`, or `gitlab` as the changelog implementation. closes #4800 --------- Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
1 parent 2c93bd7 commit 39bf666

File tree

14 files changed

+128
-43
lines changed

14 files changed

+128
-43
lines changed

internal/client/client.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,20 @@ type Client interface {
5656
CreateRelease(ctx *context.Context, body string) (releaseID string, err error)
5757
PublishRelease(ctx *context.Context, releaseID string) (err error)
5858
Upload(ctx *context.Context, releaseID string, artifact *artifact.Artifact, file *os.File) (err error)
59-
Changelog(ctx *context.Context, repo Repo, prev, current string) (string, error)
59+
Changelog(ctx *context.Context, repo Repo, prev, current string) ([]ChangelogItem, error)
6060
ReleaseURLTemplater
6161
FileCreator
6262
}
6363

64+
// ChangelogItem represents a changelog item, basically, a commit and its author.
65+
type ChangelogItem struct {
66+
SHA string
67+
Message string
68+
AuthorName string
69+
AuthorEmail string
70+
AuthorUsername string
71+
}
72+
6473
// ReleaseURLTemplater provides the release URL as a template, containing the
6574
// artifact name as well.
6675
type ReleaseURLTemplater interface {

internal/client/gitea.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,23 @@ func newGitea(ctx *context.Context, token string) (*giteaClient, error) {
7575
}
7676

7777
// Changelog fetches the changelog between two revisions.
78-
func (c *giteaClient) Changelog(_ *context.Context, repo Repo, prev, current string) (string, error) {
78+
func (c *giteaClient) Changelog(_ *context.Context, repo Repo, prev, current string) ([]ChangelogItem, error) {
7979
result, _, err := c.client.CompareCommits(repo.Owner, repo.Name, prev, current)
8080
if err != nil {
81-
return "", err
81+
return nil, err
8282
}
83-
var log []string
83+
var log []ChangelogItem
8484

8585
for _, commit := range result.Commits {
86-
log = append(log, fmt.Sprintf(
87-
"%s: %s (@%s)",
88-
commit.SHA[:7],
89-
strings.Split(commit.RepoCommit.Message, "\n")[0],
90-
commit.Author.UserName,
91-
))
86+
log = append(log, ChangelogItem{
87+
SHA: commit.SHA[:7],
88+
Message: strings.Split(commit.RepoCommit.Message, "\n")[0],
89+
AuthorName: commit.Author.FullName,
90+
AuthorEmail: commit.Author.Email,
91+
AuthorUsername: commit.Author.UserName,
92+
})
9293
}
93-
return strings.Join(log, "\n"), nil
94+
return log, nil
9495
}
9596

9697
// CloseMilestone closes a given milestone.

internal/client/gitea_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ func TestGiteaChangelog(t *testing.T) {
617617
},
618618
Author: &gitea.User{
619619
UserName: "johndoe",
620+
FullName: "John Doe",
621+
Email: "nope@nope.nope",
620622
},
621623
RepoCommit: &gitea.RepoCommit{
622624
Message: "feat: impl something\n\nnsome other lines",
@@ -646,7 +648,15 @@ func TestGiteaChangelog(t *testing.T) {
646648

647649
result, err := client.Changelog(ctx, repo, "v1.0.0", "v1.1.0")
648650
require.NoError(t, err)
649-
require.Equal(t, "c8488dc: feat: impl something (@johndoe)", result)
651+
require.Equal(t, []ChangelogItem{
652+
{
653+
SHA: "c8488dc",
654+
Message: "feat: impl something",
655+
AuthorUsername: "johndoe",
656+
AuthorName: "John Doe",
657+
AuthorEmail: "nope@nope.nope",
658+
},
659+
}, result)
650660
}
651661

652662
func TestGiteatGetInstanceURL(t *testing.T) {

internal/client/github.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,32 @@ func (c *githubClient) GenerateReleaseNotes(ctx *context.Context, repo Repo, pre
101101
return notes.Body, err
102102
}
103103

104-
func (c *githubClient) Changelog(ctx *context.Context, repo Repo, prev, current string) (string, error) {
104+
func (c *githubClient) Changelog(ctx *context.Context, repo Repo, prev, current string) ([]ChangelogItem, error) {
105105
c.checkRateLimit(ctx)
106-
var log []string
106+
var log []ChangelogItem
107107
opts := &github.ListOptions{PerPage: 100}
108108

109109
for {
110110
result, resp, err := c.client.Repositories.CompareCommits(ctx, repo.Owner, repo.Name, prev, current, opts)
111111
if err != nil {
112-
return "", err
112+
return nil, err
113113
}
114114
for _, commit := range result.Commits {
115-
log = append(log, fmt.Sprintf(
116-
"%s: %s (@%s)",
117-
commit.GetSHA(),
118-
strings.Split(commit.Commit.GetMessage(), "\n")[0],
119-
commit.GetAuthor().GetLogin(),
120-
))
115+
log = append(log, ChangelogItem{
116+
SHA: commit.GetSHA(),
117+
Message: strings.Split(commit.Commit.GetMessage(), "\n")[0],
118+
AuthorName: commit.GetAuthor().GetName(),
119+
AuthorEmail: commit.GetAuthor().GetEmail(),
120+
AuthorUsername: commit.GetAuthor().GetLogin(),
121+
})
121122
}
122123
if resp.NextPage == 0 {
123124
break
124125
}
125126
opts.Page = resp.NextPage
126127
}
127128

128-
return strings.Join(log, "\n"), nil
129+
return log, nil
129130
}
130131

131132
// getDefaultBranch returns the default branch of a github repo

internal/client/github_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,15 @@ func TestGitHubChangelog(t *testing.T) {
300300

301301
log, err := client.Changelog(ctx, repo, "v1.0.0", "v1.1.0")
302302
require.NoError(t, err)
303-
require.Equal(t, "6dcb09b5b57875f334f61aebed695e2e4193db5e: Fix all the bugs (@octocat)", log)
303+
require.Equal(t, []ChangelogItem{
304+
{
305+
SHA: "6dcb09b5b57875f334f61aebed695e2e4193db5e",
306+
Message: "Fix all the bugs",
307+
AuthorName: "Octocat",
308+
AuthorEmail: "octo@cat",
309+
AuthorUsername: "octocat",
310+
},
311+
}, log)
304312
}
305313

306314
func TestGitHubReleaseNotes(t *testing.T) {

internal/client/gitlab.go

Lines changed: 11 additions & 12 deletions
82
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,26 @@ func newGitLab(ctx *context.Context, token string) (*gitlabClient, error) {
6464
return &gitlabClient{client: client}, nil
6565
}
6666

67-
func (c *gitlabClient) Changelog(_ *context.Context, repo Repo, prev, current string) (string, error) {
67+
func (c *gitlabClient) Changelog(_ *context.Context, repo Repo, prev, current string) ([]ChangelogItem, error) {
6868
cmpOpts := &gitlab.CompareOptions{
6969
From: &prev,
7070
To: &current,
7171
}
7272
result, _, err := c.client.Repositories.Compare(repo.String(), cmpOpts)
73-
var log []string
73+
var log []ChangelogItem
7474
if err != nil {
75-
return "", err
75+
return nil, err
7676
}
7777

7878
for _, commit := range result.Commits {
79-
log = append(log, fmt.Sprintf(
80-
"%s: %s (%s <%s>)",
81-
commit.ShortID,
82-
strings.Split(commit.Message, "\n")[0],
83-
commit.AuthorName,
84-
commit.AuthorEmail,
85-
))
86-
}
87-
return strings.Join(log, "\n"), nil
79+
log = append(log, ChangelogItem{
80+
SHA: commit.ShortID,
81+
Message: strings.Split(commit.Message, "\n")[0],
+
AuthorName: commit.AuthorName,
83+
AuthorEmail: commit.AuthorEmail,
84+
})
85+
}
86+
return log, nil
8887
}
8988

9089
// getDefaultBranch get the default branch

internal/client/gitlab_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,15 @@ func TestGitLabChangelog(t *testing.T) {
484484

485485
log, err := client.Changelog(ctx, repo, "v1.0.0", "v1.1.0")
486486
require.NoError(t, err)
487-
require.Equal(t, "6dcb09b5: Fix all the bugs (Joey User <joey@user.edu>)", log)
487+
require.Equal(t, []ChangelogItem{
488+
{
489+
SHA: "6dcb09b5",
490+
Message: "Fix all the bugs",
491+
AuthorName: "Joey User",
492+
AuthorEmail: "joey@user.edu",
493+
AuthorUsername: "",
494+
},
495+
}, log)
488496
}
489497

490498
func TestGitLabCreateFile(t *testing.T) {

internal/client/mock.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type Mock struct {
3939
Lock sync.Mutex
4040
ClosedMilestone string
4141
FailToCloseMilestone bool
42-
Changes string
42+
Changes []ChangelogItem
4343
ReleaseNotes string
4444
ReleaseNotesParams []string
4545
OpenedPullRequest bool
@@ -56,11 +56,11 @@ func (c *Mock) OpenPullRequest(_ *context.Context, _, _ Repo, _ string, _ bool)
5656
return nil
5757
}
5858

59-
func (c *Mock) Changelog(_ *context.Context, _ Repo, _, _ string) (string, error) {
60-
if c.Changes != "" {
59+
func (c *Mock) Changelog(_ *context.Context, _ Repo, _, _ string) ([]ChangelogItem, error) {
60+
if len(c.Changes) > 0 {
6161
return c.Changes, nil
6262
}
63-
return "", ErrNotImplemented
63+
return nil, ErrNotImplemented
6464
}
6565

6666
func (c *Mock) GenerateReleaseNotes(_ *context.Context, _ Repo, prev, current string) (string, error) {

internal/client/testdata/github/compare.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"message": "Fix all the bugs\nlalalal"
77
},
88
"author": {
9-
"login": "octocat"
9+
"login": "octocat",
10+
"name": "Octocat",
11+
"email": "octo@cat"
1012
}
1113
}
1214
]

internal/pipe/changelog/changelog.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const (
4141
type Pipe struct{}
4242

4343
func (Pipe) String() string { return "generating changelog" }
44+
4445
func (Pipe) Skip(ctx *context.Context) (bool, error) {
4546
if ctx.Snapshot {
4647
return true, nil
@@ -53,6 +54,13 @@ func (Pipe) Skip(ctx *context.Context) (bool, error) {
5354
return tmpl.New(ctx).Bool(ctx.Config.Changelog.Disable)
5455
}
5556

57+
func (Pipe) Default(ctx *context.Context) error {
58+
if ctx.Config.Changelog.Format == "" {
59+
ctx.Config.Changelog.Format = "{{ .SHA }}: {{ .Message }} ({{ with .AuthorUsername }}@{{ . }}{{ else }}{{ .AuthorName }} <{{ .AuthorEmail }}>{{ end }})"
60+
}
61+
return nil
62+
}
63+
5664
// Run the pipe.
5765
func (Pipe) Run(ctx *context.Context) error {
5866
notes, err := loadContent(ctx, ctx.ReleaseNotesFile, ctx.ReleaseNotesTmpl)
@@ -445,7 +453,25 @@ type scmChangeloger struct {
445453

446454
func (c *scmChangeloger) Log(ctx *context.Context) (string, error) {
447455
prev, current := comparePair(ctx)
448-
return c.client.Changelog(ctx, c.repo, prev, current)
456+
items, err := c.client.Changelog(ctx, c.repo, prev, current)
457+
if err != nil {
458+
return "", err
459+
}
460+
var lines []string
461+
for _, item := range items {
462+
line, err := tmpl.New(ctx).WithExtraFields(tmpl.Fields{
463+
"SHA": item.SHA,
464+
"Message": item.Message,
465+
"AuthorUsername": item.AuthorUsername,
466+
"AuthorName": item.AuthorName,
467+
"AuthorEmail": item.AuthorEmail,
468+
}).Apply(ctx.Config.Changelog.Format)
469+
if err != nil {
470+
return "", err
471+
}
472+
lines = append(lines, line)
473+
}
474+
return strings.Join(lines, "\n"), nil
449475
}
450476

451477
type githubNativeChangeloger struct {

0 commit comments

Comments
 (0)
0