-
Notifications
You must be signed in to change notification settings - Fork 502
/
notes.go
1298 lines (1096 loc) · 35.9 KB
/
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2017 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.
*/
package notes
import (
"bufio"
"context"
"crypto/rand"
"crypto/sha1" //nolint:gosec // used for file integrity checks, NOT security
"encoding/hex"
"errors"
"fmt"
"math/big"
"net/http"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"unicode"
gogithub "github.com/google/go-github/v60/github"
"github.com/nozzle/throttler"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/sirupsen/logrus"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"gopkg.in/yaml.v2"
"sigs.k8s.io/release-sdk/github"
"k8s.io/release/pkg/notes/options"
)
var (
errNoPRIDFoundInCommitMessage = errors.New("no PR IDs found in the commit message")
errNoPRFoundForCommitSHA = errors.New("no PR found for this commit")
apiSleepTime int64 = 60
)
const (
DefaultOrg = "kubernetes"
DefaultRepo = "kubernetes"
// maxParallelRequests is the maximum parallel requests we shall make to the
// GitHub API.
maxParallelRequests = 10
)
type (
Notes []string
Kind string
)
const (
KindAPIChange Kind = "api-change"
KindBug Kind = "bug"
KindCleanup Kind = "cleanup"
KindDeprecation Kind = "deprecation"
KindDesign Kind = "design"
KindDocumentation Kind = "documentation"
KindFailingTest Kind = "failing-test"
KindFeature Kind = "feature"
KindFlake Kind = "flake"
KindRegression Kind = "regression"
KindOther Kind = "Other (Cleanup or Flake)"
KindUncategorized Kind = "Uncategorized"
)
// ReleaseNote is the type that represents the total sum of all the information
// we've gathered about a single release note.
type ReleaseNote struct {
// Commit is the SHA of the commit which is the source of this note. This is
// also effectively a unique ID for release notes.
Commit string `json:"commit"`
// Text is the actual content of the release note
Text string `json:"text"`
// Markdown is the markdown formatted note
Markdown string `json:"markdown"`
// Docs is additional documentation for the release note
Documentation []*Documentation `json:"documentation,omitempty"`
// 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"`
// PrURL is a URL to the PR
PrURL string `json:"pr_url"`
// PrNumber is the number of the PR
PrNumber int `json:"pr_number"`
// Areas is a list of the labels beginning with area/
Areas []string `json:"areas,omitempty"`
// Kinds is a list of the labels beginning with kind/
Kinds []string `json:"kinds,omitempty"`
// SIGs is a list of the labels beginning with sig/
SIGs []string `json:"sigs,omitempty"`
// Indicates whether or not a note will appear as a new feature
Feature bool `json:"feature,omitempty"`
// Indicates whether or not a note is duplicated across SIGs
Duplicate bool `json:"duplicate,omitempty"`
// Indicates whether or not a note is duplicated across Kinds
DuplicateKind bool `json:"duplicate_kind,omitempty"`
// ActionRequired indicates whether or not the release-note-action-required
// label was set on the PR
ActionRequired bool `json:"action_required,omitempty"`
// DoNotPublish by default represents release-note-none label on GitHub
DoNotPublish bool `json:"do_not_publish,omitempty"`
// DataFields a key indexed map of data fields
DataFields map[string]ReleaseNotesDataField `json:"-"`
// IsMapped is set if the note got modified from a map
IsMapped bool `json:"is_mapped,omitempty"`
// PRBody is the full PR body of the release note
PRBody string `json:"pr_body,omitempty"`
}
type Documentation struct {
// A description about the documentation
Description string `json:"description,omitempty"`
// The url to be linked
URL string `json:"url"`
// Classifies the link as something special, like a KEP
Type DocType `json:"type"`
}
type DocType string
const (
DocTypeExternal DocType = "external"
DocTypeKEP DocType = "KEP"
DocTypeOfficial DocType = "official"
)
// ReleaseNotes is the main struct for collecting release notes.
type ReleaseNotes struct {
byPR ReleaseNotesByPR
history ReleaseNotesHistory
}
// NewReleaseNotes can be used to create a new empty ReleaseNotes struct.
func NewReleaseNotes() *ReleaseNotes {
return &ReleaseNotes{
byPR: make(ReleaseNotesByPR),
}
}
// ReleaseNotes is a map of PR numbers referencing notes.
// To avoid needless loops, we need to be able to reference things by PR
// When we have to merge old and new entries, we want to be able to override
// the old entries with the new ones efficiently.
type ReleaseNotesByPR map[int]*ReleaseNote
// ReleaseNotesHistory is the sorted list of PRs in the commit history.
type ReleaseNotesHistory []int
// History returns the ReleaseNotesHistory for the ReleaseNotes.
func (r *ReleaseNotes) History() ReleaseNotesHistory {
return r.history
}
// ByPR returns the ReleaseNotesByPR for the ReleaseNotes.
func (r *ReleaseNotes) ByPR() ReleaseNotesByPR {
return r.byPR
}
// Get returns the ReleaseNote for the provided prNumber.
func (r *ReleaseNotes) Get(prNumber int) *ReleaseNote {
return r.byPR[prNumber]
}
// Set can be used to set a release note for the provided prNumber.
func (r *ReleaseNotes) Set(prNumber int, note *ReleaseNote) {
r.byPR[prNumber] = note
r.history = append(r.history, prNumber)
}
type Result struct {
commit *gogithub.RepositoryCommit
pullRequest *gogithub.PullRequest
}
type Gatherer struct {
client github.Client
context context.Context
options *options.Options
MapProviders []*MapProvider
}
// NewGatherer creates a new notes gatherer.
func NewGatherer(ctx context.Context, opts *options.Options) (*Gatherer, error) {
client, err := opts.Client()
if err != nil {
return nil, fmt.Errorf("unable to create notes client: %w", err)
}
return &Gatherer{
client: client,
context: ctx,
options: opts,
}, nil
}
// NewGathererWithClient creates a new notes gatherer with a specific client.
func NewGathererWithClient(ctx context.Context, c github.Client) *Gatherer {
return &Gatherer{
client: c,
context: ctx,
options: options.New(),
}
}
// GatherReleaseNotes creates a new gatherer and collects the release notes
// afterwards.
func GatherReleaseNotes(opts *options.Options) (*ReleaseNotes, error) {
logrus.Info("Gathering release notes")
gatherer, err := NewGatherer(context.Background(), opts)
if err != nil {
return nil, fmt.Errorf("retrieving notes gatherer: %w", err)
}
var releaseNotes *ReleaseNotes
startTime := time.Now()
if gatherer.options.ListReleaseNotesV2 {
logrus.Warn("EXPERIMENTAL IMPLEMENTATION ListReleaseNotesV2 ENABLED")
releaseNotes, err = gatherer.ListReleaseNotesV2()
} else {
releaseNotes, err = gatherer.ListReleaseNotes()
}
if err != nil {
return nil, fmt.Errorf("listing release notes: %w", err)
}
logrus.Infof("Finished gathering release notes in %v", time.Since(startTime))
return releaseNotes, nil
}
// ListReleaseNotes produces a list of fully contextualized release notes
// starting from a given commit SHA and ending at starting a given commit SHA.
func (g *Gatherer) ListReleaseNotes() (*ReleaseNotes, error) {
// Load map providers
mapProviders := []MapProvider{}
for _, initString := range g.options.MapProviderStrings {
provider, err := NewProviderFromInitString(initString)
if err != nil {
return nil, fmt.Errorf("while getting release notes map providers: %w", err)
}
mapProviders = append(mapProviders, provider)
}
commits, err := g.listCommits(g.options.Branch, g.options.StartSHA, g.options.EndSHA)
if err != nil {
return nil, fmt.Errorf("listing commits: %w", err)
}
// Get the PRs into a temporary results set
resultsTemp, err := g.gatherNotes(commits)
if err != nil {
return nil, fmt.Errorf("gathering notes: %w", err)
}
// Cycle the results and add the complete notes, as well as those that
// have a map associated with it
results := []*Result{}
logrus.Info("Checking PRs for mapped data")
for _, res := range resultsTemp {
// If the PR has no release note, check if we have to add it
if MatchesExcludeFilter(*res.pullRequest.Body) {
for _, provider := range mapProviders {
noteMaps, err := provider.GetMapsForPR(res.pullRequest.GetNumber())
if err != nil {
return nil, fmt.Errorf(
"checking if a map exists for PR %d: %w", res.pullRequest.GetNumber(),
err)
}
if len(noteMaps) != 0 {
logrus.Infof(
"Artificially adding pr #%d because a map for it was found",
res.pullRequest.GetNumber(),
)
results = append(results, res)
} else {
logrus.Debugf(
"Skipping PR #%d because it contains no release note",
res.pullRequest.GetNumber(),
)
}
}
} else {
// Append the note as it is
results = append(results, res)
}
}
dedupeCache := map[string]struct{}{}
notes := NewReleaseNotes()
for _, result := range results {
if g.options.RequiredAuthor != "" {
if result.commit.GetAuthor().GetLogin() != g.options.RequiredAuthor {
logrus.Infof(
"Skipping release note for PR #%d because required author %q does not match with %q",
result.pullRequest.GetNumber(), g.options.RequiredAuthor, result.commit.GetAuthor().GetLogin(),
)
continue
}
}
note, err := g.ReleaseNoteFromCommit(result)
if err != nil {
logrus.Errorf(
"Getting the release note from commit %s (PR #%d): %v",
result.commit.GetSHA(),
result.pullRequest.GetNumber(),
err)
continue
}
// Query our map providers for additional data for the release note
for _, provider := range mapProviders {
noteMaps, err := provider.GetMapsForPR(result.pullRequest.GetNumber())
if err != nil {
return nil, fmt.Errorf("error while looking up note map: %w", err)
}
for _, noteMap := range noteMaps {
if err := note.ApplyMap(noteMap, g.options.AddMarkdownLinks); err != nil {
return nil, fmt.Errorf("applying notemap for PR #%d: %w", result.pullRequest.GetNumber(), err)
}
}
}
if _, ok := dedupeCache[note.Markdown]; !ok {
notes.Set(note.PrNumber, note)
dedupeCache[note.Markdown] = struct{}{}
}
}
return notes, nil
}
// noteTextFromString returns the text of the release note given a string which
// may contain the commit message, the PR description, etc.
// This is generally the content inside the ```release-note ``` stanza.
func noteTextFromString(s string) (string, error) {
// check release note is not empty
// Matches "release-notes" block with no meaningful content (ex. only whitespace, empty, just newlines)
emptyExps := []*regexp.Regexp{
regexp.MustCompile("(?i)```release-notes?\\s*```\\s*"),
}
if matchesFilter(s, emptyExps) {
return "", errors.New("empty release note")
}
exps := []*regexp.Regexp{
// (?s) is needed for '.' to be matching on newlines, by default that's disabled
// we need to match ungreedy 'U', because after the notes a `docs` block can occur
regexp.MustCompile("(?sU)```release-notes?\\r\\n(?P<note>.+)\\r\\n```"),
regexp.MustCompile("(?sU)```dev-release-notes?\\r\\n(?P<note>.+)"),
regexp.MustCompile("(?sU)```\\r\\n(?P<note>.+)\\r\\n```"),
regexp.MustCompile("(?sU)```release-notes?\n(?P<note>.+)\n```"),
}
for _, exp := range exps {
match := exp.FindStringSubmatch(s)
if len(match) == 0 {
continue
}
result := map[string]string{}
for i, name := range exp.SubexpNames() {
if i != 0 && name != "" {
result[name] = match[i]
}
}
note := strings.ReplaceAll(result["note"], "\r", "")
note = stripActionRequired(note)
note = dashify(note)
note = unlist(note)
note = strings.TrimSpace(note)
return note, nil
}
return "", errors.New("no matches found when parsing note text from commit string")
}
func DocumentationFromString(s string) []*Documentation {
regex := regexp.MustCompile("(?s)```docs\\r?\\n(?P<text>.+)\\r?\\n```")
match := regex.FindStringSubmatch(s)
if len(match) < 1 {
// Nothing found, but we don't require it
return nil
}
result := []*Documentation{}
text := match[1]
text = stripStar(text)
text = stripDash(text)
scanner := bufio.NewScanner(strings.NewReader(text))
for scanner.Scan() {
const httpPrefix = "http"
s := strings.SplitN(scanner.Text(), httpPrefix, 2)
if len(s) != 2 {
continue
}
description := strings.TrimRight(strings.TrimSpace(s[0]), " :-")
urlString := httpPrefix + strings.TrimSpace(s[1])
// Validate the URL
parsedURL, err := url.Parse(urlString)
if err != nil {
continue
}
result = append(result, &Documentation{
Description: description,
URL: urlString,
Type: classifyURL(parsedURL),
})
}
return result
}
// classifyURL returns the correct DocType for the given url.
func classifyURL(u *url.URL) DocType {
// Kubernetes Enhancement Proposals (KEPs)
if strings.Contains(u.Host, "github.com") &&
strings.Contains(u.Path, "/kubernetes/enhancements/") {
return DocTypeKEP
}
// Official documentation
if strings.Contains(u.Host, "kubernetes.io") &&
strings.Contains(u.Path, "/docs/") {
return DocTypeOfficial
}
return DocTypeExternal
}
// ReleaseNoteFromCommit produces a full contextualized release note given a
// GitHub commit API resource.
func (g *Gatherer) ReleaseNoteFromCommit(result *Result) (*ReleaseNote, error) {
pr := result.pullRequest
prBody := pr.GetBody()
text, err := noteTextFromString(prBody)
if err != nil {
return nil, err
}
documentation := DocumentationFromString(prBody)
author := pr.GetUser().GetLogin()
authorURL := pr.GetUser().GetHTMLURL()
prURL := pr.GetHTMLURL()
isFeature := hasString(labelsWithPrefix(pr, "kind"), "feature")
sigLabels := labelsWithPrefix(pr, "sig")
noteSuffix := prettifySIGList(sigLabels)
isDuplicateSIG := false
if len(labelsWithPrefix(pr, "sig")) > 1 {
isDuplicateSIG = true
}
isDuplicateKind := false
if len(labelsWithPrefix(pr, "kind")) > 1 {
isDuplicateKind = true
}
// TODO: Spin this to sep function
indented := strings.ReplaceAll(text, "\n", "\n ")
markdown := fmt.Sprintf("%s (#%d, @%s)",
indented, pr.GetNumber(), author)
if g.options.AddMarkdownLinks {
markdown = fmt.Sprintf("%s ([#%d](%s), [@%s](%s))",
indented, pr.GetNumber(), prURL, author, authorURL)
}
if noteSuffix != "" {
markdown = fmt.Sprintf("%s [%s]", markdown, noteSuffix)
}
// Uppercase the first character of the markdown to make it look uniform
markdown = capitalizeString(markdown)
return &ReleaseNote{
Commit: result.commit.GetSHA(),
Text: text,
Markdown: markdown,
Documentation: documentation,
Author: author,
AuthorURL: authorURL,
PrURL: prURL,
PrNumber: pr.GetNumber(),
SIGs: sigLabels,
Kinds: labelsWithPrefix(pr, "kind"),
Areas: labelsWithPrefix(pr, "area"),
Feature: isFeature,
Duplicate: isDuplicateSIG,
DuplicateKind: isDuplicateKind,
ActionRequired: labelExactMatch(pr, "release-note-action-required"),
DoNotPublish: labelExactMatch(pr, "release-note-none"),
PRBody: prBody,
}, nil
}
// listCommits lists all commits starting from a given commit SHA and ending at
// a given commit SHA.
func (g *Gatherer) listCommits(branch, start, end string) ([]*gogithub.RepositoryCommit, error) {
startCommit, _, err := g.client.GetCommit(g.context, g.options.GithubOrg, g.options.GithubRepo, start)
if err != nil {
return nil, fmt.Errorf("retrieve start commit: %w", err)
}
endCommit, _, err := g.client.GetCommit(g.context, g.options.GithubOrg, g.options.GithubRepo, end)
if err != nil {
return nil, fmt.Errorf("retrieve end commit: %w", err)
}
allCommits := &commitList{}
worker := func(clo *gogithub.CommitsListOptions) (
commits []*gogithub.RepositoryCommit, resp *gogithub.Response, err error,
) {
for {
commits, resp, err = g.client.ListCommits(g.context, g.options.GithubOrg, g.options.GithubRepo, clo)
if err != nil {
if !canWaitAndRetry(resp, err) {
return nil, nil, err
}
} else {
break
}
}
return commits, resp, err
}
clo := gogithub.CommitsListOptions{
SHA: branch,
Since: startCommit.GetCommitter().GetDate().Time,
Until: endCommit.GetCommitter().GetDate().Time,
ListOptions: gogithub.ListOptions{
Page: 1,
PerPage: 100,
},
}
commits, resp, err := worker(&clo)
if err != nil {
return nil, err
}
allCommits.Add(commits)
remainingPages := resp.LastPage - 1
if remainingPages < 1 {
return allCommits.List(), nil
}
t := throttler.New(maxParallelRequests, remainingPages)
for page := 2; page <= resp.LastPage; page++ {
clo := clo
clo.ListOptions.Page = page
go func() {
commits, _, err := worker(&clo)
if err == nil {
allCommits.Add(commits)
}
t.Done(err)
}()
// abort all, if we got one error
if t.Throttle() > 0 {
break
}
}
if err := t.Err(); err != nil {
return nil, err
}
return allCommits.List(), nil
}
type commitList struct {
sync.RWMutex
list []*gogithub.RepositoryCommit
}
func (l *commitList) Add(c []*gogithub.RepositoryCommit) {
l.Lock()
defer l.Unlock()
l.list = append(l.list, c...)
}
func (l *commitList) List() []*gogithub.RepositoryCommit {
l.RLock()
defer l.RUnlock()
return l.list
}
// noteExclusionFilters is a list of regular expressions that match commits
// that do NOT contain release notes. Notably, this is all of the variations of
// "release note none" that appear in the commit log.
var noteExclusionFilters = []*regexp.Regexp{
// 'none','n/a','na' case-insensitive with optional trailing
// whitespace, wrapped in ``` with/without release-note identifier
// the 'none','n/a','na' can also optionally be wrapped in quotes ' or "
regexp.MustCompile("(?i)```release-notes?\\s*('\")?(none|n/a|na)('\")?\\s*```"),
// simple '/release-note-none' tag
regexp.MustCompile("/release-note-none"),
}
// MatchesExcludeFilter returns true if the string matches an excluded release note.
func MatchesExcludeFilter(msg string) bool {
return matchesFilter(msg, noteExclusionFilters)
}
func matchesFilter(msg string, filters []*regexp.Regexp) bool {
for _, filter := range filters {
if filter.MatchString(msg) {
return true
}
}
return false
}
// gatherNotes list commits that have release notes starting from a given
// commit SHA and ending at a given commit SHA. This function is similar to
// listCommits except that only commits with tagged release notes are returned.
func (g *Gatherer) gatherNotes(commits []*gogithub.RepositoryCommit) (filtered []*Result, err error) {
allResults := &resultList{}
nrOfCommits := len(commits)
// A note about prallelism:
//
// We make 2 different requests to GitHub further down the stack:
// - If we find PR numbers in a commit message, we do one API call per found
// number. The assumption is, that this is mostly just one (or just a couple
// of) PRs. The calls to the API do run in serial right now.
// - If we don't find a PR number in the commit message, we ask the API if
// GitHub knows about PRs that are connected to that specific commit. The
// assumption again is that this is either one or just a couple of PRs. The
// results probably fit into one GitHub result page. If not, and we need to
// query multiple times (paging), we currently also do that in serial.
//
// In case we parallelize the above mentioned API calls and the volume of
// them is bigger than expected, we might go well above the
// `maxParallelRequests` of parallel requests. In that case we probably
// should introduce the throttler as a global concept (on the Gatherer or so)
// and use that throttler for all API calls.
t := throttler.New(maxParallelRequests, nrOfCommits)
notesForCommit := func(commit *gogithub.RepositoryCommit) {
res, err := g.notesForCommit(commit)
if err == nil && res != nil {
allResults.Add(res)
}
t.Done(err)
}
for i, commit := range commits {
logrus.Infof(
"Starting to process commit %d of %d (%0.2f%%): %s",
i+1, nrOfCommits, (float64(i+1)/float64(nrOfCommits))*100.0,
commit.GetSHA(),
)
if g.options.ReplayDir == "" && g.options.RecordDir == "" {
go notesForCommit(commit)
} else {
// Ensure the same order like recorded
notesForCommit(commit)
}
if t.Throttle() > 0 {
break
}
} // for range commits
if err := t.Err(); err != nil {
return nil, err
}
return allResults.List(), nil
}
// ReleaseNoteForPullRequest returns a release note from a pull request number.
// If the release note is blank or.
func (g *Gatherer) ReleaseNoteForPullRequest(prNr int) (*ReleaseNote, error) {
pr, _, err := g.client.GetPullRequest(g.context, g.options.GithubOrg, g.options.GithubRepo, prNr)
if err != nil {
return nil, fmt.Errorf("reading PR from GitHub: %w", err)
}
prBody := pr.GetBody()
// This will be true when the release note is NONE or the flag is set
var doNotPublish bool
// If we match exclusion filter (release-note-none), we don't look further,
// instead we return that PR. We return that PR because it might have a map,
// which is checked after this function returns
if MatchesExcludeFilter(prBody) || len(labelsWithPrefix(pr, "release-note-none")) > 0 {
doNotPublish = true
}
// If we didn't match the exclusion filter, try to extract the release note from the PR.
// If we can't extract the release note, consider that the PR is invalid and take the next one
s, err := noteTextFromString(prBody)
if err != nil && !doNotPublish {
return nil, fmt.Errorf("PR #%d does not seem to contain a valid release note: %w", pr.GetNumber(), err)
}
// If we found a valid release note, return the PR, otherwise, take the next one
if s == "" && !doNotPublish {
return nil, fmt.Errorf("PR #%d does not seem to contain a valid release note", pr.GetNumber())
}
if doNotPublish {
s = ""
}
// Create the release notes object
note := &ReleaseNote{
Text: s,
Markdown: s,
Documentation: []*Documentation{},
Author: *pr.GetUser().Login,
AuthorURL: fmt.Sprintf("%s%s", g.options.GithubBaseURL, *pr.GetUser().Login),
PrURL: fmt.Sprintf("%s%s/%s/pull/%d", g.options.GithubBaseURL, g.options.GithubOrg, g.options.GithubRepo, prNr),
PrNumber: prNr,
SIGs: labelsWithPrefix(pr, "sig"),
Kinds: labelsWithPrefix(pr, "kind"),
Areas: labelsWithPrefix(pr, "area"),
Feature: false,
Duplicate: false,
DuplicateKind: false,
ActionRequired: false,
DoNotPublish: doNotPublish,
DataFields: map[string]ReleaseNotesDataField{},
PRBody: prBody,
}
if s != "" {
logrus.Infof("PR #%d seems to contain a release note", pr.GetNumber())
}
return note, nil
}
func (g *Gatherer) notesForCommit(commit *gogithub.RepositoryCommit) (*Result, error) {
prs, err := g.prsFromCommit(commit)
if err != nil {
if err == errNoPRIDFoundInCommitMessage || err == errNoPRFoundForCommitSHA {
logrus.Debugf(
"No matches found when parsing PR from commit SHA %s",
commit.GetSHA(),
)
return nil, nil
}
return nil, err
}
for _, pr := range prs {
prBody := pr.GetBody()
logrus.Debugf(
"Got PR #%d for commit: %s", pr.GetNumber(), commit.GetSHA(),
)
// If we match exclusion filter (release-note-none), we don't look further,
// instead we return that PR. We return that PR because it might have a map,
// which is checked after this function returns
if MatchesExcludeFilter(prBody) {
res := &Result{commit: commit, pullRequest: pr}
logrus.Infof("PR #%d contains exclusion (release-note-none)", pr.GetNumber())
return res, nil
}
// If we didn't match the exclusion filter, try to extract the release note from the PR.
// If we can't extract the release note, consider that the PR is invalid and take the next one
s, err := noteTextFromString(prBody)
if err != nil {
logrus.Infof("PR #%d does not seem to contain a valid release note, skipping", pr.GetNumber())
continue
}
// If we found a valid release note, return the PR, otherwise, take the next one
if s != "" {
res := &Result{commit: commit, pullRequest: pr}
logrus.Infof("PR #%d seems to contain a release note", pr.GetNumber())
// Do not test further PRs for this commit as soon as one PR matched
return res, nil
}
logrus.Infof("PR #%d does not seem to contain a valid release note, skipping", pr.GetNumber())
}
return nil, nil
}
type resultList struct {
sync.RWMutex
list []*Result
}
func (l *resultList) Add(r *Result) {
l.Lock()
defer l.Unlock()
l.list = append(l.list, r)
}
func (l *resultList) List() []*Result {
l.RLock()
defer l.RUnlock()
return l.list
}
// prsFromCommit return an API Pull Request struct given a commit struct. This is
// useful for going from a commit log to the PR (which contains useful info such
// as labels).
func (g *Gatherer) prsFromCommit(commit *gogithub.RepositoryCommit) (
[]*gogithub.PullRequest, error,
) {
githubPRs, err := g.prsForCommitFromMessage(*commit.Commit.Message)
if err != nil {
logrus.Debugf("No PR found for commit %s: %v", commit.GetSHA(), err)
return g.prsForCommitFromSHA(*commit.SHA)
}
return githubPRs, err
}
// labelsWithPrefix is a helper for fetching all labels on a PR that start with
// a given string. This pattern is used often in the k/k repo and we can take
// advantage of this to contextualize release note generation with the kind, sig,
// area, etc labels.
func labelsWithPrefix(pr *gogithub.PullRequest, prefix string) []string {
var labels []string
for _, label := range pr.Labels {
if strings.HasPrefix(*label.Name, prefix) {
labels = append(labels, strings.TrimPrefix(*label.Name, prefix+"/"))
}
}
return labels
}
// labelExactMatch indicates whether or not a matching label was found on PR.
func labelExactMatch(pr *gogithub.PullRequest, labelToFind string) bool {
for _, label := range pr.Labels {
if *label.Name == labelToFind {
return true
}
}
return false
}
func stripActionRequired(note string) string {
expressions := []string{
`(?i)\[action required\]\s`,
`(?i)action required:\s`,
}
for _, exp := range expressions {
re := regexp.MustCompile(exp)
note = re.ReplaceAllString(note, "")
}
return note
}
func stripStar(note string) string {
re := regexp.MustCompile(`(?i)\*\s`)
return re.ReplaceAllString(note, "")
}
func stripDash(note string) string {
re := regexp.MustCompile(`(?i)\-\s`)
return re.ReplaceAllString(note, "")
}
const listPrefix = "- "
func dashify(note string) string {
re := regexp.MustCompile(`(?m)(^\s*)\*\s`)
return re.ReplaceAllString(note, "$1- ")
}
// unlist transforms a single markdown list entry to a flat note entry.
func unlist(note string) string {
if !strings.HasPrefix(note, listPrefix) {
return note
}
res := strings.Builder{}
scanner := bufio.NewScanner(strings.NewReader(note))
firstLine := true
trim := true
for scanner.Scan() {
line := scanner.Text()
// Per default strip the two dashes from the list
prefix := " "
if strings.HasPrefix(line, listPrefix) {
if firstLine {
// First list item, strip the prefix
prefix = listPrefix
firstLine = false
} else {
// Another list item? Treat it as sublist and do not trim any
// more.
trim = false
}
}
if trim {
line = strings.TrimPrefix(line, prefix)
}
res.WriteString(line + "\n")
}
return res.String()
}
func hasString(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}
// canWaitAndRetry retruen true if the gatherer hit the GitHub API secondary rate limit.
func canWaitAndRetry(r *gogithub.Response, err error) bool {
// If we hit the secondary rate limit...
if r == nil {
return false
}
if r.StatusCode == http.StatusForbidden &&
strings.Contains(err.Error(), "secondary rate limit. Please wait") {
// ... sleep for a minute plus a random bit so that workers don't
// respawn all at the same time
rtime, err := rand.Int(rand.Reader, big.NewInt(30))
if err != nil {
logrus.Error(err)
return false
}
waitTime := rtime.Int64() + apiSleepTime
logrus.Warnf("Hit the GitHub secondary rate limit, sleeping for %d secs.", waitTime)
time.Sleep(time.Duration(waitTime) * time.Second)
return true
}
return false
}
// prsForCommitFromSHA retrieves the PR numbers for a commit given its sha.
func (g *Gatherer) prsForCommitFromSHA(sha string) (prs []*gogithub.PullRequest, err error) {
plo := &gogithub.ListOptions{
Page: 1,
PerPage: 100,
}