Skip to content

Commit

Permalink
Add a patch-to-latest discover mode, that generates the release notes…
Browse files Browse the repository at this point in the history
… from the latest patch release to the top of the release branch
  • Loading branch information
msau42 committed Apr 23, 2020
1 parent 14a4668 commit 1765c99
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/release-notes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ level=debug timestamp=2019-07-30T04:02:44.3716249Z caller=notes.go:497 msg="Excl
| repo-path | REPO_PATH | /tmp/k8s-repo | No | Path to a local Kubernetes repository, used only for tag discovery |
| start-rev | START_REV | | No | The git revision to start at. Can be used as alternative to start-sha |
| env-rev | END_REV | | No | The git revision to end at. Can be used as alternative to end-sha |
| discover | DISCOVER | none | No | The revision discovery mode for automatic revision retrieval (options: none, mergebase-to-latest, patch-to-patch, minor-to-minor) |
| 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) |
| release-bucket | RELEASE_BUCKET | kubernetes-release | No | Specify gs bucket to point to in generated notes (default "kubernetes-release") |
| release-tars | RELEASE_TARS | | No | Directory of tars to sha512 sum for display |
| **OUTPUT OPTIONS** |
Expand Down
36 changes: 36 additions & 0 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,42 @@ func (r *Repo) LatestPatchToPatch(branch string) (DiscoverResult, error) {
}, nil
}

// LatestPatchToLatest tries to discover the start (latest v1.x.x]) and
// end (release-1.x or master) revision inside the repository for the specified release
// branch.
func (r *Repo) LatestPatchToLatest(branch string) (DiscoverResult, error) {
latestTag, err := r.LatestTagForBranch(branch)
if err != nil {
return DiscoverResult{}, err
}

if len(latestTag.Pre) > 0 && latestTag.Patch > 0 {
latestTag.Patch--
latestTag.Pre = nil
}

logrus.Debugf("parsing latest tag %s%v", util.TagPrefix, latestTag)
latestVersionTag := util.SemverToTagString(latestTag)
start, err := r.RevParse(latestVersionTag)
if err != nil {
return DiscoverResult{}, errors.Wrapf(err, "parsing version %v", latestTag)
}

// If a release branch exists for the latest version, we use it. Otherwise we
// fallback to the master branch.
end, branch, err := r.releaseBranchOrMasterRev(latestTag.Major, latestTag.Minor)
if err != nil {
return DiscoverResult{}, errors.Wrapf(err, "getting release branch for %v", latestTag)
}

return DiscoverResult{
startSHA: start,
startRev: latestVersionTag,
endSHA: end,
endRev: branch,
}, nil
}

// LatestTagForBranch returns the latest available semver tag for a given branch
func (r *Repo) LatestTagForBranch(branch string) (tag semver.Version, err error) {
tags, err := r.TagsForBranch(branch)
Expand Down
72 changes: 64 additions & 8 deletions pkg/git/git_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type testRepo struct {
firstCommit string
firstBranchCommit string
secondBranchCommit string
thirdBranchCommit string
branchName string
firstTagCommit string
firstTagName string
Expand All @@ -52,6 +53,11 @@ type testRepo struct {

// newTestRepo creates a test repo with the following structure:
//
// * commit `thirdBranchCommit` (HEAD -> `branchName`, origin/`branchName`)
// | Author: John Doe <john@doe.org>
// |
// | Fourth commit
// |
// * commit `secondBranchCommit` (tag: `thirdTagName`, HEAD -> `branchName`, origin/`branchName`)
// | Author: John Doe <john@doe.org>
// |
Expand Down Expand Up @@ -159,7 +165,7 @@ func newTestRepo(t *testing.T) *testRepo {
})
require.Nil(t, err)

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

const thirdBranchTestFileName = "branch-test-file-3"
require.Nil(t, ioutil.WriteFile(
filepath.Join(cloneTempDir, thirdBranchTestFileName),
[]byte("test-content"),
os.FileMode(0644),
))
_, err = worktree.Add(thirdBranchTestFileName)
require.Nil(t, err)

thirdBranchCommit, err := worktree.Commit("Fourth commit", &gogit.CommitOptions{
Author: author,
All: true,
})
require.Nil(t, err)

// Push the test content into the bare repo
_, err = cloneRepo.CreateRemote(&config.RemoteConfig{
Name: git.DefaultRemote,
Expand All @@ -194,6 +215,7 @@ func newTestRepo(t *testing.T) *testRepo {
firstCommit: firstCommit.String(),
firstBranchCommit: firstBranchCommit.String(),
secondBranchCommit: secondBranchCommit.String(),
thirdBranchCommit: thirdBranchCommit.String(),
branchName: branchName,
firstTagName: firstTagName,
firstTagCommit: firstTagRef.Hash().String(),
Expand Down Expand Up @@ -270,7 +292,7 @@ func TestSuccessHead(t *testing.T) {

head, err := testRepo.sut.Head()
require.Nil(t, err)
require.Equal(t, head, testRepo.secondBranchCommit)
require.Equal(t, head, testRepo.thirdBranchCommit)
}

func TestSuccessMerge(t *testing.T) {
Expand Down Expand Up @@ -308,7 +330,7 @@ func TestSuccessRevParse(t *testing.T) {

branchRev, err := testRepo.sut.RevParse(testRepo.branchName)
require.Nil(t, err)
require.Equal(t, branchRev, testRepo.secondBranchCommit)
require.Equal(t, branchRev, testRepo.thirdBranchCommit)

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

branchRev, err := testRepo.sut.RevParseShort(testRepo.branchName)
require.Nil(t, err)
require.Equal(t, branchRev, testRepo.secondBranchCommit[:10])
require.Equal(t, branchRev, testRepo.thirdBranchCommit[:10])

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

func TestSuccessLatestTagForBranchRelease(t *testing.T) {
testRepo := newTestRepo(t)
defer testRepo.cleanup(t)

version, err := testRepo.sut.LatestTagForBranch("release-1.17")
require.Nil(t, err)
require.Equal(t, util.SemverToTagString(version), testRepo.thirdTagName)
}

func TestFailureLatestTagForBranchInvalidBranch(t *testing.T) {
testRepo := newTestRepo(t)
defer testRepo.cleanup(t)
Expand All @@ -399,15 +430,28 @@ func TestSuccessLatestPatchToPatch(t *testing.T) {
testRepo := newTestRepo(t)
defer testRepo.cleanup(t)

nextMinorTag := "v1.17.1"
// This test case gets commits from v1.17.0 to v1.17.1
result, err := testRepo.sut.LatestPatchToPatch(testRepo.branchName)
require.Nil(t, err)
require.Equal(t, result.StartSHA(), testRepo.firstCommit)
require.Equal(t, result.StartRev(), testRepo.firstTagName)
require.Equal(t, result.EndRev(), testRepo.thirdTagName)
}

func TestSuccessLatestPatchToPatchNewTag(t *testing.T) {
testRepo := newTestRepo(t)
defer testRepo.cleanup(t)

// This test case gets commits from v1.17.1 to a new v1.17.2
nextMinorTag := "v1.17.2"
require.Nil(t, command.NewWithWorkDir(
testRepo.sut.Dir(), "git", "tag", nextMinorTag,
).RunSuccess())

result, err := testRepo.sut.LatestPatchToPatch(testRepo.branchName)
require.Nil(t, err)
require.Equal(t, result.StartSHA(), testRepo.firstCommit)
require.Equal(t, result.StartRev(), testRepo.firstTagName)
require.Equal(t, result.StartSHA(), testRepo.secondBranchCommit)
require.Equal(t, result.StartRev(), testRepo.thirdTagName)
require.Equal(t, result.EndRev(), nextMinorTag)
}

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

func TestSuccessLatestPatchToLatest(t *testing.T) {
testRepo := newTestRepo(t)
defer testRepo.cleanup(t)

// This test case gets commits from v1.17.1 to head of release-1.17
result, err := testRepo.sut.LatestPatchToLatest(testRepo.branchName)
require.Nil(t, err)
require.Equal(t, result.StartSHA(), testRepo.secondBranchCommit)
require.Equal(t, result.StartRev(), testRepo.thirdTagName)
require.Equal(t, result.EndSHA(), testRepo.thirdBranchCommit)
}

func TestSuccessDry(t *testing.T) {
testRepo := newTestRepo(t)
defer testRepo.cleanup(t)
Expand Down Expand Up @@ -496,8 +552,8 @@ func TestTagsForBranchOnBranch(t *testing.T) {
result, err := testRepo.sut.TagsForBranch(testRepo.branchName)
require.Nil(t, err)
require.Equal(t, result, []string{
testRepo.firstTagName,
testRepo.thirdTagName,
testRepo.firstTagName,
testRepo.secondTagName,
})
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/notes/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const (
RevisionDiscoveryModeNONE = "none"
RevisionDiscoveryModeMergeBaseToLatest = "mergebase-to-latest"
RevisionDiscoveryModePatchToPatch = "patch-to-patch"
RevisionDiscoveryModePatchToLatest = "patch-to-latest"
RevisionDiscoveryModeMinorToMinor = "minor-to-minor"
)

Expand Down Expand Up @@ -238,6 +239,8 @@ func (o *Options) resolveDiscoverMode() error {
result, err = repo.LatestReleaseBranchMergeBaseToLatest()
} else if o.DiscoverMode == RevisionDiscoveryModePatchToPatch {
result, err = repo.LatestPatchToPatch(o.Branch)
} else if o.DiscoverMode == RevisionDiscoveryModePatchToLatest {
result, err = repo.LatestPatchToLatest(o.Branch)
} else if o.DiscoverMode == RevisionDiscoveryModeMinorToMinor {
result, err = repo.LatestNonPatchFinalToMinor()
}
Expand Down

0 comments on commit 1765c99

Please sign in to comment.