-
Notifications
You must be signed in to change notification settings - Fork 4
/
release_notes.go
368 lines (316 loc) · 8.62 KB
/
release_notes.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package changelog
import (
"context"
"fmt"
"regexp"
"sort"
"strings"
"time"
hclog "github.com/hashicorp/go-hclog"
"github.com/shurcooL/githubv4"
)
const (
labelBreakingChange = "breaking-change"
)
var (
labelsBug = []string{"bug", "kind/bug"}
labelsNoReleaseNote = []string{"no-release-note", "release-note-none"}
)
// ReleaseNote is the type that represents the total sum of all the information
// we've gathered about a single release note.
type ReleaseNote struct {
// Text is the actual content of the release note
Text string `json:"text"`
// Author is the GitHub username of the commit author
Author string `json:"author"`
// AuthorURL is the GitHub URL of the commit author
AuthorURL string `json:"author_url"`
//PRDate is the Date the PR was merged
PRDate time.Time `json:"pr_date"`
// PRUrl is a URL to the PR
PRURL string `json:"pr_url"`
// PRNumber is the number of the PR
PRNumber int `json:"pr_number"`
// Labels is a list of all the labels on the PR.
Labels []string `labels:"areas,omitempty"`
// Indicates whether or not a note will appear as a bug (`bug` label).
Bug bool `json:"bug,omitempty"`
// BreakingChange indicates if this change was breaking (the
// `breaking-change` label was applied to the PR).
BreakingChange bool `json:"breaking_change,omitempty"`
// Type is the type of entry the ReleaseNote is
Type string
}
// ReleaseNoteEntry is a struct containing the type of entry and the body of
// the entry. One or more ReleaseNoteEntry is extracted from a PR, and
// represents a single item within the release notes.
type ReleaseNoteEntry struct {
Type string
Text string
}
func listPullRequestIDs(
ctx context.Context,
client *githubv4.Client,
logger hclog.Logger,
owner, repo, branch string,
start, end time.Time,
) ([]string, error) {
var q struct {
Repository struct {
Ref struct {
Target struct {
Commit struct {
History struct {
Nodes []struct {
OID string
AssociatedPullRequests struct {
Nodes []struct {
Labels struct {
Nodes []struct {
Name string
}
} `graphql:"labels(first: 100)"`
BaseRef struct {
Repository struct {
Owner struct {
Login string
}
Name string
}
Name string
}
State githubv4.PullRequestState
ID string
Number int
}
} `graphql:"associatedPullRequests(first: 100)"`
}
} `graphql:"history(since: $since, until: $until)"`
} `graphql:"... on Commit"`
}
} `graphql:"ref(qualifiedName: $ref)"`
} `graphql:"repository(owner: $repoOwner, name: $repoName)"`
}
prNodeIDs := map[string]bool{}
logger = logger.With("since", start, "until", end)
logger.Info("checking commits for associated PRs")
err := client.Query(ctx, &q, map[string]interface{}{
"repoOwner": githubv4.String(owner),
"repoName": githubv4.String(repo),
"ref": githubv4.String(fmt.Sprintf("refs/heads/%s", branch)),
"since": githubv4.GitTimestamp{Time: start},
"until": githubv4.GitTimestamp{Time: end},
})
if err != nil {
return nil, err
}
for _, hn := range q.Repository.Ref.Target.Commit.History.Nodes {
logger := logger.With("commit", hn.OID)
logger.Debug("checking commit PRs")
if len(hn.AssociatedPullRequests.Nodes) == 100 {
// this is weird, should probably log it
}
for _, prn := range hn.AssociatedPullRequests.Nodes {
logger := logger.With("pr", prn.Number)
if prn.BaseRef.Name != branch ||
prn.BaseRef.Repository.Name != repo ||
prn.BaseRef.Repository.Owner.Login != owner {
logger.Debug("external PR, skipping")
continue
}
noChangelog := false
for _, ln := range prn.Labels.Nodes {
for _, nrn := range labelsNoReleaseNote {
if ln.Name == nrn {
noChangelog = true
break
}
}
}
if noChangelog {
logger.Debug("no-changelog label applied, skipping")
continue
}
if prn.State != githubv4.PullRequestStateMerged {
logger.Debug("unmerged PR, skipping")
continue
}
// TODO: check base ref on PR to make sure its master?
prNodeIDs[prn.ID] = true
}
}
prIDs := make([]string, 0, len(prNodeIDs))
for id := range prNodeIDs {
prIDs = append(prIDs, id)
}
return prIDs, nil
}
func stringInSlice(haystack []string, needle string) bool {
for _, h := range haystack {
if h == needle {
return true
}
}
return false
}
func pullRequestsToReleaseNotes(
ctx context.Context,
client *githubv4.Client,
logger hclog.Logger,
prIDs []string,
) ([]ReleaseNote, error) {
var q struct {
Nodes []struct {
PullRequest struct {
MergedAt time.Time
ID string
Number int
Title string
Body string
URL string
Author struct {
Login string
URL string
}
Labels struct {
Nodes []struct {
Name string
}
} `graphql:"labels(first: 100)"`
} `graphql:"... on PullRequest"`
} `graphql:"nodes(ids: $ids)"`
}
logger.Info("retrieving PRs to build release notes")
err := client.Query(ctx, &q, map[string]interface{}{
"ids": prIDs,
})
if err != nil {
return nil, err
}
notes := make([]ReleaseNote, 0, len(q.Nodes))
for _, n := range q.Nodes {
logger := logger.With("pr", n.PullRequest.Number, "prid", n.PullRequest.ID)
logger.Info("building release note")
author, authorURL, found := authorFromPR(n.PullRequest.Body)
if !found {
author = n.PullRequest.Author.Login
authorURL = n.PullRequest.Author.URL
}
note := ReleaseNote{
PRDate: n.PullRequest.MergedAt,
PRNumber: n.PullRequest.Number,
PRURL: n.PullRequest.URL,
Author: author,
AuthorURL: authorURL,
}
for _, ln := range n.PullRequest.Labels.Nodes {
switch {
case stringInSlice(labelsBug, ln.Name):
note.Bug = true
case ln.Name == labelBreakingChange:
note.BreakingChange = true
default:
note.Labels = append(note.Labels, ln.Name)
}
}
for _, entry := range ReleaseNoteBlocks(n.PullRequest.Title, n.PullRequest.Body) {
n := note
n.Text = entry.Text
n.Type = entry.Type
notes = append(notes, n)
}
}
return notes, nil
}
var textInBodyREs = []*regexp.Regexp{
regexp.MustCompile("(?m)^```release-note\n(?P<note>.+)\n```"),
regexp.MustCompile("(?m)^```releasenote\n(?P<note>.+)\n```"),
regexp.MustCompile("(?m)^```release-note:(?P<type>[^\n]*)\n(?P<note>.+)\n```"),
regexp.MustCompile("(?m)^```releasenote:(?P<type>[^\n]*)\n(?P<note>.+)\n```"),
}
// ReleaseNoteBlocks accepts the PR title and body contents, and parses them
// into one or more `ReleaseNoteEntry`s. It first attempts to find explicit
// releasenote code blocks within the body, and failing that, falls back on
// using the PR title as long as it is not empty.
func ReleaseNoteBlocks(title, body string) []ReleaseNoteEntry {
var res []ReleaseNoteEntry
for _, re := range textInBodyREs {
matches := re.FindAllStringSubmatch(body, -1)
if len(matches) == 0 {
continue
}
for _, match := range matches {
note := ""
typ := ""
for i, name := range re.SubexpNames() {
switch name {
case "note":
note = match[i]
case "type":
typ = match[i]
}
if note != "" && typ != "" {
break
}
}
note = stripMarkdownBullet(note)
note = strings.TrimSpace(note)
typ = strings.TrimSpace(typ)
if note == "" {
continue
}
res = append(res, ReleaseNoteEntry{
Type: typ,
Text: note,
})
}
}
if len(res) < 1 && title != "" {
res = append(res, ReleaseNoteEntry{
Text: title,
})
}
sort.Slice(res, func(i, j int) bool {
if res[i].Type < res[j].Type {
return true
} else if res[j].Type < res[i].Type {
return false
} else if res[i].Text < res[j].Text {
return true
} else if res[j].Text < res[i].Text {
return false
}
return false
})
return res
}
func stripMarkdownBullet(note string) string {
re := regexp.MustCompile(`(?i)\*\s`)
return re.ReplaceAllString(note, "")
}
var authorInBodyREs = []*regexp.Regexp{
// /cc syntax is too ambiguous probably
// regexp.MustCompile("(?m)^/[Cc][Cc] *@(?P<login>.+)"),
regexp.MustCompile("(?m)^(\\*\\*)?[Oo]riginal [Aa]uthor:(\\*\\*)? *@(?P<login>.+)"),
}
func authorFromPR(body string) (string, string, bool) {
for _, re := range authorInBodyREs {
match := re.FindStringSubmatch(body)
if len(match) == 0 {
continue
}
author := ""
for i, name := range re.SubexpNames() {
if name == "login" {
author = match[i]
break
}
}
author = strings.TrimLeft(author, "@")
if author != "" {
authorURL := fmt.Sprintf("https://github.com/%s", author)
return author, authorURL, true
}
}
return "", "", false
}