refactor: applied code review

This commit is contained in:
augustomelo 2024-04-27 11:24:16 +01:00
parent f5e27775fa
commit d878c91127
4 changed files with 71 additions and 68 deletions

40
dist/cleanup/index.js vendored
View file

@ -103695,7 +103695,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getVersionFromFileContent = exports.isCacheFeatureAvailable = exports.isGhes = exports.isJobStatusSuccess = exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0; exports.getVersionFromFile = exports.isCacheFeatureAvailable = exports.isGhes = exports.isJobStatusSuccess = exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0;
const os_1 = __importDefault(__nccwpck_require__(2037)); const os_1 = __importDefault(__nccwpck_require__(2037));
const path_1 = __importDefault(__nccwpck_require__(1017)); const path_1 = __importDefault(__nccwpck_require__(1017));
const fs = __importStar(__nccwpck_require__(7147)); const fs = __importStar(__nccwpck_require__(7147));
@ -103791,24 +103791,24 @@ function isCacheFeatureAvailable() {
return false; return false;
} }
exports.isCacheFeatureAvailable = isCacheFeatureAvailable; exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
function getVersionFromFileContent(fileName, content, distributionName) { function getVersionFromFile(fileName, content, distributionName) {
var _a; var _a;
let fileContent = null; let parsedVersion = null;
core.debug(`Getting version from: '${fileName}'`); core.debug(`Getting version from: '${fileName}'`);
if (fileName.includes('.java-version')) { if (fileName.includes('.java-version')) {
fileContent = parseJavaVersionFile(content); parsedVersion = parseJavaVersionFile(content);
} }
else if (fileName.includes('pom.xml')) { else if (fileName.includes('pom.xml')) {
fileContent = parsePomXmlFile(content); parsedVersion = parsePomXmlFile(content);
} }
else { else {
throw new Error(`File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'`); throw new Error(`File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'`);
} }
if (!fileContent) { if (!parsedVersion) {
return null; return null;
} }
core.debug(`Version from file '${fileContent}'`); core.debug(`Version from file '${parsedVersion}'`);
const tentativeVersion = avoidOldNotation(fileContent); const tentativeVersion = avoidOldNotation(parsedVersion);
const rawVersion = tentativeVersion.split('-')[0]; const rawVersion = tentativeVersion.split('-')[0];
let version = semver.validRange(rawVersion) ? tentativeVersion : semver.coerce(tentativeVersion); let version = semver.validRange(rawVersion) ? tentativeVersion : semver.coerce(tentativeVersion);
core.debug(`Range version from file is '${version}'`); core.debug(`Range version from file is '${version}'`);
@ -103821,7 +103821,7 @@ function getVersionFromFileContent(fileName, content, distributionName) {
} }
return version.toString(); return version.toString();
} }
exports.getVersionFromFileContent = getVersionFromFileContent; exports.getVersionFromFile = getVersionFromFile;
function parseJavaVersionFile(content) { function parseJavaVersionFile(content) {
var _a, _b, _c, _d; var _a, _b, _c, _d;
const javaVersionRegExp = /(?<version>(?<=(^|\s|\-))(\d+\S*))(\s|$)/; const javaVersionRegExp = /(?<version>(?<=(^|\s|\-))(\d+\S*))(\s|$)/;
@ -103849,11 +103849,8 @@ function parsePomXmlFile(xmlFileAsString) {
return null; return null;
} }
function getByMavenProperties(xmlDoc) { function getByMavenProperties(xmlDoc) {
const possibleTagsRegex = [ const versionTags = ['maven.compiler.source', 'maven.compiler.release'];
'maven.compiler.source', for (const tag of versionTags) {
'maven.compiler.release',
];
for (var tag of possibleTagsRegex) {
const version = getVersionByTagName(xmlDoc, tag); const version = getVersionByTagName(xmlDoc, tag);
if (version !== null) { if (version !== null) {
return version; return version;
@ -103878,29 +103875,32 @@ function getByMavenCompilerPluginConfig(xmlDoc) {
var _a; var _a;
const source = xmlDoc.find(n => { const source = xmlDoc.find(n => {
// Find <source> node // Find <source> node
if (n.node.nodeName !== "source") { if (n.node.nodeName !== 'source') {
return false; return false;
} }
if (n.node.childNodes.length !== 1) { if (n.node.childNodes.length !== 1) {
return false; return false;
} }
// Must be within <configuration> // Must be within <configuration>
if (n.up().node.nodeName !== "configuration") { if (n.up().node.nodeName !== 'configuration') {
return false; return false;
} }
// Which must be inside <plugin> // Which must be inside <plugin>
if (n.up().up().node.nodeName !== "plugin") { if (n.up().up().node.nodeName !== 'plugin') {
return false; return false;
} }
// Make sure the plugin is maven-compiler-plugin // Make sure the plugin is maven-compiler-plugin
const isCompilerPlugin = n.up().up().some(c => { const isCompilerPlugin = n
if (c.node.nodeName !== "artifactId") { .up()
.up()
.some(c => {
if (c.node.nodeName !== 'artifactId') {
return false; return false;
} }
if (c.node.childNodes.length !== 1) { if (c.node.childNodes.length !== 1) {
return false; return false;
} }
return c.first().toString() === "maven-compiler-plugin"; return c.first().toString() === 'maven-compiler-plugin';
}, false, true); }, false, true);
if (!isCompilerPlugin) { if (!isCompilerPlugin) {
return false; return false;

42
dist/setup/index.js vendored
View file

@ -105104,7 +105104,7 @@ function run() {
.readFileSync(versionFile) .readFileSync(versionFile)
.toString() .toString()
.trim(); .trim();
const version = util_1.getVersionFromFileContent(versionFile, content, distributionName); const version = util_1.getVersionFromFile(versionFile, content, distributionName);
core.debug(`Parsed version from file '${version}'`); core.debug(`Parsed version from file '${version}'`);
if (!version) { if (!version) {
throw new Error(`No supported version was found in file ${versionFile}`); throw new Error(`No supported version was found in file ${versionFile}`);
@ -105352,7 +105352,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getVersionFromFileContent = exports.isCacheFeatureAvailable = exports.isGhes = exports.isJobStatusSuccess = exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0; exports.getVersionFromFile = exports.isCacheFeatureAvailable = exports.isGhes = exports.isJobStatusSuccess = exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0;
const os_1 = __importDefault(__nccwpck_require__(2037)); const os_1 = __importDefault(__nccwpck_require__(2037));
const path_1 = __importDefault(__nccwpck_require__(1017)); const path_1 = __importDefault(__nccwpck_require__(1017));
const fs = __importStar(__nccwpck_require__(7147)); const fs = __importStar(__nccwpck_require__(7147));
@ -105448,24 +105448,24 @@ function isCacheFeatureAvailable() {
return false; return false;
} }
exports.isCacheFeatureAvailable = isCacheFeatureAvailable; exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
function getVersionFromFileContent(fileName, content, distributionName) { function getVersionFromFile(fileName, content, distributionName) {
var _a; var _a;
let fileContent = null; let parsedVersion = null;
core.debug(`Getting version from: '${fileName}'`); core.debug(`Getting version from: '${fileName}'`);
if (fileName.includes('.java-version')) { if (fileName.includes('.java-version')) {
fileContent = parseJavaVersionFile(content); parsedVersion = parseJavaVersionFile(content);
} }
else if (fileName.includes('pom.xml')) { else if (fileName.includes('pom.xml')) {
fileContent = parsePomXmlFile(content); parsedVersion = parsePomXmlFile(content);
} }
else { else {
throw new Error(`File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'`); throw new Error(`File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'`);
} }
if (!fileContent) { if (!parsedVersion) {
return null; return null;
} }
core.debug(`Version from file '${fileContent}'`); core.debug(`Version from file '${parsedVersion}'`);
const tentativeVersion = avoidOldNotation(fileContent); const tentativeVersion = avoidOldNotation(parsedVersion);
const rawVersion = tentativeVersion.split('-')[0]; const rawVersion = tentativeVersion.split('-')[0];
let version = semver.validRange(rawVersion) ? tentativeVersion : semver.coerce(tentativeVersion); let version = semver.validRange(rawVersion) ? tentativeVersion : semver.coerce(tentativeVersion);
core.debug(`Range version from file is '${version}'`); core.debug(`Range version from file is '${version}'`);
@ -105478,7 +105478,7 @@ function getVersionFromFileContent(fileName, content, distributionName) {
} }
return version.toString(); return version.toString();
} }
exports.getVersionFromFileContent = getVersionFromFileContent; exports.getVersionFromFile = getVersionFromFile;
function parseJavaVersionFile(content) { function parseJavaVersionFile(content) {
var _a, _b, _c, _d; var _a, _b, _c, _d;
const javaVersionRegExp = /(?<version>(?<=(^|\s|\-))(\d+\S*))(\s|$)/; const javaVersionRegExp = /(?<version>(?<=(^|\s|\-))(\d+\S*))(\s|$)/;
@ -105506,11 +105506,8 @@ function parsePomXmlFile(xmlFileAsString) {
return null; return null;
} }
function getByMavenProperties(xmlDoc) { function getByMavenProperties(xmlDoc) {
const possibleTagsRegex = [ const versionTags = ['maven.compiler.source', 'maven.compiler.release'];
'maven.compiler.source', for (const tag of versionTags) {
'maven.compiler.release',
];
for (var tag of possibleTagsRegex) {
const version = getVersionByTagName(xmlDoc, tag); const version = getVersionByTagName(xmlDoc, tag);
if (version !== null) { if (version !== null) {
return version; return version;
@ -105535,29 +105532,32 @@ function getByMavenCompilerPluginConfig(xmlDoc) {
var _a; var _a;
const source = xmlDoc.find(n => { const source = xmlDoc.find(n => {
// Find <source> node // Find <source> node
if (n.node.nodeName !== "source") { if (n.node.nodeName !== 'source') {
return false; return false;
} }
if (n.node.childNodes.length !== 1) { if (n.node.childNodes.length !== 1) {
return false; return false;
} }
// Must be within <configuration> // Must be within <configuration>
if (n.up().node.nodeName !== "configuration") { if (n.up().node.nodeName !== 'configuration') {
return false; return false;
} }
// Which must be inside <plugin> // Which must be inside <plugin>
if (n.up().up().node.nodeName !== "plugin") { if (n.up().up().node.nodeName !== 'plugin') {
return false; return false;
} }
// Make sure the plugin is maven-compiler-plugin // Make sure the plugin is maven-compiler-plugin
const isCompilerPlugin = n.up().up().some(c => { const isCompilerPlugin = n
if (c.node.nodeName !== "artifactId") { .up()
.up()
.some(c => {
if (c.node.nodeName !== 'artifactId') {
return false; return false;
} }
if (c.node.childNodes.length !== 1) { if (c.node.childNodes.length !== 1) {
return false; return false;
} }
return c.first().toString() === "maven-compiler-plugin"; return c.first().toString() === 'maven-compiler-plugin';
}, false, true); }, false, true);
if (!isCompilerPlugin) { if (!isCompilerPlugin) {
return false; return false;

View file

@ -1,7 +1,7 @@
import fs from 'fs'; import fs from 'fs';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as auth from './auth'; import * as auth from './auth';
import { getBooleanInput, isCacheFeatureAvailable, getVersionFromFileContent } from './util'; import { getBooleanInput, isCacheFeatureAvailable, getVersionFromFile } from './util';
import * as toolchains from './toolchains'; import * as toolchains from './toolchains';
import * as constants from './constants'; import * as constants from './constants';
import { restore } from './cache'; import { restore } from './cache';
@ -47,7 +47,7 @@ async function run() {
.toString() .toString()
.trim(); .trim();
const version = getVersionFromFileContent(versionFile, content, distributionName); const version = getVersionFromFile(versionFile, content, distributionName);
core.debug(`Parsed version from file '${version}'`); core.debug(`Parsed version from file '${version}'`);
if (!version) { if (!version) {

View file

@ -102,31 +102,31 @@ export function isCacheFeatureAvailable(): boolean {
return false; return false;
} }
export function getVersionFromFileContent( export function getVersionFromFile(
fileName: string, fileName: string,
content: string, content: string,
distributionName: string distributionName: string
): string | null { ): string | null {
let fileContent = null; let parsedVersion = null;
core.debug(`Getting version from: '${fileName}'`); core.debug(`Getting version from: '${fileName}'`);
if (fileName.includes('.java-version')) { if (fileName.includes('.java-version')) {
fileContent = parseJavaVersionFile(content); parsedVersion = parseJavaVersionFile(content);
} else if (fileName.includes('pom.xml')) { } else if (fileName.includes('pom.xml')) {
fileContent = parsePomXmlFile(content); parsedVersion = parsePomXmlFile(content);
} else { } else {
throw new Error( throw new Error(
`File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'` `File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'`
); );
} }
if (!fileContent) { if (!parsedVersion) {
return null; return null;
} }
core.debug(`Version from file '${fileContent}'`); core.debug(`Version from file '${parsedVersion}'`);
const tentativeVersion = avoidOldNotation(fileContent); const tentativeVersion = avoidOldNotation(parsedVersion);
const rawVersion = tentativeVersion.split('-')[0]; const rawVersion = tentativeVersion.split('-')[0];
let version = semver.validRange(rawVersion) ? tentativeVersion : semver.coerce(tentativeVersion); let version = semver.validRange(rawVersion) ? tentativeVersion : semver.coerce(tentativeVersion);
@ -177,12 +177,9 @@ function parsePomXmlFile(xmlFileAsString: string): string | null {
} }
function getByMavenProperties(xmlDoc: XMLBuilder): string | null { function getByMavenProperties(xmlDoc: XMLBuilder): string | null {
const possibleTagsRegex = [ const versionTags = ['maven.compiler.source', 'maven.compiler.release'];
'maven.compiler.source',
'maven.compiler.release',
];
for (var tag of possibleTagsRegex) { for (const tag of versionTags) {
const version = getVersionByTagName(xmlDoc, tag); const version = getVersionByTagName(xmlDoc, tag);
if (version !== null) { if (version !== null) {
@ -206,36 +203,42 @@ function getVersionByTagName(xmlDoc: XMLBuilder, tag: string): string | null {
} else { } else {
return null; return null;
} }
} }
function getByMavenCompilerPluginConfig(xmlDoc: XMLBuilder): string | null { function getByMavenCompilerPluginConfig(xmlDoc: XMLBuilder): string | null {
const source = xmlDoc.find(n => { const source = xmlDoc.find(n => {
// Find <source> node // Find <source> node
if (n.node.nodeName !== "source") { if (n.node.nodeName !== 'source') {
return false; return false;
} }
if (n.node.childNodes.length !== 1) { if (n.node.childNodes.length !== 1) {
return false; return false;
} }
// Must be within <configuration> // Must be within <configuration>
if (n.up().node.nodeName !== "configuration") { if (n.up().node.nodeName !== 'configuration') {
return false; return false;
} }
// Which must be inside <plugin> // Which must be inside <plugin>
if (n.up().up().node.nodeName !== "plugin") { if (n.up().up().node.nodeName !== 'plugin') {
return false; return false;
} }
// Make sure the plugin is maven-compiler-plugin // Make sure the plugin is maven-compiler-plugin
const isCompilerPlugin = n.up().up().some(c => { const isCompilerPlugin = n
if (c.node.nodeName !== "artifactId") { .up()
.up()
.some(
c => {
if (c.node.nodeName !== 'artifactId') {
return false; return false;
} }
if (c.node.childNodes.length !== 1) { if (c.node.childNodes.length !== 1) {
return false; return false;
} }
return c.first().toString() === "maven-compiler-plugin"; return c.first().toString() === 'maven-compiler-plugin';
}, false, true); },
false,
true
);
if (!isCompilerPlugin) { if (!isCompilerPlugin) {
return false; return false;
} }