Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check-latest flag #141

Merged
23 changes: 23 additions & 0 deletions .github/workflows/e2e-versions.yml
Expand Up @@ -69,6 +69,29 @@ jobs:
- name: Verify Java
run: bash __tests__/verify-java.sh "${{ matrix.version }}" "${{ steps.setup-java.outputs.path }}"
shell: bash

setup-java-check-latest:
name: ${{ matrix.distribution }} ${{ matrix.version }} - check-latest flag - ${{ matrix.os }}
needs: setup-java-major-versions
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
distribution: ['adopt', 'zulu']
steps:
- name: Checkout
uses: actions/checkout@v2
- name: setup-java
uses: ./
id: setup-java
with:
distribution: ${{ matrix.distribution }}
java-version: 11
check-latest: true
- name: Verify Java
run: bash __tests__/verify-java.sh "11" "${{ steps.setup-java.outputs.path }}"
shell: bash

setup-java-ea-versions-zulu:
name: zulu ${{ matrix.version }} (jdk-x64) - ${{ matrix.os }}
Expand Down
100 changes: 82 additions & 18 deletions __tests__/distributors/base-installer.test.ts
Expand Up @@ -19,13 +19,13 @@ class EmptyJavaBase extends JavaBase {

protected async downloadTool(javaRelease: JavaDownloadRelease): Promise<JavaInstallerResults> {
return {
version: '11.0.8',
path: `/toolcache/${this.toolcacheFolderName}/11.0.8/${this.architecture}`
version: '11.0.9',
path: path.join('toolcache', this.toolcacheFolderName, '11.0.9', this.architecture)
};
}

protected async findPackageForDownload(range: string): Promise<JavaDownloadRelease> {
const availableVersion = '11.0.8';
const availableVersion = '11.0.9';
if (!semver.satisfies(availableVersion, range)) {
throw new Error('Available version not found');
}
Expand All @@ -38,7 +38,7 @@ class EmptyJavaBase extends JavaBase {
}

describe('findInToolcache', () => {
const actualJavaVersion = '11.1.10';
const actualJavaVersion = '11.0.8';
const javaPath = path.join('Java_Empty_jdk', actualJavaVersion, 'x64');

let mockJavaBase: EmptyJavaBase;
Expand All @@ -62,11 +62,23 @@ describe('findInToolcache', () => {
{ version: actualJavaVersion, path: javaPath }
],
[
{ version: '11.1', architecture: 'x64', packageType: 'jdk' },
{ version: '11.0', architecture: 'x64', packageType: 'jdk' },
{ version: actualJavaVersion, path: javaPath }
],
[
{ version: '11.1.10', architecture: 'x64', packageType: 'jdk' },
{ version: '11.0.8', architecture: 'x64', packageType: 'jdk' },
{ version: actualJavaVersion, path: javaPath }
],
[
{ version: '11', architecture: 'x64', packageType: 'jdk', checkLatest: true },
{ version: actualJavaVersion, path: javaPath }
],
[
{ version: '11.0', architecture: 'x64', packageType: 'jdk', checkLatest: true },
{ version: actualJavaVersion, path: javaPath }
],
[
{ version: '11.0.8', architecture: 'x64', packageType: 'jdk', checkLatest: true },
{ version: actualJavaVersion, path: javaPath }
],
[{ version: '11', architecture: 'x64', packageType: 'jre' }, null],
Expand Down Expand Up @@ -122,8 +134,10 @@ describe('findInToolcache', () => {
});

describe('setupJava', () => {
const actualJavaVersion = '11.1.10';
const javaPath = path.join('Java_Empty_jdk', actualJavaVersion, 'x86');
const actualJavaVersion = '11.0.9';
const installedJavaVersion = '11.0.8';
const javaPath = path.join('Java_Empty_jdk', installedJavaVersion, 'x86');
const javaPathInstalled = path.join('toolcache', 'Java_Empty_jdk', actualJavaVersion, 'x86');

let mockJavaBase: EmptyJavaBase;

Expand All @@ -145,12 +159,12 @@ describe('setupJava', () => {
return '';
}

return semver.satisfies(actualJavaVersion, semverVersion) ? javaPath : '';
return semver.satisfies(installedJavaVersion, semverVersion) ? javaPath : '';
}
);

spyTcFindAllVersions = jest.spyOn(tc, 'findAllVersions');
spyTcFindAllVersions.mockReturnValue([actualJavaVersion]);
spyTcFindAllVersions.mockReturnValue([installedJavaVersion]);

// Spy on core methods
spyCoreDebug = jest.spyOn(core, 'debug');
Expand Down Expand Up @@ -178,34 +192,40 @@ describe('setupJava', () => {
it.each([
[
{ version: '11', architecture: 'x86', packageType: 'jdk' },
{ version: actualJavaVersion, path: javaPath }
{ version: installedJavaVersion, path: javaPath }
],
[
{ version: '11.1', architecture: 'x86', packageType: 'jdk' },
{ version: actualJavaVersion, path: javaPath }
{ version: '11.0', architecture: 'x86', packageType: 'jdk' },
{ version: installedJavaVersion, path: javaPath }
],
[
{ version: '11.1.10', architecture: 'x86', packageType: 'jdk' },
{ version: actualJavaVersion, path: javaPath }
{ version: '11.0.8', architecture: 'x86', packageType: 'jdk' },
{ version: installedJavaVersion, path: javaPath }
]
])('should find java locally for %s', (input, expected) => {
mockJavaBase = new EmptyJavaBase(input);
expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
expect(spyGetToolcachePath).toHaveBeenCalled();
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved Java ${expected.version} from tool-cache`);
expect(spyCoreInfo).toHaveBeenCalledWith(`Setting Java ${expected.version} as the default`);
expect(spyCoreInfo).not.toHaveBeenCalledWith(
'Trying to resolve the latest version from remote'
);
expect(spyCoreInfo).not.toHaveBeenCalledWith('Trying to download...');
});

it.each([
[
{ version: '11', architecture: 'x86', packageType: 'jre' },
{ path: `/toolcache/Java_Empty_jre/11.0.8/x86`, version: '11.0.8' }
{ path: path.join('toolcache', 'Java_Empty_jre', '11.0.9', 'x86'), version: '11.0.9' }
],
[
{ version: '11', architecture: 'x64', packageType: 'jdk' },
{ path: `/toolcache/Java_Empty_jdk/11.0.8/x64`, version: '11.0.8' }
{ path: path.join('toolcache', 'Java_Empty_jdk', '11.0.9', 'x64'), version: '11.0.9' }
],
[
{ version: '11', architecture: 'x64', packageType: 'jre' },
{ path: `/toolcache/Java_Empty_jre/11.0.8/x64`, version: '11.0.8' }
{ path: path.join('toolcache', 'Java_Empty_jre', '11.0.9', 'x64'), version: '11.0.9' }
]
])('download java with configuration %s', async (input, expected) => {
mockJavaBase = new EmptyJavaBase(input);
Expand All @@ -214,6 +234,50 @@ describe('setupJava', () => {
expect(spyCoreAddPath).toHaveBeenCalled();
expect(spyCoreExportVariable).toHaveBeenCalled();
expect(spyCoreSetOutput).toHaveBeenCalled();
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to resolve the latest version from remote');
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved latest version as ${expected.version}`);
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to download...');
expect(spyCoreInfo).toHaveBeenCalledWith(`Java ${expected.version} was downloaded`);
expect(spyCoreInfo).toHaveBeenCalledWith(`Setting Java ${expected.version} as the default`);
});

it.each([
[
{ version: '11.0.9', architecture: 'x86', packageType: 'jdk', checkLatest: true },
{ version: '11.0.9', path: javaPathInstalled }
]
])('should check the latest java version for %s and resolve locally', async (input, expected) => {
mockJavaBase = new EmptyJavaBase(input);
mockJavaBase['findInToolcache'] = () => ({ version: '11.0.9', path: expected.path });
await expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to resolve the latest version from remote');
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved latest version as ${expected.version}`);
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved Java ${expected.version} from tool-cache`);
expect(spyCoreInfo).toHaveBeenCalledWith(`Setting Java ${expected.version} as the default`);
});

it.each([
[
{ version: '11', architecture: 'x86', packageType: 'jdk', checkLatest: true },
{ version: actualJavaVersion, path: javaPathInstalled }
],
[
{ version: '11.0', architecture: 'x86', packageType: 'jdk', checkLatest: true },
{ version: actualJavaVersion, path: javaPathInstalled }
],
[
{ version: '11.0.x', architecture: 'x86', packageType: 'jdk', checkLatest: true },
{ version: actualJavaVersion, path: javaPathInstalled }
]
])('should check the latest java version for %s and download', async (input, expected) => {
mockJavaBase = new EmptyJavaBase(input);
await expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
expect(spyGetToolcachePath).toHaveBeenCalled();
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to resolve the latest version from remote');
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved latest version as ${actualJavaVersion}`);
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to download...');
expect(spyCoreInfo).toHaveBeenCalledWith(`Java ${actualJavaVersion} was downloaded`);
expect(spyCoreInfo).toHaveBeenCalledWith(`Setting Java ${expected.version} as the default`);
});

it.each([
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Expand Up @@ -20,6 +20,10 @@ inputs:
jdkFile:
description: 'Path to where the compressed JDK is located'
required: false
check-latest:
description: 'Set this option if you want the action to check for the latest available version that satisfies the version spec'
required: false
default: false
server-id:
description: 'ID of the distributionManagement repository in the pom.xml
file. Default is `github`'
Expand Down
3 changes: 2 additions & 1 deletion dist/cleanup/index.js
Expand Up @@ -6829,13 +6829,14 @@ function isUnixExecutable(stats) {
"use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_OVERWRITE_SETTINGS = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0;
exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_OVERWRITE_SETTINGS = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_CHECK_LATEST = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0;
exports.MACOS_JAVA_CONTENT_POSTFIX = 'Contents/Home';
exports.INPUT_JAVA_VERSION = 'java-version';
exports.INPUT_ARCHITECTURE = 'architecture';
exports.INPUT_JAVA_PACKAGE = 'java-package';
exports.INPUT_DISTRIBUTION = 'distribution';
exports.INPUT_JDK_FILE = 'jdkFile';
exports.INPUT_CHECK_LATEST = 'check-latest';
exports.INPUT_SERVER_ID = 'server-id';
exports.INPUT_SERVER_USERNAME = 'server-username';
exports.INPUT_SERVER_PASSWORD = 'server-password';
Expand Down
28 changes: 22 additions & 6 deletions dist/setup/index.js
Expand Up @@ -3957,6 +3957,7 @@ const httpm = __importStar(__webpack_require__(539));
const util_1 = __webpack_require__(322);
class JavaBase {
constructor(distribution, installerOptions) {
var _a;
this.distribution = distribution;
this.http = new httpm.HttpClient('actions/setup-java', undefined, {
allowRetries: true,
Expand All @@ -3965,18 +3966,29 @@ class JavaBase {
({ version: this.version, stable: this.stable } = this.normalizeVersion(installerOptions.version));
this.architecture = installerOptions.architecture;
this.packageType = installerOptions.packageType;
this.checkLatest = (_a = installerOptions.checkLatest) !== null && _a !== void 0 ? _a : false;
}
setupJava() {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
let foundJava = this.findInToolcache();
if (foundJava) {
if (foundJava && !this.checkLatest) {
core.info(`Resolved Java ${foundJava.version} from tool-cache`);
}
else {
core.info(`Java ${this.version} was not found in tool-cache. Trying to download...`);
core.info('Trying to resolve the latest version from remote');
const javaRelease = yield this.findPackageForDownload(this.version);
foundJava = yield this.downloadTool(javaRelease);
core.info(`Java ${foundJava.version} was downloaded`);
core.info(`Resolved latest version as ${javaRelease.version}`);
core.info((_a = foundJava === null || foundJava === void 0 ? void 0 : foundJava.version) !== null && _a !== void 0 ? _a : '');
core.info((_b = javaRelease.version) !== null && _b !== void 0 ? _b : '');
if ((foundJava === null || foundJava === void 0 ? void 0 : foundJava.version) === javaRelease.version) {
core.info(`Resolved Java ${foundJava.version} from tool-cache`);
}
else {
core.info('Trying to download...');
foundJava = yield this.downloadTool(javaRelease);
core.info(`Java ${foundJava.version} was downloaded`);
}
}
core.info(`Setting Java ${foundJava.version} as the default`);
this.setJavaDefault(foundJava.version, foundJava.path);
Expand Down Expand Up @@ -11302,13 +11314,14 @@ exports.HTMLCollectionImpl = HTMLCollectionImpl;
"use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_OVERWRITE_SETTINGS = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0;
exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_OVERWRITE_SETTINGS = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_CHECK_LATEST = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0;
exports.MACOS_JAVA_CONTENT_POSTFIX = 'Contents/Home';
exports.INPUT_JAVA_VERSION = 'java-version';
exports.INPUT_ARCHITECTURE = 'architecture';
exports.INPUT_JAVA_PACKAGE = 'java-package';
exports.INPUT_DISTRIBUTION = 'distribution';
exports.INPUT_JDK_FILE = 'jdkFile';
exports.INPUT_CHECK_LATEST = 'check-latest';
exports.INPUT_SERVER_ID = 'server-id';
exports.INPUT_SERVER_USERNAME = 'server-username';
exports.INPUT_SERVER_PASSWORD = 'server-password';
Expand Down Expand Up @@ -35643,6 +35656,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(__webpack_require__(470));
const auth = __importStar(__webpack_require__(331));
const util_1 = __webpack_require__(322);
const constants = __importStar(__webpack_require__(211));
const path = __importStar(__webpack_require__(622));
const distribution_factory_1 = __webpack_require__(24);
Expand All @@ -35654,10 +35668,12 @@ function run() {
const architecture = core.getInput(constants.INPUT_ARCHITECTURE);
const packageType = core.getInput(constants.INPUT_JAVA_PACKAGE);
const jdkFile = core.getInput(constants.INPUT_JDK_FILE);
const checkLatest = util_1.getBooleanInput(constants.INPUT_CHECK_LATEST, false);
const installerOptions = {
architecture,
packageType,
version
version,
checkLatest
};
const distribution = distribution_factory_1.getJavaDistribution(distributionName, installerOptions, jdkFile);
if (!distribution) {
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Expand Up @@ -4,6 +4,7 @@ export const INPUT_ARCHITECTURE = 'architecture';
export const INPUT_JAVA_PACKAGE = 'java-package';
export const INPUT_DISTRIBUTION = 'distribution';
export const INPUT_JDK_FILE = 'jdkFile';
export const INPUT_CHECK_LATEST = 'check-latest';
export const INPUT_SERVER_ID = 'server-id';
export const INPUT_SERVER_USERNAME = 'server-username';
export const INPUT_SERVER_PASSWORD = 'server-password';
Expand Down
18 changes: 14 additions & 4 deletions src/distributions/base-installer.ts
Expand Up @@ -12,6 +12,7 @@ export abstract class JavaBase {
protected architecture: string;
protected packageType: string;
protected stable: boolean;
protected checkLatest: boolean;

constructor(protected distribution: string, installerOptions: JavaInstallerOptions) {
this.http = new httpm.HttpClient('actions/setup-java', undefined, {
Expand All @@ -24,20 +25,29 @@ export abstract class JavaBase {
));
this.architecture = installerOptions.architecture;
this.packageType = installerOptions.packageType;
this.checkLatest = installerOptions.checkLatest ?? false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't need the ?? false since getBooleanInput defaults to it if there is no value

}

protected abstract downloadTool(javaRelease: JavaDownloadRelease): Promise<JavaInstallerResults>;
protected abstract findPackageForDownload(range: string): Promise<JavaDownloadRelease>;

public async setupJava(): Promise<JavaInstallerResults> {
let foundJava = this.findInToolcache();
if (foundJava) {
if (foundJava && !this.checkLatest) {
core.info(`Resolved Java ${foundJava.version} from tool-cache`);
} else {
core.info(`Java ${this.version} was not found in tool-cache. Trying to download...`);
core.info('Trying to resolve the latest version from remote');
const javaRelease = await this.findPackageForDownload(this.version);
foundJava = await this.downloadTool(javaRelease);
core.info(`Java ${foundJava.version} was downloaded`);
core.info(`Resolved latest version as ${javaRelease.version}`);
core.info(foundJava?.version ?? '');
core.info(javaRelease.version ?? '');
if (foundJava?.version === javaRelease.version) {
core.info(`Resolved Java ${foundJava.version} from tool-cache`);
} else {
core.info('Trying to download...');
foundJava = await this.downloadTool(javaRelease);
core.info(`Java ${foundJava.version} was downloaded`);
}
}

core.info(`Setting Java ${foundJava.version} as the default`);
Expand Down
1 change: 1 addition & 0 deletions src/distributions/base-models.ts
Expand Up @@ -2,6 +2,7 @@ export interface JavaInstallerOptions {
version: string;
architecture: string;
packageType: string;
checkLatest?: boolean;
}

export interface JavaInstallerResults {
Expand Down
6 changes: 4 additions & 2 deletions src/setup-java.ts
@@ -1,6 +1,6 @@
import * as core from '@actions/core';
import * as auth from './auth';

import { getBooleanInput } from './util';
import * as constants from './constants';
import * as path from 'path';
import { getJavaDistribution } from './distributions/distribution-factory';
Expand All @@ -13,11 +13,13 @@ async function run() {
const architecture = core.getInput(constants.INPUT_ARCHITECTURE);
const packageType = core.getInput(constants.INPUT_JAVA_PACKAGE);
const jdkFile = core.getInput(constants.INPUT_JDK_FILE);
const checkLatest = getBooleanInput(constants.INPUT_CHECK_LATEST, false);

const installerOptions: JavaInstallerOptions = {
architecture,
packageType,
version
version,
checkLatest
};

const distribution = getJavaDistribution(distributionName, installerOptions, jdkFile);
Expand Down