Skip to content

Commit 8472a27

Browse files
authored
Merge pull request #16 from itk-dev/feature/release-type
Added version check to determine release type
2 parents 3d23fd9 + cbe1232 commit 8472a27

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
* [PR-16](https://github.com/itk-dev/gh-itkdev/pull/16)
11+
Added version check to determine release type
1012
* [PR-15](https://github.com/itk-dev/gh-itkdev/pull/15)
1113
Added “Under udvikling” as allowed “Unreleased” header
1214

changelog/release.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,36 @@ import (
1111
"slices"
1212
"strings"
1313
"time"
14+
15+
"golang.org/x/mod/semver"
1416
)
1517

18+
func getBranchName(release string) (string, error) {
19+
version := release
20+
if !strings.HasPrefix(version, "v") {
21+
version = "v" + version
22+
}
23+
if !semver.IsValid(version) {
24+
return "", fmt.Errorf("invalid version: %s", release)
25+
}
26+
27+
branchType := "release"
28+
// Determine if the release is a "release" or a "hotfix".
29+
//
30+
// A "hotfix" is a version that's not a prerelease and has a patch version different from 0 (cf. https://semver.org/).
31+
if semver.Prerelease(version) == "" && semver.MajorMinor(version)+".0" != version {
32+
branchType = "hotfix"
33+
}
34+
35+
return branchType + "/" + release, nil
36+
}
37+
1638
func createReleaseBranch(release string, base string) (string, error) {
17-
branch := "release/" + release
39+
branch, err := getBranchName(release)
40+
if err != nil {
41+
return "", err
42+
}
43+
1844
cmd := exec.Command("git", "checkout", "-b", branch, base)
1945
output, err := cmd.CombinedOutput()
2046
if err != nil {

changelog/release_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,63 @@ import (
88
"github.com/stretchr/testify/assert"
99
)
1010

11+
func TestBranchName(t *testing.T) {
12+
testCases := []struct {
13+
release string
14+
expected string
15+
errorText string
16+
}{
17+
{
18+
"v0.0.0",
19+
"release/v0.0.0",
20+
"",
21+
},
22+
{
23+
"hest",
24+
"",
25+
"invalid version: hest",
26+
},
27+
{
28+
"v0.0.1",
29+
"hotfix/v0.0.1",
30+
"",
31+
},
32+
{
33+
"v1.0.0",
34+
"release/v1.0.0",
35+
"",
36+
},
37+
{
38+
"1.0.0",
39+
"release/1.0.0",
40+
"",
41+
},
42+
{
43+
"v1.0.0-rc1",
44+
"release/v1.0.0-rc1",
45+
"",
46+
},
47+
// Apart from testing, having release candidates for at patch release may not make sense.
48+
{
49+
"v1.0.1-rc1",
50+
"release/v1.0.1-rc1",
51+
"",
52+
},
53+
}
54+
55+
for _, testCase := range testCases {
56+
actual, err := getBranchName(testCase.release)
57+
58+
if testCase.errorText != "" {
59+
assert.NotNil(t, err)
60+
assert.EqualError(t, err, testCase.errorText)
61+
} else {
62+
assert.Nil(t, err)
63+
assert.Equal(t, testCase.expected, actual)
64+
}
65+
}
66+
}
67+
1168
func TestRelease(t *testing.T) {
1269
testCases := []struct {
1370
changelog string

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
module github.com/itk-dev/gh-itkdev
22

3-
go 1.23
3+
go 1.23.0
4+
5+
toolchain go1.24.1
46

57
require (
68
github.com/cli/go-gh v1.2.1
79
github.com/spf13/cobra v1.8.0
810
github.com/stretchr/testify v1.9.0
11+
golang.org/x/mod v0.24.0
912
)
1013

1114
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
5050
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
5151
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e h1:BuzhfgfWQbX0dWzYzT1zsORLnHRv3bcRcsaUk0VmXA8=
5252
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e/go.mod h1:/Tnicc6m/lsJE0irFMA0LfIwTBo4QP7A8IfyIv4zZKI=
53+
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
54+
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
5355
golang.org/x/net v0.0.0-20220923203811-8be639271d50/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
5456
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
5557
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=

0 commit comments

Comments
 (0)