Skip to content

Commit

Permalink
Issue #111: Bellsoft Liberica actions adds source
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonydahanne committed Oct 6, 2023
1 parent 106f03f commit 2b31f72
Showing 1 changed file with 80 additions and 31 deletions.
111 changes: 80 additions & 31 deletions actions/bellsoft-liberica-dependency/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,41 @@ func main() {
if !ok {
panic(fmt.Errorf("product must be specified"))
}
if p != "liberica" && p != "nik" {
panic(fmt.Errorf("product must either be liberica or nik"))
}

uri := fmt.Sprintf("https://api.bell-sw.com/v1/%s/releases"+
"?arch=x86"+
"&bitness=64"+
"&bundle-type=%s"+
"&os=linux"+
"&package-type=tar.gz"+
"&version-feature=%s"+
"&version-modifier=latest",
p, t, v)
if p == "nik" {
commonStaticParams := "&version-modifier=latest"
uriStaticParams := "?arch=x86" +
"&bitness=64" +
"&os=linux" +
"&package-type=tar.gz" +
commonStaticParams
sourceUriStaticParams := "?package-type=src.tar.gz" +
commonStaticParams

uri := ""
sourceUri := ""
if p == "liberica" {
uri = fmt.Sprintf("https://api.bell-sw.com/v1/%s/releases"+
uriStaticParams+
"&bundle-type=%s"+
"&version-feature=%s",
p, t, v)
sourceUri = fmt.Sprintf("https://api.bell-sw.com/v1/%s/releases"+
sourceUriStaticParams+
"&version-feature=%s",
p, v)
} else if p == "nik" {
uri = fmt.Sprintf("https://api.bell-sw.com/v1/%s/releases"+
"?arch=x86"+
"&bitness=64"+
uriStaticParams+
"&bundle-type=%s"+
"&os=linux"+
"&package-type=tar.gz"+
"&component-version=liberica%%40%s"+
"&version-modifier=latest",
"&component-version=liberica%%40%s",
p, t, v)
sourceUri = fmt.Sprintf("https://api.bell-sw.com/v1/%s/releases"+
sourceUriStaticParams+
"&component-version=liberica%%40%s",
p, v)
}

resp, err := http.Get(uri)
Expand All @@ -86,21 +101,7 @@ func main() {
for _, r := range raw {
key := fmt.Sprintf("%d.%d.%d-%d", r.FeatureVersion, r.InterimVersion, r.UpdateVersion, r.BuildVersion)
if p == "nik" {
if v := actions.ExtendedVersionPattern.FindStringSubmatch(r.Components[0].Version); v != nil {
s := fmt.Sprintf("%s.%s.%s", v[1], v[2], v[3])
if v[4] != "" {
s = fmt.Sprintf("%s-%s", s, v[4])
}
key = s

// Use NIK version for CPE/PURL
re := regexp.MustCompile(`\/vm/([\d]+\.[\d]+\.[\d]+\.?[\d]?)\/`)
matches := re.FindStringSubmatch(r.DownloadURL)
if matches == nil || len(matches) != 2 {
panic(fmt.Errorf("unable to parse NIK version: %s", matches))
}
additionalOutputs["purl"] = matches[1]
}
key = determineNikVersion(r, additionalOutputs)
}
versions[key] = r.DownloadURL
}
Expand All @@ -110,6 +111,34 @@ func main() {
panic(fmt.Errorf("unable to get latest version\n%w", err))
}

sourceResp, err := http.Get(sourceUri)
if err != nil {
panic(fmt.Errorf("unable to get %s\n%w", uri, err))
}
defer sourceResp.Body.Close()

if resp.StatusCode != 200 {
panic(fmt.Errorf("unable to download %s: %d", uri, resp.StatusCode))
}

var sourceRaw []Release
if err := json.NewDecoder(sourceResp.Body).Decode(&sourceRaw); err != nil {
panic(fmt.Errorf("unable to decode payload\n%w", err))
}

sources := make(map[string]string)
for _, r := range sourceRaw {
key := fmt.Sprintf("%d.%d.%d-%d", r.FeatureVersion, r.InterimVersion, r.UpdateVersion, r.BuildVersion)
if p == "nik" {
key = determineNikVersion(r, additionalOutputs)
}
sources[key] = r.DownloadURL
}

if sources != nil {
additionalOutputs["source"] = sources[latestVersion.Original()]
}

outputs, err := actions.NewOutputs(versions[latestVersion.Original()], latestVersion, additionalOutputs)
if err != nil {
panic(fmt.Errorf("unable to create outputs\n%w", err))
Expand All @@ -126,6 +155,26 @@ func main() {
outputs.Write()
}

func determineNikVersion(r Release, additionalOutputs actions.Outputs) string {
key := ""
if v := actions.ExtendedVersionPattern.FindStringSubmatch(r.Components[0].Version); v != nil {
s := fmt.Sprintf("%s.%s.%s", v[1], v[2], v[3])
if v[4] != "" {
s = fmt.Sprintf("%s-%s", s, v[4])
}
key = s

// Use NIK version for CPE/PURL
re := regexp.MustCompile(`\/vm/([\d]+\.[\d]+\.[\d]+\.?[\d]?)\/`)
matches := re.FindStringSubmatch(r.DownloadURL)
if matches == nil || len(matches) != 2 {
panic(fmt.Errorf("unable to parse NIK version: %s", matches))
}
additionalOutputs["purl"] = matches[1]
}
return key
}

type Release struct {
FeatureVersion int `json:"featureVersion"`
InterimVersion int `json:"interimVersion"`
Expand Down

0 comments on commit 2b31f72

Please sign in to comment.