From bbbd2bd8d977ea34c4552a8d24bb5ae3336f0f41 Mon Sep 17 00:00:00 2001 From: Foivos Zakkak Date: Mon, 30 Oct 2023 17:03:23 +0200 Subject: [PATCH] Use Foojay.io DISCO API to get latest Mandrel release --- __tests__/mandrel.test.ts | 16 +++++++++++ src/mandrel.ts | 58 +++++++++++++++++++++++++++++++++------ 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/__tests__/mandrel.test.ts b/__tests__/mandrel.test.ts index 368eae9..8cb4b8e 100644 --- a/__tests__/mandrel.test.ts +++ b/__tests__/mandrel.test.ts @@ -54,3 +54,19 @@ test('find latest', async () => { expect(tag_name).toContain(mandrel.MANDREL_TAG_PREFIX) }) +test('get latest Mandrel for specific JDK', async () => { + // Test deprecated versions that won't get updates anymore + for (var combination of [ + ['11', '22.2.0.0-Final'], + ['20', '23.0.1.2-Final']]) { + const latest = await mandrel.getLatestMandrelReleaseUrl(combination[0]) + expect(latest).toContain(`mandrel-java${combination[0]}`) + expect(latest).toContain(combination[1]) + } + // Test supported versions + for (var javaVersion of ['17', '21']) { + const latest = await mandrel.getLatestMandrelReleaseUrl(javaVersion) + expect(latest).toContain(`mandrel-java${javaVersion}`) + } +}) + diff --git a/src/mandrel.ts b/src/mandrel.ts index ebd39fc..35b7f3d 100644 --- a/src/mandrel.ts +++ b/src/mandrel.ts @@ -6,6 +6,7 @@ import {basename} from 'path' export const MANDREL_REPO = 'mandrel' export const MANDREL_TAG_PREFIX = c.MANDREL_NAMESPACE const MANDREL_DL_BASE = 'https://github.com/graalvm/mandrel/releases/download' +const DISCO_API_BASE = 'https://api.foojay.io/disco/v3.0/packages/jdks' export async function setUpMandrel( graalvmVersion: string, @@ -30,16 +31,55 @@ export async function setUpMandrel( } async function setUpMandrelLatest(javaVersion: string): Promise { - const latestRelease = await getLatestRelease(MANDREL_REPO) - const tag_name = latestRelease.tag_name - if (tag_name.startsWith(MANDREL_TAG_PREFIX)) { - const latestVersion = tag_name.substring( - MANDREL_TAG_PREFIX.length, - tag_name.length - ) - return setUpMandrelRelease(latestVersion, javaVersion) + const latest_release_url = await getLatestMandrelReleaseUrl(javaVersion) + const version_tag = getTagFromURI(latest_release_url); + const version = version_tag.substring(c.MANDREL_NAMESPACE.length, version_tag.length) + console.log(version); + + const toolName = determineToolName(javaVersion) + return downloadExtractAndCacheJDK( + async () => downloadTool(latest_release_url), + toolName, + version + ) +} + +// Download URIs are of the form https://github.com/graalvm/mandrel/releases/download// +function getTagFromURI(uri: string): string { + const parts = uri.split('/'); + try { + return parts[parts.length - 2]; + } catch (error) { + throw new Error(`Failed to extract tag from URI ${uri}: ${error}`) + } +} + +export async function getLatestMandrelReleaseUrl(javaVersion: string): Promise { + const url = `${DISCO_API_BASE}?jdk_version=${javaVersion}&distribution=${c.DISTRIBUTION_MANDREL}&architecture=${c.JDK_ARCH}&operating_system=${c.JDK_PLATFORM}&latest=per_distro` + const response = await fetch(url) + if (!response.ok) { + throw new Error(`Failed to fetch latest Mandrel release for Java ${javaVersion} from DISCO API: ${response.statusText}`) + } + const data = await response.json() + try { + const pkg_info_uri = data.result[0].links.pkg_info_uri + return getLatestMandrelReleaseUrlHelper(javaVersion, pkg_info_uri) + } catch (error) { + throw new Error(`Failed to get latest Mandrel release for Java ${javaVersion} from DISCO API: ${error}`) + } +} + +async function getLatestMandrelReleaseUrlHelper(java_version: string, pkg_info_uri: string): Promise { + const response = await fetch(pkg_info_uri) + if (!response.ok) { + throw new Error(`Failed to fetch package info of latest Mandrel release for Java ${java_version} from DISCO API: ${response.statusText}`) + } + const data = await response.json() + try { + return data.result[0].direct_download_uri + } catch (error) { + throw new Error(`Failed to get download URI of latest Mandrel release for Java ${java_version} from DISCO API: ${error}`) } - throw new Error(`Could not find latest Mandrel release: ${tag_name}`) } async function setUpMandrelRelease(