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

@ -1,5 +1,5 @@
import { HttpClient } from '@actions/http-client';
import { SapMachineDistribution } from '../../src/distributions/sapmachine/installer';
import {HttpClient} from '@actions/http-client';
import {SapMachineDistribution} from '../../src/distributions/sapmachine/installer';
import * as utils from '../../src/util';
import manifestData from '../data/sapmachine.json';
@ -43,13 +43,13 @@ describe('getAvailableVersions', () => {
spyHttpClient.mockReturnValueOnce({
statusCode: 404,
headers: {},
result: ""
})
result: ''
});
spyHttpClient.mockReturnValueOnce({
statusCode: 200,
headers: {},
result: manifestData
})
});
const version = '17';
const distribution = new SapMachineDistribution({
@ -65,8 +65,10 @@ describe('getAvailableVersions', () => {
version
);
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', () => {
@ -77,7 +79,7 @@ describe('getAvailableVersions', () => {
['16.0.1', 'x64', 'linux', 71],
['23-ea', 'x64', 'linux', 798],
['23-ea', 'aarch64', 'windows', 0],
['23-ea', 'x64', 'windows', 750],
['23-ea', 'x64', 'windows', 750]
])(
'should get right number of available versions from JSON',
async (
@ -209,7 +211,7 @@ describe('getAvailableVersions', () => {
'x64',
'jdk',
'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',
async (
@ -224,8 +226,7 @@ describe('getAvailableVersions', () => {
version: version,
architecture: arch,
packageType: packageType,
checkLatest: false,
checkLatest: false
});
mockPlatform(distribution, platform);
@ -247,15 +248,20 @@ describe('getAvailableVersions', () => {
['8-ea', 'linux', 'x64', '8'],
['21.0.3+7', 'linux', 'x64', '21.0.3+7'],
['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',
async (version: string, platform: string, arch: string, normalizedVersion: string = version) => {
async (
version: string,
platform: string,
arch: string,
normalizedVersion: string = version
) => {
const distribution = new SapMachineDistribution({
version: version,
architecture: arch,
packageType: 'jdk',
checkLatest: false,
checkLatest: false
});
mockPlatform(distribution, platform);
@ -280,7 +286,9 @@ describe('getAvailableVersions', () => {
mockPlatform(distribution, platform);
await expect(
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

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

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

@ -2,7 +2,7 @@ import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
import semver from 'semver';
import fs from 'fs';
import { OutgoingHttpHeaders } from 'http';
import {OutgoingHttpHeaders} from 'http';
import path from 'path';
import {
convertVersionToSemver,
@ -11,13 +11,13 @@ import {
getGitHubHttpHeaders,
isVersionSatisfies
} from '../../util';
import { JavaBase } from '../base-installer';
import {JavaBase} from '../base-installer';
import {
JavaDownloadRelease,
JavaInstallerOptions,
JavaInstallerResults
} from '../base-models';
import { ISapMachineAllVersions, ISapMachineVersions } from './models';
import {ISapMachineAllVersions, ISapMachineVersions} from './models';
export class SapMachineDistribution extends JavaBase {
constructor(installerOptions: JavaInstallerOptions) {
@ -27,10 +27,12 @@ export class SapMachineDistribution extends JavaBase {
protected async findPackageForDownload(
version: string
): Promise<JavaDownloadRelease> {
core.debug(`Only stable versions: ${this.stable}`)
core.debug(`Only stable versions: ${this.stable}`);
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();
@ -60,10 +62,15 @@ export class SapMachineDistribution extends JavaBase {
const platform = this.getPlatformOption();
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) {
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) {
@ -117,29 +124,42 @@ export class SapMachineDistribution extends JavaBase {
this.architecture
);
return { version: javaRelease.version, path: javaPath };
return {version: javaRelease.version, path: javaPath};
}
private parseVersions(
platform: string,
arch: string,
versions: ISapMachineAllVersions,
versions: ISapMachineAllVersions
): ISapMachineVersions[] {
const eligibleVersions: ISapMachineVersions[] = [];
for (const [, majorVersionMap] of Object.entries(versions)) {
for (const [, jdkVersionMap] of Object.entries(majorVersionMap.updates)) {
for (const [buildVersion, buildVersionMap] of Object.entries(jdkVersionMap)) {
let buildVersionWithoutPrefix = buildVersion.replace('sapmachine-', '');
for (const [buildVersion, buildVersionMap] of Object.entries(
jdkVersionMap
)) {
let buildVersionWithoutPrefix = buildVersion.replace(
'sapmachine-',
''
);
if (!buildVersionWithoutPrefix.includes('.')) {
// 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
if (buildVersionWithoutPrefix.split('.').length > 3) {
buildVersionWithoutPrefix = buildVersionWithoutPrefix.replace('+', '.');
buildVersionWithoutPrefix = buildVersionWithoutPrefix.replace(
'+',
'.'
);
}
buildVersionWithoutPrefix = convertVersionToSemver(buildVersionWithoutPrefix);
buildVersionWithoutPrefix = convertVersionToSemver(
buildVersionWithoutPrefix
);
// ignore invalid version
if (!semver.valid(buildVersionWithoutPrefix)) {
@ -148,23 +168,29 @@ export class SapMachineDistribution extends JavaBase {
}
// skip earlyAccessVersions if stable version requested
if (this.stable && buildVersionMap.ea === "true") {
if (this.stable && buildVersionMap.ea === 'true') {
continue;
}
for (const [edition, editionAssets] of Object.entries(buildVersionMap.assets)) {
for (const [edition, editionAssets] of Object.entries(
buildVersionMap.assets
)) {
if (this.packageType !== edition) {
continue;
}
for (const [archAndPlatForm, archAssets] of Object.entries(editionAssets)) {
let expectedArchAndPlatform = `${platform}-${arch}`
for (const [archAndPlatForm, archAssets] of Object.entries(
editionAssets
)) {
let expectedArchAndPlatform = `${platform}-${arch}`;
if (platform === 'linux-musl') {
expectedArchAndPlatform = `linux-${arch}-musl`
expectedArchAndPlatform = `linux-${arch}-musl`;
}
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
if (contentType !== 'tar.gz' && contentType !== 'zip') {
continue;
@ -175,7 +201,7 @@ export class SapMachineDistribution extends JavaBase {
version: buildVersionWithoutPrefix,
checksum: contentTypeAssets.checksum,
downloadLink: contentTypeAssets.url,
packageType: edition,
packageType: edition
});
}
}
@ -206,7 +232,7 @@ export class SapMachineDistribution extends JavaBase {
case 'win32':
return 'windows';
case 'darwin':
return 'macos'
return 'macos';
case 'linux':
// figure out if alpine/musl
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 {
core.debug(
`Trying to fetch available SapMachine versions info from the primary url: ${url}`
@ -229,7 +258,8 @@ export class SapMachineDistribution extends JavaBase {
return releases;
} catch (err) {
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;

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