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

feat(core): add getBooleanInput function #725

Merged
merged 3 commits into from
Apr 28, 2021
Merged
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
16 changes: 11 additions & 5 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import * as core from '@actions/core';

#### Inputs/Outputs

Action inputs can be read with `getInput`. Outputs can be set with `setOutput` which makes them available to be mapped into inputs of other actions to ensure they are decoupled.
Action inputs can be read with `getInput` which returns a `string` or `getBooleanInput` which parses a boolean based on the [yaml 1.2 specification](https://yaml.org/spec/1.2/spec.html#id2804923). If `required` set to be false, the input should have a default value in `action.yml`.

Outputs can be set with `setOutput` which makes them available to be mapped into inputs of other actions to ensure they are decoupled.

```js
const myInput = core.getInput('inputName', { required: true });

const myBooleanInput = core.getBooleanInput('booleanInputName', { required: true });
core.setOutput('outputKey', 'outputVal');
```

Expand Down Expand Up @@ -66,7 +68,6 @@ catch (err) {

Note that `setNeutral` is not yet implemented in actions V2 but equivalent functionality is being planned.


#### Logging

Finally, this library provides some utilities for logging. Note that debug logging is hidden from the logs by default. This behavior can be toggled by enabling the [Step Debug Logs](../../docs/action-debugging.md#step-debug-logs).
Expand Down Expand Up @@ -118,6 +119,7 @@ const result = await core.group('Do something async', async () => {
Colored output is supported in the Action logs via standard [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code). 3/4 bit, 8 bit and 24 bit colors are all supported.

Foreground colors:

```js
// 3/4 bit
core.info('\u001b[35mThis foreground will be magenta')
Expand All @@ -130,6 +132,7 @@ core.info('\u001b[38;2;255;0;0mThis foreground will be bright red')
```

Background colors:

```js
// 3/4 bit
core.info('\u001b[43mThis background will be yellow');
Expand All @@ -156,6 +159,7 @@ core.info('\u001b[31;46mRed foreground with a cyan background and \u001b[1mbold
```

> Note: Escape codes reset at the start of each line

```js
core.info('\u001b[35mThis foreground will be magenta')
core.info('This foreground will reset to the default')
Expand All @@ -170,9 +174,10 @@ core.info(style.color.ansi16m.hex('#abcdef') + 'Hello world!')

#### Action state

You can use this library to save state and get state for sharing information between a given wrapper action:
You can use this library to save state and get state for sharing information between a given wrapper action:

**action.yml**:

**action.yml**
```yaml
name: 'Wrapper action sample'
inputs:
Expand All @@ -193,6 +198,7 @@ core.saveState("pidToKill", 12345);
```

In action's `cleanup.js`:

```js
const core = require('@actions/core');

Expand Down
32 changes: 32 additions & 0 deletions packages/core/__tests__/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ const testEnvVars = {
INPUT_MISSING: '',
'INPUT_SPECIAL_CHARS_\'\t"\\': '\'\t"\\ response ',
INPUT_MULTIPLE_SPACES_VARIABLE: 'I have multiple spaces',
INPUT_BOOLEAN_INPUT: 'true',
INPUT_BOOLEAN_INPUT_TRUE1: 'true',
INPUT_BOOLEAN_INPUT_TRUE2: 'True',
INPUT_BOOLEAN_INPUT_TRUE3: 'TRUE',
INPUT_BOOLEAN_INPUT_FALSE1: 'false',
INPUT_BOOLEAN_INPUT_FALSE2: 'False',
INPUT_BOOLEAN_INPUT_FALSE3: 'FALSE',
INPUT_WRONG_BOOLEAN_INPUT: 'wrong',

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

it('getInput gets non-required boolean input', () => {
expect(core.getBooleanInput('boolean input')).toBe(true)
})

it('getInput gets required input', () => {
expect(core.getBooleanInput('boolean input', {required: true})).toBe(true)
})

it('getBooleanInput handles boolean input', () => {
expect(core.getBooleanInput('boolean input true1')).toBe(true)
expect(core.getBooleanInput('boolean input true2')).toBe(true)
expect(core.getBooleanInput('boolean input true3')).toBe(true)
expect(core.getBooleanInput('boolean input false1')).toBe(false)
expect(core.getBooleanInput('boolean input false2')).toBe(false)
expect(core.getBooleanInput('boolean input false3')).toBe(false)
})

it('getBooleanInput handles wrong boolean input', () => {
expect(() => core.getBooleanInput('wrong boolean input')).toThrow(
'Input does not meet YAML 1.2 "Core Schema" specification: wrong boolean input\n' +
`Support boolean input list: \`true | True | TRUE | false | False | FALSE\``
)
})

it('setOutput produces the correct command', () => {
core.setOutput('some output', 'some value')
assertWriteCalls([
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,28 @@ export function getInput(name: string, options?: InputOptions): string {
return val.trim()
}

/**
* Gets the input value of the boolean type in the YAML 1.2 "core schema" specification.
* Support boolean input list: `true | True | TRUE | false | False | FALSE` .
* The return value is also in boolean type.
* ref: https://yaml.org/spec/1.2/spec.html#id2804923
*
* @param name name of the input to get
* @param options optional. See InputOptions.
* @returns boolean
*/
export function getBooleanInput(name: string, options?: InputOptions): boolean {
const trueValue = ['true', 'True', 'TRUE']
const falseValue = ['false', 'False', 'FALSE']
const val = getInput(name, options)
if (trueValue.includes(val)) return true
if (falseValue.includes(val)) return false
throw new TypeError(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if it was required: false?

Copy link
Contributor Author

@yi-Xu-0100 yi-Xu-0100 Sep 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chamini2 The required: false will be dealt with the getInput(Line 107) and get the default value. 👀

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if there is no default value, that will make val an empty string, which we don't consider in line 108 or 109. Resulting in throw new TypeError because trueValue.includes('') and falseValue.includes('') are both false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action inputs can be read with getInput which returns a string or getBooleanInput which parses a boolean based on the yaml 1.2 specification. If required set to be false, the input should have a default value in action.yml.

https://github.com/yi-Xu-0100/toolkit/blob/main/packages/core/README.md

@chamini2 Why a boolean type input have no default value? 👀

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I did not know this was expected. If required: false and no default I thought it had just to return null or ''. Thanks, sorry for the inconvenience.

`Input does not meet YAML 1.2 "Core Schema" specification: ${name}\n` +
`Support boolean input list: \`true | True | TRUE | false | False | FALSE\``
)
}

/**
* Sets the value of an output.
*
Expand Down