fix review points

This commit is contained in:
Evgenii Korolevskii 2022-12-08 15:32:14 +01:00
parent c13e4b13ba
commit 291b946ea5
2 changed files with 33 additions and 32 deletions

View file

@ -1,14 +1,13 @@
import fs from 'fs';
import * as core from '@actions/core';
import * as auth from './auth';
import { getBooleanInput, isCacheFeatureAvailable } from './util';
import { getBooleanInput, isCacheFeatureAvailable, getVersionFromFileContent, avoidOldNotation } from './util';
import * as toolchains from './toolchains';
import * as constants from './constants';
import { restore } from './cache';
import * as path from 'path';
import { getJavaDistribution } from './distributions/distribution-factory';
import { JavaInstallerOptions } from './distributions/base-models';
import * as semver from 'semver';
async function run() {
try {
@ -122,32 +121,3 @@ interface installerInputsOptions {
toolchainIds: Array<string>;
}
function getVersionFromFileContent(content: string, distributionName: string): string | null {
const javaVersionRegExp = /(?<version>(?<=(^|\s|\-))(\d+\S*))(\s|$)/;
const fileContent = content.match(javaVersionRegExp)?.groups?.version
? (content.match(javaVersionRegExp)?.groups?.version as string)
: '';
if (!fileContent) {
return null;
}
const tentativeVersion = avoidOldNotation(fileContent);
let version = semver.validRange(tentativeVersion)
? tentativeVersion
: semver.coerce(tentativeVersion);
if (!version) {
return null;
}
if (constants.DISTRIBUTIONS_ONLY_MAJOR_VERSION.includes(distributionName)) {
version = semver.major(version).toString();
}
return version.toString();
}
// By convention, action expects version 8 in the format `8.*` instead of `1.8`
function avoidOldNotation(content: string): string {
return content.startsWith('1.') ? content.substring(2) : content;
}

View file

@ -6,7 +6,7 @@ import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
import { INPUT_JOB_STATUS } from './constants';
import { INPUT_JOB_STATUS, DISTRIBUTIONS_ONLY_MAJOR_VERSION } from './constants';
export function getTempDir() {
let tempDirectory = process.env['RUNNER_TEMP'] || os.tmpdir();
@ -99,3 +99,34 @@ export function isCacheFeatureAvailable(): boolean {
return true;
}
export function getVersionFromFileContent(content: string, distributionName: string): string | null {
const javaVersionRegExp = /(?<version>(?<=(^|\s|\-))(\d+\S*))(\s|$)/;
const fileContent = content.match(javaVersionRegExp)?.groups?.version
? (content.match(javaVersionRegExp)?.groups?.version as string)
: '';
if (!fileContent) {
return null;
}
const tentativeVersion = avoidOldNotation(fileContent);
let version = semver.validRange(tentativeVersion)
? tentativeVersion
: semver.coerce(tentativeVersion);
if (!version) {
return null;
}
if (DISTRIBUTIONS_ONLY_MAJOR_VERSION.includes(distributionName)) {
version = semver.major(version).toString();
}
return version.toString();
}
// By convention, action expects version 8 in the format `8.*` instead of `1.8`
export function avoidOldNotation(content: string): string {
return content.startsWith('1.') ? content.substring(2) : content;
}