Files

118 lines
3.0 KiB
TypeScript
Raw Permalink Normal View History

2019-05-21 14:38:29 -04:00
import * as os from 'os'
2020-09-23 11:19:20 -04:00
import {toCommandValue} from './utils'
2019-05-21 14:38:29 -04:00
2019-05-21 14:44:37 -04:00
// For internal use, subject to change.
// We use any as a valid input type
/* eslint-disable @typescript-eslint/no-explicit-any */
export interface CommandProperties {
[key: string]: any
2019-05-21 17:08:25 -04:00
}
2019-05-21 14:38:29 -04:00
/**
2025-03-13 04:47:49 -07:00
* Issues a command to the GitHub Actions runner
*
* @param command - The command name to issue
* @param properties - Additional properties for the command (key-value pairs)
* @param message - The message to include with the command
* @remarks
* This function outputs a specially formatted string to stdout that the Actions
* runner interprets as a command. These commands can control workflow behavior,
* set outputs, create annotations, mask values, and more.
2019-05-21 14:38:29 -04:00
*
* Command Format:
2020-01-18 23:52:44 -05:00
* ::name key=value,key=value::message
2019-05-21 14:38:29 -04:00
*
2025-03-13 04:47:49 -07:00
* @example
* ```typescript
* // Issue a warning annotation
* issueCommand('warning', {}, 'This is a warning message');
* // Output: ::warning::This is a warning message
*
* // Set an environment variable
* issueCommand('set-env', { name: 'MY_VAR' }, 'some value');
* // Output: ::set-env name=MY_VAR::some value
*
* // Add a secret mask
* issueCommand('add-mask', {}, 'secretValue123');
* // Output: ::add-mask::secretValue123
* ```
*
* @internal
* This is an internal utility function that powers the public API functions
* such as setSecret, warning, error, and exportVariable.
2019-05-21 14:38:29 -04:00
*/
export function issueCommand(
command: string,
2019-05-21 17:08:25 -04:00
properties: CommandProperties,
message: any
2019-05-21 17:08:25 -04:00
): void {
2019-05-21 14:38:29 -04:00
const cmd = new Command(command, properties, message)
process.stdout.write(cmd.toString() + os.EOL)
}
export function issue(name: string, message = ''): void {
2019-05-21 14:38:29 -04:00
issueCommand(name, {}, message)
}
2019-09-09 11:46:17 -04:00
const CMD_STRING = '::'
2019-05-21 14:38:29 -04:00
class Command {
2019-05-21 17:08:25 -04:00
private readonly command: string
private readonly message: string
private readonly properties: CommandProperties
constructor(command: string, properties: CommandProperties, message: string) {
2019-05-21 14:38:29 -04:00
if (!command) {
command = 'missing.command'
}
this.command = command
this.properties = properties
this.message = message
}
2019-05-21 17:08:25 -04:00
toString(): string {
2019-09-09 11:46:17 -04:00
let cmdStr = CMD_STRING + this.command
2019-05-21 14:38:29 -04:00
if (this.properties && Object.keys(this.properties).length > 0) {
cmdStr += ' '
2019-12-18 13:23:16 -05:00
let first = true
2019-05-21 14:38:29 -04:00
for (const key in this.properties) {
if (this.properties.hasOwnProperty(key)) {
const val = this.properties[key]
if (val) {
2019-12-18 13:23:16 -05:00
if (first) {
first = false
} else {
cmdStr += ','
}
2020-01-18 23:52:44 -05:00
cmdStr += `${key}=${escapeProperty(val)}`
2019-05-21 14:38:29 -04:00
}
}
}
}
2020-01-18 23:52:44 -05:00
cmdStr += `${CMD_STRING}${escapeData(this.message)}`
2019-05-21 14:38:29 -04:00
return cmdStr
}
}
function escapeData(s: any): string {
return toCommandValue(s)
2020-01-18 23:52:44 -05:00
.replace(/%/g, '%25')
.replace(/\r/g, '%0D')
.replace(/\n/g, '%0A')
2019-05-21 14:38:29 -04:00
}
function escapeProperty(s: any): string {
return toCommandValue(s)
2020-01-18 23:52:44 -05:00
.replace(/%/g, '%25')
2019-05-21 14:38:29 -04:00
.replace(/\r/g, '%0D')
.replace(/\n/g, '%0A')
2020-01-18 23:52:44 -05:00
.replace(/:/g, '%3A')
.replace(/,/g, '%2C')
2019-05-21 14:45:27 -04:00
}