From 4572b3681922b9a1b5ac740697abb30af140476d Mon Sep 17 00:00:00 2001 From: Technote Date: Sat, 3 Jul 2021 00:48:03 +0900 Subject: [PATCH] feat: add relative option (#157) --- __tests__/utils/command.test.ts | 25 +++++++++++++++++++++++++ action.yml | 3 +++ src/utils/command.ts | 5 +++++ 3 files changed, 33 insertions(+) diff --git a/__tests__/utils/command.test.ts b/__tests__/utils/command.test.ts index 1dffbf6e..2de7a515 100644 --- a/__tests__/utils/command.test.ts +++ b/__tests__/utils/command.test.ts @@ -564,6 +564,31 @@ describe('getGitDiff', () => { 'git diff \'get-diff-action/master..get-diff-action/pull/55/merge\' --shortstat -w -- \'__tests__/main.test.ts\'', ]); }); + + it('should get git diff (relative)', async() => { + process.env.GITHUB_WORKSPACE = '/home/runner/work/my-repo-name/my-repo-name'; + process.env.INPUT_GITHUB_TOKEN = 'test token'; + process.env.INPUT_RELATIVE = 'src/test'; + const mockExec = spyOnSpawn(); + setChildProcessParams({ + stdout: (command: string): string => { + if (command.startsWith('git diff')) { + return 'test.txt'; + } + return ''; + }, + }); + + expect(await getGitDiff(logger, prContext)).toEqual([ + {file: 'test.txt', ...emptyDiff}, + ]); + 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 \'--relative=src/test\' || :', + 'git diff \'get-diff-action/master...get-diff-action/pull/55/merge\' --shortstat -w -- \'test.txt\'', + ]); + }); }); describe('getFileDiff', () => { diff --git a/action.yml b/action.yml index 5feadd7a..5f4cead2 100644 --- a/action.yml +++ b/action.yml @@ -17,6 +17,9 @@ inputs: BASE: description: base required: false + RELATIVE: + description: relative filter + required: false DIFF_FILTER: description: Diff filter. default: 'AMRC' diff --git a/src/utils/command.ts b/src/utils/command.ts index 9eb59636..ea77dc32 100644 --- a/src/utils/command.ts +++ b/src/utils/command.ts @@ -12,6 +12,7 @@ const command = new Command(new Logger()); const getRawInput = (name: string): string => process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || ''; const getDot = (): string => getInput('DOT', {required: true}); const getFilter = (): string => getInput('DIFF_FILTER', {required: true}); +const getRelativePath = (): string => getInput('RELATIVE'); const getOutputFormatType = (): string => getRawInput('FORMAT'); const escapeWhenJsonFormat = (): boolean => Utils.getBoolValue(getRawInput('ESCAPE_JSON')); const getSeparator = (): string => getRawInput('SEPARATOR'); @@ -89,6 +90,7 @@ export const getGitDiff = async(logger: Logger, context: Context): Promise