From 69a0dbc4d57642c68e8d3a77c14da18f203218d1 Mon Sep 17 00:00:00 2001 From: Daniel Mikusa Date: Fri, 6 May 2022 14:45:12 -0400 Subject: [PATCH] Fixes foojay action, which was missing some updates Corrects the following issues: - Versions with a fourth digit were skipped, like 11.0.14.1 - Versions were incorrectly sorted [1], now using the latest field to pick the latest version - If an invalid distro was set, it would work and the API is not validating. It just does not filter at all, which returns all versions and confusing results. This PR validates the distro entered and panics if its bad. [1] - This happens due to some legacy issues in how we manage semver. If a version is not semver or if it's semver but has a pre-release or post-release number, then we squash all that to the format `major.minor.patch-pre` which isn't correct but changing it would require a lot of investigation to track down what it might possibly impact. At any rate, because we do this technically 11.0.14-1 is less than 11.0.14 and so 11.0.14 is picked despite what we really want being 11.0.14-1 (which was 11.0.14.1). Signed-off-by: Daniel Mikusa --- actions/foojay-dependency/main.go | 69 ++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/actions/foojay-dependency/main.go b/actions/foojay-dependency/main.go index 0271d198..4dbc7951 100644 --- a/actions/foojay-dependency/main.go +++ b/actions/foojay-dependency/main.go @@ -41,6 +41,23 @@ func main() { panic(fmt.Errorf("distro must be specified")) } + distros, err := LoadDistros() + if err != nil { + panic(fmt.Errorf("unable to load distros\n%w", err)) + } + + found := false + for _, distro := range distros { + if distro == d { + found = true + break + } + } + + if !found { + panic(fmt.Errorf("invalid distro %q, valid: %s", d, distros)) + } + // type i.e. jre or jdk t, ok := inputs["type"] if !ok { @@ -114,12 +131,20 @@ func LoadPackages(d string, t string, v int) actions.Versions { panic(fmt.Errorf("unable to decode package payload\n%w", err)) } - re := regexp.MustCompile(`(\d+\.\d+\.\d+|\d+)\+?.*`) + re := regexp.MustCompile(`(\d+\.\d+\.\d+\.\d+|\d+\.\d+\.\d+\+\d+|\d+\.\d+\.\d+|\d+)\+?.*`) versions := make(actions.Versions) for _, result := range raw.Result { + if !result.LatestBuild { + continue + } + if ver := re.FindStringSubmatch(result.JavaVersion); ver != nil { - versions[ver[1]] = LoadDownloadURI(result.Links.URI) + version, err := actions.NormalizeVersion(ver[1]) + if err != nil { + panic(fmt.Errorf("unable to normalize version %s\n%w", version, err)) + } + versions[version] = LoadDownloadURI(result.Links.URI) } else { fmt.Println(result.JavaVersion, "failed to parse") } @@ -157,12 +182,44 @@ func LoadDownloadURI(uri string) string { return raw.Result[0].DirectDownloadURI } +func LoadDistros() ([]string, error) { + uri := "https://api.foojay.io/disco/v3.0/distributions?include_versions=false&include_synonyms=false" + request, err := http.NewRequest("GET", uri, nil) + if err != nil { + panic(fmt.Errorf("unable to get %s\n%w", uri, err)) + } + request.Header.Set("User-Agent", userAgent) + + resp, err := client.Do(request) + if err != nil { + panic(fmt.Errorf("unable to get %s\n%w", uri, err)) + } + defer resp.Body.Close() + + if resp.StatusCode != 200 { + panic(fmt.Errorf("unable to download %s: %d", uri, resp.StatusCode)) + } + + var raw DistroResponse + if err := json.NewDecoder(resp.Body).Decode(&raw); err != nil { + panic(fmt.Errorf("unable to decode download payload\n%w", err)) + } + + distros := []string{} + for _, distro := range raw.Result { + distros = append(distros, distro.APIParameter) + } + + return distros, nil +} + type PackagesResponse struct { Result []struct { JavaVersion string `json:"java_version"` Links struct { URI string `json:"pkg_info_uri"` } + LatestBuild bool `json:"latest_build_available"` } Message string } @@ -174,3 +231,11 @@ type DownloadResponse struct { } Message string } + +type DistroResponse struct { + Result []struct { + APIParameter string `json:"api_parameter"` + Maintained bool `json:"maintained"` + } + Message string +}