From 33b10b64eb638c2717d7f7c3e01e6a71f9735954 Mon Sep 17 00:00:00 2001 From: Nikolai Laevskii Date: Tue, 18 Jul 2023 09:28:12 +0200 Subject: [PATCH] Use archive as fallback only when dealing with major version --- .../distributors/oracle-installer.test.ts | 26 +++++++++-- dist/setup/index.js | 32 +++++++++---- src/distributions/oracle/installer.ts | 46 ++++++++++++++----- 3 files changed, 79 insertions(+), 25 deletions(-) diff --git a/__tests__/distributors/oracle-installer.test.ts b/__tests__/distributors/oracle-installer.test.ts index baf1d8e4..ad16ecac 100644 --- a/__tests__/distributors/oracle-installer.test.ts +++ b/__tests__/distributors/oracle-installer.test.ts @@ -25,7 +25,12 @@ describe('findPackageForDownload', () => { [ '20', '20', - 'https://download.oracle.com/java/20/archive/jdk-20_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' + 'https://download.oracle.com/java/20/latest/jdk-20_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' + ], + [ + '18', + '18', + 'https://download.oracle.com/java/18/archive/jdk-18_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' ], [ '20.0.1', @@ -35,7 +40,7 @@ describe('findPackageForDownload', () => { [ '17', '17', - 'https://download.oracle.com/java/17/archive/jdk-17_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' + 'https://download.oracle.com/java/17/latest/jdk-17_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' ], [ '17.0.1', @@ -53,6 +58,19 @@ describe('findPackageForDownload', () => { }) ); + /** + * NOTE - Should fail to retrieve 18 from latest and check archive instead + */ + if (input === '18') { + spyHttpClient.mockReturnValueOnce( + Promise.resolve({ + message: { + statusCode: 404 + } + }) + ); + } + const result = await distribution['findPackageForDownload'](input); jest.restoreAllMocks(); @@ -75,7 +93,7 @@ describe('findPackageForDownload', () => { jest.spyOn(os, 'arch').mockReturnValue(osArch); jest.spyOn(os, 'platform').mockReturnValue('linux'); - const version = '17'; + const version = '18'; const distro = new OracleDistribution({ version, architecture: '', // to get default value @@ -89,7 +107,7 @@ describe('findPackageForDownload', () => { } const archiveType = getDownloadArchiveExtension(); const result = await distro['findPackageForDownload'](version); - const expectedUrl = `https://download.oracle.com/java/17/archive/jdk-17_${osType}-${distroArch}_bin.${archiveType}`; + const expectedUrl = `https://download.oracle.com/java/18/archive/jdk-18_${osType}-${distroArch}_bin.${archiveType}`; expect(result.url).toBe(expectedUrl); } diff --git a/dist/setup/index.js b/dist/setup/index.js index f5d2e017..fe8ba78f 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -102973,19 +102973,33 @@ class OracleDistribution extends base_installer_1.JavaBase { } const platform = this.getPlatform(); const extension = util_1.getDownloadArchiveExtension(); - const major = range.includes('.') ? range.split('.')[0] : range; - const fileUrl = `${ORACLE_DL_BASE}/${major}/archive/jdk-${range}_${platform}-${arch}_bin.${extension}`; + const isOnlyMajorProvided = !range.includes('.'); + const major = isOnlyMajorProvided ? range : range.split('.')[0]; + const possibleUrls = []; + /** + * NOTE + * If only major version was provided we will check it under /latest first + * in order to retrieve the latest possible version if possible, + * otherwise we will fall back to /archive where we are guaranteed to + * find any version if it exists + */ + if (isOnlyMajorProvided) { + possibleUrls.push(`${ORACLE_DL_BASE}/${major}/latest/jdk-${major}_${platform}-${arch}_bin.${extension}`); + } + possibleUrls.push(`${ORACLE_DL_BASE}/${major}/archive/jdk-${range}_${platform}-${arch}_bin.${extension}`); if (parseInt(major) < 17) { throw new Error('Oracle JDK is only supported for JDK 17 and later'); } - const response = yield this.http.head(fileUrl); - if (response.message.statusCode === http_client_1.HttpCodes.NotFound) { - throw new Error(`Could not find Oracle JDK for SemVer ${range}`); + for (const url of possibleUrls) { + const response = yield this.http.head(url); + if (response.message.statusCode === http_client_1.HttpCodes.OK) { + return { url, version: range }; + } + if (response.message.statusCode !== http_client_1.HttpCodes.NotFound) { + throw new Error(`Http request for Oracle JDK failed with status code: ${response.message.statusCode}`); + } } - if (response.message.statusCode !== http_client_1.HttpCodes.OK) { - throw new Error(`Http request for Oracle JDK failed with status code: ${response.message.statusCode}`); - } - return { url: fileUrl, version: range }; + throw new Error(`Could not find Oracle JDK for SemVer ${range}`); }); } getPlatform(platform = process.platform) { diff --git a/src/distributions/oracle/installer.ts b/src/distributions/oracle/installer.ts index 8f68d9dd..ec0ab215 100644 --- a/src/distributions/oracle/installer.ts +++ b/src/distributions/oracle/installer.ts @@ -65,26 +65,48 @@ export class OracleDistribution extends JavaBase { const platform = this.getPlatform(); const extension = getDownloadArchiveExtension(); - const major = range.includes('.') ? range.split('.')[0] : range; - const fileUrl = `${ORACLE_DL_BASE}/${major}/archive/jdk-${range}_${platform}-${arch}_bin.${extension}`; + + const isOnlyMajorProvided = !range.includes('.'); + const major = isOnlyMajorProvided ? range : range.split('.')[0]; + + const possibleUrls: string[] = []; + + /** + * NOTE + * If only major version was provided we will check it under /latest first + * in order to retrieve the latest possible version if possible, + * otherwise we will fall back to /archive where we are guaranteed to + * find any version if it exists + */ + if (isOnlyMajorProvided) { + possibleUrls.push( + `${ORACLE_DL_BASE}/${major}/latest/jdk-${major}_${platform}-${arch}_bin.${extension}` + ); + } + + possibleUrls.push( + `${ORACLE_DL_BASE}/${major}/archive/jdk-${range}_${platform}-${arch}_bin.${extension}` + ); if (parseInt(major) < 17) { throw new Error('Oracle JDK is only supported for JDK 17 and later'); } - const response = await this.http.head(fileUrl); + for (const url of possibleUrls) { + const response = await this.http.head(url); - if (response.message.statusCode === HttpCodes.NotFound) { - throw new Error(`Could not find Oracle JDK for SemVer ${range}`); + if (response.message.statusCode === HttpCodes.OK) { + return {url, version: range}; + } + + if (response.message.statusCode !== HttpCodes.NotFound) { + throw new Error( + `Http request for Oracle JDK failed with status code: ${response.message.statusCode}` + ); + } } - if (response.message.statusCode !== HttpCodes.OK) { - throw new Error( - `Http request for Oracle JDK failed with status code: ${response.message.statusCode}` - ); - } - - return {url: fileUrl, version: range}; + throw new Error(`Could not find Oracle JDK for SemVer ${range}`); } public getPlatform(platform: NodeJS.Platform = process.platform): OsVersions {