Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes foojay action, which was missing some updates #676

Merged
merged 1 commit into from
May 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 67 additions & 2 deletions actions/foojay-dependency/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
dmikusa marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}