feat: finished implementation and added e2e tests

This commit is contained in:
AUGMELO 2023-01-15 16:25:06 +00:00
parent e1a751b5d3
commit d105eb1d73
6 changed files with 45157 additions and 4871 deletions

View File

@ -339,7 +339,7 @@ jobs:
shell: bash shell: bash
setup-java-version-from-pom-spring-boot-specification: setup-java-version-from-pom-spring-boot-specification:
name: ${{ matrix.distribution }} version from file 'openjdk64-11.0.2' - ${{ matrix.os }} name: ${{ matrix.distribution }} version from pom.xml 'openjdk64-11.0.2' - ${{ matrix.os }}
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
strategy: strategy:
fail-fast: false fail-fast: false
@ -349,10 +349,10 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Create .java-version file - name: Create pom.xml file
shell: bash shell: bash
run: | run: |
echo "<?xml version="1.0" encoding="UTF-8"?>" > pom.xml echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > pom.xml
echo "<project>" >> pom.xml echo "<project>" >> pom.xml
echo " <modelVersion>4.0.0</modelVersion>" >> pom.xml echo " <modelVersion>4.0.0</modelVersion>" >> pom.xml
echo " <groupId>com.test</groupId>" >> pom.xml echo " <groupId>com.test</groupId>" >> pom.xml
@ -380,5 +380,179 @@ jobs:
distribution: ${{ matrix.distribution }} distribution: ${{ matrix.distribution }}
java-version-file: 'pom.xml' java-version-file: 'pom.xml'
- name: Verify Java - name: Verify Java
run: bash __tests__/verify-java.sh "11.0.2" "${{ steps.setup-java.outputs.path }}" run: bash __tests__/verify-java.sh "11" "${{ steps.setup-java.outputs.path }}"
shell: bash
setup-java-version-from-pom-maven-compiler-source-specification:
name: ${{ matrix.distribution }} version from pom.xml 'openjdk64-11.0.2' - ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
distribution: ['adopt', 'zulu', 'liberica']
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Create pom.xml file
shell: bash
run: |
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > pom.xml
echo "<project>" >> pom.xml
echo " <modelVersion>4.0.0</modelVersion>" >> pom.xml
echo " <groupId>foo.bar</groupId>" >> pom.xml
echo " <artifactId>FooBarr</artifactId>" >> pom.xml
echo " <version>0.0.1-SNAPSHOT</version>" >> pom.xml
echo " <properties>" >> pom.xml
echo " <maven.compiler.source>1.8</maven.compiler.source>" >> pom.xml
echo " <maven.compiler.target>1.8</maven.compiler.target>" >> pom.xml
echo " </properties>" >> pom.xml
echo " <build>" >> pom.xml
echo " <plugins>" >> pom.xml
echo " <plugin>" >> pom.xml
echo " <groupId>org.apache.maven.plugins</groupId>" >> pom.xml
echo " <artifactId>maven-compiler-plugin</artifactId>" >> pom.xml
echo " <version>3.10.1</version>" >> pom.xml
echo " </plugin>" >> pom.xml
echo " </plugins>" >> pom.xml
echo " </build>" >> pom.xml
echo "</project>" >> pom.xml
- name: setup-java
uses: ./
id: setup-java
with:
distribution: ${{ matrix.distribution }}
java-version-file: 'pom.xml'
- name: Verify Java
run: bash __tests__/verify-java.sh "8" "${{ steps.setup-java.outputs.path }}"
shell: bash
setup-java-version-from-pom-maven-compiler-release-specification:
name: ${{ matrix.distribution }} version from pom.xml 'openjdk64-11.0.2' - ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
distribution: ['adopt', 'zulu', 'liberica']
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Create pom.xml file
shell: bash
run: |
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > pom.xml
echo "<project>" >> pom.xml
echo " <modelVersion>4.0.0</modelVersion>" >> pom.xml
echo " <groupId>foo.bar</groupId>" >> pom.xml
echo " <artifactId>FooBarr</artifactId>" >> pom.xml
echo " <version>0.0.1-SNAPSHOT</version>" >> pom.xml
echo " <properties>" >> pom.xml
echo " <maven.compiler.release>8</maven.compiler.release>" >> pom.xml
echo " </properties>" >> pom.xml
echo " <build>" >> pom.xml
echo " <plugins>" >> pom.xml
echo " <plugin>" >> pom.xml
echo " <groupId>org.apache.maven.plugins</groupId>" >> pom.xml
echo " <artifactId>maven-compiler-plugin</artifactId>" >> pom.xml
echo " <version>3.10.1</version>" >> pom.xml
echo " </plugin>" >> pom.xml
echo " </plugins>" >> pom.xml
echo " </build>" >> pom.xml
echo "</project>" >> pom.xml
- name: setup-java
uses: ./
id: setup-java
with:
distribution: ${{ matrix.distribution }}
java-version-file: 'pom.xml'
- name: Verify Java
run: bash __tests__/verify-java.sh "8" "${{ steps.setup-java.outputs.path }}"
shell: bash
setup-java-version-from-pom-maven-compiler-configuration-source-specification:
name: ${{ matrix.distribution }} version from pom.xml 'openjdk64-11.0.2' - ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
distribution: ['adopt', 'zulu', 'liberica']
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Create pom.xml file
shell: bash
run: |
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > pom.xml
echo "<project>" >> pom.xml
echo " <modelVersion>4.0.0</modelVersion>" >> pom.xml
echo " <groupId>foo.bar</groupId>" >> pom.xml
echo " <artifactId>FooBarr</artifactId>" >> pom.xml
echo " <version>0.0.1-SNAPSHOT</version>" >> pom.xml
echo " <build>" >> pom.xml
echo " <plugins>" >> pom.xml
echo " <plugin>" >> pom.xml
echo " <groupId>org.apache.maven.plugins</groupId>" >> pom.xml
echo " <artifactId>maven-compiler-plugin</artifactId>" >> pom.xml
echo " <version>3.10.1</version>" >> pom.xml
echo " <configuration>" >> pom.xml
echo " <source>1.8</source>" >> pom.xml
echo " <target>1.8</target>" >> pom.xml
echo " </configuration>" >> pom.xml
echo " </plugin>" >> pom.xml
echo " </plugins>" >> pom.xml
echo " </build>" >> pom.xml
echo "</project>" >> pom.xml
- name: setup-java
uses: ./
id: setup-java
with:
distribution: ${{ matrix.distribution }}
java-version-file: 'pom.xml'
- name: Verify Java
run: bash __tests__/verify-java.sh "8" "${{ steps.setup-java.outputs.path }}"
shell: bash
setup-java-version-from-pom-maven-compiler-configuration-release-specification:
name: ${{ matrix.distribution }} version from pom.xml 'openjdk64-11.0.2' - ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
distribution: ['adopt', 'zulu', 'liberica']
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Create pom.xml file
shell: bash
run: |
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > pom.xml
echo "<project>" >> pom.xml
echo " <modelVersion>4.0.0</modelVersion>" >> pom.xml
echo " <groupId>foo.bar</groupId>" >> pom.xml
echo " <artifactId>FooBarr</artifactId>" >> pom.xml
echo " <version>0.0.1-SNAPSHOT</version>" >> pom.xml
echo " <build>" >> pom.xml
echo " <plugins>" >> pom.xml
echo " <plugin>" >> pom.xml
echo " <groupId>org.apache.maven.plugins</groupId>" >> pom.xml
echo " <artifactId>maven-compiler-plugin</artifactId>" >> pom.xml
echo " <version>3.10.1</version>" >> pom.xml
echo " <configuration>" >> pom.xml
echo " <release>11</release>" >> pom.xml
echo " </configuration>" >> pom.xml
echo " </plugin>" >> pom.xml
echo " </plugins>" >> pom.xml
echo " </build>" >> pom.xml
echo "</project>" >> pom.xml
- name: setup-java
uses: ./
id: setup-java
with:
distribution: ${{ matrix.distribution }}
java-version-file: 'pom.xml'
- name: Verify Java
run: bash __tests__/verify-java.sh "11" "${{ steps.setup-java.outputs.path }}"
shell: bash shell: bash

40004
dist/cleanup/index.js vendored

File diff suppressed because it is too large Load Diff

4927
dist/setup/index.js vendored

File diff suppressed because it is too large Load Diff

4789
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -33,7 +33,8 @@
"@actions/io": "^1.0.2", "@actions/io": "^1.0.2",
"@actions/tool-cache": "^1.6.1", "@actions/tool-cache": "^1.6.1",
"semver": "^7.3.4", "semver": "^7.3.4",
"xmlbuilder2": "^2.4.0" "xmlbuilder2": "^2.4.0",
"xpath": "0.0.32"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^27.0.2", "@types/jest": "^27.0.2",

View File

@ -1,12 +1,15 @@
import os from 'os'; import os, { version } from 'os';
import path from 'path'; import path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
import * as semver from 'semver'; import * as semver from 'semver';
import * as cache from '@actions/cache'; import * as cache from '@actions/cache';
import * as core from '@actions/core'; import * as core from '@actions/core';
import { create as xmlCreate } from 'xmlbuilder2';
import { select } from 'xpath';
import * as tc from '@actions/tool-cache'; import * as tc from '@actions/tool-cache';
import { INPUT_JOB_STATUS, DISTRIBUTIONS_ONLY_MAJOR_VERSION } from './constants'; import { INPUT_JOB_STATUS, DISTRIBUTIONS_ONLY_MAJOR_VERSION } from './constants';
import { XMLBuilder } from 'xmlbuilder2/lib/interfaces';
export function getTempDir() { export function getTempDir() {
let tempDirectory = process.env['RUNNER_TEMP'] || os.tmpdir(); let tempDirectory = process.env['RUNNER_TEMP'] || os.tmpdir();
@ -106,12 +109,16 @@ export function getVersionFromFileContent(
distributionName: string distributionName: string
): string | null { ): string | null {
let fileContent = null; let fileContent = null;
core.debug(`Getting version from: '${fileName}'`);
if (fileName.includes('.java-version')) { if (fileName.includes('.java-version')) {
fileContent = parseJavaVersionFile(content) fileContent = parseJavaVersionFile(content);
} else if (fileName.includes('pom.xml')) { } else if (fileName.includes('pom.xml')) {
fileContent = parsePomXmlFile(content) fileContent = 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 (!fileContent) {
@ -148,75 +155,59 @@ function parseJavaVersionFile(content: string): string | null {
return null; return null;
} }
return fileContent return fileContent;
} }
function parsePomXmlFile(content: string): string | null { function parsePomXmlFile(xmlString: string): string | null {
const parser = new DOMParser(); const xmlDoc = xmlCreate(xmlString);
const xmlDoc = parser.parseFromString(content, "text/xml"); const versionDefinitionTypes = [getByMavenCompilerSpecification, getBySpringBootSpecification];
const versionDefinitionTypes = [
getByMavenCompilerSource,
getByMavenCompilerRelease,
getBySpringBootSpecification,
]
const versionFound = versionDefinitionTypes.filter(function(definitionType){
const version = definitionType(xmlDoc)
return version !== null for (var definitionType of versionDefinitionTypes) {
}) var version = definitionType(xmlDoc);
if (version !== null) {
return versionFound?.at(0)?.toString() ?? null; return version;
}
} }
function getByMavenCompilerSource(xmlDoc: Document): string | null { return null;
}
function getByMavenCompilerSpecification(xmlDoc: XMLBuilder): string | null {
const possibleTags = [ const possibleTags = [
"maven.compiler.source", '//properties/maven.compiler.source',
"source" '//configuration/source',
] '//properties/maven.compiler.release',
'//configuration/release'
];
const tagFound = possibleTags.filter(function(tag) { for (var tag of possibleTags) {
const version = getVersionByTagName(xmlDoc, tag) const version = getVersionByTagName(xmlDoc, tag);
return version !== null
})
return tagFound?.at(0)?.toString() ?? null
if (version !== null) {
return version;
}
} }
function getByMavenCompilerRelease(xmlDoc: Document): string | null { return null;
const possibleTags = [
"maven.compiler.release",
"release"
]
const tagFound = possibleTags.filter(function(tag) {
const version = getVersionByTagName(xmlDoc, tag)
return version !== null
})
return tagFound?.at(0)?.toString() ?? null
} }
function getBySpringBootSpecification(xmlDoc: Document): string | null { function getBySpringBootSpecification(xmlDoc: XMLBuilder): string | null {
return getVersionByTagName(xmlDoc, "java.version") return getVersionByTagName(xmlDoc, '//properties/java.version');
} }
function getVersionByTagName(xmlDoc: Document, tagName: string): string | null { function getVersionByTagName(xmlDoc: XMLBuilder, xpathQuery: string): string | null {
const element = xmlDoc.getElementsByTagName(tagName) const element = select(`string(${xpathQuery})`, xmlDoc.node as any);
if (element.length < 1) { core.debug(`Found: '${element}' using xpath query: '${xpathQuery}'`);
return null if (element == undefined || element.length == 0) {
return null;
} }
return element[0].textContent return element.toString();
} }
// By convention, action expects version 8 in the format `8.*` instead of `1.8` // By convention, action expects version 8 in the format `8.*` instead of `1.8`
function avoidOldNotation(content: string): string { function avoidOldNotation(content: string): string {
return content.startsWith('1.') ? content.substring(2) : content; return content.startsWith('1.') ? content.substring(2) : content;
} }