Skip to content

Commit

Permalink
Merge pull request #5644 from koba1t/fix/version_subcommand_is_broken
Browse files Browse the repository at this point in the history
fix version subcommand is not working after release build
  • Loading branch information
k8s-ci-robot authored Apr 4, 2024
2 parents 2b00d88 + 277da9e commit 42873c8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
16 changes: 12 additions & 4 deletions api/provenance/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,22 @@ func GetProvenance() Provenance {

for _, dep := range info.Deps {
if dep != nil && dep.Path == "sigs.k8s.io/kustomize/kustomize/v5" {
p.Version = GetMostRecentTag(*dep)
if dep.Version != "devel" {
continue
}
v, err := GetMostRecentTag(*dep)
if err != nil {
fmt.Printf("failed to get most recent tag for %s: %v\n", dep.Path, err)
continue
}
p.Version = v
}
}

return p
}

func GetMostRecentTag(m debug.Module) string {
func GetMostRecentTag(m debug.Module) (string, error) {
for m.Replace != nil {
m = *m.Replace
}
Expand All @@ -83,13 +91,13 @@ func GetMostRecentTag(m debug.Module) string {
sv, err := semver.Parse(strings.TrimPrefix(split[0], "v"))

if err != nil {
return "unknown"
return "", fmt.Errorf("failed to parse version %s: %w", m.Version, err)
}

if len(split) > 1 && sv.Patch > 0 {
sv.Patch -= 1
}
return fmt.Sprintf("v%s", sv.FinalizeVersion())
return fmt.Sprintf("v%s", sv.FinalizeVersion()), nil
}

// Short returns the shortened provenance stamp.
Expand Down
17 changes: 12 additions & 5 deletions api/provenance/provenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestGetMostRecentTag(t *testing.T) {
tests := []struct {
name string
module debug.Module
isError bool
expectedTag string
}{
{
Expand All @@ -72,9 +73,9 @@ func TestGetMostRecentTag(t *testing.T) {
expectedTag: "v0.0.0",
},
{
name: "Invalid semver string",
module: mockModule("invalid-version"),
expectedTag: "unknown",
name: "Invalid semver string",
module: mockModule("invalid-version"),
isError: true,
},
{
name: "Valid semver with patch increment and pre-release info",
Expand All @@ -90,8 +91,14 @@ func TestGetMostRecentTag(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tag := provenance.GetMostRecentTag(tt.module)
assert.Equal(t, tt.expectedTag, tag)
tag, err := provenance.GetMostRecentTag(tt.module)
if err != nil {
if !tt.isError {
assert.NoError(t, err)
}
} else {
assert.Equal(t, tt.expectedTag, tag)
}
})
}
}

0 comments on commit 42873c8

Please sign in to comment.