Use Foojay.io DISCO API to get latest Mandrel release

This commit is contained in:
Foivos Zakkak 2023-10-30 17:03:23 +02:00 committed by Fabio Niephaus
parent c2fd2d6d8e
commit 22b65d7de0
2 changed files with 65 additions and 9 deletions

View File

@ -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}`)
}
})

View File

@ -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<string> {
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/<tag>/<archive-name>
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<string> {
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<string> {
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(