From 2b23675424496872cc9e489cc9c37f5f085c26c6 Mon Sep 17 00:00:00 2001 From: Technote Date: Sun, 19 Jan 2020 04:08:17 +0900 Subject: [PATCH 1/3] chore: tweaks --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 9f64bb24..c9c7552f 100644 --- a/action.yml +++ b/action.yml @@ -41,7 +41,7 @@ outputs: description: 'git diff results' branding: # https://feathericons.com/ - icon: 'package' + icon: 'file' color: 'orange' runs: using: node12 From 4b7ba2ec8a5bc655c450fbb05af013ea6ec238e4 Mon Sep 17 00:00:00 2001 From: Technote Date: Sun, 19 Jan 2020 04:08:45 +0900 Subject: [PATCH 2/3] feat: Update package version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bf8e8776..1aec5288 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@technote-space/get-diff-action", - "version": "1.0.2", + "version": "1.0.3", "description": "GitHub actions to get git diff.", "author": "Technote (https://technote.space)", "license": "MIT", From 5b69450308a4a7ee8c29f90eb7482369a0b7fff0 Mon Sep 17 00:00:00 2001 From: Technote Date: Sun, 19 Jan 2020 04:10:33 +0900 Subject: [PATCH 3/3] feat: set env (#9) --- __tests__/utils/misc.test.ts | 53 +++++++++++++++++++++++++++++++++++- action.yml | 3 ++ src/main.ts | 14 ++++------ src/utils/misc.ts | 19 +++++++++++++ 4 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 src/utils/misc.ts diff --git a/__tests__/utils/misc.test.ts b/__tests__/utils/misc.test.ts index 7a8e5746..2aa57efb 100644 --- a/__tests__/utils/misc.test.ts +++ b/__tests__/utils/misc.test.ts @@ -1,7 +1,13 @@ +/* eslint-disable no-magic-numbers */ +import path from 'path'; import { isTargetEvent } from '@technote-space/filter-github-action'; -import { getContext } from '@technote-space/github-action-test-helper'; +import { testEnv, getContext, spyOnStdout, stdoutCalledWith } from '@technote-space/github-action-test-helper'; +import { Logger } from '@technote-space/github-action-helper'; +import { dumpOutput, setResult } from '../../src/utils/misc'; import { TARGET_EVENTS } from '../../src/constant'; +const rootDir = path.resolve(__dirname, '..', '..'); + describe('isTargetEvent', () => { it('should return true', () => { expect(isTargetEvent(TARGET_EVENTS, getContext({ @@ -28,3 +34,48 @@ describe('isTargetEvent', () => { }))).toBe(false); }); }); + +describe('dumpOutput', () => { + testEnv(rootDir); + + it('should dump output', () => { + const mockStdout = spyOnStdout(); + + dumpOutput(['test1', 'test2'], new Logger()); + + stdoutCalledWith(mockStdout, [ + '::group::Dump output', + '[\n' + + '\t"test1",\n' + + '\t"test2"\n' + + ']', + '::endgroup::', + ]); + }); +}); + +describe('setResult', () => { + testEnv(rootDir); + + it('should set result', () => { + const mockStdout = spyOnStdout(); + + setResult(['test1', 'test2']); + + stdoutCalledWith(mockStdout, [ + '::set-output name=diff::test1 test2', + ]); + }); + + it('should set result with env', () => { + process.env.INPUT_SET_ENV_NAME = 'DIFF'; + const mockStdout = spyOnStdout(); + + setResult(['test1', 'test2']); + + stdoutCalledWith(mockStdout, [ + '::set-output name=diff::test1 test2', + '::set-env name=DIFF::test1 test2', + ]); + }); +}); diff --git a/action.yml b/action.yml index c9c7552f..d50b6846 100644 --- a/action.yml +++ b/action.yml @@ -36,6 +36,9 @@ inputs: description: Get as absolute path. default: 'true' required: false + SET_ENV_NAME: + description: Env name. + required: false outputs: diff: description: 'git diff results' diff --git a/src/main.ts b/src/main.ts index 9f90797d..43888eaf 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,9 +1,10 @@ import path from 'path'; -import { setFailed, setOutput } from '@actions/core'; +import { setFailed } from '@actions/core'; import { context } from '@actions/github'; import { isTargetEvent } from '@technote-space/filter-github-action'; import { Logger, ContextHelper } from '@technote-space/github-action-helper'; -import { getGitDiff, getGitDiffOutput } from './utils/command'; +import { getGitDiff } from './utils/command'; +import { dumpOutput, setResult } from './utils/misc'; import { TARGET_EVENTS } from './constant'; /** @@ -15,16 +16,13 @@ async function run(): Promise { if (!isTargetEvent(TARGET_EVENTS, context)) { logger.info('This is not target event.'); - setOutput('diff', ''); + setResult([]); return; } const diff = await getGitDiff(); - logger.startProcess('Dump output'); - console.log(diff); - logger.endProcess(); - - setOutput('diff', getGitDiffOutput(diff)); + dumpOutput(diff, logger); + setResult(diff); } run().catch(error => setFailed(error.message)); diff --git a/src/utils/misc.ts b/src/utils/misc.ts new file mode 100644 index 00000000..0e297c02 --- /dev/null +++ b/src/utils/misc.ts @@ -0,0 +1,19 @@ +import { Logger } from '@technote-space/github-action-helper'; +import { exportVariable, getInput, setOutput } from '@actions/core' ; +import { getGitDiffOutput } from './command'; + +export const dumpOutput = (diff: string[], logger: Logger): void => { + logger.startProcess('Dump output'); + console.log(diff); + logger.endProcess(); +}; + +export const setResult = (diff: string[]): void => { + const result = getGitDiffOutput(diff); + setOutput('diff', result); + + const envName = getInput('SET_ENV_NAME'); + if (envName) { + exportVariable(envName, result); + } +};