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);
}
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 <source> node
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
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);

55
dist/setup/index.js vendored
View file

@ -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 <source> node
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
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);

View file

@ -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 <source> node
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
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) {