6
6
"os"
7
7
"strings"
8
8
9
+ "github.com/blang/semver/v4"
9
10
"github.com/hashicorp/go-retryablehttp"
10
11
"github.com/samber/lo"
11
12
"golang.org/x/mod/modfile"
@@ -49,13 +50,17 @@ func DependencyModuleVersionGit(dep string) (string, error) {
49
50
return "" , err
50
51
}
51
52
53
+ // NOTE: When we rely on a pseudo-version (e.g. `v1.1.1-0.20250217181409-44e5ddce290d`),
54
+ // we need to extract the commit hash from it to use it in the GitHub API.
55
+ // When version is set to a tag (e.g. `v1.1.1`), we could use it against the /commits
56
+ // endpoint as well but there's no need for that as we can use the tag directly.
57
+
52
58
// The same logic as in scripts/generate-crd-kustomize.sh:11:18
53
- if versionParts := strings .Split (version , "-" ); len (versionParts ) >= 2 {
54
- // If there are 2 or more hyphens, extract the part after the last hyphen as
55
- // that's a git commit hash (e.g. `v1.1.1-0.20250217181409-44e5ddce290d`).
56
- version = versionParts [len (versionParts )- 1 ]
57
59
58
- resp , err := retryablehttp .Get ("https://api.github.com/repos/Kong/kubernetes-configuration/commits/" + version )
60
+ // If there are 2 or more hyphens, extract the part after the last hyphen as
61
+ // that's a git commit hash (e.g. `v1.1.1-0.20250217181409-44e5ddce290d`).
62
+ if hash , ok := GetHashFromPseudoVersion (version ); ok {
63
+ resp , err := retryablehttp .Get ("https://api.github.com/repos/Kong/kubernetes-configuration/commits/" + hash )
59
64
if err != nil {
60
65
return "" , fmt .Errorf ("failed to fetch commit data: %w" , err )
61
66
}
@@ -72,3 +77,24 @@ func DependencyModuleVersionGit(dep string) (string, error) {
72
77
73
78
return version , nil
74
79
}
80
+
81
+ // GetHashFromPseudoVersion extracts the commit hash from a pseudo version string.
82
+ // It returns the hash and a boolean indicating whether the provided string
83
+ // is a valid pseudo version.
84
+ func GetHashFromPseudoVersion (pseudoVersion string ) (string , bool ) {
85
+ pseudoVersion = strings .TrimSpace (pseudoVersion )
86
+ pseudoVersion = strings .TrimPrefix (pseudoVersion , "v" ) // Remove leading 'v' if present
87
+
88
+ // The pseudo version is expected to be in the format:
89
+ // v1.1.1-0.20250217181409-44e5ddce290d
90
+ // We need to extract the last part after the last hyphen.
91
+ parts := strings .Split (pseudoVersion , "-" )
92
+ if len (parts ) <= 2 {
93
+ return "" , false
94
+ }
95
+
96
+ if _ , err := semver .Parse (parts [0 ]); err != nil {
97
+ return "" , false
98
+ }
99
+ return parts [len (parts )- 1 ], true
100
+ }
0 commit comments