generated from kubernetes/kubernetes-template-project
/
notes.go
579 lines (514 loc) · 18.6 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
/*
Copyright 2020 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 compose
import (
"fmt"
golog "log"
"regexp"
"strconv"
"strings"
"github.com/blang/semver/v4"
"sigs.k8s.io/kubebuilder-release-tools/notes/common"
"sigs.k8s.io/kubebuilder-release-tools/notes/git"
)
var (
releaseRE = regexp.MustCompile(`^release-((?:0\.(?P<minor>[[:digit:]]+))|(?P<major>[[:digit:]]+))$`)
)
// TODO(directxman12): we could use go-git, but it doesn't implement
// git-describe, which is a pain to implement by hand.
// ReleaseFromBranch extracts a major-ish (X or 0.Y) release given a branch name.
func ReleaseFromBranch(branchName string) (ReleaseBranch, error) {
parts := releaseRE.FindStringSubmatch(branchName)
if parts == nil {
return ReleaseBranch{}, fmt.Errorf("%q is not a valid release branch (release-0.Y or release-X)", branchName)
}
minorRaw := parts[releaseRE.SubexpIndex("minor")]
majorRaw := parts[releaseRE.SubexpIndex("major")]
switch {
case minorRaw != "":
minor, err := strconv.ParseUint(minorRaw, 10, 64)
if err != nil {
return ReleaseBranch{}, fmt.Errorf("could not parse minor version from %q: %w", minorRaw, err)
}
if minor == 0 {
return ReleaseBranch{}, fmt.Errorf("release-0.0 is not a valid release")
}
return ReleaseBranch{
Version: semver.Version{Major: 0, Minor: minor},
}, nil
case majorRaw != "":
major, err := strconv.ParseUint(majorRaw, 10, 64)
if err != nil {
return ReleaseBranch{}, fmt.Errorf("could not parse major version from %q: %w", majorRaw, err)
}
if major == 0 {
return ReleaseBranch{}, fmt.Errorf("release-0 is not a valid release")
}
return ReleaseBranch{
Version: semver.Version{Major: major},
}, nil
default:
return ReleaseBranch{}, fmt.Errorf("%q is not a valid release branch (release-0.Y or release-X)", branchName)
}
}
// ReleaseBranch represents a branch associated with major-ish (X or 0.Y) set
// of releases.
type ReleaseBranch struct {
semver.Version
UseUpstream bool
}
func (b ReleaseBranch) String() string {
upstreamPart := ""
if b.UseUpstream {
upstreamPart = "@{u}"
}
if b.Major == 0 {
return fmt.Sprintf("release-0.%d%s", b.Minor, upstreamPart)
}
return fmt.Sprintf("release-%d%s", b.Major, upstreamPart)
}
func (b ReleaseBranch) Committish() string {
return b.String()
}
// ReleaseTag is a Committish that's actually a version-tag for a release.
type ReleaseTag semver.Version
func (v ReleaseTag) Committish() string {
return "v" + semver.Version(v).String()
}
func (v ReleaseTag) String() string {
return v.Committish()
}
func (v ReleaseTag) Validate() error {
if len(v.Pre) != 0 && len(v.Pre) != 2 {
return fmt.Errorf("invalid pre-release info (must be -{alpha,beta,rc}.version)")
}
if len(v.Pre) == 2 && (v.Pre[0].IsNum || !v.Pre[1].IsNum) {
// TODO: validate alpha, beta, rc
return fmt.Errorf("invalid pre-release info (must be -{alpha,beta,rc}.version)")
}
return nil
}
// FirstCommit is a Committish that's the first commit on a branch, generally
// used when the previous release tag does not exist.
type FirstCommit struct {
Commit git.Commit
Branch ReleaseBranch
}
func (c FirstCommit) Committish() string {
return c.Commit.Committish()
}
// parseReleaseTag parses a git tag name into a ReleaseTag.
func parseReleaseTag(tagRaw git.Tag) (*ReleaseTag, error) {
tagRawBytes := []byte(tagRaw)
if tagRawBytes[0] != 'v' {
return nil, fmt.Errorf("not a version tag (vX.Y.Z)")
}
tagRawBytes = tagRawBytes[1:] // skip the 'v'
tagParsed, err := semver.Parse(string(tagRawBytes))
if err != nil {
return nil, err
}
tag := ReleaseTag(tagParsed)
if err := tag.Validate(); err != nil {
return nil, err
}
return &tag, nil
}
// LatestRelease returns the most recent ReleaseTag on this branch, or a the
// FirstCommit if none existed.
func (b ReleaseBranch) LatestRelease(gitImpl git.Git, checkVersion bool) (git.Committish, error) {
tagRaw, err := gitImpl.ClosestTag(b)
if err != nil {
golog.Printf("unable to get latest tag starting at %q, assuming we need to look for the first commit instead (%v)", b, err)
// try to get the first commit
commitSHA, commitErr := gitImpl.FirstCommit(b.String())
if commitErr != nil {
// double wrap to get both errors
return nil, fmt.Errorf("unable to grab first commit on branch %q (%v), also unable to fetch most recent tag: %w", b, err, commitErr)
}
return FirstCommit{
Branch: b,
Commit: commitSHA,
}, nil
}
tag, err := parseReleaseTag(tagRaw)
if err != nil {
return nil, fmt.Errorf("latest tag %q on branch %q was not a (valid?) version: %w", tag, b, err)
}
if !checkVersion {
golog.Printf("latest release on branch %q is probably %q", b, tag)
return ReleaseTag(*tag), nil
}
golog.Printf("latest release on branch %q is probably %q", b, tag)
relTag := ReleaseTag(*tag)
return relTag, b.VerifyTagBelongs(relTag)
}
// VerifyTagBelongs checks that a given tag has the correct major-ish version
// for this branch.
func (b ReleaseBranch) VerifyTagBelongs(tag ReleaseTag) error {
if tag.Major != b.Major || (tag.Major == 0 && tag.Minor != b.Minor) {
return fmt.Errorf("tag's version %v does not match the branch's version %v", tag, b)
}
return nil
}
// checkOrClearUpstream verifies that the upstream exists for this branch and
// clears UseUpstream if it does not. If UseUpstream is already false, this is
// a no-op.
func checkOrClearUpstream(gitImpl git.Git, branch *ReleaseBranch) {
if !branch.UseUpstream {
return
}
if err := gitImpl.HasUpstream(branch.String()); err != nil {
branch.UseUpstream = false
golog.Printf("branch %q did not have an upstream, falling back to non-upstream (%v)", branch, err)
}
}
// CurrentVersion locates the closest current version (release tag or first
// commit), starting at the HEAD of the current branch. If the branch has an
// upstream and is configured to use it, it'll try that first. If that doesn't
// work, it'll clear the UseUpstream field and try the non-upstream version.
//
// Furthermore, if it looks like the closest release actually shoulbelongs to the previous
// release branch, it'll double-check that branch instead, to get the actual most recent release.
// For instance, on a fresh `release-0.7` branch, the "latest" release might be `v0.6.0`
// (since the `v0.Y.0` releases are always off of the main branch), so it'll check `release-0.6`
// to find that the *actual* latest release is `v0.6.3`.
func CurrentVersion(gitImpl git.Git, branch *ReleaseBranch) (git.Committish, error) {
origUseUpstream := branch.UseUpstream // keep this around to keep trying later if necessary
checkOrClearUpstream(gitImpl, branch)
latestHere, err := branch.LatestRelease(gitImpl, false)
if err != nil {
return nil, err
}
tag, isTag := latestHere.(ReleaseTag)
if !isTag {
golog.Printf("no latest tag, not double-checking version matches")
return latestHere, nil
}
switch {
case branch.Major == 0 && tag.Major == 0 && tag.Minor == branch.Minor-1:
// most recent tag is a release behind, check the previous branch:
// on the first release on a release branch, we'll generally end up
// seeing the first release of the last "major-ish" (X, or 0.Y), since
// that'll be the only one that ends up on master (the rest are on
// a release branch). Therefore, switch branches backwards to get the actual
// last tag.
prevRel := ReleaseBranch{
Version: semver.Version{Major: 0, Minor: tag.Minor},
UseUpstream: origUseUpstream,
}
golog.Printf("most recent tag %q is from last version (probably a 0.Y bump), double-checking previous release branch %q for actual latest version", tag.Committish(), prevRel)
checkOrClearUpstream(gitImpl, &prevRel)
return prevRel.LatestRelease(gitImpl, true)
case branch.Major == 1 && tag.Major == 0:
// ditto as above, except with 1 releases instead to 0.Y
prevRel := ReleaseBranch{
Version: semver.Version{Minor: tag.Minor},
UseUpstream: branch.UseUpstream,
}
golog.Printf("most recent tag %q is from last version (probably a 0.Y --> 1 bump), double-checking previous release branch %q for actual latest version", tag.Committish(), prevRel)
checkOrClearUpstream(gitImpl, &prevRel)
return prevRel.LatestRelease(gitImpl, true)
case branch.Major > 0 && tag.Major == branch.Major-1:
// same as the first case, except with X releases instead of 0.Y
prevRel := ReleaseBranch{
Version: semver.Version{Major: tag.Major},
UseUpstream: branch.UseUpstream,
}
golog.Printf("most recent tag %q is from last version (probably a X bump), double-checking previous release branch %q for actual latest version", tag.Committish(), prevRel)
checkOrClearUpstream(gitImpl, &prevRel)
return prevRel.LatestRelease(gitImpl, true)
default:
return tag, branch.VerifyTagBelongs(tag)
}
}
// LogEntry contains a single changelog entry from a PR.
type LogEntry struct {
PRNumber string
Title string
}
// ChangeLog holds all changes between a release and HEAD, organized by release type.
type ChangeLog struct {
Breaking []LogEntry
Features []LogEntry
Bugs []LogEntry
Docs []LogEntry
Infra []LogEntry
Uncategorized []LogEntry
}
// entryFromCommit adds a changelog entry to this changelog
// based on the emoji marker in the title.
func (l *ChangeLog) entryFromCommit(prNum, title string) {
entry := LogEntry{PRNumber: prNum}
prType, title := common.PRTypeFromTitle(title)
entry.Title = title
switch prType {
case common.FeaturePR:
l.Features = append(l.Features, entry)
case common.BugfixPR:
l.Bugs = append(l.Bugs, entry)
case common.DocsPR:
l.Docs = append(l.Docs, entry)
case common.InfraPR:
l.Infra = append(l.Infra, entry)
case common.BreakingPR:
l.Breaking = append(l.Breaking, entry)
case common.UncategorizedPR:
l.Uncategorized = append(l.Uncategorized, entry)
default:
panic(fmt.Sprintf("unrecognized internal PR type %v", prType))
}
}
// ReleaseKind indicates the "finality" of this release -- pre-release (alpha,
// beta, rc) or final.
type ReleaseKind int
const (
ReleaseFinal ReleaseKind = 0
ReleaseAlpha ReleaseKind = 1
ReleaseBeta ReleaseKind = 2
ReleaseCandidate ReleaseKind = 3
)
// ReleaseInfo describes the desired type of release.
type ReleaseInfo struct {
// Kind is the finality of the release.
Kind ReleaseKind
// Pre10 indicates that if the current release is 0.Y, and we'd need a new
// major-ish version, choose v0.(Y+1) and not v1.0.0.
Pre10 bool
}
// ExpectedNextVersion computes what the next version for should be given a set
// of changes, and desired type of release.
//
// Roughly, this means that, if one of the releases (current or desired next)
// is a final release:
//
// - 0.Y releases are equivalent to either X or X.Y releases
// - Breaking changes bump X
// - Features bump Y
// - Anything else just bumps Z
//
// If we're jumping between prereleases, ignore all that and either increment
// the pre-release number or reset the number to zero if we're switching types.
//
// If Pre10 is set, never jump to v1.0.0.
func (c ChangeLog) ExpectedNextVersion(currentVersion git.Committish, info ReleaseInfo) (ReleaseTag, error) {
tag, isTag := currentVersion.(ReleaseTag)
if !isTag {
res := ReleaseTag(semver.Version{
Minor: 1,
})
switch info.Kind {
case ReleaseAlpha:
res.Pre = []semver.PRVersion{{VersionStr: "alpha"}, {VersionNum: 0, IsNum: true}}
case ReleaseBeta:
res.Pre = []semver.PRVersion{{VersionStr: "beta"}, {VersionNum: 0, IsNum: true}}
case ReleaseCandidate:
res.Pre = []semver.PRVersion{{VersionStr: "rc"}, {VersionNum: 0, IsNum: true}}
}
return res, nil
}
// final releases
newTag := tag
if info.Kind == ReleaseFinal {
// pre --> final: reset pre, keep version
if len(newTag.Pre) > 0 {
newTag.Pre = nil
return newTag, nil
}
// final --> final: bump according to rules
return c.nextFinalVersion(tag, info.Pre10), nil
}
// easy pre-release case: same type of pre-release
// alpha --> alpha || beta --> beta || rc --> rc
wasPre := len(tag.Pre) > 0
alphaToAlpha := wasPre && tag.Pre[0] == semver.PRVersion{VersionStr: "alpha"} && info.Kind == ReleaseAlpha
betaToBeta := wasPre && tag.Pre[0] == semver.PRVersion{VersionStr: "beta"} && info.Kind == ReleaseBeta
candidateToCandidate := wasPre && tag.Pre[0] == semver.PRVersion{VersionStr: "candidate"} && info.Kind == ReleaseCandidate
if alphaToAlpha || betaToBeta || candidateToCandidate {
newTag := tag
// don't clobber old release
newTag.Pre = make([]semver.PRVersion, len(tag.Pre))
copy(newTag.Pre, tag.Pre)
newTag.Pre[1].VersionNum++
return newTag, nil
}
// otherwise, if the old release was a final release...
if tag.Pre == nil {
// ...bump according to rules...
newTag = c.nextFinalVersion(tag, info.Pre10)
}
// ...either way, add the appropriate new pre tag @ 0
switch info.Kind {
case ReleaseAlpha:
newTag.Pre = []semver.PRVersion{{VersionStr: "alpha"}, {VersionNum: 0, IsNum: true}}
case ReleaseBeta:
newTag.Pre = []semver.PRVersion{{VersionStr: "beta"}, {VersionNum: 0, IsNum: true}}
case ReleaseCandidate:
newTag.Pre = []semver.PRVersion{{VersionStr: "rc"}, {VersionNum: 0, IsNum: true}}
}
if semver.Version(newTag).LE(semver.Version(tag)) {
return newTag, fmt.Errorf("\"new\" version %q actually would be an older version than current %q", newTag.Committish(), tag.Committish())
}
return newTag, nil
}
// nextFinalVersion computes the next "final" release given the current one and
// the desired (or lack thereof) to go to v1.0.0.
func (c ChangeLog) nextFinalVersion(current ReleaseTag, pre10 bool) ReleaseTag {
newTag := semver.Version(current)
newTag.Pre = nil
newTag.Build = nil
switch {
case len(c.Breaking) > 0:
if current.Major == 0 && pre10 {
newTag.IncrementMinor()
} else {
newTag.IncrementMajor()
}
case len(c.Features) > 0:
newTag.IncrementMinor()
// we're doing a new version anyway, so we probably at least need a patch
default:
newTag.IncrementPatch()
}
return ReleaseTag(newTag)
}
// Changes computes the changelog from last release TO HEAD, returning both the
// changelog and the last release used.
func Changes(gitImpl git.Git, branch *ReleaseBranch) (log ChangeLog, since git.Committish, err error) {
since, err = CurrentVersion(gitImpl, branch)
if err != nil {
return ChangeLog{}, nil, err
}
changes, err := ChangesSince(gitImpl, *branch, since)
return changes, since, err
}
// ChangesSince computes the changelog from the given point to HEAD.
func ChangesSince(gitImpl git.Git, branch ReleaseBranch, since git.Committish) (ChangeLog, error) {
golog.Printf("finding changes since %q", since.Committish())
commitsRaw, err := gitImpl.MergeCommitsBetween(since, branch)
if err != nil {
return ChangeLog{}, fmt.Errorf("unable to list commits since %s on branch %q: %w", since.Committish(), branch, err)
}
log := ChangeLog{}
// do this parser-style
commitLines := strings.Split(commitsRaw, "\n")
lines := &lineReader{lines: commitLines}
for lines.more() {
var commit, prNumber, fork string
if !lines.expectScanf("commit %s", &commit) {
// skip terminating blank line, and others
// basically, just get to the next known good state
if lines.line() != "" {
golog.Printf("ignoring seemly non-commit line %q", lines.line())
}
continue
}
if !lines.expectScanf("Merge pull request #%s from %s", &prNumber, &fork) {
// might be one of the mistakes that got into our history, just
// bail till the next commit they look like `Merge branch 'BR'`,
// generally
golog.Printf("skipping non-official merge commit (%q) with title %q", commit, lines.line())
continue
}
if !lines.expectBlank() {
golog.Printf("got unexpected non-blank line %q, skipping till next commit", lines.line())
continue
}
if !lines.next() {
break
}
log.entryFromCommit(prNumber, lines.line())
}
return log, nil
}
// lineReader helps parsing line-by-line data, like rev-list output.
// start by setting lines.
type lineReader struct {
lines []string
cur string
}
// next loads the next line, returning false if none are available.
func (l *lineReader) next() bool {
if len(l.lines) == 0 {
l.cur = ""
return false
}
l.cur = l.lines[0]
l.lines = l.lines[1:]
return true
}
// more checks if the next call to next would return true.
func (l *lineReader) more() bool {
return len(l.lines) > 0
}
// line grabs the current line.
func (l *lineReader) line() string {
return l.cur
}
// expectScanf loads a new line and scans it according to the supplied args,
// returning false if it didn't scan or no lines were available.
func (l *lineReader) expectScanf(fmtStr string, args ...interface{}) bool {
if !l.next() {
return false
}
n, err := fmt.Sscanf(l.cur, fmtStr, args...)
return err == nil && n == len(args)
}
// expectScanf loads a new line and scans it according to the supplied args,
// returning false if it's not blank or no lines were available.
func (l *lineReader) expectBlank() bool {
if !l.next() {
return false
}
return l.cur == ""
}
// IsPreReleaseToFinal figures out if we're going from a pre-release
// version to a final version. If true, current is guaranteed to be
// a ReleaseTag.
func IsPreReleaseToFinal(current git.Committish, next ReleaseTag) bool {
if len(next.Pre) != 0 {
return false
}
tag, isTag := current.(ReleaseTag)
if !isTag {
return false
}
return len(tag.Pre) != 0
}
// ClosestFinal finds the "closest" previous final release. For example, given
// `v0.7.0-rc.3`, the closest final release might be `v0.6.3`.
func ClosestFinal(gitImpl git.Git, current ReleaseTag) (*ReleaseTag, error) {
currentFinal := semver.Version(current)
currentFinal.Pre = nil
toCheckTag := semver.Version(current)
var toCheck git.Committish = current
for len(toCheckTag.Pre) != 0 || toCheckTag.EQ(currentFinal) {
tagRaw, err := gitImpl.ClosestTag(git.SomeCommittish(toCheck.Committish() + "~1"))
if err != nil {
return nil, err
}
latestTag, err := parseReleaseTag(tagRaw)
if err != nil {
golog.Printf("skipping non-release tag %q: %v", string(tagRaw), err)
toCheck = git.SomeCommittish(string(tagRaw))
continue
}
toCheck = latestTag
toCheckTag = semver.Version(*latestTag)
}
if toCheckTag.EQ(semver.Version(current)) || len(toCheckTag.Pre) != 0 {
return nil, fmt.Errorf("unable to locate previous final release, just found current one")
}
tag := ReleaseTag(toCheckTag)
return &tag, nil
}