From b62614fa25245590d2c8efa5895b9c4e8b20d43e Mon Sep 17 00:00:00 2001 From: Tingluo Huang Date: Thu, 19 Sep 2019 22:02:45 -0400 Subject: [PATCH 1/5] add core method to saveState and getState. --- packages/core/__tests__/lib.test.ts | 20 +++++++++++++++---- packages/core/src/core.ts | 30 ++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/packages/core/__tests__/lib.test.ts b/packages/core/__tests__/lib.test.ts index 0bbe43d8..8124f596 100644 --- a/packages/core/__tests__/lib.test.ts +++ b/packages/core/__tests__/lib.test.ts @@ -17,7 +17,10 @@ const testEnvVars = { INPUT_MY_INPUT: 'val', INPUT_MISSING: '', 'INPUT_SPECIAL_CHARS_\'\t"\\': '\'\t"\\ response ', - INPUT_MULTIPLE_SPACES_VARIABLE: 'I have multiple spaces' + INPUT_MULTIPLE_SPACES_VARIABLE: 'I have multiple spaces', + + // Save inputs + STATE_state_1: 'state_val' } describe('@actions/core', () => { @@ -93,17 +96,17 @@ describe('@actions/core', () => { }) it('getInput gets required input', () => { - expect(core.getInput('my input', {required: true})).toBe('val') + expect(core.getInput('my input', { required: true })).toBe('val') }) it('getInput throws on missing required input', () => { - expect(() => core.getInput('missing', {required: true})).toThrow( + expect(() => core.getInput('missing', { required: true })).toThrow( 'Input required and not supplied: missing' ) }) it('getInput does not throw on missing non-required input', () => { - expect(core.getInput('missing', {required: false})).toBe('') + expect(core.getInput('missing', { required: false })).toBe('') }) it('getInput is case insensitive', () => { @@ -194,6 +197,15 @@ describe('@actions/core', () => { core.debug('\r\ndebug\n') assertWriteCalls([`::debug::%0D%0Adebug%0A${os.EOL}`]) }) + + it('saveState produces the correct command', () => { + core.saveState('state_1', 'some value') + assertWriteCalls([`::save-state name=state_1,::some value${os.EOL}`]) + }) + + it('getState gets wrapper action state', () => { + expect(core.getState('state_1')).toBe('state_val') + }) }) // Assert that process.stdout.write calls called only with the given arguments. diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index 072f2323..4bf44356 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -1,4 +1,4 @@ -import {issue, issueCommand} from './command' +import { issue, issueCommand } from './command' import * as os from 'os' import * as path from 'path' @@ -37,7 +37,7 @@ export enum ExitCode { */ export function exportVariable(name: string, val: string): void { process.env[name] = val - issueCommand('set-env', {name}, val) + issueCommand('set-env', { name }, val) } /** @@ -86,7 +86,7 @@ export function getInput(name: string, options?: InputOptions): string { * @param value value to store */ export function setOutput(name: string, value: string): void { - issueCommand('set-output', {name}, value) + issueCommand('set-output', { name }, value) } //----------------------------------------------------------------------- @@ -178,3 +178,27 @@ export async function group(name: string, fn: () => Promise): Promise { return result } + +//----------------------------------------------------------------------- +// Wrapper action state +//----------------------------------------------------------------------- + +/** + * Saves state for current action, the state can only be retrieved by this action's post job execution. + * + * @param name name of the state to store + * @param value value to store + */ +export function saveState(name: string, value: string): void { + issueCommand('save-state', { name }, value) +} + +/** + * Gets the value of an state set by this action's main execution. + * + * @param name name of the state to get + * @returns string + */ +export function getState(name: string): string { + return process.env[`STATE_${name}`] || '' +} From 4d152182528e3e7e0c7448cfc8230dbe25907b3f Mon Sep 17 00:00:00 2001 From: Tingluo Huang Date: Thu, 19 Sep 2019 22:14:12 -0400 Subject: [PATCH 2/5] fix lint. --- packages/core/__tests__/lib.test.ts | 10 +++++----- packages/core/src/core.ts | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/core/__tests__/lib.test.ts b/packages/core/__tests__/lib.test.ts index 8124f596..316d653d 100644 --- a/packages/core/__tests__/lib.test.ts +++ b/packages/core/__tests__/lib.test.ts @@ -20,7 +20,7 @@ const testEnvVars = { INPUT_MULTIPLE_SPACES_VARIABLE: 'I have multiple spaces', // Save inputs - STATE_state_1: 'state_val' + STATE_TEST_1: 'state_val' } describe('@actions/core', () => { @@ -96,17 +96,17 @@ describe('@actions/core', () => { }) it('getInput gets required input', () => { - expect(core.getInput('my input', { required: true })).toBe('val') + expect(core.getInput('my input', { required: true})).toBe('val') }) it('getInput throws on missing required input', () => { - expect(() => core.getInput('missing', { required: true })).toThrow( + expect(() => core.getInput('missing', { required: true})).toThrow( 'Input required and not supplied: missing' ) }) it('getInput does not throw on missing non-required input', () => { - expect(core.getInput('missing', { required: false })).toBe('') + expect(core.getInput('missing', { required: false})).toBe('') }) it('getInput is case insensitive', () => { @@ -204,7 +204,7 @@ describe('@actions/core', () => { }) it('getState gets wrapper action state', () => { - expect(core.getState('state_1')).toBe('state_val') + expect(core.getState('TEST_1')).toBe('state_val') }) }) diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index 4bf44356..b1a7054a 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -1,4 +1,4 @@ -import { issue, issueCommand } from './command' +import { issue, issueCommand} from './command' import * as os from 'os' import * as path from 'path' @@ -37,7 +37,7 @@ export enum ExitCode { */ export function exportVariable(name: string, val: string): void { process.env[name] = val - issueCommand('set-env', { name }, val) + issueCommand('set-env', {name}, val) } /** From 81b71dc6e643b3625eeaa6ca013b8fc5dc3fccbf Mon Sep 17 00:00:00 2001 From: Tingluo Huang Date: Thu, 19 Sep 2019 22:18:51 -0400 Subject: [PATCH 3/5] fix lint. --- packages/core/__tests__/lib.test.ts | 6 +++--- packages/core/src/core.ts | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/core/__tests__/lib.test.ts b/packages/core/__tests__/lib.test.ts index 316d653d..364cfd19 100644 --- a/packages/core/__tests__/lib.test.ts +++ b/packages/core/__tests__/lib.test.ts @@ -96,17 +96,17 @@ describe('@actions/core', () => { }) it('getInput gets required input', () => { - expect(core.getInput('my input', { required: true})).toBe('val') + expect(core.getInput('my input', {required: true})).toBe('val') }) it('getInput throws on missing required input', () => { - expect(() => core.getInput('missing', { required: true})).toThrow( + expect(() => core.getInput('missing', {required: true})).toThrow( 'Input required and not supplied: missing' ) }) it('getInput does not throw on missing non-required input', () => { - expect(core.getInput('missing', { required: false})).toBe('') + expect(core.getInput('missing', {required: false})).toBe('') }) it('getInput is case insensitive', () => { diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index b1a7054a..3fee6bb5 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -1,4 +1,4 @@ -import { issue, issueCommand} from './command' +import {issue, issueCommand} from './command' import * as os from 'os' import * as path from 'path' @@ -86,7 +86,7 @@ export function getInput(name: string, options?: InputOptions): string { * @param value value to store */ export function setOutput(name: string, value: string): void { - issueCommand('set-output', { name }, value) + issueCommand('set-output', {name}, value) } //----------------------------------------------------------------------- @@ -190,7 +190,7 @@ export async function group(name: string, fn: () => Promise): Promise { * @param value value to store */ export function saveState(name: string, value: string): void { - issueCommand('save-state', { name }, value) + issueCommand('save-state', {name}, value) } /** From 5ce4932391d819ac86818818d7865130480c40f1 Mon Sep 17 00:00:00 2001 From: Tingluo Huang Date: Thu, 3 Oct 2019 00:41:30 -0400 Subject: [PATCH 4/5] update doc. --- packages/core/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/core/README.md b/packages/core/README.md index 58a8287f..46e405f2 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -94,4 +94,37 @@ const result = await core.group('Do something async', async () => { const response = await doSomeHTTPRequest() return response }) +``` + +#### Action state + +You can use this library to save state and get state for sharing information between a given wrapper action: + +**action.yml** +```yaml +name: 'Wrapper action sample' +inputs: + name: + default: 'GitHub' +runs: + using: 'node12' + main: 'main.js' + post: 'cleanup.js' +``` + +In action's `main.js`: + +```js +const core = require('@actions/core'); + +core.saveState("pidToKill", 12345); +``` + +In action's `cleanup.js`: +```js +const core = require('@actions/core'); + +var pid = core.getState("pidToKill"); + +kill(pid); ``` \ No newline at end of file From ae706665a165e8345412b4da5e456eda090fb39e Mon Sep 17 00:00:00 2001 From: Tingluo Huang Date: Thu, 3 Oct 2019 14:48:21 -0400 Subject: [PATCH 5/5] PR feedback. --- packages/core/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/README.md b/packages/core/README.md index 46e405f2..d34b9508 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -126,5 +126,5 @@ const core = require('@actions/core'); var pid = core.getState("pidToKill"); -kill(pid); +process.kill(pid); ``` \ No newline at end of file