Skip to content

Commit aea7ad5

Browse files
authored
chore(release-tool): normalize version tags
Automatically normalize git tags based on kumahq/kuma conventions: - Add v prefix for newer versions (>= 2.13.x, >= 3.x, specific patches) - Remove v prefix for older versions - Release names always use plain version numbers (e.g., `2.12.5`) - Git tags follow version-specific conventions Also adds `mise.toml` with pinned `golangci-lint` version. Signed-off-by: Bart Smykla <bartek@smykla.com>
1 parent 1a03686 commit aea7ad5

File tree

7 files changed

+85
-26
lines changed

7 files changed

+85
-26
lines changed

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ build/release-tool:
99
mkdir -p build
1010
go build -o build ./cmd/release-tool/...
1111

12-
GOPATH?=$(shell go env GOPATH)
13-
1412
.PHONY: check
1513
check:
1614
go fmt ./...
1715
go mod tidy
18-
test -n "$$CI" || $(GOPATH)/bin/golangci-lint run -v
16+
test -n "$$CI" || mise exec -- golangci-lint run -v
1917

2018
.PHONY: test
2119
test:

cmd/internal/github/graphql.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,36 +311,57 @@ func (c GQLClient) graphqlQuery(query string, variables map[string]interface{})
311311
return out, err
312312
}
313313

314-
func (c GQLClient) UpsertRelease(ctx context.Context, repo string, release string, contentModifier func(repositoryRelease *github.RepositoryRelease) error) error {
314+
func (c GQLClient) UpsertRelease(
315+
ctx context.Context,
316+
repo string,
317+
releaseName string,
318+
tagName string,
319+
contentModifier func(repositoryRelease *github.RepositoryRelease) error,
320+
) error {
315321
releases, err := c.ReleaseGraphQL(repo)
316322
if err != nil {
317323
return err
318324
}
325+
319326
var existingRelease *GQLRelease
327+
320328
for _, r := range releases {
321-
if r.Name == release {
329+
if r.Name == releaseName || r.Name == tagName {
322330
existingRelease = &r
323331
break
324332
}
325333
}
334+
326335
owner, name := SplitRepo(repo)
336+
327337
if existingRelease == nil {
328-
releasePayload := &github.RepositoryRelease{Name: &release, Draft: github.Ptr(true), TagName: &release}
338+
releasePayload := &github.RepositoryRelease{
339+
Name: &releaseName,
340+
Draft: github.Ptr(true),
341+
TagName: &tagName,
342+
}
343+
329344
err := contentModifier(releasePayload)
330345
if err != nil {
331346
return err
332347
}
348+
333349
_, _, err = c.Cl.Repositories.CreateRelease(ctx, owner, name, releasePayload)
350+
334351
return err
335352
}
353+
336354
releasePayload, _, err := c.Cl.Repositories.GetRelease(ctx, owner, name, int64(existingRelease.Id))
337355
if err != nil {
338356
return err
339357
}
358+
340359
err = contentModifier(releasePayload)
341360
if err != nil {
342361
return err
343362
}
363+
344364
_, _, err = c.Cl.Repositories.EditRelease(ctx, owner, name, int64(existingRelease.Id), releasePayload)
365+
345366
return err
346367
}

cmd/release-tool/changelog.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ It will then output a changelog with all PRs with the same changelog grouped tog
107107
return err
108108
}
109109

110-
out, err := getChangelog(gqlClient, config.repo, config.branch, config.fromTag)
110+
// Use warnOnNormalize=true since config.fromTag is user-provided
111+
out, err := getChangelog(gqlClient, config.repo, config.branch, config.fromTag, true)
111112
if err != nil {
112113
return err
113114
}
@@ -128,9 +129,14 @@ It will then output a changelog with all PRs with the same changelog grouped tog
128129
},
129130
}
130131

131-
func getChangelog(gqlClient *github.GQLClient, repo string, branch string, tag string) (changeloggenerator.Changelog, error) {
132+
func getChangelog(gqlClient *github.GQLClient, repo string, branch string, tag string, warnOnNormalize bool) (changeloggenerator.Changelog, error) {
132133
// Normalize tag to ensure correct v-prefix based on kumahq/kuma conventions
133-
normalizedTag := NormalizeVersionTag(tag)
134+
var normalizedTag string
135+
if warnOnNormalize {
136+
normalizedTag = NormalizeVersionTagWithWarning(tag)
137+
} else {
138+
normalizedTag = NormalizeVersionTag(tag)
139+
}
134140

135141
// Retrieve data from github
136142
commit, err := gqlClient.CommitByRef(repo, normalizedTag)

cmd/release-tool/release.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,16 @@ TODO summary of some simple stuff.
6363
return err
6464
}
6565

66-
_, err = fmt.Fprintf(cmd.OutOrStdout(), "getting changelog from %s on repo %s and branch %s\n", prevVersion, config.repo, branch)
66+
// Normalize the previous version tag for display and lookup
67+
prevTag := NormalizeVersionTag(prevVersion.String())
68+
69+
_, err = fmt.Fprintf(cmd.OutOrStdout(), "getting changelog from %s on repo %s and branch %s\n", prevTag, config.repo, branch)
6770
if err != nil {
6871
return err
6972
}
7073

71-
changelog, err := getChangelog(gqlClient, config.repo, branch, prevVersion.String())
74+
// Use warnOnNormalize=false since prevVersion is auto-derived, not user-provided
75+
changelog, err := getChangelog(gqlClient, config.repo, branch, prevTag, false)
7276
if err != nil {
7377
return err
7478
}
@@ -112,13 +116,13 @@ TODO summary of some simple stuff.
112116
return err
113117
}
114118

115-
// Ensure release name has v-prefix to match Git tag format
116-
releaseTag := config.release
117-
if !strings.HasPrefix(releaseTag, "v") {
118-
releaseTag = "v" + releaseTag
119-
}
119+
// Normalize release tag to match kumahq/kuma Git tag format
120+
// Use WithWarning since config.release is user-provided
121+
releaseTag := NormalizeVersionTagWithWarning(config.release)
122+
// Release name should not have v prefix (just the version number)
123+
releaseName := strings.TrimPrefix(releaseTag, "v")
120124

121-
return gqlClient.UpsertRelease(cmd.Context(), config.repo, releaseTag, func(release *github2.RepositoryRelease) error {
125+
return gqlClient.UpsertRelease(cmd.Context(), config.repo, releaseName, releaseTag, func(release *github2.RepositoryRelease) error {
122126
if !release.GetDraft() {
123127
return fmt.Errorf("release :%s has already published release notes, updating release-notes of released versions is not supported", release)
124128
}
@@ -130,6 +134,8 @@ TODO summary of some simple stuff.
130134
return fmt.Errorf("release body exceeds GitHub limit: %d characters (max %d). Use --dry-run to preview the body and consider manually truncating", len(body), GitHubMaxBodySize)
131135
}
132136

137+
// Normalize release name to not have v prefix (SLSA provenance may create releases with v prefix)
138+
release.Name = github2.Ptr(releaseName)
133139
release.Body = github2.Ptr(body)
134140

135141
return nil

cmd/release-tool/version.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
)
1010

1111
// NormalizeVersionTag ensures the version tag has the correct format (with or without v prefix)
12-
// based on kumahq/kuma tagging conventions. Logs a warning if v prefix was auto-added.
12+
// based on kumahq/kuma tagging conventions. Does not log warnings (use NormalizeVersionTagWithWarning
13+
// for user-provided values that should warn).
1314
//
1415
// Tagging convention:
1516
// - >= 2.13.x or >= 3.x: always v prefix
@@ -19,22 +20,37 @@ import (
1920
// - 2.7.x: v prefix if patch >= 20
2021
// - Other older versions: no v prefix
2122
func NormalizeVersionTag(tag string) string {
22-
if strings.HasPrefix(tag, "v") {
23-
return tag
24-
}
23+
return normalizeVersionTag(tag, false)
24+
}
25+
26+
// NormalizeVersionTagWithWarning normalizes the tag and logs a warning if the prefix was added or removed.
27+
// Use this for user-provided values where the warning is helpful.
28+
func NormalizeVersionTagWithWarning(tag string) string {
29+
return normalizeVersionTag(tag, true)
30+
}
2531

26-
v, err := semver.NewVersion(tag)
32+
func normalizeVersionTag(tag string, warn bool) string {
33+
hasPrefix := strings.HasPrefix(tag, "v")
34+
cleanTag := strings.TrimPrefix(tag, "v")
35+
36+
v, err := semver.NewVersion(cleanTag)
2737
if err != nil {
2838
return tag
2939
}
3040

3141
if needsVPrefix(v) {
32-
_, _ = fmt.Fprintf(os.Stderr, "Warning: auto-adding 'v' prefix to tag %s -> v%s (kumahq/kuma uses v-prefixed tags for this version)\n", tag, tag)
42+
if !hasPrefix && warn {
43+
_, _ = fmt.Fprintf(os.Stderr, "Warning: auto-adding 'v' prefix to tag %s -> v%s (kumahq/kuma uses v-prefixed tags for this version)\n", tag, tag)
44+
}
45+
46+
return "v" + cleanTag
47+
}
3348

34-
return "v" + tag
49+
if hasPrefix && warn {
50+
_, _ = fmt.Fprintf(os.Stderr, "Warning: auto-removing 'v' prefix from tag %s -> %s (kumahq/kuma uses non-prefixed tags for this version)\n", tag, cleanTag)
3551
}
3652

37-
return tag
53+
return cleanTag
3854
}
3955

4056
// needsVPrefix determines if a version should have a v prefix based on kumahq/kuma conventions.

cmd/release-tool/version_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestNormalizeVersionTag(t *testing.T) {
8181
input string
8282
expected string
8383
}{
84-
// Already has v prefix - unchanged
84+
// Already has v prefix and needs it - unchanged
8585
{"v2.12.4", "v2.12.4"},
8686
{"v2.11.8", "v2.11.8"},
8787
{"v3.0.0", "v3.0.0"},
@@ -103,8 +103,18 @@ func TestNormalizeVersionTag(t *testing.T) {
103103
{"2.9.0", "2.9.0"},
104104
{"2.8.5", "2.8.5"},
105105

106+
// Has v prefix but shouldn't - should be removed
107+
{"v2.12.3", "2.12.3"},
108+
{"v2.11.7", "2.11.7"},
109+
{"v2.10.8", "2.10.8"},
110+
{"v2.7.19", "2.7.19"},
111+
{"v2.9.0", "2.9.0"},
112+
{"v2.8.5", "2.8.5"},
113+
{"v1.5.0", "1.5.0"},
114+
106115
// Invalid version - unchanged
107116
{"invalid", "invalid"},
117+
{"vinvalid", "vinvalid"},
108118
{"", ""},
109119
}
110120

mise.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tools]
2+
golangci-lint = "2.6.2"

0 commit comments

Comments
 (0)