2021-04-05 10:02:27 +00:00
import os from 'os' ;
import path from 'path' ;
import * as fs from 'fs' ;
import * as semver from 'semver' ;
2022-03-31 19:09:57 +00:00
import * as cache from '@actions/cache' ;
2021-04-05 10:02:27 +00:00
import * as core from '@actions/core' ;
2020-05-02 11:33:15 +00:00
2021-04-05 10:02:27 +00:00
import * as tc from '@actions/tool-cache' ;
2023-03-09 12:49:35 +00:00
import { INPUT_JOB_STATUS , DISTRIBUTIONS_ONLY_MAJOR_VERSION } from './constants' ;
2023-09-20 11:22:11 +00:00
import { OutgoingHttpHeaders } from 'http' ;
2021-08-19 17:19:35 +00:00
2020-05-02 11:33:15 +00:00
export function getTempDir() {
2023-03-09 12:49:35 +00:00
const tempDirectory = process . env [ 'RUNNER_TEMP' ] || os . tmpdir ( ) ;
2021-04-05 10:02:27 +00:00
return tempDirectory ;
}
2023-03-09 12:49:35 +00:00
export function getBooleanInput ( inputName : string , defaultValue = false ) {
return (
( core . getInput ( inputName ) || String ( defaultValue ) ) . toUpperCase ( ) === 'TRUE'
) ;
2021-04-05 10:02:27 +00:00
}
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 ) {
2023-03-09 12:49:35 +00:00
extension = toolPath . endsWith ( '.tar.gz' )
? 'tar.gz'
: path . extname ( toolPath ) ;
2021-04-05 10:02:27 +00:00
if ( extension . startsWith ( '.' ) ) {
extension = extension . substring ( 1 ) ;
2020-05-02 11:33:15 +00:00
}
}
2021-04-05 10:02:27 +00: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 11:33:15 +00:00
}
2021-04-05 10:02:27 +00:00
export function isVersionSatisfies ( range : string , version : string ) : boolean {
if ( semver . valid ( range ) ) {
2022-03-31 15:19:24 +00:00
// if full version with build digit is provided as a range (such as '1.2.3+4')
2021-04-05 10:02:27 +00: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 ) ;
}
2023-03-09 12:49:35 +00:00
export function getToolcachePath (
toolName : string ,
version : string ,
architecture : string
) {
2021-04-05 10:02:27 +00:00
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 11:33:15 +00:00
}
2021-08-19 17:19:35 +00:00
export function isJobStatusSuccess() {
const jobStatus = core . getInput ( INPUT_JOB_STATUS ) ;
return jobStatus === 'success' ;
}
2022-03-31 19:09:57 +00:00
export function isGhes ( ) : boolean {
2023-03-09 12:49:35 +00:00
const ghUrl = new URL (
process . env [ 'GITHUB_SERVER_URL' ] || 'https://github.com'
) ;
2022-03-31 19:09:57 +00:00
return ghUrl . hostname . toUpperCase ( ) !== 'GITHUB.COM' ;
}
export function isCacheFeatureAvailable ( ) : boolean {
2022-12-16 14:04:57 +00:00
if ( cache . isFeatureAvailable ( ) ) {
return true ;
}
2022-03-31 19:09:57 +00:00
2022-12-16 14:04:57 +00: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-03-31 19:09:57 +00:00
return false ;
}
2023-03-09 12:49:35 +00:00
core . warning (
'The runner was not able to contact the cache service. Caching will be skipped'
) ;
2022-12-16 14:04:57 +00:00
return false ;
2022-03-31 19:09:57 +00:00
}
2022-12-13 11:45:14 +00:00
export function getVersionFromFileContent (
content : string ,
2024-03-12 13:45:42 +00:00
distributionName : string ,
versionFile : string
2022-12-13 11:45:14 +00:00
) : string | null {
2024-03-12 13:45:42 +00:00
let javaVersionRegExp : RegExp ;
2024-03-14 14:12:58 +00:00
function getFileName ( versionFile : string ) {
return path . basename ( versionFile ) ;
}
const versionFileName = getFileName ( versionFile ) ;
if ( versionFileName == '.tool-versions' ) {
2024-03-12 13:45:42 +00:00
javaVersionRegExp =
/^(java\s+)(?:\S*-)?v?(?<version>(\d+)(\.\d+)?(\.\d+)?(\+\d+)?(-ea(\.\d+)?)?)$/m ;
} else {
2024-03-14 14:12:58 +00:00
javaVersionRegExp = /(?<version>(?<=(^|\s|-))(\d+\S*))(\s|$)/ ;
2024-03-12 13:45:42 +00:00
}
2022-12-13 11:45:14 +00:00
const fileContent = content . match ( javaVersionRegExp ) ? . groups ? . version
? ( content . match ( javaVersionRegExp ) ? . groups ? . version as string )
: '' ;
if ( ! fileContent ) {
return null ;
}
core . debug ( ` Version from file ' ${ fileContent } ' ` ) ;
const tentativeVersion = avoidOldNotation ( fileContent ) ;
const rawVersion = tentativeVersion . split ( '-' ) [ 0 ] ;
2023-03-09 12:49:35 +00:00
let version = semver . validRange ( rawVersion )
? tentativeVersion
: semver . coerce ( tentativeVersion ) ;
2022-12-13 11:45:14 +00:00
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 ( ) ;
}
// 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 ;
}
2023-04-10 08:29:19 +00:00
export function convertVersionToSemver ( version : number [ ] | string ) {
// Some distributions may use semver-like notation (12.10.2.1, 12.10.2.1.1)
const versionArray = Array . isArray ( version ) ? version : version.split ( '.' ) ;
const mainVersion = versionArray . slice ( 0 , 3 ) . join ( '.' ) ;
if ( versionArray . length > 3 ) {
return ` ${ mainVersion } + ${ versionArray . slice ( 3 ) . join ( '.' ) } ` ;
}
return mainVersion ;
}
2023-09-20 11:22:11 +00:00
export function getGitHubHttpHeaders ( ) : OutgoingHttpHeaders {
const token = core . getInput ( 'token' ) ;
const auth = ! token ? undefined : ` token ${ token } ` ;
2023-12-01 13:55:03 +00:00
2023-09-20 11:22:11 +00:00
const headers : OutgoingHttpHeaders = {
accept : 'application/vnd.github.VERSION.raw'
} ;
2023-12-01 13:55:03 +00:00
if ( auth ) {
headers . authorization = auth ;
}
2023-09-20 11:22:11 +00:00
return headers ;
}