Add option to respect existing tags and adjust calculations using them.

This commit is contained in:
2021-12-11 10:43:12 +00:00
parent e32b4b9544
commit a0fa4ee4cd
6 changed files with 74 additions and 4 deletions
+12
View File
@@ -6,6 +6,8 @@ Project created overnight, to prove that management of semantic versioning is NO
- [Semantic version generator](#semantic-version-generator)
- [How does it work](#how-does-it-work)
- [Additional features](#additional-features)
- [Important changes](#important-changes)
- [Usage](#usage)
- [Authentication](#authentication)
- [As a binary](#as-a-binary)
@@ -23,6 +25,15 @@ Project created overnight, to prove that management of semantic versioning is NO
* Iterates through the list of commits looking for the keywords specified in config file for additional bumps of versions
* Returns the semantic version which can be included in the release
### Additional features
* With flag `-e` or config `force.existing: true` the existing tags in versioning will be respected, helping you to avoid the version conflicts.
* With config `force.commit: deadbeef` where `deadbeef` is the commit hash - calculations will start from the specified commit.
### Important changes
* From version `1.4.2+` as pointed out in [issue #12](https://github.com/lukaszraczylo/semver-generator/issues/12) commits from merge will not be included in the calculations and commits themselves will bump the version on first match ( starting checks from `patch` upwards ).
### Usage
#### Authentication
@@ -63,6 +74,7 @@ Available Commands:
Flags:
-c, --config string Path to config file (default "semver.yaml")
-d, --debug Enable debug mode
-e, --existing Respect existing tags
-h, --help help for semver-gen
-l, --local Use local repository
-r, --repository string Remote repository URL. (default "https://github.com/lukaszraczylo/simple-gql-client")
+57 -4
View File
@@ -22,6 +22,7 @@ import (
"net/url"
"os"
"sort"
"strconv"
"strings"
"time"
@@ -48,10 +49,11 @@ type Wording struct {
}
type Force struct {
Patch int
Minor int
Major int
Commit string
Patch int
Minor int
Major int
Commit string
Existing bool
}
type SemVer struct {
@@ -67,6 +69,7 @@ type Setup struct {
RepositoryLocalPath string
RepositoryHandler *git.Repository
Commits []CommitDetails
Tags []TagDetails
Semver SemVer
Wording Wording
Force Force
@@ -82,7 +85,16 @@ type CommitDetails struct {
Timestamp time.Time
}
type TagDetails struct {
Name string
Hash string
}
func checkMatches(content []string, targets []string) bool {
if fuzzy.MatchNormalizedFold(strings.Join(content, " "), "Merge branch") {
debugPrint(fmt.Sprintln("Merge detected, ignoring commits within:", content))
return false
}
var r []string
for _, tgt := range targets {
r = fuzzy.FindNormalizedFold(tgt, content)
@@ -100,8 +112,30 @@ func debugPrint(content string) {
}
}
func parseExistingSemver(tagName string) (semanticVersion SemVer) {
var tagNameParts []string
tagNameParts = strings.Split(tagName, ".")
semanticVersion.Major, _ = strconv.Atoi(tagNameParts[0])
semanticVersion.Minor, _ = strconv.Atoi(tagNameParts[1])
semanticVersion.Patch, _ = strconv.Atoi(tagNameParts[2])
tagReleaseParts := strings.Split(tagNameParts[2], "-rc.")
if len(tagReleaseParts) > 1 {
semanticVersion.Release, _ = strconv.Atoi(tagNameParts[3])
}
return
}
func (s *Setup) CalculateSemver() SemVer {
for _, commit := range s.Commits {
if params.varExisting || s.Force.Existing {
for _, tagHash := range s.Tags {
if commit.Hash == tagHash.Hash {
debugPrint(fmt.Sprintln("Found existing tag:", tagHash.Name, "related to", commit.Message))
s.Semver = parseExistingSemver(tagHash.Name)
}
}
}
if !params.varStrict {
s.Semver.Patch++
debugPrint(fmt.Sprintln("Incrementing patch (DEFAULT) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver()))
@@ -144,6 +178,21 @@ func (s *Setup) CalculateSemver() SemVer {
return s.Semver
}
func (s *Setup) ListExistingTags() {
debugPrint(fmt.Sprintln("Listing existing tags"))
refs, err := s.RepositoryHandler.Tags()
if err != nil {
panic(err)
}
if err := refs.ForEach(func(ref *plumbing.Reference) error {
s.Tags = append(s.Tags, TagDetails{Name: ref.Name().Short(), Hash: ref.Hash().String()})
debugPrint(fmt.Sprintln("Found tag:", ref.Name().Short(), ref.Hash().String()))
return nil
}); err != nil {
panic(err)
}
}
func (s *Setup) ListCommits() ([]CommitDetails, error) {
var ref *plumbing.Reference
var err error
@@ -195,6 +244,7 @@ func (s *Setup) Prepare() error {
Username: os.Getenv("GITHUB_USERNAME"),
Password: os.Getenv("GITHUB_TOKEN"),
},
Tags: git.AllTags,
})
if err != nil {
fmt.Println("Unable to reach repository", s.RepositoryName, "Error:", err.Error())
@@ -276,6 +326,9 @@ func main() {
os.Exit(1)
}
repo.ListCommits()
if params.varExisting {
repo.ListExistingTags()
}
repo.ForcedVersioning()
repo.CalculateSemver()
fmt.Println("SEMVER", repo.getSemver())
+1
View File
@@ -487,6 +487,7 @@ func (suite *Tests) Test_main() {
varUpdate bool
varStrict bool
varGenerateInTest bool
varExisting bool
}
tests := []struct {
name string
+2
View File
@@ -60,6 +60,7 @@ type myParams struct {
varUpdate bool
varStrict bool
varGenerateInTest bool
varExisting bool
}
var params myParams
@@ -74,4 +75,5 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&params.varDebug, "debug", "d", false, "Enable debug mode")
rootCmd.PersistentFlags().BoolVarP(&params.varUpdate, "update", "u", false, "Update binary with latest")
rootCmd.PersistentFlags().BoolVarP(&params.varStrict, "strict", "s", false, "Strict matching")
rootCmd.PersistentFlags().BoolVarP(&params.varExisting, "existing", "e", false, "Respect existing tags")
}
+1
View File
@@ -1,6 +1,7 @@
version: 1
force:
major: 1
existing: true
wording:
patch:
- update
+1
View File
@@ -1,6 +1,7 @@
version: 1
force:
major: 1
existing: true
wording:
patch:
- update