66 "os"
77 "strings"
88
9+ "github.com/blang/semver/v4"
910 "github.com/hashicorp/go-retryablehttp"
1011 "github.com/samber/lo"
1112 "golang.org/x/mod/modfile"
@@ -49,13 +50,17 @@ func DependencyModuleVersionGit(dep string) (string, error) {
4950 return "" , err
5051 }
5152
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+
5258 // 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 ]
5759
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 )
5964 if err != nil {
6065 return "" , fmt .Errorf ("failed to fetch commit data: %w" , err )
6166 }
@@ -72,3 +77,24 @@ func DependencyModuleVersionGit(dep string) (string, error) {
7277
7378 return version , nil
7479}
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