|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "fmt" |
5 | 6 | "log" |
| 7 | + "net/url" |
6 | 8 | "os" |
| 9 | + "strings" |
7 | 10 | "time" |
8 | 11 |
|
9 | 12 | "github.com/Masterminds/semver" |
| 13 | + "github.com/google/go-github/v25/github" |
10 | 14 | "github.com/pkg/browser" |
11 | 15 | ) |
12 | 16 |
|
13 | | -// build set by goreleaser on production builds |
| 17 | +// build info set by goreleaser during production builds |
14 | 18 | var ( |
15 | 19 | buildVersion = "dev" |
16 | 20 | buildCommit = "none" |
@@ -55,45 +59,125 @@ func main() { |
55 | 59 | logVerbose("%v", err) |
56 | 60 | usage() |
57 | 61 | } |
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) |
59 | 63 | } |
60 | 64 |
|
61 | 65 | // get latest release version from github |
62 | 66 | logVerbose("checking github for latest release of %v/%v", owner, repo) |
63 | | - release, err := getLatestRelease(owner, repo) |
| 67 | + previousRelease, err := getLatestRelease(owner, repo) |
64 | 68 | if err != nil { |
65 | 69 | log.Fatal(err) |
66 | 70 | } |
67 | 71 |
|
68 | 72 | // 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()) |
71 | 74 | if err != nil { |
72 | 75 | log.Fatal(err) |
73 | 76 | } |
| 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) |
74 | 92 |
|
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) |
77 | 95 | if err != nil { |
78 | 96 | log.Fatal(err) |
79 | 97 | } |
80 | 98 |
|
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! |
83 | 107 | if opts.NoOpen { |
84 | | - fmt.Println("To draft release:", nextURL) |
| 108 | + fmt.Println("To draft release, visit:", draftURL) |
85 | 109 | } 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) |
88 | 113 | if err != nil { |
89 | 114 | log.Fatal(err) |
90 | 115 | } |
91 | 116 | } |
92 | 117 | } |
93 | 118 |
|
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 { |
95 | 180 | 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, |
98 | 182 | ) |
99 | 183 | } |
0 commit comments