Skip to content

Commit b24df5b

Browse files
authored
Make the Adoptopenjdk package type look at the Temurin repo first for latest assets (#522)
* Make the Adoptopenjdk package type look at the Temurin repo first for latest assets * Address Copilot code review comments - Use strict equality (===, !==) instead of loose equality (==, !=) for all comparisons - Properly handle caught errors with instanceof type narrowing before accessing properties - Only fall back to legacy AdoptOpenJDK for specific version-not-found errors - Rethrow unexpected errors to avoid masking real issues (network failures, rate limits, etc.) - Fix error message check to match actual error text ('No matching version found') - Remove unnecessary undefined check since method return type is never undefined - Add @internal JSDoc annotation to TemurinDistribution.findPackageForDownload() - Update tests to properly mock Temurin lookup failures for fallback behavior testing - Rebuild dist files * Always fall back to legacy AdoptOpenJDK but log all Temurin failures - Change error handling to gracefully fall back for all errors, not just version-not-found - Log version-not-found errors as notices with migration guidance - Log other Temurin failures as debug messages for troubleshooting - Improves resilience: users always get a result even if Temurin API has issues - Maintains visibility: failures are still logged for debugging * Fixes from review * Fixes from review * Fixes from review * Regenerate dist
1 parent 43120bc commit b24df5b

6 files changed

Lines changed: 163 additions & 20 deletions

File tree

__tests__/distributors/adopt-installer.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
AdoptDistribution,
55
AdoptImplementation
66
} from '../../src/distributions/adopt/installer';
7+
import {TemurinDistribution} from '../../src/distributions/temurin/installer';
78
import {JavaInstallerOptions} from '../../src/distributions/base-models';
89

910
import os from 'os';
@@ -256,6 +257,38 @@ describe('getAvailableVersions', () => {
256257
});
257258

258259
describe('findPackageForDownload', () => {
260+
it('returns Temurin result and does not query Adopt API when Temurin succeeds', async () => {
261+
const temurinRelease = {
262+
version: '11.0.31+11',
263+
url: 'https://example.test/temurin-11.tar.gz'
264+
};
265+
const temurinFindPackageForDownload = jest
266+
.fn()
267+
.mockResolvedValue(temurinRelease);
268+
const temurinDistribution = {
269+
findPackageForDownload: temurinFindPackageForDownload
270+
} as unknown as TemurinDistribution;
271+
272+
const distribution = new AdoptDistribution(
273+
{
274+
version: '11',
275+
architecture: 'x64',
276+
packageType: 'jdk',
277+
checkLatest: false
278+
},
279+
AdoptImplementation.Hotspot,
280+
temurinDistribution
281+
);
282+
const adoptLookupSpy = jest.fn();
283+
distribution['getAvailableVersions'] = adoptLookupSpy;
284+
285+
const resolvedVersion = await distribution['findPackageForDownload']('11');
286+
287+
expect(resolvedVersion).toEqual(temurinRelease);
288+
expect(temurinFindPackageForDownload).toHaveBeenCalledWith('11');
289+
expect(adoptLookupSpy).not.toHaveBeenCalled();
290+
});
291+
259292
it.each([
260293
['9', '9.0.7+10'],
261294
['15', '15.0.2+7'],
@@ -278,6 +311,11 @@ describe('findPackageForDownload', () => {
278311
},
279312
AdoptImplementation.Hotspot
280313
);
314+
// Mock Temurin to fail so fallback to AdoptOpenJDK is tested
315+
distribution['temurinDistribution']!['findPackageForDownload'] =
316+
async () => {
317+
throw new Error('No matching version found for SemVer');
318+
};
281319
distribution['getAvailableVersions'] = async () => manifestData as any;
282320
const resolvedVersion = await distribution['findPackageForDownload'](input);
283321
expect(resolvedVersion.version).toBe(expected);
@@ -293,6 +331,11 @@ describe('findPackageForDownload', () => {
293331
},
294332
AdoptImplementation.Hotspot
295333
);
334+
// Mock Temurin to fail so fallback to AdoptOpenJDK is tested
335+
distribution['temurinDistribution']!['findPackageForDownload'] =
336+
async () => {
337+
throw new Error('No matching version found for SemVer');
338+
};
296339
distribution['getAvailableVersions'] = async () => manifestData as any;
297340
await expect(
298341
distribution['findPackageForDownload']('9.0.8')
@@ -309,6 +352,11 @@ describe('findPackageForDownload', () => {
309352
},
310353
AdoptImplementation.Hotspot
311354
);
355+
// Mock Temurin to fail so fallback to AdoptOpenJDK is tested
356+
distribution['temurinDistribution']!['findPackageForDownload'] =
357+
async () => {
358+
throw new Error('No matching version found for SemVer');
359+
};
312360
distribution['getAvailableVersions'] = async () => manifestData as any;
313361
await expect(distribution['findPackageForDownload']('7.x')).rejects.toThrow(
314362
/No matching version found for SemVer */
@@ -325,6 +373,11 @@ describe('findPackageForDownload', () => {
325373
},
326374
AdoptImplementation.Hotspot
327375
);
376+
// Mock Temurin to fail so fallback to AdoptOpenJDK is tested
377+
distribution['temurinDistribution']!['findPackageForDownload'] =
378+
async () => {
379+
throw new Error('No matching version found for SemVer');
380+
};
328381
distribution['getAvailableVersions'] = async () => [];
329382
await expect(distribution['findPackageForDownload']('11')).rejects.toThrow(
330383
/No matching version found for SemVer */

dist/setup/index.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77815,17 +77815,54 @@ const path_1 = __importDefault(__nccwpck_require__(71017));
7781577815
const semver_1 = __importDefault(__nccwpck_require__(11383));
7781677816
const base_installer_1 = __nccwpck_require__(59741);
7781777817
const util_1 = __nccwpck_require__(92629);
77818+
const installer_1 = __nccwpck_require__(18579);
7781877819
var AdoptImplementation;
7781977820
(function (AdoptImplementation) {
7782077821
AdoptImplementation["Hotspot"] = "Hotspot";
7782177822
AdoptImplementation["OpenJ9"] = "OpenJ9";
7782277823
})(AdoptImplementation || (exports.AdoptImplementation = AdoptImplementation = {}));
7782377824
class AdoptDistribution extends base_installer_1.JavaBase {
77824-
constructor(installerOptions, jvmImpl) {
77825+
constructor(installerOptions, jvmImpl, temurinDistribution = null) {
7782577826
super(`Adopt-${jvmImpl}`, installerOptions);
7782677827
this.jvmImpl = jvmImpl;
77828+
if (temurinDistribution !== null &&
77829+
jvmImpl !== AdoptImplementation.Hotspot) {
77830+
throw new Error('Only Hotspot JVM is supported by Temurin.');
77831+
}
77832+
// Only use the temurin repo for Hotspot JVMs
77833+
this.temurinDistribution =
77834+
temurinDistribution !== null && temurinDistribution !== void 0 ? temurinDistribution : (jvmImpl === AdoptImplementation.Hotspot
77835+
? new installer_1.TemurinDistribution(installerOptions, installer_1.TemurinImplementation.Hotspot)
77836+
: null);
7782777837
}
7782877838
findPackageForDownload(version) {
77839+
return __awaiter(this, void 0, void 0, function* () {
77840+
if (this.jvmImpl === AdoptImplementation.Hotspot) {
77841+
core.notice("AdoptOpenJDK has moved to Eclipse Temurin https://github.com/actions/setup-java#supported-distributions please consider changing to the 'temurin' distribution type in your setup-java configuration.");
77842+
}
77843+
if (this.jvmImpl === AdoptImplementation.Hotspot &&
77844+
this.temurinDistribution !== null) {
77845+
try {
77846+
return yield this.temurinDistribution.findPackageForDownload(version);
77847+
}
77848+
catch (error) {
77849+
// Log the failure but always fall back to legacy AdoptOpenJDK for resilience
77850+
const errorMessage = error instanceof Error ? error.message : String(error);
77851+
if (error instanceof Error && error.name === 'VersionNotFoundError') {
77852+
core.notice('The JVM you are looking for could not be found in the Temurin repository, this likely indicates ' +
77853+
'that you are using an out of date version of Java, consider updating and moving to using the Temurin distribution type in setup-java.');
77854+
}
77855+
else {
77856+
// Log other errors for debugging but gracefully fall back
77857+
core.debug(`Temurin lookup failed: ${errorMessage}. Falling back to AdoptOpenJDK API.`);
77858+
}
77859+
}
77860+
}
77861+
// failed to find a Temurin version, so fall back to AdoptOpenJDK
77862+
return this.findPackageForDownloadOldAdoptOpenJdk(version);
77863+
});
77864+
}
77865+
findPackageForDownloadOldAdoptOpenJdk(version) {
7782977866
return __awaiter(this, void 0, void 0, function* () {
7783077867
const availableVersionsRaw = yield this.getAvailableVersions();
7783177868
const availableVersionsWithBinaries = availableVersionsRaw
@@ -78223,7 +78260,9 @@ class JavaBase {
7822378260
parts.push(`(showing first ${maxVersionsToShow} of ${availableVersions.length} versions, enable debug mode to see all)`);
7822478261
}
7822578262
}
78226-
return new Error(parts.join('\n'));
78263+
const error = new Error(parts.join('\n'));
78264+
error.name = 'VersionNotFoundError';
78265+
return error;
7822778266
}
7822878267
setJavaDefault(version, toolPath) {
7822978268
const majorVersion = version.split('.')[0];
@@ -80195,6 +80234,9 @@ class TemurinDistribution extends base_installer_1.JavaBase {
8019580234
super(`Temurin-${jvmImpl}`, installerOptions);
8019680235
this.jvmImpl = jvmImpl;
8019780236
}
80237+
/**
80238+
* @internal For cross-distribution reuse only. Not intended as a public API.
80239+
*/
8019880240
findPackageForDownload(version) {
8019980241
return __awaiter(this, void 0, void 0, function* () {
8020080242
const availableVersionsRaw = yield this.getAvailableVersions();

package-lock.json

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/distributions/adopt/installer.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,80 @@ import {
2121
MAX_PAGINATION_PAGES,
2222
validatePaginationUrl
2323
} from '../../util';
24+
import {TemurinDistribution, TemurinImplementation} from '../temurin/installer';
2425

2526
export enum AdoptImplementation {
2627
Hotspot = 'Hotspot',
2728
OpenJ9 = 'OpenJ9'
2829
}
2930

3031
export class AdoptDistribution extends JavaBase {
32+
private readonly temurinDistribution: TemurinDistribution | null;
33+
3134
constructor(
3235
installerOptions: JavaInstallerOptions,
33-
private readonly jvmImpl: AdoptImplementation
36+
private readonly jvmImpl: AdoptImplementation,
37+
temurinDistribution: TemurinDistribution | null = null
3438
) {
3539
super(`Adopt-${jvmImpl}`, installerOptions);
40+
41+
if (
42+
temurinDistribution !== null &&
43+
jvmImpl !== AdoptImplementation.Hotspot
44+
) {
45+
throw new Error('Only Hotspot JVM is supported by Temurin.');
46+
}
47+
48+
// Only use the temurin repo for Hotspot JVMs
49+
this.temurinDistribution =
50+
temurinDistribution ??
51+
(jvmImpl === AdoptImplementation.Hotspot
52+
? new TemurinDistribution(
53+
installerOptions,
54+
TemurinImplementation.Hotspot
55+
)
56+
: null);
3657
}
3758

3859
protected async findPackageForDownload(
3960
version: string
61+
): Promise<JavaDownloadRelease> {
62+
if (this.jvmImpl === AdoptImplementation.Hotspot) {
63+
core.notice(
64+
"AdoptOpenJDK has moved to Eclipse Temurin https://github.com/actions/setup-java#supported-distributions please consider changing to the 'temurin' distribution type in your setup-java configuration."
65+
);
66+
}
67+
68+
if (
69+
this.jvmImpl === AdoptImplementation.Hotspot &&
70+
this.temurinDistribution !== null
71+
) {
72+
try {
73+
return await this.temurinDistribution.findPackageForDownload(version);
74+
} catch (error) {
75+
// Log the failure but always fall back to legacy AdoptOpenJDK for resilience
76+
const errorMessage =
77+
error instanceof Error ? error.message : String(error);
78+
if (error instanceof Error && error.name === 'VersionNotFoundError') {
79+
core.notice(
80+
'The JVM you are looking for could not be found in the Temurin repository, this likely indicates ' +
81+
'that you are using an out of date version of Java, consider updating and moving to using the Temurin distribution type in setup-java.'
82+
);
83+
} else {
84+
// Log other errors for debugging but gracefully fall back
85+
core.debug(
86+
`Temurin lookup failed: ${errorMessage}. Falling back to AdoptOpenJDK API.`
87+
);
88+
}
89+
}
90+
}
91+
92+
// failed to find a Temurin version, so fall back to AdoptOpenJDK
93+
return this.findPackageForDownloadOldAdoptOpenJdk(version);
94+
}
95+
96+
private async findPackageForDownloadOldAdoptOpenJdk(
97+
version: string
4098
): Promise<JavaDownloadRelease> {
4199
const availableVersionsRaw = await this.getAvailableVersions();
42100
const availableVersionsWithBinaries = availableVersionsRaw

src/distributions/base-installer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,9 @@ export abstract class JavaBase {
292292
}
293293
}
294294

295-
return new Error(parts.join('\n'));
295+
const error = new Error(parts.join('\n'));
296+
error.name = 'VersionNotFoundError';
297+
return error;
296298
}
297299

298300
protected setJavaDefault(version: string, toolPath: string) {

src/distributions/temurin/installer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ export class TemurinDistribution extends JavaBase {
3434
super(`Temurin-${jvmImpl}`, installerOptions);
3535
}
3636

37-
protected async findPackageForDownload(
37+
/**
38+
* @internal For cross-distribution reuse only. Not intended as a public API.
39+
*/
40+
public async findPackageForDownload(
3841
version: string
3942
): Promise<JavaDownloadRelease> {
4043
const availableVersionsRaw = await this.getAvailableVersions();

0 commit comments

Comments
 (0)