basic validation failure fix (#682)

This commit is contained in:
aparnajyothi-y 2024-09-07 01:12:39 +05:30 committed by GitHub
parent 7467385c61
commit 2dfa2011c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 105 additions and 67 deletions

View file

@ -1,5 +1,5 @@
import { HttpClient } from '@actions/http-client'; import {HttpClient} from '@actions/http-client';
import { SapMachineDistribution } from '../../src/distributions/sapmachine/installer'; import {SapMachineDistribution} from '../../src/distributions/sapmachine/installer';
import * as utils from '../../src/util'; import * as utils from '../../src/util';
import manifestData from '../data/sapmachine.json'; import manifestData from '../data/sapmachine.json';
@ -43,13 +43,13 @@ describe('getAvailableVersions', () => {
spyHttpClient.mockReturnValueOnce({ spyHttpClient.mockReturnValueOnce({
statusCode: 404, statusCode: 404,
headers: {}, headers: {},
result: "" result: ''
}) });
spyHttpClient.mockReturnValueOnce({ spyHttpClient.mockReturnValueOnce({
statusCode: 200, statusCode: 200,
headers: {}, headers: {},
result: manifestData result: manifestData
}) });
const version = '17'; const version = '17';
const distribution = new SapMachineDistribution({ const distribution = new SapMachineDistribution({
@ -65,8 +65,10 @@ describe('getAvailableVersions', () => {
version version
); );
expect(availableVersion).not.toBeNull(); expect(availableVersion).not.toBeNull();
expect(availableVersion.url).toBe('https://github.com/SAP/SapMachine/releases/download/sapmachine-17.0.10/sapmachine-jdk-17.0.10_linux-x64_bin.tar.gz'); expect(availableVersion.url).toBe(
}) 'https://github.com/SAP/SapMachine/releases/download/sapmachine-17.0.10/sapmachine-jdk-17.0.10_linux-x64_bin.tar.gz'
);
});
}); });
describe('getAvailableVersions', () => { describe('getAvailableVersions', () => {
@ -77,7 +79,7 @@ describe('getAvailableVersions', () => {
['16.0.1', 'x64', 'linux', 71], ['16.0.1', 'x64', 'linux', 71],
['23-ea', 'x64', 'linux', 798], ['23-ea', 'x64', 'linux', 798],
['23-ea', 'aarch64', 'windows', 0], ['23-ea', 'aarch64', 'windows', 0],
['23-ea', 'x64', 'windows', 750], ['23-ea', 'x64', 'windows', 750]
])( ])(
'should get right number of available versions from JSON', 'should get right number of available versions from JSON',
async ( async (
@ -209,7 +211,7 @@ describe('getAvailableVersions', () => {
'x64', 'x64',
'jdk', 'jdk',
'https://github.com/SAP/SapMachine/releases/download/sapmachine-17.0.10/sapmachine-jdk-17.0.10_linux-x64-musl_bin.tar.gz' 'https://github.com/SAP/SapMachine/releases/download/sapmachine-17.0.10/sapmachine-jdk-17.0.10_linux-x64-musl_bin.tar.gz'
], ]
])( ])(
'should return proper link according to the specified java-version, platform and arch', 'should return proper link according to the specified java-version, platform and arch',
async ( async (
@ -224,8 +226,7 @@ describe('getAvailableVersions', () => {
version: version, version: version,
architecture: arch, architecture: arch,
packageType: packageType, packageType: packageType,
checkLatest: false, checkLatest: false
}); });
mockPlatform(distribution, platform); mockPlatform(distribution, platform);
@ -247,15 +248,20 @@ describe('getAvailableVersions', () => {
['8-ea', 'linux', 'x64', '8'], ['8-ea', 'linux', 'x64', '8'],
['21.0.3+7', 'linux', 'x64', '21.0.3+7'], ['21.0.3+7', 'linux', 'x64', '21.0.3+7'],
['21.0.3+8-ea', 'linux', 'x64', '21.0.3+8'], ['21.0.3+8-ea', 'linux', 'x64', '21.0.3+8'],
['17', 'linux-muse', 'aarch64'], ['17', 'linux-muse', 'aarch64']
])( ])(
'should throw when required version of JDK can not be found in the JSON', 'should throw when required version of JDK can not be found in the JSON',
async (version: string, platform: string, arch: string, normalizedVersion: string = version) => { async (
version: string,
platform: string,
arch: string,
normalizedVersion: string = version
) => {
const distribution = new SapMachineDistribution({ const distribution = new SapMachineDistribution({
version: version, version: version,
architecture: arch, architecture: arch,
packageType: 'jdk', packageType: 'jdk',
checkLatest: false, checkLatest: false
}); });
mockPlatform(distribution, platform); mockPlatform(distribution, platform);
@ -280,7 +286,9 @@ describe('getAvailableVersions', () => {
mockPlatform(distribution, platform); mockPlatform(distribution, platform);
await expect( await expect(
distribution['findPackageForDownload'](jdkVersion) distribution['findPackageForDownload'](jdkVersion)
).rejects.toThrow('SapMachine provides only the `jdk` and `jre` package type'); ).rejects.toThrow(
'SapMachine provides only the `jdk` and `jre` package type'
);
}); });
}); });
}); });

4
dist/setup/index.js vendored
View file

@ -125012,7 +125012,7 @@ class SapMachineDistribution extends base_installer_1.JavaBase {
continue; continue;
} }
// skip earlyAccessVersions if stable version requested // skip earlyAccessVersions if stable version requested
if (this.stable && buildVersionMap.ea === "true") { if (this.stable && buildVersionMap.ea === 'true') {
continue; continue;
} }
for (const [edition, editionAssets] of Object.entries(buildVersionMap.assets)) { for (const [edition, editionAssets] of Object.entries(buildVersionMap.assets)) {
@ -125038,7 +125038,7 @@ class SapMachineDistribution extends base_installer_1.JavaBase {
version: buildVersionWithoutPrefix, version: buildVersionWithoutPrefix,
checksum: contentTypeAssets.checksum, checksum: contentTypeAssets.checksum,
downloadLink: contentTypeAssets.url, downloadLink: contentTypeAssets.url,
packageType: edition, packageType: edition
}); });
} }
} }

View file

@ -25,7 +25,7 @@ enum JavaDistribution {
Corretto = 'corretto', Corretto = 'corretto',
Oracle = 'oracle', Oracle = 'oracle',
Dragonwell = 'dragonwell', Dragonwell = 'dragonwell',
SapMachine = 'sapmachine', SapMachine = 'sapmachine'
} }
export function getJavaDistribution( export function getJavaDistribution(

View file

@ -2,7 +2,7 @@ import * as core from '@actions/core';
import * as tc from '@actions/tool-cache'; import * as tc from '@actions/tool-cache';
import semver from 'semver'; import semver from 'semver';
import fs from 'fs'; import fs from 'fs';
import { OutgoingHttpHeaders } from 'http'; import {OutgoingHttpHeaders} from 'http';
import path from 'path'; import path from 'path';
import { import {
convertVersionToSemver, convertVersionToSemver,
@ -11,13 +11,13 @@ import {
getGitHubHttpHeaders, getGitHubHttpHeaders,
isVersionSatisfies isVersionSatisfies
} from '../../util'; } from '../../util';
import { JavaBase } from '../base-installer'; import {JavaBase} from '../base-installer';
import { import {
JavaDownloadRelease, JavaDownloadRelease,
JavaInstallerOptions, JavaInstallerOptions,
JavaInstallerResults JavaInstallerResults
} from '../base-models'; } from '../base-models';
import { ISapMachineAllVersions, ISapMachineVersions } from './models'; import {ISapMachineAllVersions, ISapMachineVersions} from './models';
export class SapMachineDistribution extends JavaBase { export class SapMachineDistribution extends JavaBase {
constructor(installerOptions: JavaInstallerOptions) { constructor(installerOptions: JavaInstallerOptions) {
@ -27,10 +27,12 @@ export class SapMachineDistribution extends JavaBase {
protected async findPackageForDownload( protected async findPackageForDownload(
version: string version: string
): Promise<JavaDownloadRelease> { ): Promise<JavaDownloadRelease> {
core.debug(`Only stable versions: ${this.stable}`) core.debug(`Only stable versions: ${this.stable}`);
if (!['jdk', 'jre'].includes(this.packageType)) { if (!['jdk', 'jre'].includes(this.packageType)) {
throw new Error('SapMachine provides only the `jdk` and `jre` package type'); throw new Error(
'SapMachine provides only the `jdk` and `jre` package type'
);
} }
const availableVersions = await this.getAvailableVersions(); const availableVersions = await this.getAvailableVersions();
@ -60,10 +62,15 @@ export class SapMachineDistribution extends JavaBase {
const platform = this.getPlatformOption(); const platform = this.getPlatformOption();
const arch = this.distributionArchitecture(); const arch = this.distributionArchitecture();
let fetchedReleasesJson = await this.fetchReleasesFromUrl('https://sap.github.io/SapMachine/assets/data/sapmachine-releases-all.json') let fetchedReleasesJson = await this.fetchReleasesFromUrl(
'https://sap.github.io/SapMachine/assets/data/sapmachine-releases-all.json'
);
if (!fetchedReleasesJson) { if (!fetchedReleasesJson) {
fetchedReleasesJson = await this.fetchReleasesFromUrl('https://api.github.com/repos/SAP/SapMachine/contents/assets/data/sapmachine-releases-all.json?ref=gh-pages', getGitHubHttpHeaders()); fetchedReleasesJson = await this.fetchReleasesFromUrl(
'https://api.github.com/repos/SAP/SapMachine/contents/assets/data/sapmachine-releases-all.json?ref=gh-pages',
getGitHubHttpHeaders()
);
} }
if (!fetchedReleasesJson) { if (!fetchedReleasesJson) {
@ -117,29 +124,42 @@ export class SapMachineDistribution extends JavaBase {
this.architecture this.architecture
); );
return { version: javaRelease.version, path: javaPath }; return {version: javaRelease.version, path: javaPath};
} }
private parseVersions( private parseVersions(
platform: string, platform: string,
arch: string, arch: string,
versions: ISapMachineAllVersions, versions: ISapMachineAllVersions
): ISapMachineVersions[] { ): ISapMachineVersions[] {
const eligibleVersions: ISapMachineVersions[] = []; const eligibleVersions: ISapMachineVersions[] = [];
for (const [, majorVersionMap] of Object.entries(versions)) { for (const [, majorVersionMap] of Object.entries(versions)) {
for (const [, jdkVersionMap] of Object.entries(majorVersionMap.updates)) { for (const [, jdkVersionMap] of Object.entries(majorVersionMap.updates)) {
for (const [buildVersion, buildVersionMap] of Object.entries(jdkVersionMap)) { for (const [buildVersion, buildVersionMap] of Object.entries(
let buildVersionWithoutPrefix = buildVersion.replace('sapmachine-', ''); jdkVersionMap
)) {
let buildVersionWithoutPrefix = buildVersion.replace(
'sapmachine-',
''
);
if (!buildVersionWithoutPrefix.includes('.')) { if (!buildVersionWithoutPrefix.includes('.')) {
// replace major version with major.minor.patch and keep the remaining build identifier after the + as is with regex // replace major version with major.minor.patch and keep the remaining build identifier after the + as is with regex
buildVersionWithoutPrefix = buildVersionWithoutPrefix.replace(/(\d+)(\+.*)?/, '$1.0.0$2'); buildVersionWithoutPrefix = buildVersionWithoutPrefix.replace(
/(\d+)(\+.*)?/,
'$1.0.0$2'
);
} }
// replace + with . to convert to semver format if we have more than 3 version digits // replace + with . to convert to semver format if we have more than 3 version digits
if (buildVersionWithoutPrefix.split('.').length > 3) { if (buildVersionWithoutPrefix.split('.').length > 3) {
buildVersionWithoutPrefix = buildVersionWithoutPrefix.replace('+', '.'); buildVersionWithoutPrefix = buildVersionWithoutPrefix.replace(
'+',
'.'
);
} }
buildVersionWithoutPrefix = convertVersionToSemver(buildVersionWithoutPrefix); buildVersionWithoutPrefix = convertVersionToSemver(
buildVersionWithoutPrefix
);
// ignore invalid version // ignore invalid version
if (!semver.valid(buildVersionWithoutPrefix)) { if (!semver.valid(buildVersionWithoutPrefix)) {
@ -148,23 +168,29 @@ export class SapMachineDistribution extends JavaBase {
} }
// skip earlyAccessVersions if stable version requested // skip earlyAccessVersions if stable version requested
if (this.stable && buildVersionMap.ea === "true") { if (this.stable && buildVersionMap.ea === 'true') {
continue; continue;
} }
for (const [edition, editionAssets] of Object.entries(buildVersionMap.assets)) { for (const [edition, editionAssets] of Object.entries(
buildVersionMap.assets
)) {
if (this.packageType !== edition) { if (this.packageType !== edition) {
continue; continue;
} }
for (const [archAndPlatForm, archAssets] of Object.entries(editionAssets)) { for (const [archAndPlatForm, archAssets] of Object.entries(
let expectedArchAndPlatform = `${platform}-${arch}` editionAssets
)) {
let expectedArchAndPlatform = `${platform}-${arch}`;
if (platform === 'linux-musl') { if (platform === 'linux-musl') {
expectedArchAndPlatform = `linux-${arch}-musl` expectedArchAndPlatform = `linux-${arch}-musl`;
} }
if (archAndPlatForm !== expectedArchAndPlatform) { if (archAndPlatForm !== expectedArchAndPlatform) {
continue continue;
} }
for (const [contentType, contentTypeAssets] of Object.entries(archAssets)) { for (const [contentType, contentTypeAssets] of Object.entries(
archAssets
)) {
// skip if not tar.gz and zip files // skip if not tar.gz and zip files
if (contentType !== 'tar.gz' && contentType !== 'zip') { if (contentType !== 'tar.gz' && contentType !== 'zip') {
continue; continue;
@ -175,7 +201,7 @@ export class SapMachineDistribution extends JavaBase {
version: buildVersionWithoutPrefix, version: buildVersionWithoutPrefix,
checksum: contentTypeAssets.checksum, checksum: contentTypeAssets.checksum,
downloadLink: contentTypeAssets.url, downloadLink: contentTypeAssets.url,
packageType: edition, packageType: edition
}); });
} }
} }
@ -206,7 +232,7 @@ export class SapMachineDistribution extends JavaBase {
case 'win32': case 'win32':
return 'windows'; return 'windows';
case 'darwin': case 'darwin':
return 'macos' return 'macos';
case 'linux': case 'linux':
// figure out if alpine/musl // figure out if alpine/musl
if (fs.existsSync('/etc/alpine-release')) { if (fs.existsSync('/etc/alpine-release')) {
@ -218,7 +244,10 @@ export class SapMachineDistribution extends JavaBase {
} }
} }
private async fetchReleasesFromUrl(url: string, headers: OutgoingHttpHeaders = {}): Promise<ISapMachineAllVersions | null> { private async fetchReleasesFromUrl(
url: string,
headers: OutgoingHttpHeaders = {}
): Promise<ISapMachineAllVersions | null> {
try { try {
core.debug( core.debug(
`Trying to fetch available SapMachine versions info from the primary url: ${url}` `Trying to fetch available SapMachine versions info from the primary url: ${url}`
@ -229,7 +258,8 @@ export class SapMachineDistribution extends JavaBase {
return releases; return releases;
} catch (err) { } catch (err) {
core.debug( core.debug(
`Fetching SapMachine versions info from the link: ${url} ended up with the error: ${(err as Error).message `Fetching SapMachine versions info from the link: ${url} ended up with the error: ${
(err as Error).message
}` }`
); );
return null; return null;

View file

@ -1,33 +1,33 @@
export interface ISapMachineAllVersions { export interface ISapMachineAllVersions {
[major: string]: { [major: string]: {
lts: string, lts: string;
updates: { updates: {
[full_version: string]: { [full_version: string]: {
[sapmachineBuild: string]: { [sapmachineBuild: string]: {
release_url: string, release_url: string;
ea: string, ea: string;
assets: { assets: {
[packageType: string]: { [packageType: string]: {
[arch: string]: { [arch: string]: {
[content_type: string]: { [content_type: string]: {
name: string, name: string;
checksum: string, checksum: string;
url: string url: string;
};
};
};
};
}; };
};
}; };
};
}; };
};
}; };
};
} }
export interface ISapMachineVersions { export interface ISapMachineVersions {
os: string; os: string;
architecture: string; architecture: string;
version: string; version: string;
checksum: string; checksum: string;
downloadLink: string; downloadLink: string;
packageType: string; packageType: string;
} }