Skip to content

Commit

Permalink
fix version subcommand is not working after release build
Browse files Browse the repository at this point in the history
  • Loading branch information
koba1t committed Apr 4, 2024
1 parent 2b00d88 commit 277da9e
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
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
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 277da9e

Please sign in to comment.