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.
This commit is contained in:
augustomelo 2024-04-29 16:52:52 +01:00
parent c8dd9f9d18
commit 897cdc868d
3 changed files with 55 additions and 120 deletions

55
dist/cleanup/index.js vendored
View file

@ -103805,7 +103805,7 @@ function getVersionFromFile(fileName, content, distributionName) {
parsedVersion = parseBuildGradleFile(content); parsedVersion = parseBuildGradleFile(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' 'pom.xml' and 'build.gradle'`);
} }
if (!parsedVersion) { if (!parsedVersion) {
return null; return null;
@ -103865,7 +103865,7 @@ function getBySpringBootSpecification(xmlDoc) {
return getVersionByTagName(xmlDoc, 'java.version'); return getVersionByTagName(xmlDoc, 'java.version');
} }
function getVersionByTagName(xmlDoc, tag) { 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) { if (match !== undefined) {
core.debug(`Found java version: '${match.first().toString()}' using tag: '${tag}'`); core.debug(`Found java version: '${match.first().toString()}' using tag: '${tag}'`);
return match.first().toString(); return match.first().toString();
@ -103875,42 +103875,19 @@ function getVersionByTagName(xmlDoc, tag) {
} }
} }
function getByMavenCompilerPluginConfig(xmlDoc) { function getByMavenCompilerPluginConfig(xmlDoc) {
var _a; const mavenCompilePlugin = xmlDoc.find(n => n.node.nodeName === 'artifactId' && n.first().toString() === 'maven-compiler-plugin', false, true);
const source = xmlDoc.find(n => { if (mavenCompilePlugin != undefined) {
// Find <source> node const sourceOrTag = mavenCompilePlugin
if (n.node.nodeName !== 'source') {
return false;
}
if (n.node.childNodes.length !== 1) {
return false;
}
// Must be within <configuration>
if (n.up().node.nodeName !== 'configuration') {
return false;
}
// Which must be inside <plugin>
if (n.up().up().node.nodeName !== 'plugin') {
return false;
}
// Make sure the plugin is maven-compiler-plugin
const isCompilerPlugin = n
.up() .up()
.up() .find(n => n.node.nodeName === 'source' || n.node.nodeName === 'target', false, true);
.some(c => { if (sourceOrTag !== undefined) {
if (c.node.nodeName !== 'artifactId') { core.debug(`Found java version: '${sourceOrTag
return false; .first()
} .toString()}' defined on the maven-compiler-plugin'`);
if (c.node.childNodes.length !== 1) { return sourceOrTag.first().toString();
return false;
}
return c.first().toString() === 'maven-compiler-plugin';
}, false, true);
if (!isCompilerPlugin) {
return false;
} }
return true; }
}); return null;
return (_a = source === null || source === void 0 ? void 0 : source.first().toString()) !== null && _a !== void 0 ? _a : null;
} }
function parseBuildGradleFile(buildGradle) { function parseBuildGradleFile(buildGradle) {
const versionDefinitionTypes = [getByJavaLibraryPlugin, getByJavaPlugin]; const versionDefinitionTypes = [getByJavaLibraryPlugin, getByJavaPlugin];
@ -103923,12 +103900,12 @@ function parseBuildGradleFile(buildGradle) {
return null; return null;
} }
function getByJavaLibraryPlugin(buildGradle) { function getByJavaLibraryPlugin(buildGradle) {
return getVersionByRegex(buildGradle, 'JavaLanguageVersion.of((d+))'); return getVersionByRegex(buildGradle, 'JavaLanguageVersion.of\\((\\d+)\\)');
} }
function getByJavaPlugin(buildGradle) { function getByJavaPlugin(buildGradle) {
const possibleRegex = [ const possibleRegex = [
'sourceCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)', 'sourceCompatibility\\s?=\\s?JavaVersion.VERSION_(?:1_)?(\\d+)',
'targetCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)' 'targetCompatibility\\s?=\\s?JavaVersion.VERSION_(?:1_)?(\\d+)'
]; ];
for (var regex of possibleRegex) { for (var regex of possibleRegex) {
const version = getVersionByRegex(buildGradle, regex); const version = getVersionByRegex(buildGradle, regex);

55
dist/setup/index.js vendored
View file

@ -105462,7 +105462,7 @@ function getVersionFromFile(fileName, content, distributionName) {
parsedVersion = parseBuildGradleFile(content); parsedVersion = parseBuildGradleFile(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' 'pom.xml' and 'build.gradle'`);
} }
if (!parsedVersion) { if (!parsedVersion) {
return null; return null;
@ -105522,7 +105522,7 @@ function getBySpringBootSpecification(xmlDoc) {
return getVersionByTagName(xmlDoc, 'java.version'); return getVersionByTagName(xmlDoc, 'java.version');
} }
function getVersionByTagName(xmlDoc, tag) { 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) { if (match !== undefined) {
core.debug(`Found java version: '${match.first().toString()}' using tag: '${tag}'`); core.debug(`Found java version: '${match.first().toString()}' using tag: '${tag}'`);
return match.first().toString(); return match.first().toString();
@ -105532,42 +105532,19 @@ function getVersionByTagName(xmlDoc, tag) {
} }
} }
function getByMavenCompilerPluginConfig(xmlDoc) { function getByMavenCompilerPluginConfig(xmlDoc) {
var _a; const mavenCompilePlugin = xmlDoc.find(n => n.node.nodeName === 'artifactId' && n.first().toString() === 'maven-compiler-plugin', false, true);
const source = xmlDoc.find(n => { if (mavenCompilePlugin != undefined) {
// Find <source> node const sourceOrTag = mavenCompilePlugin
if (n.node.nodeName !== 'source') {
return false;
}
if (n.node.childNodes.length !== 1) {
return false;
}
// Must be within <configuration>
if (n.up().node.nodeName !== 'configuration') {
return false;
}
// Which must be inside <plugin>
if (n.up().up().node.nodeName !== 'plugin') {
return false;
}
// Make sure the plugin is maven-compiler-plugin
const isCompilerPlugin = n
.up() .up()
.up() .find(n => n.node.nodeName === 'source' || n.node.nodeName === 'target', false, true);
.some(c => { if (sourceOrTag !== undefined) {
if (c.node.nodeName !== 'artifactId') { core.debug(`Found java version: '${sourceOrTag
return false; .first()
} .toString()}' defined on the maven-compiler-plugin'`);
if (c.node.childNodes.length !== 1) { return sourceOrTag.first().toString();
return false;
}
return c.first().toString() === 'maven-compiler-plugin';
}, false, true);
if (!isCompilerPlugin) {
return false;
} }
return true; }
}); return null;
return (_a = source === null || source === void 0 ? void 0 : source.first().toString()) !== null && _a !== void 0 ? _a : null;
} }
function parseBuildGradleFile(buildGradle) { function parseBuildGradleFile(buildGradle) {
const versionDefinitionTypes = [getByJavaLibraryPlugin, getByJavaPlugin]; const versionDefinitionTypes = [getByJavaLibraryPlugin, getByJavaPlugin];
@ -105580,12 +105557,12 @@ function parseBuildGradleFile(buildGradle) {
return null; return null;
} }
function getByJavaLibraryPlugin(buildGradle) { function getByJavaLibraryPlugin(buildGradle) {
return getVersionByRegex(buildGradle, 'JavaLanguageVersion.of((d+))'); return getVersionByRegex(buildGradle, 'JavaLanguageVersion.of\\((\\d+)\\)');
} }
function getByJavaPlugin(buildGradle) { function getByJavaPlugin(buildGradle) {
const possibleRegex = [ const possibleRegex = [
'sourceCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)', 'sourceCompatibility\\s?=\\s?JavaVersion.VERSION_(?:1_)?(\\d+)',
'targetCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)' 'targetCompatibility\\s?=\\s?JavaVersion.VERSION_(?:1_)?(\\d+)'
]; ];
for (var regex of possibleRegex) { for (var regex of possibleRegex) {
const version = getVersionByRegex(buildGradle, regex); const version = getVersionByRegex(buildGradle, regex);

View file

@ -119,7 +119,7 @@ export function getVersionFromFile(
parsedVersion = parseBuildGradleFile(content); parsedVersion = parseBuildGradleFile(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' 'pom.xml' and 'build.gradle'`
); );
} }
@ -198,7 +198,7 @@ function getBySpringBootSpecification(xmlDoc: XMLBuilder): string | null {
} }
function getVersionByTagName(xmlDoc: XMLBuilder, tag: string): 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) { if (match !== undefined) {
core.debug(`Found java version: '${match.first().toString()}' using tag: '${tag}'`); 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 { function getByMavenCompilerPluginConfig(xmlDoc: XMLBuilder): string | null {
const source = xmlDoc.find(n => { const mavenCompilePlugin = xmlDoc.find(
// Find <source> node n => n.node.nodeName === 'artifactId' && n.first().toString() === 'maven-compiler-plugin',
if (n.node.nodeName !== 'source') { false,
return false; true
} );
if (n.node.childNodes.length !== 1) {
return false; if (mavenCompilePlugin != undefined) {
} const sourceOrTag = mavenCompilePlugin
// Must be within <configuration>
if (n.up().node.nodeName !== 'configuration') {
return false;
}
// Which must be inside <plugin>
if (n.up().up().node.nodeName !== 'plugin') {
return false;
}
// Make sure the plugin is maven-compiler-plugin
const isCompilerPlugin = n
.up() .up()
.up() .find(n => n.node.nodeName === 'source' || n.node.nodeName === 'target', false, true);
.some(
c => { if (sourceOrTag !== undefined) {
if (c.node.nodeName !== 'artifactId') { core.debug(
return false; `Found java version: '${sourceOrTag
} .first()
if (c.node.childNodes.length !== 1) { .toString()}' defined on the maven-compiler-plugin'`
return false;
}
return c.first().toString() === 'maven-compiler-plugin';
},
false,
true
); );
if (!isCompilerPlugin) { return sourceOrTag.first().toString();
return false;
} }
}
return true; return null;
});
return source?.first().toString() ?? null;
} }
function parseBuildGradleFile(buildGradle: string): any { function parseBuildGradleFile(buildGradle: string): any {
@ -267,13 +248,13 @@ function parseBuildGradleFile(buildGradle: string): any {
} }
function getByJavaLibraryPlugin(buildGradle: string) { function getByJavaLibraryPlugin(buildGradle: string) {
return getVersionByRegex(buildGradle, 'JavaLanguageVersion.of((d+))'); return getVersionByRegex(buildGradle, 'JavaLanguageVersion.of\\((\\d+)\\)');
} }
function getByJavaPlugin(buildGradle: string) { function getByJavaPlugin(buildGradle: string) {
const possibleRegex = [ const possibleRegex = [
'sourceCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)', 'sourceCompatibility\\s?=\\s?JavaVersion.VERSION_(?:1_)?(\\d+)',
'targetCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)' 'targetCompatibility\\s?=\\s?JavaVersion.VERSION_(?:1_)?(\\d+)'
]; ];
for (var regex of possibleRegex) { for (var regex of possibleRegex) {