From c621cdf9e5ee72ea5c0c41c6d6216eb20fd622ae Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Mon, 12 Jul 2021 16:53:19 +0100 Subject: [PATCH] Add the release candidate to versioning. --- README.md | 36 +++++++++++++++++++++--------------- cmd/main.go | 35 ++++++++++++++++++++++++++--------- cmd/main_test.go | 26 ++++++++++++++++++++++++++ config.yaml | 5 +++-- 4 files changed, 76 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index dbcbe19..42e33aa 100644 --- a/README.md +++ b/README.md @@ -114,23 +114,27 @@ Linux/arm64, Linux/amd64 #### Calculations example [standard] -* 0.0.1 - PATCH - starting commit -* 0.0.2 - PATCH - another commit -* 0.0.4 - PATCH - another commit with word 'Update' => DOUBLE increment PATCH -* 0.1.0 - MINOR - after commit with word 'Change' => increment MINOR, reset PATCH -* 0.1.1 - PATCH - additional commit -* 1.0.1 - MAJOR - commit with word 'BREAKING' = > INCREMENT MAJOR, reset MINOR -* 1.0.2 - PATCH - another commit +```bash +- 0.0.1 - PATCH - starting commit +- 0.0.2 - PATCH - another commit +- 0.0.4 - PATCH - another commit with word 'Update' => DOUBLE increment PATCH +- 0.1.0 - MINOR - after commit with word 'Change' => increment MINOR, reset PATCH +- 0.1.1 - PATCH - additional commit +- 1.0.1 - MAJOR - commit with word 'BREAKING' = > INCREMENT MAJOR, reset MINOR +- 1.0.2 - PATCH - another commit +``` #### Calculations example [strict matching] -* 0.0.1 - PATCH - starting commit -* 0.0.1 - PATCH - another commit -* 0.0.1 - PATCH - another commit with word 'Update' => SINGLE increment PATCH -* 0.1.0 - MINOR - after commit with word 'Change' => increment MINOR, reset PATCH -* 0.1.0 - PATCH - additional commit -* 1.0.0 - MAJOR - commit with word 'BREAKING' = > INCREMENT MAJOR, reset MINOR -* 1.0.0 - PATCH - another commit +```bash +- 0.0.1 - PATCH - starting commit +- 0.0.1 - PATCH - another commit +- 0.0.1 - PATCH - another commit with word 'Update' => SINGLE increment PATCH +- 0.1.0 - MINOR - after commit with word 'Change' => increment MINOR, reset PATCH +- 0.1.0 - PATCH - additional commit +- 1.0.0 - MAJOR - commit with word 'BREAKING' = > INCREMENT MAJOR, reset MINOR +- 1.0.0 - PATCH - another commit +``` #### Example configuration @@ -146,12 +150,14 @@ wording: - update - initial minor: - - add - change - improve major: - breaking - the # For testing purposes + release: + - release-candidate + - add-rc ``` * `version`: is not respected at the moment, introduced for potential backwards compatibility in future diff --git a/cmd/main.go b/cmd/main.go index c923e55..4f42a80 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -41,9 +41,10 @@ var ( ) type Wording struct { - Patch []string - Minor []string - Major []string + Patch []string + Minor []string + Major []string + Release []string } type Force struct { @@ -54,9 +55,11 @@ type Force struct { } type SemVer struct { - Patch int - Minor int - Major int + Patch int + Minor int + Major int + Release int + EnableReleaseCandidate bool } type Setup struct { @@ -106,19 +109,30 @@ func (s *Setup) CalculateSemver() SemVer { matchPatch := checkMatches(commitSlice, s.Wording.Patch) matchMinor := checkMatches(commitSlice, s.Wording.Minor) matchMajor := checkMatches(commitSlice, s.Wording.Major) + matchReleaseCandidate := checkMatches(commitSlice, s.Wording.Release) if matchPatch { s.Semver.Patch++ debugPrint(fmt.Sprintln("Incrementing patch (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())) } + if matchReleaseCandidate { + s.Semver.Release++ + s.Semver.Patch = 1 + s.Semver.EnableReleaseCandidate = true + debugPrint(fmt.Sprintln("Incrementing release candidate (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())) + } if matchMinor { s.Semver.Minor++ s.Semver.Patch = 1 + s.Semver.EnableReleaseCandidate = false + s.Semver.Release = 0 debugPrint(fmt.Sprintln("Incrementing minor (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())) } if matchMajor { s.Semver.Major++ s.Semver.Minor = 0 s.Semver.Patch = 1 + s.Semver.EnableReleaseCandidate = false + s.Semver.Release = 0 debugPrint(fmt.Sprintln("Incrementing major (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())) } } @@ -156,7 +170,6 @@ func (s *Setup) ListCommits() ([]CommitDetails, error) { } debugPrint(fmt.Sprintln("\n---COMMITS AFTER CUT---\n", s.Commits)) - return s.Commits, err } @@ -219,8 +232,12 @@ func (s *Setup) ReadConfig(file string) error { return err } -func (s *Setup) getSemver() string { - return fmt.Sprintf("%d.%d.%d", s.Semver.Major, s.Semver.Minor, s.Semver.Patch) +func (s *Setup) getSemver() (semverReturned string) { + semverReturned = fmt.Sprintf("%d.%d.%d", s.Semver.Major, s.Semver.Minor, s.Semver.Patch) + if s.Semver.EnableReleaseCandidate { + semverReturned = fmt.Sprintf("%s-rc.%d", semverReturned, s.Semver.Release) + } + return semverReturned } func main() { diff --git a/cmd/main_test.go b/cmd/main_test.go index 2f346a2..a386042 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -66,6 +66,32 @@ func (suite *Tests) TestSetup_getSemver() { }, want: "1.3.7", }, + { + name: "Return 1.3.7-rc.2", + fields: fields{ + Semver: SemVer{ + Major: 1, + Minor: 3, + Patch: 7, + Release: 2, + EnableReleaseCandidate: true, + }, + }, + want: "1.3.7-rc.2", + }, + { + name: "Return 1.3.9", + fields: fields{ + Semver: SemVer{ + Major: 1, + Minor: 3, + Patch: 9, + Release: 2, + EnableReleaseCandidate: false, + }, + }, + want: "1.3.9", + }, } for _, tt := range tests { suite.T().Run(tt.name, func(t *testing.T) { diff --git a/config.yaml b/config.yaml index db2a72b..5dfe9a5 100644 --- a/config.yaml +++ b/config.yaml @@ -7,8 +7,9 @@ wording: - initial - fix minor: - - add - change - improve major: - - breaking \ No newline at end of file + - breaking + release: + - release-candidate \ No newline at end of file