2022-01-03 08:52:11 +01:00
import * as c from './constants'
import * as core from '@actions/core'
2022-11-03 17:13:30 +01:00
import * as github from '@actions/github'
2022-01-03 08:52:11 +01:00
import * as httpClient from '@actions/http-client'
import * as tc from '@actions/tool-cache'
2022-03-11 10:35:03 +01:00
import { ExecOptions , exec as e } from '@actions/exec'
2022-03-03 17:24:24 +01:00
import { readFileSync , readdirSync } from 'fs'
2022-01-03 08:52:11 +01:00
import { Octokit } from '@octokit/core'
2022-03-03 17:24:24 +01:00
import { createHash } from 'crypto'
2022-01-03 08:52:11 +01:00
import { join } from 'path'
// Set up Octokit in the same way as @actions/github (see https://git.io/Jy9YP)
const baseUrl = process . env [ 'GITHUB_API_URL' ] || 'https://api.github.com'
const GitHub = Octokit . defaults ( {
baseUrl ,
request : {
agent : new httpClient . HttpClient ( ) . getAgent ( baseUrl )
}
} )
2022-03-11 10:35:03 +01:00
export async function exec (
commandLine : string ,
args? : string [ ] ,
options? : ExecOptions | undefined
) : Promise < void > {
const exitCode = await e ( commandLine , args , options )
if ( exitCode !== 0 ) {
throw new Error (
` ' ${ [ commandLine ]
. concat ( args || [ ] )
. join ( ' ' ) } ' exited with a non - zero code : $ { exitCode } `
)
}
}
2022-01-03 08:52:11 +01:00
export async function getLatestRelease (
repo : string
) : Promise < c.LatestReleaseResponse [ 'data' ] > {
2022-11-03 17:13:30 +01:00
const githubToken = getGitHubToken ( )
2022-01-03 08:52:11 +01:00
const options = githubToken . length > 0 ? { auth : githubToken } : { }
const octokit = new GitHub ( options )
return (
await octokit . request ( 'GET /repos/{owner}/{repo}/releases/latest' , {
owner : c.GRAALVM_GH_USER ,
repo
} )
) . data
}
export async function downloadAndExtractJDK (
downloadUrl : string
) : Promise < string > {
2022-03-03 17:24:24 +01:00
return findJavaHomeInSubfolder (
await extract ( await tc . downloadTool ( downloadUrl ) )
)
2022-03-01 10:50:00 +01:00
}
export async function downloadExtractAndCacheJDK (
2022-03-03 17:24:24 +01:00
downloader : ( ) = > Promise < string > ,
2022-03-01 10:50:00 +01:00
toolName : string ,
version : string
) : Promise < string > {
2022-03-08 11:12:22 +01:00
const semVersion = toSemVer ( version )
let toolPath = tc . find ( toolName , semVersion )
2022-03-01 10:50:00 +01:00
if ( toolPath ) {
core . info ( ` Found ${ toolName } ${ version } in tool-cache @ ${ toolPath } ` )
} else {
2022-03-03 17:24:24 +01:00
const extractDir = await extract ( await downloader ( ) )
2022-03-01 10:50:00 +01:00
core . info ( ` Adding ${ toolName } ${ version } to tool-cache ... ` )
2022-03-08 11:12:22 +01:00
toolPath = await tc . cacheDir ( extractDir , toolName , semVersion )
2022-03-01 10:50:00 +01:00
}
return findJavaHomeInSubfolder ( toolPath )
}
2022-03-03 17:24:24 +01:00
export function calculateSHA256 ( filePath : string ) : string {
const hashSum = createHash ( 'sha256' )
hashSum . update ( readFileSync ( filePath ) )
return hashSum . digest ( 'hex' )
}
async function extract ( downloadPath : string ) : Promise < string > {
if ( c . GRAALVM_FILE_EXTENSION === '.tar.gz' ) {
2022-03-01 10:50:00 +01:00
return await tc . extractTar ( downloadPath )
2022-03-03 17:24:24 +01:00
} else if ( c . GRAALVM_FILE_EXTENSION === '.zip' ) {
2022-03-01 10:50:00 +01:00
return await tc . extractZip ( downloadPath )
2022-01-03 08:52:11 +01:00
} else {
2022-03-03 17:24:24 +01:00
throw new Error (
` Unexpected filetype downloaded: ${ c . GRAALVM_FILE_EXTENSION } `
)
2022-01-03 08:52:11 +01:00
}
}
2022-03-01 10:50:00 +01:00
function findJavaHomeInSubfolder ( searchPath : string ) : string {
2022-01-03 08:52:11 +01:00
const baseContents = readdirSync ( searchPath )
if ( baseContents . length === 1 ) {
return join ( searchPath , baseContents [ 0 ] , c . JDK_HOME_SUFFIX )
} else {
throw new Error (
` Unexpected amount of directory items found: ${ baseContents . length } `
)
}
}
2022-03-08 11:12:22 +01:00
/ * *
* This helper turns GraalVM version numbers ( e . g . , ` 22.0.0.2 ` ) into valid
* semver . org versions ( e . g . , ` 22.0.0-2 ` ) , which is needed because
* @actions / tool - cache uses ` semver ` to validate versions .
* /
2022-11-02 14:00:51 +01:00
export function toSemVer ( version : string ) : string {
2022-03-08 11:12:22 +01:00
const parts = version . split ( '.' )
const major = parts [ 0 ]
const minor = parts . length > 1 ? parts [ 1 ] : '0'
const patch = parts . length > 2 ? parts . slice ( 2 ) . join ( '-' ) : '0'
return ` ${ major } . ${ minor } . ${ patch } `
}
2022-11-03 17:13:30 +01:00
export function isPREvent ( ) : boolean {
return process . env [ c . ENV_GITHUB_EVENT_NAME ] === c . EVENT_NAME_PULL_REQUEST
}
function getGitHubToken ( ) : string {
return core . getInput ( c . INPUT_GITHUB_TOKEN )
}
export async function createPRComment ( content : string ) : Promise < void > {
if ( ! isPREvent ( ) ) {
throw new Error ( 'Not a PR event.' )
}
const context = github . context
2022-11-08 18:15:51 +01:00
try {
await github . getOctokit ( getGitHubToken ( ) ) . rest . issues . createComment ( {
. . . context . repo ,
issue_number : context.payload.pull_request?.number as number ,
body : content
} )
} catch ( err ) {
core . error (
` Failed to create pull request comment. Please make sure this job has 'write' permissions for the 'pull-requests' scope (see https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#permissions)? Internal error: ${ err } `
)
}
2022-11-03 17:13:30 +01:00
}