Skip to content

Commit a18c333

Browse files
Add versions-manifest.json for Microsoft Build of OpenJDK (actions#383)
1 parent 26eeac8 commit a18c333

File tree

6 files changed

+102
-163
lines changed

6 files changed

+102
-163
lines changed
Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { MicrosoftDistributions } from '../../src/distributions/microsoft/installer';
2+
import * as tc from '@actions/tool-cache';
3+
import data from '../../src/distributions/microsoft/microsoft-openjdk-versions.json';
4+
import * as httpm from '@actions/http-client';
5+
import * as core from '@actions/core';
26

37
describe('findPackageForDownload', () => {
48
let distribution: MicrosoftDistributions;
9+
let spyGetManifestFromRepo: jest.SpyInstance;
10+
let spyDebug: jest.SpyInstance;
511

612
beforeEach(() => {
713
distribution = new MicrosoftDistributions({
@@ -10,12 +16,22 @@ describe('findPackageForDownload', () => {
1016
packageType: 'jdk',
1117
checkLatest: false
1218
});
19+
20+
spyGetManifestFromRepo = jest.spyOn(httpm.HttpClient.prototype, 'getJson');
21+
spyGetManifestFromRepo.mockReturnValue({
22+
result: data,
23+
statusCode: 200,
24+
headers: {}
25+
});
26+
27+
spyDebug = jest.spyOn(core, 'debug');
28+
spyDebug.mockImplementation(() => {});
1329
});
1430

1531
it.each([
1632
[
1733
'17.0.1',
18-
'17.0.1',
34+
'17.0.1+12.1',
1935
'https://aka.ms/download-jdk/microsoft-jdk-17.0.1.12.1-{{OS_TYPE}}-x64.{{ARCHIVE_TYPE}}'
2036
],
2137
[
@@ -25,12 +41,12 @@ describe('findPackageForDownload', () => {
2541
],
2642
[
2743
'16.0.x',
28-
'16.0.2',
44+
'16.0.2+7.1',
2945
'https://aka.ms/download-jdk/microsoft-jdk-16.0.2.7.1-{{OS_TYPE}}-x64.{{ARCHIVE_TYPE}}'
3046
],
3147
[
3248
'11.0.13',
33-
'11.0.13',
49+
'11.0.13+8.1',
3450
'https://aka.ms/download-jdk/microsoft-jdk-11.0.13.8.1-{{OS_TYPE}}-x64.{{ARCHIVE_TYPE}}'
3551
],
3652
[
@@ -67,32 +83,3 @@ describe('findPackageForDownload', () => {
6783
);
6884
});
6985
});
70-
71-
describe('getPlatformOption', () => {
72-
const distributions = new MicrosoftDistributions({
73-
architecture: 'x64',
74-
version: '11',
75-
packageType: 'jdk',
76-
checkLatest: false
77-
});
78-
79-
it.each([
80-
['linux', 'tar.gz', 'linux'],
81-
['darwin', 'tar.gz', 'macos'],
82-
['win32', 'zip', 'windows']
83-
])('os version %s -> %s', (input, expectedArchive, expectedOs) => {
84-
const actual = distributions['getPlatformOption'](input as NodeJS.Platform);
85-
86-
expect(actual.archive).toEqual(expectedArchive);
87-
expect(actual.os).toEqual(expectedOs);
88-
});
89-
90-
it.each(['aix', 'android', 'freebsd', 'openbsd', 'netbsd', 'solaris', 'cygwin'])(
91-
'not support os version %s',
92-
input => {
93-
expect(() => distributions['getPlatformOption'](input as NodeJS.Platform)).toThrow(
94-
/Platform '\w+' is not supported\. Supported platforms: .+/
95-
);
96-
}
97-
);
98-
});

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ inputs:
5959
job-status:
6060
description: 'Workaround to pass job status to post job step. This variable is not intended for manual setting'
6161
default: ${{ job.status }}
62+
token:
63+
description: Used to pull java versions from setup-java. Since there is a default value, token is typically not supplied by the user.
64+
default: ${{ github.token }}
6265
outputs:
6366
distribution:
6467
description: 'Distribution of Java that has been installed'

dist/setup/index.js

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -104405,7 +104405,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
104405104405
Object.defineProperty(exports, "__esModule", ({ value: true }));
104406104406
exports.MicrosoftDistributions = void 0;
104407104407
const base_installer_1 = __nccwpck_require__(9741);
104408-
const semver_1 = __importDefault(__nccwpck_require__(1383));
104409104408
const util_1 = __nccwpck_require__(2629);
104410104409
const core = __importStar(__nccwpck_require__(2186));
104411104410
const tc = __importStar(__nccwpck_require__(7784));
@@ -104439,70 +104438,51 @@ class MicrosoftDistributions extends base_installer_1.JavaBase {
104439104438
if (this.packageType !== 'jdk') {
104440104439
throw new Error('Microsoft Build of OpenJDK provides only the `jdk` package type');
104441104440
}
104442-
const availableVersionsRaw = yield this.getAvailableVersions();
104443-
const opts = this.getPlatformOption();
104444-
const availableVersions = availableVersionsRaw.map(item => ({
104445-
url: `https://aka.ms/download-jdk/microsoft-jdk-${item.version.join('.')}-${opts.os}-${this.architecture}.${opts.archive}`,
104446-
version: this.convertVersionToSemver(item)
104447-
}));
104448-
const satisfiedVersion = availableVersions
104449-
.filter(item => util_1.isVersionSatisfies(range, item.version))
104450-
.sort((a, b) => -semver_1.default.compareBuild(a.version, b.version))[0];
104451-
if (!satisfiedVersion) {
104452-
const availableOptions = availableVersions.map(item => item.version).join(', ');
104453-
const availableOptionsMessage = availableOptions
104454-
? `\nAvailable versions: ${availableOptions}`
104455-
: '';
104456-
throw new Error(`Could not find satisfied version for SemVer ${range}. ${availableOptionsMessage}`);
104441+
const manifest = yield this.getAvailableVersions();
104442+
if (!manifest) {
104443+
throw new Error('Could not load manifest for Microsoft Build of OpenJDK');
104457104444
}
104458-
return satisfiedVersion;
104445+
const foundRelease = yield tc.findFromManifest(range, true, manifest, this.architecture);
104446+
if (!foundRelease) {
104447+
throw new Error(`Could not find satisfied version for SemVer ${range}. ${manifest
104448+
.map(item => item.version)
104449+
.join(', ')}`);
104450+
}
104451+
return { url: foundRelease.files[0].download_url, version: foundRelease.version };
104459104452
});
104460104453
}
104461104454
getAvailableVersions() {
104462104455
return __awaiter(this, void 0, void 0, function* () {
104463104456
// TODO get these dynamically!
104464104457
// We will need Microsoft to add an endpoint where we can query for versions.
104465-
const jdkVersions = [
104466-
{
104467-
version: [17, 0, 3]
104468-
},
104469-
{
104470-
version: [17, 0, 1, 12, 1]
104471-
},
104472-
{
104473-
version: [16, 0, 2, 7, 1]
104474-
},
104475-
{
104476-
version: [11, 0, 15]
104458+
const token = core.getInput('token');
104459+
const owner = 'actions';
104460+
const repository = 'setup-java';
104461+
const branch = 'main';
104462+
const filePath = 'src/distributions/microsoft/microsoft-openjdk-versions.json';
104463+
let releases = null;
104464+
const fileUrl = `https://api.github.com/repos/${owner}/${repository}/contents/${filePath}?ref=${branch}`;
104465+
const headers = {
104466+
authorization: token,
104467+
accept: 'application/vnd.github.VERSION.raw'
104468+
};
104469+
let response = null;
104470+
try {
104471+
response = yield this.http.getJson(fileUrl, headers);
104472+
if (!response.result) {
104473+
return null;
104477104474
}
104478-
];
104479-
// M1 is only supported for Java 16 & 17
104480-
if (process.platform !== 'darwin' || this.architecture !== 'aarch64') {
104481-
jdkVersions.push({
104482-
version: [11, 0, 13, 8, 1]
104483-
});
104484104475
}
104485-
return jdkVersions;
104476+
catch (err) {
104477+
core.debug(`Http request for microsoft-openjdk-versions.json failed with status code: ${response === null || response === void 0 ? void 0 : response.statusCode}`);
104478+
return null;
104479+
}
104480+
if (response.result) {
104481+
releases = response.result;
104482+
}
104483+
return releases;
104486104484
});
104487104485
}
104488-
getPlatformOption(platform = process.platform /* for testing */) {
104489-
switch (platform) {
104490-
case 'darwin':
104491-
return { archive: 'tar.gz', os: 'macos' };
104492-
case 'win32':
104493-
return { archive: 'zip', os: 'windows' };
104494-
case 'linux':
104495-
return { archive: 'tar.gz', os: 'linux' };
104496-
default:
104497-
throw new Error(`Platform '${platform}' is not supported. Supported platforms: 'darwin', 'linux', 'win32'`);
104498-
}
104499-
}
104500-
convertVersionToSemver(version) {
104501-
const major = version.version[0];
104502-
const minor = version.version[1];
104503-
const patch = version.version[2];
104504-
return `${major}.${minor}.${patch}`;
104505-
}
104506104486
}
104507104487
exports.MicrosoftDistributions = MicrosoftDistributions;
104508104488

Lines changed: 45 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { JavaBase } from '../base-installer';
22
import { JavaDownloadRelease, JavaInstallerOptions, JavaInstallerResults } from '../base-models';
3-
import semver from 'semver';
4-
import { extractJdkFile, getDownloadArchiveExtension, isVersionSatisfies } from '../../util';
3+
import { extractJdkFile, getDownloadArchiveExtension } from '../../util';
54
import * as core from '@actions/core';
6-
import { MicrosoftVersion, PlatformOptions } from './models';
75
import * as tc from '@actions/tool-cache';
6+
import { OutgoingHttpHeaders } from 'http';
87
import fs from 'fs';
98
import path from 'path';
9+
import { ITypedResponse } from '@actions/http-client/interfaces';
1010

1111
export class MicrosoftDistributions extends JavaBase {
1212
constructor(installerOptions: JavaInstallerOptions) {
@@ -49,82 +49,60 @@ export class MicrosoftDistributions extends JavaBase {
4949
throw new Error('Microsoft Build of OpenJDK provides only the `jdk` package type');
5050
}
5151

52-
const availableVersionsRaw = await this.getAvailableVersions();
53-
54-
const opts = this.getPlatformOption();
55-
const availableVersions = availableVersionsRaw.map(item => ({
56-
url: `https://aka.ms/download-jdk/microsoft-jdk-${item.version.join('.')}-${opts.os}-${
57-
this.architecture
58-
}.${opts.archive}`,
59-
version: this.convertVersionToSemver(item)
60-
}));
61-
62-
const satisfiedVersion = availableVersions
63-
.filter(item => isVersionSatisfies(range, item.version))
64-
.sort((a, b) => -semver.compareBuild(a.version, b.version))[0];
65-
66-
if (!satisfiedVersion) {
67-
const availableOptions = availableVersions.map(item => item.version).join(', ');
68-
const availableOptionsMessage = availableOptions
69-
? `\nAvailable versions: ${availableOptions}`
70-
: '';
52+
const manifest = await this.getAvailableVersions();
53+
54+
if (!manifest) {
55+
throw new Error('Could not load manifest for Microsoft Build of OpenJDK');
56+
}
57+
58+
const foundRelease = await tc.findFromManifest(range, true, manifest, this.architecture);
59+
60+
if (!foundRelease) {
7161
throw new Error(
72-
`Could not find satisfied version for SemVer ${range}. ${availableOptionsMessage}`
62+
`Could not find satisfied version for SemVer ${range}. ${manifest
63+
.map(item => item.version)
64+
.join(', ')}`
7365
);
7466
}
7567

76-
return satisfiedVersion;
68+
return { url: foundRelease.files[0].download_url, version: foundRelease.version };
7769
}
7870

79-
private async getAvailableVersions(): Promise<MicrosoftVersion[]> {
71+
private async getAvailableVersions(): Promise<tc.IToolRelease[] | null> {
8072
// TODO get these dynamically!
8173
// We will need Microsoft to add an endpoint where we can query for versions.
82-
const jdkVersions = [
83-
{
84-
version: [17, 0, 3]
85-
},
86-
{
87-
version: [17, 0, 1, 12, 1]
88-
},
89-
{
90-
version: [16, 0, 2, 7, 1]
91-
},
92-
{
93-
version: [11, 0, 15]
74+
const token = core.getInput('token');
75+
const owner = 'actions';
76+
const repository = 'setup-java';
77+
const branch = 'main';
78+
const filePath = 'src/distributions/microsoft/microsoft-openjdk-versions.json';
79+
80+
let releases: tc.IToolRelease[] | null = null;
81+
const fileUrl = `https://api.github.com/repos/${owner}/${repository}/contents/${filePath}?ref=${branch}`;
82+
83+
const headers: OutgoingHttpHeaders = {
84+
authorization: token,
85+
accept: 'application/vnd.github.VERSION.raw'
86+
};
87+
88+
let response: ITypedResponse<tc.IToolRelease[]> | null = null;
89+
90+
try {
91+
response = await this.http.getJson<tc.IToolRelease[]>(fileUrl, headers);
92+
if (!response.result) {
93+
return null;
9494
}
95-
];
96-
97-
// M1 is only supported for Java 16 & 17
98-
if (process.platform !== 'darwin' || this.architecture !== 'aarch64') {
99-
jdkVersions.push({
100-
version: [11, 0, 13, 8, 1]
101-
});
95+
} catch (err) {
96+
core.debug(
97+
`Http request for microsoft-openjdk-versions.json failed with status code: ${response?.statusCode}`
98+
);
99+
return null;
102100
}
103101

104-
return jdkVersions;
105-
}
106-
107-
private getPlatformOption(
108-
platform: NodeJS.Platform = process.platform /* for testing */
109-
): PlatformOptions {
110-
switch (platform) {
111-
case 'darwin':
112-
return { archive: 'tar.gz', os: 'macos' };
113-
case 'win32':
114-
return { archive: 'zip', os: 'windows' };
115-
case 'linux':
116-
return { archive: 'tar.gz', os: 'linux' };
117-
default:
118-
throw new Error(
119-
`Platform '${platform}' is not supported. Supported platforms: 'darwin', 'linux', 'win32'`
120-
);
102+
if (response.result) {
103+
releases = response.result;
121104
}
122-
}
123105

124-
private convertVersionToSemver(version: MicrosoftVersion): string {
125-
const major = version.version[0];
126-
const minor = version.version[1];
127-
const patch = version.version[2];
128-
return `${major}.${minor}.${patch}`;
106+
return releases;
129107
}
130108
}

src/distributions/microsoft/models.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,2 @@
11
type OsVersions = 'linux' | 'macos' | 'windows';
22
type ArchiveType = 'tar.gz' | 'zip';
3-
4-
export interface PlatformOptions {
5-
archive: ArchiveType;
6-
os: OsVersions;
7-
}
8-
9-
export interface MicrosoftVersion {
10-
downloadUrl?: string;
11-
version: Array<number>;
12-
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
77
// "allowJs": true, /* Allow javascript files to be compiled. */
88
// "checkJs": true, /* Report errors in .js files. */
9+
"resolveJsonModule": true,
910
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
1011
// "declaration": true, /* Generates corresponding '.d.ts' file. */
1112
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */

0 commit comments

Comments
 (0)