Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): handle boolean input values from yaml #362

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions packages/artifact/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/core/__tests__/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const testEnvVars = {
INPUT_MISSING: '',
'INPUT_SPECIAL_CHARS_\'\t"\\': '\'\t"\\ response ',
INPUT_MULTIPLE_SPACES_VARIABLE: 'I have multiple spaces',
INPUT_YAML_BOOLEAN_FALSE: 'OFF',
INPUT_YAML_BOOLEAN_TRUE: 'y',

// Save inputs
STATE_TEST_1: 'state_val'
Expand Down Expand Up @@ -99,6 +101,11 @@ describe('@actions/core', () => {
)
})

it('getInput handles boolean values', () => {
expect(core.getInput('yaml boolean false')).toBe(false)
expect(core.getInput('yaml boolean true')).toBe(true)
})

it('setOutput produces the correct command', () => {
core.setOutput('some output', 'some value')
assertWriteCalls([`::set-output name=some output::some value${os.EOL}`])
Expand Down
17 changes: 13 additions & 4 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,29 @@ export function addPath(inputPath: string): void {
process.env['PATH'] = `${inputPath}${path.delimiter}${process.env['PATH']}`
}

const isBoolRegex = /^(y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$/
const isTrueRegex = /^(y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON)$/
/**
* Gets the value of an input. The value is also trimmed.
*
* @param name name of the input to get
* @param options optional. See InputOptions.
* @returns string
* @returns {(string|boolean)}
*/
export function getInput(name: string, options?: InputOptions): string {
export function getInput(
name: string,
options?: InputOptions
): string | boolean {
const val: string =
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || ''
if (options && options.required && !val) {
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] ?? ''
if (options?.required && val === '') {
throw new Error(`Input required and not supplied: ${name}`)
}

if (isBoolRegex.test(val)) {
return isTrueRegex.test(val)
}

return val.trim()
}

Expand Down