Skip to content

Commit

Permalink
basic validation failure fix (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
aparnajyothi-y authored Sep 6, 2024
1 parent 7467385 commit 2dfa201
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 67 deletions.
38 changes: 23 additions & 15 deletions __tests__/distributors/sapmachine-installer.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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({
Expand All @@ -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', () => {
Expand All @@ -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 (
Expand Down Expand Up @@ -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 (
Expand All @@ -224,8 +226,7 @@ describe('getAvailableVersions', () => {
version: version,
architecture: arch,
packageType: packageType,
checkLatest: false,

checkLatest: false
});
mockPlatform(distribution, platform);

Expand All @@ -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);

Expand All @@ -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 changes: 2 additions & 2 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -125038,7 +125038,7 @@ class SapMachineDistribution extends base_installer_1.JavaBase {
version: buildVersionWithoutPrefix,
checksum: contentTypeAssets.checksum,
downloadLink: contentTypeAssets.url,
packageType: edition,
packageType: edition
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/distributions/distribution-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ enum JavaDistribution {
Corretto = 'corretto',
Oracle = 'oracle',
Dragonwell = 'dragonwell',
SapMachine = 'sapmachine',
SapMachine = 'sapmachine'
}

export function getJavaDistribution(
Expand Down
80 changes: 55 additions & 25 deletions src/distributions/sapmachine/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
Expand All @@ -175,7 +201,7 @@ export class SapMachineDistribution extends JavaBase {
version: buildVersionWithoutPrefix,
checksum: contentTypeAssets.checksum,
downloadLink: contentTypeAssets.url,
packageType: edition,
packageType: edition
});
}
}
Expand Down Expand Up @@ -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')) {
Expand All @@ -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}`
Expand All @@ -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;
Expand Down
48 changes: 24 additions & 24 deletions src/distributions/sapmachine/models.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 2dfa201

Please sign in to comment.