Skip to content

Commit

Permalink
strict mode to check tag history for validity (#23)
Browse files Browse the repository at this point in the history
* --strict mode to check tag history for validity
  • Loading branch information
sstarcher authored Apr 6, 2020
1 parent e1ed526 commit b7edf12
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
path = tests/notags
url = https://github.com/sstarcher/helm-release-notags.git
branch = master
[submodule "tests/tags_bad"]
path = tests/tags_bad
url = https://github.com/sstarcher/helm-release-tags_bad.git
22 changes: 15 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import (
"github.com/sstarcher/helm-release/version"
)

var cfgFile string
var tag string
var tagPath string
var printComputedVersion bool
var bump string
var source string
var (
cfgFile string
tag string
tagPath string
printComputedVersion bool
bump string
source string
strict bool
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -55,7 +58,11 @@ var rootCmd = &cobra.Command{

nextType := version.NewNextType(bump)
version, err := getter.NextVersion(nextType)
if err != nil {
if version == nil {
return err
}

if err != nil && strict {
return err
}

Expand Down Expand Up @@ -108,6 +115,7 @@ func init() {
rootCmd.Flags().BoolVar(&printComputedVersion, "print-computed-version", false, "Print the computed version string to stdout")
rootCmd.Flags().StringVar(&bump, "bump", "", "Specifies to bump major, minor, or patch when using print-computed-version")
rootCmd.Flags().StringVar(&source, "source", "git", "Specifies the source of the version information options (git, helm)")
rootCmd.Flags().BoolVar(&strict, "strict", false, "When enabled it will look through all tags for semver tags and fail if tags exist outside of master")
}

// initConfig reads in config file and ENV variables if set.
Expand Down
52 changes: 50 additions & 2 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,58 @@ func (g *Git) NextVersion(nextType *version.NextType) (*semver.Version, error) {
return nil, err
}

tags, err := g.tags()
if err != nil {
return nil, err
}

var nextVersion *semver.Version
if nextType == nil { // Determine from git history
return g.versionFromHistory(ver)
nextVersion, err = g.versionFromHistory(ver)
if err != nil {
return nil, err
}
} else {
nextVersion, err = version.NextVersion(ver, nextType)
if err != nil {
return nil, err
}
}

wrongTags := false
for _, item := range tags {
if nextVersion.Compare(item) != 1 {
wrongTags = true
log.Warnf("next version is behind one of the semver tags %v", item)
}
}

if wrongTags {
err = fmt.Errorf("tags in history are out of sync with next version %d.%d.%d", nextVersion.Major(), nextVersion.Minor(), nextVersion.Patch())
}

return nextVersion, err
}

func (g *Git) tags() ([]*semver.Version, error) {
cmd := exec.Command("git", "tag")
cmd.Dir = g.directory
out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
return version.NextVersion(ver, nextType)

s := strings.TrimSpace(string(out))
tagString := strings.Split(s, "\n")
tags := []*semver.Version{}
for _, item := range tagString {
ver, err := semver.NewVersion(item)
if err == nil {
tags = append(tags, ver)
}
}

return tags, nil
}

func validate(dir string) error {
Expand Down
12 changes: 12 additions & 0 deletions git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

var dir = "../tests/tags"
var noTagsDir = "../tests/notags"
var badTags = "../tests/tags_bad"

func TestValidGitRepo(t *testing.T) {
assert := assert.New(t)
Expand Down Expand Up @@ -103,3 +104,14 @@ func TestNoTags(t *testing.T) {
assert.NotNil(err)
assert.Empty(tag)
}

func TestBadTags(t *testing.T) {
assert := assert.New(t)

git := Git{
directory: badTags,
}

_, err := git.NextVersion(nil)
assert.NotNil(err)
}
2 changes: 1 addition & 1 deletion helm/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestVersions(t *testing.T) {
assert.Nil(err)

actual, err := git.NextVersion(nil)
assert.Nil(err)
assert.NotNil(actual)
if actual != nil {
assert.Equal(tt.expected, actual.String())
} else {
Expand Down
1 change: 1 addition & 0 deletions tests/tags_bad
Submodule tags_bad added at 672449

0 comments on commit b7edf12

Please sign in to comment.