From 353ea22e6fc7b15ce3a30064d8cd19eef2a86b71 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Tue, 6 Jun 2023 06:00:56 -0600 Subject: [PATCH] feat: add dir_names_exclude_current_dir input and cleaned up logic to retrieve the current sha (#1229) Co-authored-by: GitHub Action Co-authored-by: tj-actions[bot] <109116665+tj-actions-bot@users.noreply.github.com> Co-authored-by: jackton1 --- action.yml | 7 ++++++- dist/index.js | Bin 318229 -> 318730 bytes dist/index.js.map | Bin 373768 -> 374335 bytes src/changedFiles.ts | 3 ++- src/commitSha.ts | 25 +++++++++++++++++-------- src/inputs.ts | 8 ++++++++ src/utils.ts | 6 +++--- 7 files changed, 36 insertions(+), 13 deletions(-) diff --git a/action.yml b/action.yml index d698bfcf..38efa8ef 100644 --- a/action.yml +++ b/action.yml @@ -79,15 +79,20 @@ inputs: default: "true" dir_names: default: "false" - description: "Output unique changed directories instead of filenames. **NOTE:** This returns `.` for changed files located in the root of the project." + description: "Output unique changed directories instead of filenames. **NOTE:** This returns `.` for changed files located in the current working directory which defaults to `$GITHUB_WORKSPACE`." required: false dir_names_max_depth: description: "Limit the directory output to a maximum depth e.g `test/test1/test2` with max depth of `2` returns `test/test1`." required: false + dir_names_exclude_current_dir: + description: "Exclude the current directory represented by `.` from the output when `dir_names` is set to `true`." + required: false + default: "false" dir_names_exclude_root: description: "Exclude the root directory represented by `.` from the output when `dir_names`is set to `true`." required: false default: "false" + deprecationMessage: "This input is deprecated. Use `dir_names_exclude_current_dir` instead." json: description: "Output list of changed files in a JSON formatted string which can be used for matrix jobs." required: false diff --git a/dist/index.js b/dist/index.js index ebeffe7e18c4cf82dcb332d96026ba86f7810ff4..ac18bc1a3bbccb7036a0a390f022945007e70c74 100644 GIT binary patch delta 449 zcmbQbO}J~9@P@;p-p-{(MX7luE}2DE3YmEYr6t9BDVarniMgr8t`*5Sr75XF`S~RZ zH8nV75&F5N%eOO$ZvH9C$H|hKSEe(ypD}WCy!?Hp=@VEOStfrm6rB9mhIev9JS%gs zhvW2%0Zbx{8j}s(R3>jQ7MvdNhmn;zBhhN|MGtwHn#qp5Vv`qm=}zV`QJK#7hmnn0 zQ^9s}g00A813lKsZ`?R13kT{?fBuwFck*jDuF3KWVw2;P^e0yvh)iA|z&824nbzcJ zH?7TYl{DF5E|Qt-V8}LE)w0c delta 181 zcmeC$B|LST@P@;pEJ6ACC6mvJ%53HpQ{kLk&LOecPhOsR@;wjs$@7)jCZE%0n=Iqa zKUqOVck(+2w#j=e3^q?x31*+{!Y?t|L4|WOm(Dy%rkvEt`?XbJVw1Ua)HchyNia>G zsG_ww!NY`W^0g2t76mmmg~^3VYAl)x)m)P&C_8TM3Rj zeI&EF_R(*S$$hsaHcx$R$vj<8jFEkE{s+&=zCZY;Z(wEA+Wh}RD?3Pjx`8mG;pR&} zB&3*fQYXj%Pz4EY2MPu-P5%Eud$Qc`+uPg47-w@%w)`o@te~bg-BFTJe)5NR9IToO z)iHUK6F!Je=J}wzU0i{2uHtr6Cq_+{?M-fs%w1r&OfS5|D6)O=1x5izFu#58MMfZI O0%GRvdoQvyJplk-ky{=B diff --git a/src/changedFiles.ts b/src/changedFiles.ts index f1cfac28..71871588 100644 --- a/src/changedFiles.ts +++ b/src/changedFiles.ts @@ -130,7 +130,8 @@ export const getDiffFiles = async ({ getDirnameMaxDepth({ pathStr: file, dirNamesMaxDepth: inputs.dirNamesMaxDepth, - excludeRoot: inputs.dirNamesExcludeRoot + excludeCurrentDir: + inputs.dirNamesExcludeRoot || inputs.dirNamesExcludeCurrentDir }) ) files = [...new Set(files)] diff --git a/src/commitSha.ts b/src/commitSha.ts index d0d6847b..0d5251e3 100644 --- a/src/commitSha.ts +++ b/src/commitSha.ts @@ -15,9 +15,11 @@ import { } from './utils' const getCurrentSHA = async ({ + env, inputs, workingDirectory }: { + env: Env inputs: Inputs workingDirectory: string }): Promise => { @@ -47,7 +49,18 @@ const getCurrentSHA = async ({ } } else { if (!currentSha) { - currentSha = await getHeadSha({cwd: workingDirectory}) + if ( + env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA && + (await verifyCommitSha({ + sha: env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA, + cwd: workingDirectory, + showAsErrorMessage: false + })) === 0 + ) { + currentSha = env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA + } else { + currentSha = await getHeadSha({cwd: workingDirectory}) + } } } @@ -124,7 +137,7 @@ export const getSHAForPushEvent = async ( } } - const currentSha = await getCurrentSHA({inputs, workingDirectory}) + const currentSha = await getCurrentSHA({env, inputs, workingDirectory}) let previousSha = inputs.baseSha const diff = '..' @@ -321,7 +334,7 @@ export const getSHAForPullRequestEvent = async ( core.info('Completed fetching more history.') } - let currentSha = await getCurrentSHA({inputs, workingDirectory}) + const currentSha = await getCurrentSHA({env, inputs, workingDirectory}) let previousSha = inputs.baseSha let diff = '...' @@ -336,7 +349,7 @@ export const getSHAForPullRequestEvent = async ( throw new Error('Similar commit hashes detected.') } - await verifyCommitSha({sha: currentSha, cwd: workingDirectory}) + await verifyCommitSha({sha: previousSha, cwd: workingDirectory}) core.debug(`Previous SHA: ${previousSha}`) return { @@ -425,10 +438,6 @@ export const getSHAForPullRequestEvent = async ( } } - if (previousSha === currentSha && env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA) { - currentSha = env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA - } - if ( !(await canDiffCommits({ cwd: workingDirectory, diff --git a/src/inputs.ts b/src/inputs.ts index bca5e600..762dd07c 100644 --- a/src/inputs.ts +++ b/src/inputs.ts @@ -23,6 +23,7 @@ export type Inputs = { dirNames: boolean dirNamesMaxDepth?: number dirNamesExcludeRoot: boolean + dirNamesExcludeCurrentDir: boolean json: boolean escapeJson: boolean fetchDepth?: number @@ -93,6 +94,12 @@ export const getInputs = (): Inputs => { const dirNamesExcludeRoot = core.getBooleanInput('dir_names_exclude_root', { required: false }) + const dirNamesExcludeCurrentDir = core.getBooleanInput( + 'dir_names_exclude_current_dir', + { + required: false + } + ) const json = core.getBooleanInput('json', {required: false}) const escapeJson = core.getBooleanInput('escape_json', {required: false}) const fetchDepth = core.getInput('fetch_depth', {required: false}) @@ -127,6 +134,7 @@ export const getInputs = (): Inputs => { diffRelative, dirNames, dirNamesExcludeRoot, + dirNamesExcludeCurrentDir, json, escapeJson, sinceLastRemoteCommit, diff --git a/src/utils.ts b/src/utils.ts index 73ef5996..9fd4d67f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -642,11 +642,11 @@ export const canDiffCommits = async ({ export const getDirnameMaxDepth = ({ pathStr, dirNamesMaxDepth, - excludeRoot + excludeCurrentDir }: { pathStr: string dirNamesMaxDepth?: number - excludeRoot?: boolean + excludeCurrentDir?: boolean }): string => { const pathArr = dirname(pathStr).split(path.sep) const maxDepth = Math.min(dirNamesMaxDepth || pathArr.length, pathArr.length) @@ -656,7 +656,7 @@ export const getDirnameMaxDepth = ({ output = path.join(output, pathArr[i]) } - if (excludeRoot && output === '.') { + if (excludeCurrentDir && output === '.') { return '' }