2023-04-08 13:41:11 +01:00
import os from 'os' ;
2021-04-05 13:02:27 +03:00
import path from 'path' ;
import * as fs from 'fs' ;
import * as semver from 'semver' ;
2022-04-01 00:39:57 +05:30
import * as cache from '@actions/cache' ;
2021-04-05 13:02:27 +03:00
import * as core from '@actions/core' ;
2020-05-02 04:33:15 -07:00
2021-04-05 13:02:27 +03:00
import * as tc from '@actions/tool-cache' ;
2022-12-13 12:45:14 +01:00
import { INPUT_JOB_STATUS , DISTRIBUTIONS_ONLY_MAJOR_VERSION } from './constants' ;
2024-03-04 08:21:00 +00:00
import { create } from 'xmlbuilder2' ;
import { XMLBuilder } from 'xmlbuilder2/lib/interfaces' ;
2021-08-20 01:19:35 +08:00
2020-05-02 04:33:15 -07:00
export function getTempDir() {
2021-04-05 13:02:27 +03:00
let tempDirectory = process . env [ 'RUNNER_TEMP' ] || os . tmpdir ( ) ;
return tempDirectory ;
}
export function getBooleanInput ( inputName : string , defaultValue : boolean = false ) {
return ( core . getInput ( inputName ) || String ( defaultValue ) ) . toUpperCase ( ) === 'TRUE' ;
}
export function getVersionFromToolcachePath ( toolPath : string ) {
if ( toolPath ) {
return path . basename ( path . dirname ( toolPath ) ) ;
}
return toolPath ;
}
export async function extractJdkFile ( toolPath : string , extension? : string ) {
if ( ! extension ) {
extension = toolPath . endsWith ( '.tar.gz' ) ? 'tar.gz' : path . extname ( toolPath ) ;
if ( extension . startsWith ( '.' ) ) {
extension = extension . substring ( 1 ) ;
2020-05-02 04:33:15 -07:00
}
}
2021-04-05 13:02:27 +03:00
switch ( extension ) {
case 'tar.gz' :
case 'tar' :
return await tc . extractTar ( toolPath ) ;
case 'zip' :
return await tc . extractZip ( toolPath ) ;
default :
return await tc . extract7z ( toolPath ) ;
}
}
export function getDownloadArchiveExtension() {
return process . platform === 'win32' ? 'zip' : 'tar.gz' ;
2020-05-02 04:33:15 -07:00
}
2021-04-05 13:02:27 +03:00
export function isVersionSatisfies ( range : string , version : string ) : boolean {
if ( semver . valid ( range ) ) {
2022-03-31 20:49:24 +05:30
// if full version with build digit is provided as a range (such as '1.2.3+4')
2021-04-05 13:02:27 +03:00
// we should check for exact equal via compareBuild
// since semver.satisfies doesn't handle 4th digit
const semRange = semver . parse ( range ) ;
if ( semRange && semRange . build ? . length > 0 ) {
return semver . compareBuild ( range , version ) === 0 ;
}
}
return semver . satisfies ( version , range ) ;
}
export function getToolcachePath ( toolName : string , version : string , architecture : string ) {
const toolcacheRoot = process . env [ 'RUNNER_TOOL_CACHE' ] ? ? '' ;
const fullPath = path . join ( toolcacheRoot , toolName , version , architecture ) ;
if ( fs . existsSync ( fullPath ) ) {
return fullPath ;
}
return null ;
2020-05-02 04:33:15 -07:00
}
2021-08-20 01:19:35 +08:00
export function isJobStatusSuccess() {
const jobStatus = core . getInput ( INPUT_JOB_STATUS ) ;
return jobStatus === 'success' ;
}
2022-04-01 00:39:57 +05:30
export function isGhes ( ) : boolean {
const ghUrl = new URL ( process . env [ 'GITHUB_SERVER_URL' ] || 'https://github.com' ) ;
return ghUrl . hostname . toUpperCase ( ) !== 'GITHUB.COM' ;
}
export function isCacheFeatureAvailable ( ) : boolean {
2022-12-16 23:04:57 +09:00
if ( cache . isFeatureAvailable ( ) ) {
return true ;
}
2022-04-01 00:39:57 +05:30
2022-12-16 23:04:57 +09:00
if ( isGhes ( ) ) {
core . warning (
'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.'
) ;
2022-04-01 00:39:57 +05:30
return false ;
}
2022-12-16 23:04:57 +09:00
core . warning ( 'The runner was not able to contact the cache service. Caching will be skipped' ) ;
return false ;
2022-04-01 00:39:57 +05:30
}
2022-12-13 12:45:14 +01:00
export function getVersionFromFileContent (
2023-01-08 19:25:29 +00:00
fileName : string ,
2022-12-13 12:45:14 +01:00
content : string ,
distributionName : string
) : string | null {
2023-01-08 19:25:29 +00:00
let fileContent = null ;
2023-01-15 16:25:06 +00:00
core . debug ( ` Getting version from: ' ${ fileName } ' ` ) ;
2023-01-08 19:25:29 +00:00
if ( fileName . includes ( '.java-version' ) ) {
2023-01-15 16:25:06 +00:00
fileContent = parseJavaVersionFile ( content ) ;
} else if ( fileName . includes ( 'pom.xml' ) ) {
fileContent = parsePomXmlFile ( content ) ;
2023-01-08 19:25:29 +00:00
} else {
2023-01-15 16:25:06 +00:00
throw new Error (
` File ${ fileName } not supported, files supported: '.java-version' and 'pom.xml' `
) ;
2023-01-08 19:25:29 +00:00
}
2022-12-13 12:45:14 +01:00
if ( ! fileContent ) {
return null ;
}
core . debug ( ` Version from file ' ${ fileContent } ' ` ) ;
const tentativeVersion = avoidOldNotation ( fileContent ) ;
const rawVersion = tentativeVersion . split ( '-' ) [ 0 ] ;
let version = semver . validRange ( rawVersion ) ? tentativeVersion : semver.coerce ( tentativeVersion ) ;
core . debug ( ` Range version from file is ' ${ version } ' ` ) ;
if ( ! version ) {
return null ;
}
if ( DISTRIBUTIONS_ONLY_MAJOR_VERSION . includes ( distributionName ) ) {
const coerceVersion = semver . coerce ( version ) ? ? version ;
version = semver . major ( coerceVersion ) . toString ( ) ;
}
return version . toString ( ) ;
}
2023-01-08 19:25:29 +00:00
function parseJavaVersionFile ( content : string ) : string | null {
const javaVersionRegExp = /(?<version>(?<=(^|\s|\-))(\d+\S*))(\s|$)/ ;
const fileContent = content . match ( javaVersionRegExp ) ? . groups ? . version
? ( content . match ( javaVersionRegExp ) ? . groups ? . version as string )
: '' ;
if ( ! fileContent ) {
return null ;
}
2023-01-15 16:25:06 +00:00
return fileContent ;
2023-01-08 19:25:29 +00:00
}
2024-03-04 08:21:00 +00:00
function parsePomXmlFile ( xmlFileAsString : string ) : string | null {
2024-04-23 15:51:22 +02:00
const xmlDoc = create ( xmlFileAsString ) ;
const versionDefinitionTypes = [
getByMavenProperties ,
getBySpringBootSpecification ,
getByMavenCompilerPluginConfig
] ;
2023-01-08 19:25:29 +00:00
2024-04-23 15:51:22 +02:00
for ( const definitionType of versionDefinitionTypes ) {
const version = definitionType ( xmlDoc ) ;
2023-01-08 19:25:29 +00:00
2023-01-15 16:25:06 +00:00
if ( version !== null ) {
return version ;
}
}
2023-01-08 19:25:29 +00:00
2023-01-15 16:25:06 +00:00
return null ;
2023-01-08 19:25:29 +00:00
}
2024-04-23 15:51:22 +02:00
function getByMavenProperties ( xmlDoc : XMLBuilder ) : string | null {
2023-04-08 13:41:11 +01:00
const possibleTagsRegex = [
2024-03-04 08:21:00 +00:00
'maven.compiler.source' ,
'maven.compiler.release' ,
2023-01-15 16:25:06 +00:00
] ;
2023-01-08 19:25:29 +00:00
2023-04-08 13:41:11 +01:00
for ( var tag of possibleTagsRegex ) {
2024-03-04 08:21:00 +00:00
const version = getVersionByTagName ( xmlDoc , tag ) ;
2023-01-08 19:25:29 +00:00
2023-01-15 16:25:06 +00:00
if ( version !== null ) {
return version ;
}
}
2023-01-08 19:25:29 +00:00
2023-01-15 16:25:06 +00:00
return null ;
2023-01-08 19:25:29 +00:00
}
2024-03-04 08:21:00 +00:00
function getBySpringBootSpecification ( xmlDoc : XMLBuilder ) : string | null {
return getVersionByTagName ( xmlDoc , 'java.version' ) ;
2023-01-08 19:25:29 +00:00
}
2024-03-04 08:21:00 +00:00
function getVersionByTagName ( xmlDoc : XMLBuilder , tag : string ) : string | null {
const match = xmlDoc . find ( n = > n . node . nodeName === tag ) ;
2023-01-08 19:25:29 +00:00
2024-03-04 08:21:00 +00:00
if ( match !== undefined ) {
core . debug ( ` Found java version: ' ${ match . first ( ) . toString ( ) } ' using tag: ' ${ tag } ' ` ) ;
return match . first ( ) . toString ( ) ;
2023-04-08 13:41:11 +01:00
} else {
2023-01-15 16:25:06 +00:00
return null ;
2023-01-08 19:25:29 +00:00
}
}
2024-04-23 15:51:22 +02:00
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 . 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 ;
}
return true ;
} ) ;
return source ? . first ( ) . toString ( ) ? ? null ;
}
2022-12-13 12:45:14 +01:00
// By convention, action expects version 8 in the format `8.*` instead of `1.8`
function avoidOldNotation ( content : string ) : string {
return content . startsWith ( '1.' ) ? content . substring ( 2 ) : content ;
}