mirror of
https://github.com/lukaszraczylo/semver-generator.git
synced 2026-07-06 03:54:54 +00:00
Add option to respect existing tags and adjust calculations using them.
This commit is contained in:
@@ -6,6 +6,8 @@ Project created overnight, to prove that management of semantic versioning is NO
|
|||||||
|
|
||||||
- [Semantic version generator](#semantic-version-generator)
|
- [Semantic version generator](#semantic-version-generator)
|
||||||
- [How does it work](#how-does-it-work)
|
- [How does it work](#how-does-it-work)
|
||||||
|
- [Additional features](#additional-features)
|
||||||
|
- [Important changes](#important-changes)
|
||||||
- [Usage](#usage)
|
- [Usage](#usage)
|
||||||
- [Authentication](#authentication)
|
- [Authentication](#authentication)
|
||||||
- [As a binary](#as-a-binary)
|
- [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
|
* 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
|
* 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
|
### Usage
|
||||||
|
|
||||||
#### Authentication
|
#### Authentication
|
||||||
@@ -63,6 +74,7 @@ Available Commands:
|
|||||||
Flags:
|
Flags:
|
||||||
-c, --config string Path to config file (default "semver.yaml")
|
-c, --config string Path to config file (default "semver.yaml")
|
||||||
-d, --debug Enable debug mode
|
-d, --debug Enable debug mode
|
||||||
|
-e, --existing Respect existing tags
|
||||||
-h, --help help for semver-gen
|
-h, --help help for semver-gen
|
||||||
-l, --local Use local repository
|
-l, --local Use local repository
|
||||||
-r, --repository string Remote repository URL. (default "https://github.com/lukaszraczylo/simple-gql-client")
|
-r, --repository string Remote repository URL. (default "https://github.com/lukaszraczylo/simple-gql-client")
|
||||||
|
|||||||
+57
-4
@@ -22,6 +22,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -48,10 +49,11 @@ type Wording struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Force struct {
|
type Force struct {
|
||||||
Patch int
|
Patch int
|
||||||
Minor int
|
Minor int
|
||||||
Major int
|
Major int
|
||||||
Commit string
|
Commit string
|
||||||
|
Existing bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type SemVer struct {
|
type SemVer struct {
|
||||||
@@ -67,6 +69,7 @@ type Setup struct {
|
|||||||
RepositoryLocalPath string
|
RepositoryLocalPath string
|
||||||
RepositoryHandler *git.Repository
|
RepositoryHandler *git.Repository
|
||||||
Commits []CommitDetails
|
Commits []CommitDetails
|
||||||
|
Tags []TagDetails
|
||||||
Semver SemVer
|
Semver SemVer
|
||||||
Wording Wording
|
Wording Wording
|
||||||
Force Force
|
Force Force
|
||||||
@@ -82,7 +85,16 @@ type CommitDetails struct {
|
|||||||
Timestamp time.Time
|
Timestamp time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TagDetails struct {
|
||||||
|
Name string
|
||||||
|
Hash string
|
||||||
|
}
|
||||||
|
|
||||||
func checkMatches(content []string, targets []string) bool {
|
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
|
var r []string
|
||||||
for _, tgt := range targets {
|
for _, tgt := range targets {
|
||||||
r = fuzzy.FindNormalizedFold(tgt, content)
|
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 {
|
func (s *Setup) CalculateSemver() SemVer {
|
||||||
for _, commit := range s.Commits {
|
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 {
|
if !params.varStrict {
|
||||||
s.Semver.Patch++
|
s.Semver.Patch++
|
||||||
debugPrint(fmt.Sprintln("Incrementing patch (DEFAULT) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver()))
|
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
|
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) {
|
func (s *Setup) ListCommits() ([]CommitDetails, error) {
|
||||||
var ref *plumbing.Reference
|
var ref *plumbing.Reference
|
||||||
var err error
|
var err error
|
||||||
@@ -195,6 +244,7 @@ func (s *Setup) Prepare() error {
|
|||||||
Username: os.Getenv("GITHUB_USERNAME"),
|
Username: os.Getenv("GITHUB_USERNAME"),
|
||||||
Password: os.Getenv("GITHUB_TOKEN"),
|
Password: os.Getenv("GITHUB_TOKEN"),
|
||||||
},
|
},
|
||||||
|
Tags: git.AllTags,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Unable to reach repository", s.RepositoryName, "Error:", err.Error())
|
fmt.Println("Unable to reach repository", s.RepositoryName, "Error:", err.Error())
|
||||||
@@ -276,6 +326,9 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
repo.ListCommits()
|
repo.ListCommits()
|
||||||
|
if params.varExisting {
|
||||||
|
repo.ListExistingTags()
|
||||||
|
}
|
||||||
repo.ForcedVersioning()
|
repo.ForcedVersioning()
|
||||||
repo.CalculateSemver()
|
repo.CalculateSemver()
|
||||||
fmt.Println("SEMVER", repo.getSemver())
|
fmt.Println("SEMVER", repo.getSemver())
|
||||||
|
|||||||
@@ -487,6 +487,7 @@ func (suite *Tests) Test_main() {
|
|||||||
varUpdate bool
|
varUpdate bool
|
||||||
varStrict bool
|
varStrict bool
|
||||||
varGenerateInTest bool
|
varGenerateInTest bool
|
||||||
|
varExisting bool
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ type myParams struct {
|
|||||||
varUpdate bool
|
varUpdate bool
|
||||||
varStrict bool
|
varStrict bool
|
||||||
varGenerateInTest bool
|
varGenerateInTest bool
|
||||||
|
varExisting bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var params myParams
|
var params myParams
|
||||||
@@ -74,4 +75,5 @@ func init() {
|
|||||||
rootCmd.PersistentFlags().BoolVarP(¶ms.varDebug, "debug", "d", false, "Enable debug mode")
|
rootCmd.PersistentFlags().BoolVarP(¶ms.varDebug, "debug", "d", false, "Enable debug mode")
|
||||||
rootCmd.PersistentFlags().BoolVarP(¶ms.varUpdate, "update", "u", false, "Update binary with latest")
|
rootCmd.PersistentFlags().BoolVarP(¶ms.varUpdate, "update", "u", false, "Update binary with latest")
|
||||||
rootCmd.PersistentFlags().BoolVarP(¶ms.varStrict, "strict", "s", false, "Strict matching")
|
rootCmd.PersistentFlags().BoolVarP(¶ms.varStrict, "strict", "s", false, "Strict matching")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(¶ms.varExisting, "existing", "e", false, "Respect existing tags")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
version: 1
|
version: 1
|
||||||
force:
|
force:
|
||||||
major: 1
|
major: 1
|
||||||
|
existing: true
|
||||||
wording:
|
wording:
|
||||||
patch:
|
patch:
|
||||||
- update
|
- update
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
version: 1
|
version: 1
|
||||||
force:
|
force:
|
||||||
major: 1
|
major: 1
|
||||||
|
existing: true
|
||||||
wording:
|
wording:
|
||||||
patch:
|
patch:
|
||||||
- update
|
- update
|
||||||
|
|||||||
Reference in New Issue
Block a user