Skip to content

Commit b764989

Browse files
authored
Merge pull request #1245 from msau42/add-patch-to-latest
Add a patch-to-latest discover mode
2 parents 0a1672c + 1765c99 commit b764989

File tree

4 files changed

+104
-9
lines changed

4 files changed

+104
-9
lines changed

cmd/release-notes/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ level=debug timestamp=2019-07-30T04:02:44.3716249Z caller=notes.go:497 msg="Excl
8282
| repo-path | REPO_PATH | /tmp/k8s-repo | No | Path to a local Kubernetes repository, used only for tag discovery |
8383
| start-rev | START_REV | | No | The git revision to start at. Can be used as alternative to start-sha |
8484
| env-rev | END_REV | | No | The git revision to end at. Can be used as alternative to end-sha |
85-
| discover | DISCOVER | none | No | The revision discovery mode for automatic revision retrieval (options: none, mergebase-to-latest, patch-to-patch, minor-to-minor) |
85+
| discover | DISCOVER | none | No | The revision discovery mode for automatic revision retrieval (options: none, mergebase-to-latest, patch-to-patch, patch-to-latest, minor-to-minor) |
8686
| release-bucket | RELEASE_BUCKET | kubernetes-release | No | Specify gs bucket to point to in generated notes (default "kubernetes-release") |
8787
| release-tars | RELEASE_TARS | | No | Directory of tars to sha512 sum for display |
8888
| **OUTPUT OPTIONS** |

pkg/git/git.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,42 @@ func (r *Repo) LatestPatchToPatch(branch string) (DiscoverResult, error) {
629629
}, nil
630630
}
631631

632+
// LatestPatchToLatest tries to discover the start (latest v1.x.x]) and
633+
// end (release-1.x or master) revision inside the repository for the specified release
634+
// branch.
635+
func (r *Repo) LatestPatchToLatest(branch string) (DiscoverResult, error) {
636+
latestTag, err := r.LatestTagForBranch(branch)
637+
if err != nil {
638+
return DiscoverResult{}, err
639+
}
640+
641+
if len(latestTag.Pre) > 0 && latestTag.Patch > 0 {
642+
latestTag.Patch--
643+
latestTag.Pre = nil
644+
}
645+
646+
logrus.Debugf("parsing latest tag %s%v", util.TagPrefix, latestTag)
647+
latestVersionTag := util.SemverToTagString(latestTag)
648+
start, err := r.RevParse(latestVersionTag)
649+
if err != nil {
650+
return DiscoverResult{}, errors.Wrapf(err, "parsing version %v", latestTag)
651+
}
652+
653+
// If a release branch exists for the latest version, we use it. Otherwise we
654+
// fallback to the master branch.
655+
end, branch, err := r.releaseBranchOrMasterRev(latestTag.Major, latestTag.Minor)
656+
if err != nil {
657+
return DiscoverResult{}, errors.Wrapf(err, "getting release branch for %v", latestTag)
658+
}
659+
660+
return DiscoverResult{
661+
startSHA: start,
662+
startRev: latestVersionTag,
663+
endSHA: end,
664+
endRev: branch,
665+
}, nil
666+
}
667+
632668
// LatestTagForBranch returns the latest available semver tag for a given branch
633669
func (r *Repo) LatestTagForBranch(branch string) (tag semver.Version, err error) {
634670
tags, err := r.TagsForBranch(branch)

pkg/git/git_integration_test.go

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type testRepo struct {
4040
firstCommit string
4141
firstBranchCommit string
4242
secondBranchCommit string
43+
thirdBranchCommit string
4344
branchName string
4445
firstTagCommit string
4546
firstTagName string
@@ -52,6 +53,11 @@ type testRepo struct {
5253

5354
// newTestRepo creates a test repo with the following structure:
5455
//
56+
// * commit `thirdBranchCommit` (HEAD -> `branchName`, origin/`branchName`)
57+
// | Author: John Doe <[email protected]>
58+
// |
59+
// | Fourth commit
60+
// |
5561
// * commit `secondBranchCommit` (tag: `thirdTagName`, HEAD -> `branchName`, origin/`branchName`)
5662
// | Author: John Doe <[email protected]>
5763
// |
@@ -159,7 +165,7 @@ func newTestRepo(t *testing.T) *testRepo {
159165
})
160166
require.Nil(t, err)
161167

162-
thirdTagName := "v0.1.2"
168+
thirdTagName := "v1.17.1"
163169
thirdTagRef, err := cloneRepo.CreateTag(thirdTagName, secondBranchCommit,
164170
&gogit.CreateTagOptions{
165171
Tagger: author,
@@ -168,6 +174,21 @@ func newTestRepo(t *testing.T) *testRepo {
168174
)
169175
require.Nil(t, err)
170176

177+
const thirdBranchTestFileName = "branch-test-file-3"
178+
require.Nil(t, ioutil.WriteFile(
179+
filepath.Join(cloneTempDir, thirdBranchTestFileName),
180+
[]byte("test-content"),
181+
os.FileMode(0644),
182+
))
183+
_, err = worktree.Add(thirdBranchTestFileName)
184+
require.Nil(t, err)
185+
186+
thirdBranchCommit, err := worktree.Commit("Fourth commit", &gogit.CommitOptions{
187+
Author: author,
188+
All: true,
189+
})
190+
require.Nil(t, err)
191+
171192
// Push the test content into the bare repo
172193
_, err = cloneRepo.CreateRemote(&config.RemoteConfig{
173194
Name: git.DefaultRemote,
@@ -194,6 +215,7 @@ func newTestRepo(t *testing.T) *testRepo {
194215
firstCommit: firstCommit.String(),
195216
firstBranchCommit: firstBranchCommit.String(),
196217
secondBranchCommit: secondBranchCommit.String(),
218+
thirdBranchCommit: thirdBranchCommit.String(),
197219
branchName: branchName,
198220
firstTagName: firstTagName,
199221
firstTagCommit: firstTagRef.Hash().String(),
@@ -270,7 +292,7 @@ func TestSuccessHead(t *testing.T) {
270292

271293
head, err := testRepo.sut.Head()
272294
require.Nil(t, err)
273-
require.Equal(t, head, testRepo.secondBranchCommit)
295+
require.Equal(t, head, testRepo.thirdBranchCommit)
274296
}
275297

276298
func TestSuccessMerge(t *testing.T) {
@@ -308,7 +330,7 @@ func TestSuccessRevParse(t *testing.T) {
308330

309331
branchRev, err := testRepo.sut.RevParse(testRepo.branchName)
310332
require.Nil(t, err)
311-
require.Equal(t, branchRev, testRepo.secondBranchCommit)
333+
require.Equal(t, branchRev, testRepo.thirdBranchCommit)
312334

313335
tagRev, err := testRepo.sut.RevParse(testRepo.firstTagName)
314336
require.Nil(t, err)
@@ -333,7 +355,7 @@ func TestSuccessRevParseShort(t *testing.T) {
333355

334356
branchRev, err := testRepo.sut.RevParseShort(testRepo.branchName)
335357
require.Nil(t, err)
336-
require.Equal(t, branchRev, testRepo.secondBranchCommit[:10])
358+
require.Equal(t, branchRev, testRepo.thirdBranchCommit[:10])
337359

338360
tagRev, err := testRepo.sut.RevParseShort(testRepo.firstTagName)
339361
require.Nil(t, err)
@@ -386,6 +408,15 @@ func TestSuccessLatestTagForBranch(t *testing.T) {
386408
require.Equal(t, util.SemverToTagString(version), testRepo.firstTagName)
387409
}
388410

411+
func TestSuccessLatestTagForBranchRelease(t *testing.T) {
412+
testRepo := newTestRepo(t)
413+
defer testRepo.cleanup(t)
414+
415+
version, err := testRepo.sut.LatestTagForBranch("release-1.17")
416+
require.Nil(t, err)
417+
require.Equal(t, util.SemverToTagString(version), testRepo.thirdTagName)
418+
}
419+
389420
func TestFailureLatestTagForBranchInvalidBranch(t *testing.T) {
390421
testRepo := newTestRepo(t)
391422
defer testRepo.cleanup(t)
@@ -399,15 +430,28 @@ func TestSuccessLatestPatchToPatch(t *testing.T) {
399430
testRepo := newTestRepo(t)
400431
defer testRepo.cleanup(t)
401432

402-
nextMinorTag := "v1.17.1"
433+
// This test case gets commits from v1.17.0 to v1.17.1
434+
result, err := testRepo.sut.LatestPatchToPatch(testRepo.branchName)
435+
require.Nil(t, err)
436+
require.Equal(t, result.StartSHA(), testRepo.firstCommit)
437+
require.Equal(t, result.StartRev(), testRepo.firstTagName)
438+
require.Equal(t, result.EndRev(), testRepo.thirdTagName)
439+
}
440+
441+
func TestSuccessLatestPatchToPatchNewTag(t *testing.T) {
442+
testRepo := newTestRepo(t)
443+
defer testRepo.cleanup(t)
444+
445+
// This test case gets commits from v1.17.1 to a new v1.17.2
446+
nextMinorTag := "v1.17.2"
403447
require.Nil(t, command.NewWithWorkDir(
404448
testRepo.sut.Dir(), "git", "tag", nextMinorTag,
405449
).RunSuccess())
406450

407451
result, err := testRepo.sut.LatestPatchToPatch(testRepo.branchName)
408452
require.Nil(t, err)
409-
require.Equal(t, result.StartSHA(), testRepo.firstCommit)
410-
require.Equal(t, result.StartRev(), testRepo.firstTagName)
453+
require.Equal(t, result.StartSHA(), testRepo.secondBranchCommit)
454+
require.Equal(t, result.StartRev(), testRepo.thirdTagName)
411455
require.Equal(t, result.EndRev(), nextMinorTag)
412456
}
413457

@@ -420,6 +464,18 @@ func TestFailureLatestPatchToPatchWrongBranch(t *testing.T) {
420464
require.Equal(t, git.DiscoverResult{}, result)
421465
}
422466

467+
func TestSuccessLatestPatchToLatest(t *testing.T) {
468+
testRepo := newTestRepo(t)
469+
defer testRepo.cleanup(t)
470+
471+
// This test case gets commits from v1.17.1 to head of release-1.17
472+
result, err := testRepo.sut.LatestPatchToLatest(testRepo.branchName)
473+
require.Nil(t, err)
474+
require.Equal(t, result.StartSHA(), testRepo.secondBranchCommit)
475+
require.Equal(t, result.StartRev(), testRepo.thirdTagName)
476+
require.Equal(t, result.EndSHA(), testRepo.thirdBranchCommit)
477+
}
478+
423479
func TestSuccessDry(t *testing.T) {
424480
testRepo := newTestRepo(t)
425481
defer testRepo.cleanup(t)
@@ -496,8 +552,8 @@ func TestTagsForBranchOnBranch(t *testing.T) {
496552
result, err := testRepo.sut.TagsForBranch(testRepo.branchName)
497553
require.Nil(t, err)
498554
require.Equal(t, result, []string{
499-
testRepo.firstTagName,
500555
testRepo.thirdTagName,
556+
testRepo.firstTagName,
501557
testRepo.secondTagName,
502558
})
503559
}

pkg/notes/options/options.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ const (
116116
RevisionDiscoveryModeNONE = "none"
117117
RevisionDiscoveryModeMergeBaseToLatest = "mergebase-to-latest"
118118
RevisionDiscoveryModePatchToPatch = "patch-to-patch"
119+
RevisionDiscoveryModePatchToLatest = "patch-to-latest"
119120
RevisionDiscoveryModeMinorToMinor = "minor-to-minor"
120121
)
121122

@@ -238,6 +239,8 @@ func (o *Options) resolveDiscoverMode() error {
238239
result, err = repo.LatestReleaseBranchMergeBaseToLatest()
239240
} else if o.DiscoverMode == RevisionDiscoveryModePatchToPatch {
240241
result, err = repo.LatestPatchToPatch(o.Branch)
242+
} else if o.DiscoverMode == RevisionDiscoveryModePatchToLatest {
243+
result, err = repo.LatestPatchToLatest(o.Branch)
241244
} else if o.DiscoverMode == RevisionDiscoveryModeMinorToMinor {
242245
result, err = repo.LatestNonPatchFinalToMinor()
243246
}

0 commit comments

Comments
 (0)