-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
main.go
295 lines (246 loc) · 8.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//go:build tools
// +build tools
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// main is the main package for the release notes generator.
package main
import (
"context"
"flag"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
release "sigs.k8s.io/cluster-api/hack/tools/release/internal"
)
/*
This tool prints all the titles of all PRs from previous release to HEAD.
This needs to be run *before* a tag is created.
Use these as the base of your release notes.
*/
var (
outputOrder = []string{
release.Proposals,
release.Warning,
release.Features,
release.Bugs,
release.Other,
release.Documentation,
release.Unknown,
}
since string
until string
branch string
timeLayout = "2006-01-02"
repo = "cluster-api"
owner = "kubernetes-sigs"
tagRegex = regexp.MustCompile(`^\[release-[\w-\.]*\]`)
)
func main() {
flag.StringVar(&since, "since", "", "Include commits starting from and including this date. Accepts format: YYYY-MM-DD")
flag.StringVar(&until, "until", "", "Include commits up to and including this date. Accepts format: YYYY-MM-DD")
flag.StringVar(&branch, "branch", "release-1.6", "Release branch. Accepts formats: main, release-1.6")
flag.Parse()
os.Exit(run())
}
func run() int {
if since == "" && until == "" {
fmt.Println("--since and --until are required together or both unset")
return 1
}
ghToken := os.Getenv("GITHUB_TOKEN")
client := createGitHubClient(ghToken)
branchValid, err := isValidBranch(branch, owner, repo, client)
if err != nil {
fmt.Printf("Unable to verify if branch '%s' is valid: %s\n", branch, err.Error())
return 1
}
if !branchValid {
fmt.Printf("Invalid branch '%s': branch does not exist. Example of valid branches: main, release-1.5.\n", branch)
return 1
}
sinceTime, err := parseTime(since)
if err != nil {
fmt.Printf("Unable to parse time for 'since' parameter: %s\n", since)
return 1
}
untilTime, err := parseTime(until)
if err != nil {
fmt.Printf("Unable to parse time for 'until' parameter: %s\n", until)
return 1
}
pullRequests, err := getMergedPullRequests(client, owner, repo, branch, sinceTime, untilTime)
if err != nil {
fmt.Println("Unable to get merged pull requests:", err.Error())
return 1
}
merges := map[string][]string{
release.Features: {},
release.Bugs: {},
release.Documentation: {},
release.Warning: {},
release.Other: {},
release.Unknown: {},
}
for _, pr := range pullRequests {
prTitle := trimTitle(pr.GetTitle())
var key, prNumber string
switch {
case strings.HasPrefix(prTitle, ":sparkles:"), strings.HasPrefix(prTitle, "✨"):
key = release.Features
prTitle = strings.TrimPrefix(prTitle, ":sparkles:")
prTitle = strings.TrimPrefix(prTitle, "✨")
case strings.HasPrefix(prTitle, ":bug:"), strings.HasPrefix(prTitle, "🐛"):
key = release.Bugs
prTitle = strings.TrimPrefix(prTitle, ":bug:")
prTitle = strings.TrimPrefix(prTitle, "🐛")
case strings.HasPrefix(prTitle, ":book:"), strings.HasPrefix(prTitle, "📖"):
key = release.Documentation
prTitle = strings.TrimPrefix(prTitle, ":book:")
prTitle = strings.TrimPrefix(prTitle, "📖")
if strings.Contains(prTitle, "CAEP") || strings.Contains(prTitle, "proposal") {
key = release.Proposals
}
case strings.HasPrefix(prTitle, ":seedling:"), strings.HasPrefix(prTitle, "🌱"):
key = release.Other
prTitle = strings.TrimPrefix(prTitle, ":seedling:")
prTitle = strings.TrimPrefix(prTitle, "🌱")
case strings.HasPrefix(prTitle, ":warning:"), strings.HasPrefix(prTitle, "⚠️"):
key = release.Warning
prTitle = strings.TrimPrefix(prTitle, ":warning:")
prTitle = strings.TrimPrefix(prTitle, "⚠️")
default:
key = release.Unknown
}
prTitle = strings.TrimSpace(prTitle)
if prTitle == "" {
continue
}
prTitle = fmt.Sprintf("\t - %s", prTitle)
if key == release.Documentation {
merges[key] = append(merges[key], prNumber)
continue
}
merges[key] = append(merges[key], formatMerge(prTitle, strconv.Itoa(pr.GetNumber())))
}
// TODO Turn this into a link (requires knowing the project name + organization).
fmt.Println("Weekly update :rotating_light:")
fmt.Printf("From %s to %s a total of %d changes merged into %s.\n\n", sinceTime.Format(timeLayout), untilTime.Format(timeLayout), len(pullRequests), branch)
for _, key := range outputOrder {
mergeslice := merges[key]
if len(mergeslice) == 0 {
continue
}
switch key {
case release.Documentation:
fmt.Printf("- %d Documentation and book contributions :book: \n\n", len(mergeslice))
case release.Other:
fmt.Printf("- %d Other changes :seedling:\n\n", len(merges[release.Other]))
default:
fmt.Printf("- %d %s\n", len(merges[key]), key)
for _, merge := range mergeslice {
fmt.Println(merge)
}
fmt.Println()
}
}
fmt.Println("All merged PRs can be viewed in GitHub:")
fmt.Println("https://github.com/kubernetes-sigs/cluster-api/pulls?q=is%3Apr+closed%3A" + sinceTime.Format(timeLayout) + ".." + untilTime.Format(timeLayout) + "+is%3Amerged+base%3A" + branch + "+\n")
fmt.Println("_Thanks to all our contributors!_ 😊")
fmt.Println("/Your friendly comms release team")
return 0
}
func trimTitle(title string) string {
// Remove a tag prefix if found.
title = tagRegex.ReplaceAllString(title, "")
return strings.TrimSpace(title)
}
func formatMerge(line, prNumber string) string {
if prNumber == "" {
return line
}
return fmt.Sprintf("%s (#%s)", line, prNumber)
}
// Parse the time from string to time.Time format.
func parseTime(date string) (time.Time, error) {
datetime, err := time.Parse(timeLayout, date)
if err != nil {
return time.Time{}, err
}
return datetime, nil
}
func getMergedPullRequests(client *github.Client, owner string, repo string, branch string, since time.Time, until time.Time) ([]*github.PullRequest, error) {
var allPullRequests []*github.PullRequest
listOptions := &github.ListOptions{PerPage: 100}
for {
pullRequests, resp, err := client.PullRequests.List(context.Background(), owner, repo, &github.PullRequestListOptions{
State: "closed",
Sort: "updated",
Direction: "desc",
ListOptions: *listOptions,
Base: branch,
})
if err != nil {
return nil, err
}
// Include PRs until EOD.
until = until.Add(time.Hour * 24)
for _, pr := range pullRequests {
// Our query returned PRs sorted by most recently _updated_.
// The moment we get a PR that is updated before our `since` timestamp,
// we have seen all _merged_ PRs since `since`.
if pr.UpdatedAt.Before(since) {
return allPullRequests, nil
}
if pr.MergedAt != nil && pr.MergedAt.After(since) && pr.MergedAt.Before(until) {
allPullRequests = append(allPullRequests, pr)
}
}
if resp.NextPage == 0 {
break
}
listOptions.Page = resp.NextPage
}
return allPullRequests, nil
}
// Checks if the branch exists.
func isValidBranch(branchName string, owner string, repo string, client *github.Client) (bool, error) {
branches, _, err := client.Repositories.ListBranches(context.Background(), owner, repo, nil)
if err != nil {
return false, err
}
for _, branch := range branches {
if branchName == branch.GetName() {
return true, nil
}
}
return false, nil
}
// If the token is empty, we create a GitHub client without authentication
// Using a token enables more api requests until we run into rate limitation
// This is not necessary, but a nice to have when extending this script and going beyond
// normal usage.
func createGitHubClient(ghToken string) *github.Client {
if ghToken != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ghToken},
)
tc := oauth2.NewClient(context.Background(), ts)
return github.NewClient(tc)
}
return github.NewClient(nil)
}