Skip to content

Commit

Permalink
Merge pull request #975 from saschagrunert/patch-to-patch
Browse files Browse the repository at this point in the history
Add support for patch-to-patch discovery mode
  • Loading branch information
k8s-ci-robot authored Dec 17, 2019
2 parents 073b226 + 2ea7898 commit 76b525a
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 37 deletions.
20 changes: 16 additions & 4 deletions cmd/release-notes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ var opts = &options{}
const (
revisionDiscoveryModeNONE = "none"
revisionDiscoveryModeMinorToLatest = "minor-to-latest"
revisionDiscoveryModePatchToPatch = "patch-to-patch"
)

var cmd = &cobra.Command{
Expand Down Expand Up @@ -196,6 +197,7 @@ func init() {
strings.Join([]string{
revisionDiscoveryModeNONE,
revisionDiscoveryModeMinorToLatest,
revisionDiscoveryModePatchToPatch,
}, ", "),
),
)
Expand Down Expand Up @@ -347,7 +349,7 @@ func validateOptions(*cobra.Command, []string) error {
}

// Check if we want to automatically discover the revisions
if opts.discoverMode == revisionDiscoveryModeMinorToLatest {
if opts.discoverMode != revisionDiscoveryModeNONE {
repo, err := git.CloneOrOpenGitHubRepo(
opts.repoPath,
opts.githubOrg,
Expand All @@ -357,10 +359,20 @@ func validateOptions(*cobra.Command, []string) error {
if err != nil {
return err
}
start, end, err := repo.LatestNonPatchFinalToLatest()
if err != nil {
return err

var start, end string
if opts.discoverMode == revisionDiscoveryModeMinorToLatest {
start, end, err = repo.LatestNonPatchFinalToLatest()
if err != nil {
return err
}
} else if opts.discoverMode == revisionDiscoveryModePatchToPatch {
start, end, err = repo.LatestPatchToPatch(opts.branch)
if err != nil {
return err
}
}

opts.startSHA = start
opts.endSHA = end
logrus.Infof("discovered start SHA %s", start)
Expand Down
84 changes: 81 additions & 3 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
branchRE = `master|release-([0-9]{1,})\.([0-9]{1,})(\.([0-9]{1,}))*$`
defaultGithubAuthRoot = "[email protected]:"
gitExecutable = "git"
tagPrefix = "v"
)

// Wrapper type for a Kubernetes repository instance
Expand Down Expand Up @@ -212,7 +213,7 @@ func (r *Repo) LatestNonPatchFinalToLatest() (start, end string, err error) {
if err != nil {
return "", "", err
}
versionTag := "v" + version.String()
versionTag := addTagPrefix(version.String())
logrus.Infof("latest non patch version %s", versionTag)
start, err = r.RevParse(versionTag)
if err != nil {
Expand All @@ -239,7 +240,7 @@ func (r *Repo) latestNonPatchFinalVersion() (semver.Version, error) {

found := false
_ = tags.ForEach(func(t *plumbing.Reference) error { // nolint: errcheck
tag := strings.TrimPrefix(t.Name().Short(), "v")
tag := trimTagPrefix(t.Name().Short())
ver, err := semver.Make(tag)

if err == nil {
Expand Down Expand Up @@ -386,7 +387,7 @@ func (r *Repo) DescribeTag(rev string) (string, error) {
return "", err
}
if !status.Success() {
return "", errors.New("git describe command failed")
return "", errors.Errorf("git describe command failed: %s", status.Error())
}

return strings.TrimSpace(status.Output()), nil
Expand Down Expand Up @@ -420,3 +421,80 @@ func (r *Repo) Head() (string, error) {
}
return ref.Hash().String(), nil
}

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

if len(latestTag.Pre) > 0 {
return "", "", errors.Errorf(
"found pre-release %v as latest tag on branch %s",
latestTag, branch,
)
}

if latestTag.Patch == 0 {
return "", "", errors.Errorf(
"found non-patch version %v as latest tag on branch %s",
latestTag, branch,
)
}

prevTag := semver.Version{
Major: latestTag.Major,
Minor: latestTag.Minor,
Patch: latestTag.Patch - 1,
}

logrus.Infof("Parsing latest tag %s%v", tagPrefix, latestTag)
end, err = r.RevParse(addTagPrefix(latestTag.String()))
if err != nil {
return "", "", errors.Wrapf(err, "parsing version %v", latestTag)
}

logrus.Infof("Parsing previous tag %s%v", tagPrefix, prevTag)
start, err = r.RevParse(addTagPrefix(prevTag.String()))
if err != nil {
return "", "", errors.Wrapf(err, "parsing previous version %v", prevTag)
}

return start, end, nil
}

// latestTagForBranch returns the latest available semver tag for a given branch
func (r *Repo) latestTagForBranch(branch string) (*semver.Version, error) {
status, err := command.NewWithWorkDir(
r.Dir(), gitExecutable, "rev-list", branch, "--tags", "--max-count=1",
).RunSilent()
if err != nil {
return nil, err
}
if !status.Success() {
return nil, errors.Errorf("git rev-list command failed: %s", status.Error())
}

rev, err := r.DescribeTag(strings.TrimSpace(status.Output()))
if err != nil {
return nil, err
}

tag, err := semver.Make(trimTagPrefix(rev))
if err != nil {
return nil, err
}

return &tag, nil
}

func addTagPrefix(tag string) string {
return tagPrefix + tag
}

func trimTagPrefix(tag string) string {
return strings.TrimPrefix(tag, tagPrefix)
}
Loading

0 comments on commit 76b525a

Please sign in to comment.