Add save-state and set-output file commands (#1178)

This commit is contained in:
Francesco Renzi
2022-09-29 14:45:02 +01:00
committed by GitHub
parent 4df45177e4
commit b00a9fd033
3 changed files with 165 additions and 31 deletions

View File

@@ -1,10 +1,9 @@
import {issue, issueCommand} from './command'
import {issueCommand as issueFileCommand} from './file-command'
import {issueFileCommand, prepareKeyValueMessage} from './file-command'
import {toCommandProperties, toCommandValue} from './utils'
import * as os from 'os'
import * as path from 'path'
import {v4 as uuidv4} from 'uuid'
import {OidcClient} from './oidc-utils'
@@ -87,26 +86,10 @@ export function exportVariable(name: string, val: any): void {
const filePath = process.env['GITHUB_ENV'] || ''
if (filePath) {
const delimiter = `ghadelimiter_${uuidv4()}`
// These should realistically never happen, but just in case someone finds a way to exploit uuid generation let's not allow keys or values that contain the delimiter.
if (name.includes(delimiter)) {
throw new Error(
`Unexpected input: name should not contain the delimiter "${delimiter}"`
)
}
if (convertedVal.includes(delimiter)) {
throw new Error(
`Unexpected input: value should not contain the delimiter "${delimiter}"`
)
}
const commandValue = `${name}<<${delimiter}${os.EOL}${convertedVal}${os.EOL}${delimiter}`
issueFileCommand('ENV', commandValue)
} else {
issueCommand('set-env', {name}, convertedVal)
return issueFileCommand('ENV', prepareKeyValueMessage(name, val))
}
issueCommand('set-env', {name}, convertedVal)
}
/**
@@ -207,8 +190,13 @@ export function getBooleanInput(name: string, options?: InputOptions): boolean {
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function setOutput(name: string, value: any): void {
const filePath = process.env['GITHUB_OUTPUT'] || ''
if (filePath) {
return issueFileCommand('OUTPUT', prepareKeyValueMessage(name, value))
}
process.stdout.write(os.EOL)
issueCommand('set-output', {name}, value)
issueCommand('set-output', {name}, toCommandValue(value))
}
/**
@@ -362,7 +350,12 @@ export async function group<T>(name: string, fn: () => Promise<T>): Promise<T> {
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function saveState(name: string, value: any): void {
issueCommand('save-state', {name}, value)
const filePath = process.env['GITHUB_STATE'] || ''
if (filePath) {
return issueFileCommand('STATE', prepareKeyValueMessage(name, value))
}
issueCommand('save-state', {name}, toCommandValue(value))
}
/**