Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit eeadf7e

Browse files
Merge pull request #11 from technote-space/release/v1.0.3
Release/v1.0.3
2 parents 6d7d22d + 08f6dea commit eeadf7e

File tree

5 files changed

+82
-11
lines changed

5 files changed

+82
-11
lines changed

__tests__/utils/misc.test.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
/* eslint-disable no-magic-numbers */
2+
import path from 'path';
13
import { isTargetEvent } from '@technote-space/filter-github-action';
2-
import { getContext } from '@technote-space/github-action-test-helper';
4+
import { testEnv, getContext, spyOnStdout, stdoutCalledWith } from '@technote-space/github-action-test-helper';
5+
import { Logger } from '@technote-space/github-action-helper';
6+
import { dumpOutput, setResult } from '../../src/utils/misc';
37
import { TARGET_EVENTS } from '../../src/constant';
48

9+
const rootDir = path.resolve(__dirname, '..', '..');
10+
511
describe('isTargetEvent', () => {
612
it('should return true', () => {
713
expect(isTargetEvent(TARGET_EVENTS, getContext({
@@ -28,3 +34,48 @@ describe('isTargetEvent', () => {
2834
}))).toBe(false);
2935
});
3036
});
37+
38+
describe('dumpOutput', () => {
39+
testEnv(rootDir);
40+
41+
it('should dump output', () => {
42+
const mockStdout = spyOnStdout();
43+
44+
dumpOutput(['test1', 'test2'], new Logger());
45+
46+
stdoutCalledWith(mockStdout, [
47+
'::group::Dump output',
48+
'[\n' +
49+
'\t"test1",\n' +
50+
'\t"test2"\n' +
51+
']',
52+
'::endgroup::',
53+
]);
54+
});
55+
});
56+
57+
describe('setResult', () => {
58+
testEnv(rootDir);
59+
60+
it('should set result', () => {
61+
const mockStdout = spyOnStdout();
62+
63+
setResult(['test1', 'test2']);
64+
65+
stdoutCalledWith(mockStdout, [
66+
'::set-output name=diff::test1 test2',
67+
]);
68+
});
69+
70+
it('should set result with env', () => {
71+
process.env.INPUT_SET_ENV_NAME = 'DIFF';
72+
const mockStdout = spyOnStdout();
73+
74+
setResult(['test1', 'test2']);
75+
76+
stdoutCalledWith(mockStdout, [
77+
'::set-output name=diff::test1 test2',
78+
'::set-env name=DIFF::test1 test2',
79+
]);
80+
});
81+
});

action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ inputs:
3636
description: Get as absolute path.
3737
default: 'true'
3838
required: false
39+
SET_ENV_NAME:
40+
description: Env name.
41+
required: false
3942
outputs:
4043
diff:
4144
description: 'git diff results'
4245
branding:
4346
# https://feathericons.com/
44-
icon: 'package'
47+
icon: 'file'
4548
color: 'orange'
4649
runs:
4750
using: node12

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@technote-space/get-diff-action",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "GitHub actions to get git diff.",
55
"author": "Technote <[email protected]> (https://technote.space)",
66
"license": "MIT",

src/main.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import path from 'path';
2-
import { setFailed, setOutput } from '@actions/core';
2+
import { setFailed } from '@actions/core';
33
import { context } from '@actions/github';
44
import { isTargetEvent } from '@technote-space/filter-github-action';
55
import { Logger, ContextHelper } from '@technote-space/github-action-helper';
6-
import { getGitDiff, getGitDiffOutput } from './utils/command';
6+
import { getGitDiff } from './utils/command';
7+
import { dumpOutput, setResult } from './utils/misc';
78
import { TARGET_EVENTS } from './constant';
89

910
/**
@@ -15,16 +16,13 @@ async function run(): Promise<void> {
1516

1617
if (!isTargetEvent(TARGET_EVENTS, context)) {
1718
logger.info('This is not target event.');
18-
setOutput('diff', '');
19+
setResult([]);
1920
return;
2021
}
2122

2223
const diff = await getGitDiff();
23-
logger.startProcess('Dump output');
24-
console.log(diff);
25-
logger.endProcess();
26-
27-
setOutput('diff', getGitDiffOutput(diff));
24+
dumpOutput(diff, logger);
25+
setResult(diff);
2826
}
2927

3028
run().catch(error => setFailed(error.message));

src/utils/misc.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Logger } from '@technote-space/github-action-helper';
2+
import { exportVariable, getInput, setOutput } from '@actions/core' ;
3+
import { getGitDiffOutput } from './command';
4+
5+
export const dumpOutput = (diff: string[], logger: Logger): void => {
6+
logger.startProcess('Dump output');
7+
console.log(diff);
8+
logger.endProcess();
9+
};
10+
11+
export const setResult = (diff: string[]): void => {
12+
const result = getGitDiffOutput(diff);
13+
setOutput('diff', result);
14+
15+
const envName = getInput('SET_ENV_NAME');
16+
if (envName) {
17+
exportVariable(envName, result);
18+
}
19+
};

0 commit comments

Comments
 (0)