Skip to content

Commit 5108dd1

Browse files
authored
Add automatic changelog generation (#1)
* initial support for changelog generation * tell goreleaser not to manage changelogs
1 parent 14dc08a commit 5108dd1

4 files changed

Lines changed: 124 additions & 41 deletions

File tree

‎.goreleaser.yml‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ brew:
3232
description: "Draft GitHub Release of the next semver in web browser"
3333
homepage: "https://github.com/mroth/bump"
3434
skip_upload: false
35+
# disable changelog generation entirely, since bump will manage that itself!
3536
changelog:
36-
sort: asc
37-
filters:
38-
exclude:
39-
- '^docs:'
40-
- '^chore:'
41-
- '^trivial:'
37+
skip: true

‎git.go‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,25 @@ import (
1919
// It will automatically use an OAuth scoped token if GITHUB_RELEASE environment
2020
// variable is set, or an unauthed client otherwise.
2121
func getLatestRelease(owner, repo string) (*github.RepositoryRelease, error) {
22-
client := defaultGithubClient()
23-
ctx := context.Background()
22+
client, ctx := defaultGithubClient(), context.Background()
2423
defer timeTrack(time.Now(), "client.Repositories.GetLatestRelease()")
2524
release, _, err := client.Repositories.GetLatestRelease(ctx, owner, repo)
2625
return release, err
2726
}
2827

28+
// compareRelease is a convenience function wrapping retrieval of a commits
29+
// comparison between current default branch HEAD and a given release tagName,
30+
// via the GitHub API.
31+
//
32+
// It will automatically use an OAuth scoped token if GITHUB_RELEASE environment
33+
// variable is set, or an unauthed client otherwise.
34+
func compareRelease(owner, repo, tagName string) (*github.CommitsComparison, error) {
35+
client, ctx := defaultGithubClient(), context.Background()
36+
defer timeTrack(time.Now(), "client.Repositories.CompareCommits()")
37+
cc, _, err := client.Repositories.CompareCommits(ctx, owner, repo, tagName, "HEAD")
38+
return cc, err
39+
}
40+
2941
// defaultGithubClient returns a OAuth scoped Github API Client if GITHUB_TOKEN
3042
// is set the local environment, or an unauthorized one otherwise.
3143
//

‎main.go‎

Lines changed: 99 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package main
22

33
import (
4+
"bytes"
45
"fmt"
56
"log"
7+
"net/url"
68
"os"
9+
"strings"
710
"time"
811

912
"github.com/Masterminds/semver"
13+
"github.com/google/go-github/v25/github"
1014
"github.com/pkg/browser"
1115
)
1216

13-
// build set by goreleaser on production builds
17+
// build info set by goreleaser during production builds
1418
var (
1519
buildVersion = "dev"
1620
buildCommit = "none"
@@ -55,45 +59,125 @@ func main() {
5559
logVerbose("%v", err)
5660
usage()
5761
}
58-
logVerbose("wd detected as git repo with github remote %v/%v", owner, repo)
62+
logVerbose("detected .git repo with github remote %v/%v", owner, repo)
5963
}
6064

6165
// get latest release version from github
6266
logVerbose("checking github for latest release of %v/%v", owner, repo)
63-
release, err := getLatestRelease(owner, repo)
67+
previousRelease, err := getLatestRelease(owner, repo)
6468
if err != nil {
6569
log.Fatal(err)
6670
}
6771

6872
// try to parse tag name from current release into a semantic version
69-
tag := release.GetTagName()
70-
version, err := semver.NewVersion(tag)
73+
previousVersion, err := semver.NewVersion(previousRelease.GetTagName())
7174
if err != nil {
7275
log.Fatal(err)
7376
}
77+
fmt.Printf("🌻 Latest release of %v (published %v)\n",
78+
boldStyler(fmt.Sprintf("%v/%v: %v", owner, repo, previousVersion)),
79+
previousRelease.GetPublishedAt().Format("2006 Jan 2"),
80+
)
81+
82+
// retrieve changes since last release via GitHub API
83+
comparison, err := compareRelease(owner, repo, previousRelease.GetTagName())
84+
if err != nil {
85+
log.Fatal("failed to retrieve commits", err)
86+
}
87+
88+
// display abbreviated changelog to user in CLI, to hopefully aide them in
89+
// making a decision about what the next semver should be.
90+
changelog := screenChangelog(comparison)
91+
fmt.Println(changelog)
7492

75-
// invoke interactive prompt UI helping user select next version
76-
nextVersion, err := prompt(owner, repo, version, release)
93+
// invoke interactive prompt UI allowing user to select next version
94+
nextVersion, err := prompt(previousVersion)
7795
if err != nil {
7896
log.Fatal(err)
7997
}
8098

81-
// create draft URL for next version, send user to visit it
82-
nextURL := releaseURL(owner, repo, nextVersion)
99+
// create draft URL embedding markdown changelog for next version...
100+
body := strings.Join([]string{
101+
markdownChangelog(comparison),
102+
comparisonURL(owner, repo, previousVersion, nextVersion),
103+
}, "\n")
104+
draftURL := draftReleaseURL(owner, repo, nextVersion, body)
105+
106+
// ...then send user to visit in their web browser!
83107
if opts.NoOpen {
84-
fmt.Println("To draft release:", nextURL)
108+
fmt.Println("To draft release, visit:", draftURL)
85109
} else {
86-
fmt.Println("✨ Opening", nextURL)
87-
err = browser.OpenURL(nextURL)
110+
fmt.Println("✨ Drafting new release on GitHub!")
111+
logVerbose("Opening browser to: %s", draftURL)
112+
err = browser.OpenURL(draftURL)
88113
if err != nil {
89114
log.Fatal(err)
90115
}
91116
}
92117
}
93118

94-
func releaseURL(owner, repo string, version *semver.Version) string {
119+
// draftReleaseURL constructs a URL to open a new draft release on GitHub for
120+
// given owner/repo with a semver compatible tag based on the semver.Version in
121+
// the tag and title fields, and an encoded body payload to prepopulate the
122+
// form.
123+
func draftReleaseURL(owner, repo string, version *semver.Version, body string) string {
124+
return fmt.Sprintf(
125+
"https://github.com/%s/%s/releases/new?tag=v%s&title=v%s&body=%s",
126+
owner, repo, version.String(), version.String(), url.QueryEscape(body),
127+
)
128+
}
129+
130+
// markdownChangelog formats a CommitsComparison suitable for displaying on the
131+
// screen to the user, abbreviated to try to not overflow a 80x24 terminal.
132+
//
133+
// Because of this, we only display the 10 most recent commits, with a
134+
// comparison URL targetting HEAD (as draft is not released), so user can view
135+
// the full list on GitHub if desired.
136+
func screenChangelog(comparison *github.CommitsComparison) string {
137+
var buf bytes.Buffer
138+
buf.WriteString("Changes since previous release:\n\n")
139+
const max = 10
140+
for i, c := range comparison.Commits {
141+
if i >= max {
142+
break
143+
}
144+
buf.WriteString(
145+
fmt.Sprintf(" - %v\n",
146+
strings.Split(c.Commit.GetMessage(), "\n")[0],
147+
),
148+
)
149+
}
150+
numExtraCommits := len(comparison.Commits) - max
151+
if numExtraCommits > 0 {
152+
buf.WriteString(
153+
fmt.Sprintf("\n...%d more commits, %s\n",
154+
numExtraCommits, comparison.GetHTMLURL()),
155+
)
156+
}
157+
return buf.String()
158+
}
159+
160+
// markdownChangelog formats a CommitsComparison suitable for markdown display
161+
// in a GitHub Flavored Markdown release notes field.
162+
//
163+
// TODO: cap max number of commits to display? API returns <=250
164+
func markdownChangelog(comparison *github.CommitsComparison) string {
165+
var buf bytes.Buffer
166+
buf.WriteString("## Changelog\n\n")
167+
for _, c := range comparison.Commits {
168+
buf.WriteString(
169+
fmt.Sprintf("- %v %.7s\n",
170+
strings.Split(c.Commit.GetMessage(), "\n")[0],
171+
c.GetSHA(),
172+
),
173+
)
174+
}
175+
return buf.String()
176+
}
177+
178+
// comparisonURL makes a GitHub web view URL for comparing two tagged semvers.
179+
func comparisonURL(owner, repo string, base, next *semver.Version) string {
95180
return fmt.Sprintf(
96-
"https://github.com/%s/%s/releases/new?tag=v%s&title=v%s",
97-
owner, repo, version.String(), version.String(),
181+
"https://github.com/%s/%s/compare/v%s...v%s", owner, repo, base, next,
98182
)
99183
}

‎prompter.go‎

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,26 @@ import (
66

77
"github.com/Masterminds/semver"
88
"github.com/chzyer/readline"
9-
"github.com/google/go-github/v25/github"
109
"github.com/manifoldco/promptui"
1110
)
1211

12+
var (
13+
boldStyler = promptui.Styler(promptui.FGBold)
14+
faintStyler = promptui.Styler(promptui.FGFaint)
15+
)
16+
1317
type cliVersionOption struct {
1418
Name string
1519
Version semver.Version
1620
}
1721

1822
func (o cliVersionOption) String() string {
19-
return fmt.Sprintf(
20-
"%v%v",
21-
o.Name,
22-
promptui.Styler(promptui.FGFaint)(
23-
fmt.Sprintf(" (%v)", o.Version.String()),
24-
),
23+
return fmt.Sprintf("%v%v",
24+
o.Name, faintStyler(fmt.Sprintf(" (%v)", o.Version.String())),
2525
)
2626
}
2727

28-
func prompt(
29-
owner, repo string, currVersion *semver.Version,
30-
release *github.RepositoryRelease) (*semver.Version, error) {
31-
32-
fmt.Printf("🌻 Current version of %v (released %v)\n",
33-
promptui.Styler(promptui.FGBold)(fmt.Sprintf("%v/%v: %v",
34-
owner, repo, currVersion)),
35-
release.GetPublishedAt().Format("2006 Jan 2"),
36-
)
37-
28+
func prompt(currVersion *semver.Version) (*semver.Version, error) {
3829
// promptui.IconInitial = "🚀" // default is colored ASCII question mark
3930
choices := []cliVersionOption{
4031
{"patch", currVersion.IncPatch()},
@@ -43,7 +34,7 @@ func prompt(
4334
}
4435

4536
prompt := promptui.Select{
46-
Label: "Select semver increment to specify new version",
37+
Label: "Select semver increment to specify next version",
4738
Items: choices,
4839
}
4940

0 commit comments

Comments
 (0)
⌂ 换网址