From 897cdc868d4a920f90f1dfd0002f978b680cfd77 Mon Sep 17 00:00:00 2001 From: augustomelo <4723788+augustomelo@users.noreply.github.com> Date: Mon, 29 Apr 2024 16:52:52 +0100 Subject: [PATCH] fix: not transversing the XML tree, and the regex declared wrong When trying to find a node the function wasn't iterating over the whole XML tree, which made the function not able to correctly identify the java version The regex is declared as string, which made it necessary to add more escaping. --- dist/cleanup/index.js | 55 +++++++++++------------------------- dist/setup/index.js | 55 +++++++++++------------------------- src/util.ts | 65 +++++++++++++++---------------------------- 3 files changed, 55 insertions(+), 120 deletions(-) diff --git a/dist/cleanup/index.js b/dist/cleanup/index.js index a101016..eb91090 100644 --- a/dist/cleanup/index.js +++ b/dist/cleanup/index.js @@ -103805,7 +103805,7 @@ function getVersionFromFile(fileName, content, distributionName) { parsedVersion = parseBuildGradleFile(content); } 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' 'pom.xml' and 'build.gradle'`); } if (!parsedVersion) { return null; @@ -103865,7 +103865,7 @@ function getBySpringBootSpecification(xmlDoc) { return getVersionByTagName(xmlDoc, 'java.version'); } function getVersionByTagName(xmlDoc, tag) { - const match = xmlDoc.find(n => n.node.nodeName === tag); + const match = xmlDoc.find(n => n.node.nodeName === tag, false, true); if (match !== undefined) { core.debug(`Found java version: '${match.first().toString()}' using tag: '${tag}'`); return match.first().toString(); @@ -103875,42 +103875,19 @@ function getVersionByTagName(xmlDoc, tag) { } } function getByMavenCompilerPluginConfig(xmlDoc) { - var _a; - const source = xmlDoc.find(n => { - // Find node - if (n.node.nodeName !== 'source') { - return false; - } - if (n.node.childNodes.length !== 1) { - return false; - } - // Must be within - if (n.up().node.nodeName !== 'configuration') { - return false; - } - // Which must be inside - if (n.up().up().node.nodeName !== 'plugin') { - return false; - } - // Make sure the plugin is maven-compiler-plugin - const isCompilerPlugin = n + const mavenCompilePlugin = xmlDoc.find(n => n.node.nodeName === 'artifactId' && n.first().toString() === 'maven-compiler-plugin', false, true); + if (mavenCompilePlugin != undefined) { + const sourceOrTag = mavenCompilePlugin .up() - .up() - .some(c => { - if (c.node.nodeName !== 'artifactId') { - return false; - } - if (c.node.childNodes.length !== 1) { - return false; - } - return c.first().toString() === 'maven-compiler-plugin'; - }, false, true); - if (!isCompilerPlugin) { - return false; + .find(n => n.node.nodeName === 'source' || n.node.nodeName === 'target', false, true); + if (sourceOrTag !== undefined) { + core.debug(`Found java version: '${sourceOrTag + .first() + .toString()}' defined on the maven-compiler-plugin'`); + return sourceOrTag.first().toString(); } - return true; - }); - return (_a = source === null || source === void 0 ? void 0 : source.first().toString()) !== null && _a !== void 0 ? _a : null; + } + return null; } function parseBuildGradleFile(buildGradle) { const versionDefinitionTypes = [getByJavaLibraryPlugin, getByJavaPlugin]; @@ -103923,12 +103900,12 @@ function parseBuildGradleFile(buildGradle) { return null; } function getByJavaLibraryPlugin(buildGradle) { - return getVersionByRegex(buildGradle, 'JavaLanguageVersion.of((d+))'); + return getVersionByRegex(buildGradle, 'JavaLanguageVersion.of\\((\\d+)\\)'); } function getByJavaPlugin(buildGradle) { const possibleRegex = [ - 'sourceCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)', - 'targetCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)' + 'sourceCompatibility\\s?=\\s?JavaVersion.VERSION_(?:1_)?(\\d+)', + 'targetCompatibility\\s?=\\s?JavaVersion.VERSION_(?:1_)?(\\d+)' ]; for (var regex of possibleRegex) { const version = getVersionByRegex(buildGradle, regex); diff --git a/dist/setup/index.js b/dist/setup/index.js index 33f2f17..277e1e9 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -105462,7 +105462,7 @@ function getVersionFromFile(fileName, content, distributionName) { parsedVersion = parseBuildGradleFile(content); } 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' 'pom.xml' and 'build.gradle'`); } if (!parsedVersion) { return null; @@ -105522,7 +105522,7 @@ function getBySpringBootSpecification(xmlDoc) { return getVersionByTagName(xmlDoc, 'java.version'); } function getVersionByTagName(xmlDoc, tag) { - const match = xmlDoc.find(n => n.node.nodeName === tag); + const match = xmlDoc.find(n => n.node.nodeName === tag, false, true); if (match !== undefined) { core.debug(`Found java version: '${match.first().toString()}' using tag: '${tag}'`); return match.first().toString(); @@ -105532,42 +105532,19 @@ function getVersionByTagName(xmlDoc, tag) { } } function getByMavenCompilerPluginConfig(xmlDoc) { - var _a; - const source = xmlDoc.find(n => { - // Find node - if (n.node.nodeName !== 'source') { - return false; - } - if (n.node.childNodes.length !== 1) { - return false; - } - // Must be within - if (n.up().node.nodeName !== 'configuration') { - return false; - } - // Which must be inside - if (n.up().up().node.nodeName !== 'plugin') { - return false; - } - // Make sure the plugin is maven-compiler-plugin - const isCompilerPlugin = n + const mavenCompilePlugin = xmlDoc.find(n => n.node.nodeName === 'artifactId' && n.first().toString() === 'maven-compiler-plugin', false, true); + if (mavenCompilePlugin != undefined) { + const sourceOrTag = mavenCompilePlugin .up() - .up() - .some(c => { - if (c.node.nodeName !== 'artifactId') { - return false; - } - if (c.node.childNodes.length !== 1) { - return false; - } - return c.first().toString() === 'maven-compiler-plugin'; - }, false, true); - if (!isCompilerPlugin) { - return false; + .find(n => n.node.nodeName === 'source' || n.node.nodeName === 'target', false, true); + if (sourceOrTag !== undefined) { + core.debug(`Found java version: '${sourceOrTag + .first() + .toString()}' defined on the maven-compiler-plugin'`); + return sourceOrTag.first().toString(); } - return true; - }); - return (_a = source === null || source === void 0 ? void 0 : source.first().toString()) !== null && _a !== void 0 ? _a : null; + } + return null; } function parseBuildGradleFile(buildGradle) { const versionDefinitionTypes = [getByJavaLibraryPlugin, getByJavaPlugin]; @@ -105580,12 +105557,12 @@ function parseBuildGradleFile(buildGradle) { return null; } function getByJavaLibraryPlugin(buildGradle) { - return getVersionByRegex(buildGradle, 'JavaLanguageVersion.of((d+))'); + return getVersionByRegex(buildGradle, 'JavaLanguageVersion.of\\((\\d+)\\)'); } function getByJavaPlugin(buildGradle) { const possibleRegex = [ - 'sourceCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)', - 'targetCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)' + 'sourceCompatibility\\s?=\\s?JavaVersion.VERSION_(?:1_)?(\\d+)', + 'targetCompatibility\\s?=\\s?JavaVersion.VERSION_(?:1_)?(\\d+)' ]; for (var regex of possibleRegex) { const version = getVersionByRegex(buildGradle, regex); diff --git a/src/util.ts b/src/util.ts index a874880..d228925 100644 --- a/src/util.ts +++ b/src/util.ts @@ -119,7 +119,7 @@ export function getVersionFromFile( parsedVersion = parseBuildGradleFile(content); } else { throw new Error( - `File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'` + `File ${fileName} not supported, files supported: '.java-version' 'pom.xml' and 'build.gradle'` ); } @@ -198,7 +198,7 @@ function getBySpringBootSpecification(xmlDoc: XMLBuilder): string | null { } function getVersionByTagName(xmlDoc: XMLBuilder, tag: string): string | null { - const match = xmlDoc.find(n => n.node.nodeName === tag); + const match = xmlDoc.find(n => n.node.nodeName === tag, false, true); if (match !== undefined) { core.debug(`Found java version: '${match.first().toString()}' using tag: '${tag}'`); @@ -209,47 +209,28 @@ function getVersionByTagName(xmlDoc: XMLBuilder, tag: string): string | null { } function getByMavenCompilerPluginConfig(xmlDoc: XMLBuilder): string | null { - const source = xmlDoc.find(n => { - // Find node - if (n.node.nodeName !== 'source') { - return false; - } - if (n.node.childNodes.length !== 1) { - return false; - } - // Must be within - if (n.up().node.nodeName !== 'configuration') { - return false; - } - // Which must be inside - if (n.up().up().node.nodeName !== 'plugin') { - return false; - } - // Make sure the plugin is maven-compiler-plugin - const isCompilerPlugin = n + const mavenCompilePlugin = xmlDoc.find( + n => n.node.nodeName === 'artifactId' && n.first().toString() === 'maven-compiler-plugin', + false, + true + ); + + if (mavenCompilePlugin != undefined) { + const sourceOrTag = mavenCompilePlugin .up() - .up() - .some( - c => { - if (c.node.nodeName !== 'artifactId') { - return false; - } - if (c.node.childNodes.length !== 1) { - return false; - } - return c.first().toString() === 'maven-compiler-plugin'; - }, - false, - true + .find(n => n.node.nodeName === 'source' || n.node.nodeName === 'target', false, true); + + if (sourceOrTag !== undefined) { + core.debug( + `Found java version: '${sourceOrTag + .first() + .toString()}' defined on the maven-compiler-plugin'` ); - if (!isCompilerPlugin) { - return false; + return sourceOrTag.first().toString(); } + } - return true; - }); - - return source?.first().toString() ?? null; + return null; } function parseBuildGradleFile(buildGradle: string): any { @@ -267,13 +248,13 @@ function parseBuildGradleFile(buildGradle: string): any { } function getByJavaLibraryPlugin(buildGradle: string) { - return getVersionByRegex(buildGradle, 'JavaLanguageVersion.of((d+))'); + return getVersionByRegex(buildGradle, 'JavaLanguageVersion.of\\((\\d+)\\)'); } function getByJavaPlugin(buildGradle: string) { const possibleRegex = [ - 'sourceCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)', - 'targetCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)' + 'sourceCompatibility\\s?=\\s?JavaVersion.VERSION_(?:1_)?(\\d+)', + 'targetCompatibility\\s?=\\s?JavaVersion.VERSION_(?:1_)?(\\d+)' ]; for (var regex of possibleRegex) {