Skip to content

Commit b6790a3

Browse files
feat: improve changelog generator
1 parent 427a6c7 commit b6790a3

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

semrel.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,10 @@ func (repo *Repository) GetLatestRelease() (*Release, error) {
108108

109109
func (repo *Repository) CreateRelease(commits []*Commit, latestRelease *Release, newVersion *semver.Version) error {
110110
tag := fmt.Sprintf("v%s", newVersion.String())
111-
sha := commits[0].SHA
112111
changelog := GetChangelog(commits, latestRelease, newVersion)
113112
opts := &github.RepositoryRelease{
114-
TagName: &tag,
115-
TargetCommitish: &sha,
116-
Body: &changelog,
113+
TagName: &tag,
114+
Body: &changelog,
117115
}
118116
_, _, err := repo.Client.Repositories.CreateRelease(repo.Ctx, repo.Owner, repo.Repo, opts)
119117
if err != nil {
@@ -160,16 +158,49 @@ func GetNewVersion(commits []*Commit, latestRelease *Release) *semver.Version {
160158
return ApplyChange(latestRelease.Version, CaluclateChange(commits, latestRelease))
161159
}
162160

161+
func formatCommit(c *Commit) string {
162+
ret := "* "
163+
if c.Scope != "" {
164+
ret += fmt.Sprintf("**%s:** ", c.Scope)
165+
}
166+
ret += fmt.Sprintf("%s (%s)\n", c.Message, c.SHA[:8])
167+
return ret
168+
}
169+
170+
var typeToText = map[string]string{
171+
"feat": "Feature",
172+
"fix": "Bug Fixes",
173+
"perf": "Performance Improvements",
174+
"revert": "Reverts",
175+
"docs": "Documentation",
176+
"style": "Styles",
177+
"refactor": "Code Refactoring",
178+
"test": "Tests",
179+
"chore": "Chores",
180+
"%%bc%%": "Breaking Changes",
181+
}
182+
163183
func GetChangelog(commits []*Commit, latestRelease *Release, newVersion *semver.Version) string {
164184
ret := fmt.Sprintf("## %s (%s)\n\n", newVersion.String(), time.Now().UTC().Format("2006-01-02"))
185+
typeScopeMap := make(map[string]string)
165186
for _, commit := range commits {
166187
if latestRelease.SHA == commit.SHA {
167188
break
168189
}
190+
if commit.Change.Major {
191+
typeScopeMap["%%bc%%"] += fmt.Sprintf("%s\n```%s\n```\n", formatCommit(commit), strings.Join(commit.Raw[1:], "\n"))
192+
}
169193
if commit.Type == "" {
170194
continue
171195
}
172-
ret += fmt.Sprintf("%s (%s)\n", commit.Raw[0], commit.SHA[:8])
196+
typeScopeMap[commit.Type] += formatCommit(commit)
197+
}
198+
for t, msg := range typeScopeMap {
199+
typeName, found := typeToText[t]
200+
if !found {
201+
typeName = t
202+
}
203+
ret += fmt.Sprintf("#### %s\n\n%s\n", typeName, msg)
173204
}
174205
return ret
175206
}

0 commit comments

Comments
 (0)