Skip to content

Commit fc7718d

Browse files
author
Huy Mai
committed
Add new reusable release note generator
Compare to the current script that we are having in all releasing repositories, this tool: - Is simpler - Take custom repo name and repo owner - Doesn't use os.Exec(), hence doesn't need to run inside the repo - Doesn't require uptream tag fetching - Doesn't require, but still supports, GITHUB_TOKEN - Is more reliable as everything is fetched from github Signed-off-by: Huy Mai <[email protected]>
1 parent e07e600 commit fc7718d

File tree

3 files changed

+239
-0
lines changed

3 files changed

+239
-0
lines changed

hack/release/go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/metal3-io/project-infra/hack/release
2+
3+
go 1.23.2
4+
5+
require (
6+
github.com/blang/semver v3.5.1+incompatible
7+
github.com/google/go-github v17.0.0+incompatible
8+
golang.org/x/oauth2 v0.25.0
9+
)
10+
11+
require github.com/google/go-querystring v1.1.0 // indirect

hack/release/go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
2+
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
3+
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
5+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
6+
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
7+
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
8+
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
9+
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
10+
golang.org/x/oauth2 v0.25.0 h1:CY4y7XT9v0cRI9oupztF8AgiIu99L/ksR/Xp/6jrZ70=
11+
golang.org/x/oauth2 v0.25.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
12+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

hack/release/main.go

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"log"
23+
"os"
24+
"regexp"
25+
"strings"
26+
27+
"github.com/blang/semver"
28+
"github.com/google/go-github/github"
29+
"golang.org/x/oauth2"
30+
)
31+
32+
/*
33+
This tool prints all the titles of all PRs from previous release to HEAD.
34+
This needs to be run *before* a tag is created.
35+
36+
Use these as the base of your release notes.
37+
*/
38+
39+
const (
40+
features = ":sparkles: New Features"
41+
bugs = ":bug: Bug Fixes"
42+
documentation = ":book: Documentation"
43+
warning = ":warning: Breaking Changes"
44+
other = ":seedling: Others"
45+
unknown = ":question: Sort these by hand"
46+
superseded = ":recycle: Superseded or Reverted"
47+
warningTemplate = ":rotating_light: This is a %s. Use it only for testing purposes.\nIf you find any bugs, file an [issue](https://github.com/%s/%s/issues/new/).\n\n"
48+
)
49+
50+
var (
51+
outputOrder = []string{
52+
warning,
53+
features,
54+
bugs,
55+
documentation,
56+
other,
57+
unknown,
58+
superseded,
59+
}
60+
releaseTag string
61+
repoOwner string
62+
repoName string
63+
semVersion semver.Version
64+
lastReleaseTag string
65+
)
66+
67+
func main() {
68+
releaseTag = os.Getenv("RELEASE_TAG")
69+
if releaseTag == "" {
70+
log.Fatal("RELEASE_TAG is required")
71+
}
72+
repoOwner = os.Getenv("REPO_OWNER")
73+
if repoOwner == "" {
74+
log.Fatal("REPO_OWNER is required")
75+
}
76+
repoName = os.Getenv("REPO_NAME")
77+
if repoName == "" {
78+
log.Fatal("REPO_NAME is required")
79+
}
80+
81+
// Create a context
82+
ctx := context.Background()
83+
84+
// Authenticate with GitHub token if available
85+
token := os.Getenv("GITHUB_TOKEN")
86+
var client *github.Client
87+
if token != "" {
88+
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
89+
tc := oauth2.NewClient(ctx, ts)
90+
client = github.NewClient(tc)
91+
} else {
92+
client = github.NewClient(nil)
93+
}
94+
releaseName := strings.TrimPrefix(releaseTag, "v")
95+
var err error
96+
semVersion, err = semver.Make(releaseName)
97+
if err != nil {
98+
log.Fatalf("Incorrect releaseTag: %v", err)
99+
}
100+
101+
// Get the name of the release branch. Default to "main" if it's a minor release
102+
releaseBranch := fmt.Sprintf("release-%d.%d", semVersion.Major, semVersion.Minor)
103+
if semVersion.Patch == 0 {
104+
releaseBranch = "main"
105+
}
106+
107+
// Get the release tag used for comparison
108+
lastVersion := semVersion
109+
if lastVersion.Patch == 0 {
110+
lastVersion.Minor--
111+
} else {
112+
lastVersion.Patch--
113+
}
114+
lastReleaseTag = fmt.Sprintf("v%d.%d.%d", lastVersion.Major, lastVersion.Minor, lastVersion.Patch)
115+
116+
// Compare commits between the tag and the release branch
117+
comparison, _, err := client.Repositories.CompareCommits(ctx, repoOwner, repoName, lastReleaseTag, releaseBranch)
118+
if err != nil {
119+
log.Fatalf("failed to compare commits: %v", err)
120+
}
121+
merges := map[string][]string{
122+
features: {},
123+
bugs: {},
124+
documentation: {},
125+
warning: {},
126+
other: {},
127+
unknown: {},
128+
superseded: {},
129+
}
130+
131+
for _, commit := range comparison.Commits {
132+
// Only takes the merge commits.
133+
if len(commit.Parents) == 1 {
134+
continue
135+
}
136+
mergeCommitRegex := regexp.MustCompile(`Merge pull request #(\d+) from`)
137+
matches := mergeCommitRegex.FindStringSubmatch(commit.GetCommit().GetMessage())
138+
var prNumber string
139+
if len(matches) > 1 {
140+
// This is a merge commit, extract the PR number
141+
prNumber = matches[1]
142+
}
143+
144+
// Append commit message and PR number
145+
lines := strings.Split(commit.GetCommit().GetMessage(), "\n")
146+
body := lines[len(lines)-1]
147+
if body == "" {
148+
continue
149+
}
150+
var key string
151+
switch {
152+
case strings.HasPrefix(body, ":sparkles:"), strings.HasPrefix(body, "✨"):
153+
key = features
154+
body = strings.TrimPrefix(body, ":sparkles:")
155+
body = strings.TrimPrefix(body, "✨")
156+
case strings.HasPrefix(body, ":bug:"), strings.HasPrefix(body, "🐛"):
157+
key = bugs
158+
body = strings.TrimPrefix(body, ":bug:")
159+
body = strings.TrimPrefix(body, "🐛")
160+
case strings.HasPrefix(body, ":book:"), strings.HasPrefix(body, "📖"):
161+
key = documentation
162+
body = strings.TrimPrefix(body, ":book:")
163+
body = strings.TrimPrefix(body, "📖")
164+
case strings.HasPrefix(body, ":seedling:"), strings.HasPrefix(body, "🌱"):
165+
key = other
166+
body = strings.TrimPrefix(body, ":seedling:")
167+
body = strings.TrimPrefix(body, "🌱")
168+
case strings.HasPrefix(body, ":warning:"), strings.HasPrefix(body, "⚠️"):
169+
key = warning
170+
body = strings.TrimPrefix(body, ":warning:")
171+
body = strings.TrimPrefix(body, "⚠️")
172+
case strings.HasPrefix(body, ":rocket:"), strings.HasPrefix(body, "🚀"):
173+
continue
174+
default:
175+
key = unknown
176+
}
177+
merges[key] = append(merges[key], fmt.Sprintf("- %s (#%d)", body, prNumber))
178+
}
179+
fmt.Println("<!-- markdownlint-disable no-inline-html line-length -->")
180+
// if we're doing beta/rc, print breaking changes and hide the rest of the changes
181+
if len(semVersion.Pre) > 0 {
182+
switch semVersion.Pre[0].VersionStr {
183+
case "beta":
184+
fmt.Printf(warningTemplate, "BETA RELEASE", repoOwner, repoName)
185+
case "rc":
186+
fmt.Printf(warningTemplate, "RELEASE CANDIDATE", repoOwner, repoName)
187+
}
188+
fmt.Printf("<details>\n")
189+
fmt.Printf("<summary>More details about the release</summary>\n\n")
190+
}
191+
fmt.Printf("# Changes since [%s](https://github.com/%s/%s/tree/%s)\n\n", lastReleaseTag, repoOwner, repoName, lastReleaseTag)
192+
// print the changes by category
193+
for _, key := range outputOrder {
194+
mergeslice := merges[key]
195+
if len(mergeslice) > 0 {
196+
fmt.Printf("## %v\n\n", key)
197+
for _, merge := range mergeslice {
198+
fmt.Println(merge)
199+
}
200+
fmt.Println()
201+
}
202+
}
203+
204+
// close the details tag if we had it open, else add the Superseded or Reverted section
205+
if len(semVersion.Pre) > 0 {
206+
fmt.Printf("</details>\n\n")
207+
} else {
208+
fmt.Println("\n## :recycle: Superseded or Reverted")
209+
}
210+
211+
fmt.Printf("The container image for this release is: %s\n", releaseTag)
212+
if repoName == "cluster-api-provider-metal3" {
213+
fmt.Printf("Mariadb image tag is: capm3-%s\n", releaseTag)
214+
}
215+
fmt.Println("\n_Thanks to all our contributors!_ 😊")
216+
}

0 commit comments

Comments
 (0)