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

Commit

Permalink
Merge pull request #175 from technote-space/feature/#173
Browse files Browse the repository at this point in the history
feat: add flag whether to get file diff (#173)
  • Loading branch information
technote-space authored Jan 5, 2022
2 parents 8e29491 + 1b6c9eb commit 4ec9175
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 162 deletions.
6 changes: 3 additions & 3 deletions README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ jobs:
|:---|:---|:---|
| diff | 差分のあるファイルの結果<br>`SET_ENV_NAME`(default: `GIT_DIFF`) が設定されている場合、その名前で環境変数が設定されます | `src/main.ts src/docs.md` |
| count | 差分のあるファイル数<br>`SET_ENV_NAME_COUNT`(default: `''`) が設定されている場合、その名前で環境変数が設定されます | `100` |
| insertions | 追加された行数<br>`SET_ENV_NAME_INSERTIONS`(default: `''`) が設定されている場合、その名前で環境変数が設定されます | `100` |
| deletions | 削除された行数<br>`SET_ENV_NAME_DELETIONS`(default: `''`) が設定されている場合、その名前で環境変数が設定されます | `100` |
| lines | 追加された行数と削除された行数の和<br>`SET_ENV_NAME_LINES`(default: `''`) が設定されている場合、その名前で環境変数が設定されます | `200` |
| insertions | 追加された行数 (`GET_FILE_DIFF` が `true` の場合のみ利用可能)<br>`SET_ENV_NAME_INSERTIONS`(default: `''`) が設定されている場合、その名前で環境変数が設定されます | `100` |
| deletions | 削除された行数 (`GET_FILE_DIFF` が `true` の場合のみ利用可能)<br>`SET_ENV_NAME_DELETIONS`(default: `''`) が設定されている場合、その名前で環境変数が設定されます | `100` |
| lines | 追加された行数と削除された行数の和 (`GET_FILE_DIFF` が `true` の場合のみ利用可能)<br>`SET_ENV_NAME_LINES`(default: `''`) が設定されている場合、その名前で環境変数が設定されます | `200` |

## Action イベント詳細
### 対象イベント
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ If there is no difference in the source code below, this workflow will skip the
|:---|:---|:---|
| diff | The results of diff file names.<br>If inputs `SET_ENV_NAME`(default: `GIT_DIFF`) is set, an environment variable is set with that name. | `src/main.ts src/docs.md` |
| count | The number of diff files.<br>If inputs `SET_ENV_NAME_COUNT`(default: `''`) is set, an environment variable is set with that name. | `100` |
| insertions | The number of insertions lines.<br>If inputs `SET_ENV_NAME_INSERTIONS`(default: `''`) is set, an environment variable is set with that name. | `100` |
| deletions | The number of deletions lines.<br>If inputs `SET_ENV_NAME_DELETIONS`(default: `''`) is set, an environment variable is set with that name. | `100` |
| lines | The number of diff lines.<br>If inputs `SET_ENV_NAME_LINES`(default: `''`) is set, an environment variable is set with that name. | `200` |
| insertions | The number of insertions lines. (Available only if `GET_FILE_DIFF` is `true`)<br>If inputs `SET_ENV_NAME_INSERTIONS`(default: `''`) is set, an environment variable is set with that name. | `100` |
| deletions | The number of deletions lines. (Available only if `GET_FILE_DIFF` is `true`)<br>If inputs `SET_ENV_NAME_DELETIONS`(default: `''`) is set, an environment variable is set with that name. | `100` |
| lines | The number of diff lines. (Available only if `GET_FILE_DIFF` is `true`)<br>If inputs `SET_ENV_NAME_LINES`(default: `''`) is set, an environment variable is set with that name. | `200` |

## Action event details
### Target events
Expand Down
134 changes: 107 additions & 27 deletions __tests__/process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,36 @@ describe('setResult', () => {

setResult(diffs, false, logger);

stdoutCalledWith(mockStdout, [
'::group::Dump output',
'',
'::set-output name=diff::test1 test2 test4',
'"diff: test1 test2 test4"',
'',
'::set-output name=filtered_diff::test1 test2',
'"filtered_diff: test1 test2"',
'',
'::set-output name=matched_files::test4',
'"matched_files: test4"',
'',
'::set-output name=count::3',
'"count: 3"',
'::endgroup::',
]);
exportVariableCalledWith(mockEnv, [
{name: 'GIT_DIFF', val: 'test1 test2 test4'},
{name: 'GIT_DIFF_FILTERED', val: 'test1 test2'},
{name: 'MATCHED_FILES', val: 'test4'},
]);
});

it('should set result with file diff', () => {
process.env.INPUT_GET_FILE_DIFF = '1';
const mockStdout = spyOnStdout();
const mockEnv = spyOnExportVariable();

setResult(diffs, false, logger);

stdoutCalledWith(mockStdout, [
'::group::Dump output',
'',
Expand Down Expand Up @@ -157,6 +187,7 @@ describe('setResult', () => {
process.env.INPUT_SET_ENV_NAME_INSERTIONS = 'INSERTIONS';
process.env.INPUT_SET_ENV_NAME_DELETIONS = 'DELETIONS';
process.env.INPUT_SET_ENV_NAME_LINES = 'LINES';
process.env.INPUT_GET_FILE_DIFF = '1';
const mockStdout = spyOnStdout();
const mockEnv = spyOnExportVariable();

Expand Down Expand Up @@ -208,6 +239,81 @@ describe('execute', () => {
process.env.INPUT_FILES = 'package.json\ncomposer.json';
process.env.INPUT_PATTERNS = 'src/**/*.+(ts|txt)\n__tests__/**/*.+(ts|txt)';

const mockExec = spyOnSpawn();
const mockStdout = spyOnStdout();
const mockEnv = spyOnExportVariable();
setChildProcessParams({
stdout: (command: string): string => {
if (command.startsWith('git diff')) {
return 'package.json\nabc/package.json\nREADME.md\nsrc/main.ts';
}
return '';
},
});

await execute(logger, prContext);

execCalledWith(mockExec, [
'git remote add get-diff-action \'https://octocat:test token@github.com/hello/world.git\' || :',
'git fetch --no-tags --no-recurse-submodules \'--depth=10000\' get-diff-action \'refs/pull/55/merge:refs/remotes/get-diff-action/pull/55/merge\' \'refs/heads/master:refs/remotes/get-diff-action/master\' || :',
'git diff \'get-diff-action/master...get-diff-action/pull/55/merge\' \'--diff-filter=AMRC\' --name-only',
]);
stdoutCalledWith(mockStdout, [
'[command]git remote add get-diff-action',
'[command]git fetch --no-tags --no-recurse-submodules \'--depth=10000\' get-diff-action \'refs/pull/55/merge:refs/remotes/get-diff-action/pull/55/merge\' \'refs/heads/master:refs/remotes/get-diff-action/master\'',
'[command]git diff \'get-diff-action/master...get-diff-action/pull/55/merge\' \'--diff-filter=AMRC\' --name-only',
' >> package.json',
' >> abc/package.json',
' >> README.md',
' >> src/main.ts',
'::group::Dump diffs',
getLogStdout([
{
'file': 'package.json',
'filterIgnored': true,
'isMatched': false,
},
{
'file': 'abc/package.json',
'filterIgnored': true,
'isMatched': false,
},
{
'file': 'src/main.ts',
'filterIgnored': false,
'isMatched': true,
},
]),
'::endgroup::',
'::group::Dump output',
'',
'::set-output name=diff::\'package.json\' \'abc/package.json\' \'src/main.ts\'',
'"diff: \'package.json\' \'abc/package.json\' \'src/main.ts\'"',
'',
'::set-output name=filtered_diff::\'src/main.ts\'',
'"filtered_diff: \'src/main.ts\'"',
'',
'::set-output name=matched_files::\'package.json\' \'abc/package.json\'',
'"matched_files: \'package.json\' \'abc/package.json\'"',
'',
'::set-output name=count::3',
'"count: 3"',
'::endgroup::',
]);
exportVariableCalledWith(mockEnv, [
{name: 'GIT_DIFF', val: '\'package.json\' \'abc/package.json\' \'src/main.ts\''},
{name: 'GIT_DIFF_FILTERED', val: '\'src/main.ts\''},
{name: 'MATCHED_FILES', val: '\'package.json\' \'abc/package.json\''},
]);
});

it('should execute with file diff', async() => {
process.env.GITHUB_WORKSPACE = '/home/runner/work/my-repo-name/my-repo-name';
process.env.INPUT_GITHUB_TOKEN = 'test token';
process.env.INPUT_FILES = 'package.json\ncomposer.json';
process.env.INPUT_PATTERNS = 'src/**/*.+(ts|txt)\n__tests__/**/*.+(ts|txt)';
process.env.INPUT_GET_FILE_DIFF = '1';

const mockExec = spyOnSpawn();
const mockStdout = spyOnStdout();
const mockEnv = spyOnExportVariable();
Expand Down Expand Up @@ -357,15 +463,6 @@ describe('execute', () => {
'',
'::set-output name=count::0',
'"count: 0"',
'',
'::set-output name=insertions::0',
'"insertions: 0"',
'',
'::set-output name=deletions::0',
'"deletions: 0"',
'',
'::set-output name=lines::0',
'"lines: 0"',
'::endgroup::',
]);
exportVariableCalledWith(mockEnv, [
Expand Down Expand Up @@ -402,15 +499,6 @@ describe('execute', () => {
'',
'::set-output name=count::0',
'"count: 0"',
'',
'::set-output name=insertions::0',
'"insertions: 0"',
'',
'::set-output name=deletions::0',
'"deletions: 0"',
'',
'::set-output name=lines::0',
'"lines: 0"',
'::endgroup::',
]);
exportVariableCalledWith(mockEnv, [
Expand All @@ -422,6 +510,7 @@ describe('execute', () => {

it('should execute empty with default value', async() => {
process.env.GITHUB_WORKSPACE = '/home/runner/work/my-repo-name/my-repo-name';
process.env.INPUT_GET_FILE_DIFF = '1';
process.env.INPUT_DIFF_DEFAULT = '1';
process.env.INPUT_FILTERED_DIFF_DEFAULT = '2';
process.env.INPUT_MATCHED_FILES_DEFAULT = '3';
Expand Down Expand Up @@ -512,15 +601,6 @@ describe('execute', () => {
'',
'::set-output name=count::0',
'"count: 0"',
'',
'::set-output name=insertions::0',
'"insertions: 0"',
'',
'::set-output name=deletions::0',
'"deletions: 0"',
'',
'::set-output name=lines::0',
'"lines: 0"',
'::endgroup::',
]);
exportVariableCalledWith(mockEnv, [
Expand Down
Loading

0 comments on commit 4ec9175

Please sign in to comment.