Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.
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
53 changes: 52 additions & 1 deletion __tests__/utils/misc.test.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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',
]);
});
});
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ inputs:
description: Get as absolute path.
default: 'true'
required: false
SET_ENV_NAME:
description: Env name.
required: false
outputs:
diff:
description: 'git diff results'
branding:
# https://feathericons.com/
icon: 'package'
icon: 'file'
color: 'orange'
runs:
using: node12
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]> (https://technote.space)",
"license": "MIT",
Expand Down
14 changes: 6 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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';

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

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));
19 changes: 19 additions & 0 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -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);
}
};