|
1 | 1 | import { JavaBase } from '../base-installer'; |
2 | 2 | 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'; |
5 | 4 | import * as core from '@actions/core'; |
6 | | -import { MicrosoftVersion, PlatformOptions } from './models'; |
7 | 5 | import * as tc from '@actions/tool-cache'; |
| 6 | +import { OutgoingHttpHeaders } from 'http'; |
8 | 7 | import fs from 'fs'; |
9 | 8 | import path from 'path'; |
| 9 | +import { ITypedResponse } from '@actions/http-client/interfaces'; |
10 | 10 |
|
11 | 11 | export class MicrosoftDistributions extends JavaBase { |
12 | 12 | constructor(installerOptions: JavaInstallerOptions) { |
@@ -49,82 +49,60 @@ export class MicrosoftDistributions extends JavaBase { |
49 | 49 | throw new Error('Microsoft Build of OpenJDK provides only the `jdk` package type'); |
50 | 50 | } |
51 | 51 |
|
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) { |
71 | 61 | 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(', ')}` |
73 | 65 | ); |
74 | 66 | } |
75 | 67 |
|
76 | | - return satisfiedVersion; |
| 68 | + return { url: foundRelease.files[0].download_url, version: foundRelease.version }; |
77 | 69 | } |
78 | 70 |
|
79 | | - private async getAvailableVersions(): Promise<MicrosoftVersion[]> { |
| 71 | + private async getAvailableVersions(): Promise<tc.IToolRelease[] | null> { |
80 | 72 | // TODO get these dynamically! |
81 | 73 | // 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; |
94 | 94 | } |
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; |
102 | 100 | } |
103 | 101 |
|
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; |
121 | 104 | } |
122 | | - } |
123 | 105 |
|
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; |
129 | 107 | } |
130 | 108 | } |
0 commit comments