From f8ead03bf1665f8dcae6821ddcb74e7ffc21cca1 Mon Sep 17 00:00:00 2001 From: Vladimir Starkov Date: Sun, 5 Jan 2025 11:54:59 +0100 Subject: [PATCH 1/3] feat: add core.getStringAsArray to parse array-like inputs (separated by either commas or new lines) --- packages/core/README.md | 1 + packages/core/__tests__/core.test.ts | 13 +++++++++++++ packages/core/src/core.ts | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/packages/core/README.md b/packages/core/README.md index ac8ced92bb..0799701b05 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -24,6 +24,7 @@ Outputs can be set with `setOutput` which makes them available to be mapped into const myInput = core.getInput('inputName', { required: true }); const myBooleanInput = core.getBooleanInput('booleanInputName', { required: true }); const myMultilineInput = core.getMultilineInput('multilineInputName', { required: true }); +const myArrayInput = core.getStringAsArray('stringAsArray', { required: true }); core.setOutput('outputKey', 'outputVal'); ``` diff --git a/packages/core/__tests__/core.test.ts b/packages/core/__tests__/core.test.ts index 2928788d7f..fedc3224d1 100644 --- a/packages/core/__tests__/core.test.ts +++ b/packages/core/__tests__/core.test.ts @@ -294,6 +294,19 @@ describe('@actions/core', () => { ).toEqual([' val1 ', ' val2 ', ' ']) }) + it('getStringAsArray; separated by either comma or new line', () => { + expect( + core.getStringAsArray(` + line 1, + line 2, + + comma 1, comma 2,, + `, { + trimWhitespace: false + }) + ).toEqual(['line 1', 'line 2', 'comma 1', 'comma 2']) + }) + it('legacy setOutput produces the correct command', () => { core.setOutput('some output', 'some value') assertWriteCalls([ diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index 0a1416937c..1725af68e9 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -160,6 +160,25 @@ export function getMultilineInput( return inputs.map(input => input.trim()) } +/** + * Gets the values of an array-like input (separated by comma or new lines). Each value is also trimmed. + * + * @param name name of the input to get + * @param options optional. See InputOptions. + * @returns string[] + * + */ +export function getStringAsArray( + name: string, + options?: InputOptions +): string[] { + const inputs: string[] = getInput(name, options) + .split(/[\n,]+/) + .map(s => s.trim()) + .filter(x => x !== ''); + return inputs; +} + /** * 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` . From f43201768f9c5a7307c88d326501608300f32c48 Mon Sep 17 00:00:00 2001 From: Vladimir Starkov Date: Sun, 5 Jan 2025 11:59:53 +0100 Subject: [PATCH 2/3] test: getStringAsArray doesnt take options, so those are removed from tests --- packages/core/__tests__/core.test.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/core/__tests__/core.test.ts b/packages/core/__tests__/core.test.ts index fedc3224d1..4556032a3d 100644 --- a/packages/core/__tests__/core.test.ts +++ b/packages/core/__tests__/core.test.ts @@ -301,9 +301,7 @@ describe('@actions/core', () => { line 2, comma 1, comma 2,, - `, { - trimWhitespace: false - }) + `) ).toEqual(['line 1', 'line 2', 'comma 1', 'comma 2']) }) From c35b6ed97d2529a80fadab052f1e6235d93892a3 Mon Sep 17 00:00:00 2001 From: Vladimir Starkov Date: Wed, 26 Mar 2025 23:02:43 +0100 Subject: [PATCH 3/3] update to a better getListInput name --- packages/core/README.md | 26 +++++++++++++------------- packages/core/__tests__/core.test.ts | 4 ++-- packages/core/src/core.ts | 4 ++-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/core/README.md b/packages/core/README.md index 0799701b05..e43da2fe04 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -24,7 +24,7 @@ Outputs can be set with `setOutput` which makes them available to be mapped into const myInput = core.getInput('inputName', { required: true }); const myBooleanInput = core.getBooleanInput('booleanInputName', { required: true }); const myMultilineInput = core.getMultilineInput('multilineInputName', { required: true }); -const myArrayInput = core.getStringAsArray('stringAsArray', { required: true }); +const myListInput = core.getListInput('listInputName', { required: true }); core.setOutput('outputKey', 'outputVal'); ``` @@ -80,11 +80,11 @@ const core = require('@actions/core'); const myInput = core.getInput('input'); try { core.debug('Inside try block'); - + if (!myInput) { core.warning('myInput was not set'); } - + if (core.isDebug()) { // curl -v https://github.com } else { @@ -120,7 +120,7 @@ const result = await core.group('Do something async', async () => { #### Annotations -This library has 3 methods that will produce [annotations](https://docs.github.com/en/rest/reference/checks#create-a-check-run). +This library has 3 methods that will produce [annotations](https://docs.github.com/en/rest/reference/checks#create-a-check-run). ```js core.error('This is a bad error, action may still succeed though.') @@ -133,9 +133,9 @@ These will surface to the UI in the Actions page and on Pull Requests. They look ![Annotations Image](../../docs/assets/annotations.png) -These annotations can also be attached to particular lines and columns of your source files to show exactly where a problem is occuring. +These annotations can also be attached to particular lines and columns of your source files to show exactly where a problem is occuring. -These options are: +These options are: ```typescript export interface AnnotationProperties { /** @@ -282,12 +282,12 @@ In action's `main.ts`: ```js const core = require('@actions/core'); async function getIDTokenAction(): Promise { - + const audience = core.getInput('audience', {required: false}) - + const id_token1 = await core.getIDToken() // ID Token with default audience const id_token2 = await core.getIDToken(audience) // ID token with custom audience - + // this id_token can be used to get access token from third party cloud providers } getIDTokenAction() @@ -299,13 +299,13 @@ In action's `actions.yml`: name: 'GetIDToken' description: 'Get ID token from Github OIDC provider' inputs: - audience: + audience: description: 'Audience for which the ID token is intended for' required: false outputs: - id_token1: + id_token1: description: 'ID token obtained from OIDC provider' - id_token2: + id_token2: description: 'ID token obtained from OIDC provider' runs: using: 'node12' @@ -484,4 +484,4 @@ core.summary.emptyBuffer() // Writes text in the buffer to the summary buffer file and empties the buffer, optionally overwriting all existing content in the summary file with buffer contents. Defaults to false. core.summary.write({overwrite: true}) -``` \ No newline at end of file +``` diff --git a/packages/core/__tests__/core.test.ts b/packages/core/__tests__/core.test.ts index 4556032a3d..1716d25ca4 100644 --- a/packages/core/__tests__/core.test.ts +++ b/packages/core/__tests__/core.test.ts @@ -294,9 +294,9 @@ describe('@actions/core', () => { ).toEqual([' val1 ', ' val2 ', ' ']) }) - it('getStringAsArray; separated by either comma or new line', () => { + it('getListInput; separated by either comma or new line', () => { expect( - core.getStringAsArray(` + core.getListInput(` line 1, line 2, diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index 1725af68e9..817f0caf43 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -161,14 +161,14 @@ export function getMultilineInput( } /** - * Gets the values of an array-like input (separated by comma or new lines). Each value is also trimmed. + * Gets the values of an list input (separated by comma or new lines). Each value is also trimmed. * * @param name name of the input to get * @param options optional. See InputOptions. * @returns string[] * */ -export function getStringAsArray( +export function getListInput( name: string, options?: InputOptions ): string[] {