From 90ef0b1b22b674a0dea8c832029e63eed9fb8e13 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Wed, 14 Jun 2023 13:59:31 -0600 Subject: [PATCH] feat: add support for outputting renamed files as deleted and added (#1260) Co-authored-by: GitHub Action --- .github/workflows/test.yml | 35 +++++++++++++++++++++++++++++++++++ action.yml | 4 ++++ dist/index.js | Bin 335105 -> 335866 bytes dist/index.js.map | Bin 393336 -> 394157 bytes src/changedFiles.ts | 10 +++++++--- src/commitSha.ts | 7 +------ src/inputs.ts | 8 +++++++- src/main.ts | 11 ++++++++--- src/utils.ts | 16 +++++++++++++--- 9 files changed, 75 insertions(+), 16 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8afc9fb4..0b51ec28 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -869,6 +869,41 @@ jobs: exit 1 shell: bash + - name: Run changed-files for old new filenames test rename 2 output as deleted and added + id: changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added + uses: ./ + with: + base_sha: 4d04215 + sha: fe238e6 + fetch_depth: 60000 + include_all_old_new_renamed_files: true + output_renamed_files_as_deleted_and_added: true + - name: Show output + run: | + echo '${{ toJSON(steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs) }}' + shell: + bash + - name: Check all_old_new_renamed_files output + if: "!contains(steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.all_old_new_renamed_files, 'test/test rename 2.txt,test/test rename-2.txt')" + run: | + echo "Invalid output: Expected to include (test/test rename 2.txt test/test rename-2.txt) got (${{ steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.all_old_new_renamed_files }})" + exit 1 + shell: + bash + - name: Check deleted_files output + if: "!contains(steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.deleted_files, 'test/test rename 2.txt')" + run: | + echo "Invalid output: Expected to include (test/test rename 2.txt) got (${{ steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.deleted_files }})" + exit 1 + shell: + bash + - name: Check added_files output + if: "!contains(steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.added_files, 'test/test rename-2.txt')" + run: | + echo "Invalid output: Expected to include (test/test rename-2.txt) got (${{ steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.added_files }})" + exit 1 + shell: + bash - name: Run changed-files with specific files id: changed-files-specific uses: ./ diff --git a/action.yml b/action.yml index e9384f12..b9f7039c 100644 --- a/action.yml +++ b/action.yml @@ -117,6 +117,10 @@ inputs: description: "Directory to store output files." required: false default: ".github/outputs" + output_renamed_files_as_deleted_and_added: + description: "Output renamed files as deleted and added files." + required: false + default: "false" outputs: added_files: diff --git a/dist/index.js b/dist/index.js index 0b817fb7ef1a98e3a0958d445702b8d348509cd5..cf79fe7b3c4bc2ccd34bd62eda326dbb86a14910 100644 GIT binary patch delta 772 zcmZqNCGu;($Ocy-RUL)=(vpJGlAzSQ#N5;rx6GW>V#i{a)ST3k)D*|O6vvd5)Rf5! zUpq}skmi`&FSK4)hf4tph|sy&PIxu*b$as~7Pe&my zwH#zTP>H(w^yO@f8>Sy+V-#TaOD&&%la0}3^1uI_)79A-Wto8rr+ct7+Nx)!DQFPm zk3vz=$pRa>r(a}Zl%K4ylYKfr2cydLJG@LBlleFDOjqP&R29}MC@s#=$jdLvP0Y!x zN=*S-d+sok;&i*oOyXc=6WJNPrr$lx#5cL%71!i-HX@Vz7IL?GgE+Va?%#j$?=;6 zHaE@h6`q`afOoUp<^}9v#@Ss~?vo|{1Wxw)YB+h;pU~~x%#2rArp7bMP3PfYoHSjR zlTmMa9zUb-^p%{964PgMF=|hj{>Q{WJ%OK5t(}vb5r~<9m>Gy!wsUf`eiQ)!mdaZi diff --git a/dist/index.js.map b/dist/index.js.map index 828401b05c4129ba42326ca71770dffe44f52c96..eef097d175d4da8a7eb4389aab9e507b50107b73 100644 GIT binary patch delta 925 zcmey-AhEVxV#7w}i2+=b9kRHX3Vb$Slwjcivkjv*7TPj`n45KZOPQcb81*+FOs;2} z7%&m6s49H(#*W|IAU1PRuG7ZClOP@Jo3m7&9aGg&$S*A^C@l#}%}dNpO>xW2NiB9P zc1g`iElEvr%u8`hNl8tae(?>X^Yj2mCYH(5UvJmbiOExd0%9~TyvQiL`Of12rp+7Q zMKexb@m6@cKr=I|5)t+u)L>MeK4BXZ|7N=nMYhxb=ramU-{;N9Hrfsfum?o}5gMl-tN{k1Kouj) zcJoR`9uY1Lkk11WOENUKd-pP4V@DRyoXU9JgUCej_6Vcv^nh=SqSL=!U^JYbbAvH* z`<;7?Us(8b6!KEbK{fz=sIET!!ehp*!pZr0#U%=P`9-;jIhj?dDSoNt(^;MXP566< zk#l^_6eX=t5u(SwZCkdQ2{c;?e_`pobzBM5W{{m!=|6C=%k5za{D~ z2$T!m!Gca zT%UpPQ|Y17EUOcL-+2#R=2#2)wJ@u^KL13}M=mW|1@ni3N)A{k=0aa~5E{yLqqiv+ zE@%wVB+oCz*S&ls9$h4a*fyHmWev2NViaWKV3!IGe7Pl01R^8Z=CN#&YHM(+D0B=h zJ#NE9^)!qUzX83K`VV1{_A => { const files = await getAllChangedFiles({ cwd: workingDirectory, sha1: diffResult.previousSha, sha2: diffResult.currentSha, - diff: diffResult.diff + diff: diffResult.diff, + outputRenamedFilesAsDeletedAndAdded }) if (hasSubmodule) { @@ -124,7 +127,8 @@ export const getAllDiffFiles = async ({ sha2: submoduleShaResult.currentSha, diff: diffResult.diff, isSubmodule: true, - parentDir: submodulePath + parentDir: submodulePath, + outputRenamedFilesAsDeletedAndAdded }) for (const changeType of Object.keys( diff --git a/src/commitSha.ts b/src/commitSha.ts index d6338c86..c109346c 100644 --- a/src/commitSha.ts +++ b/src/commitSha.ts @@ -370,12 +370,7 @@ export const getSHAForPullRequestEvent = async ( if (!previousSha) { if (inputs.sinceLastRemoteCommit) { - previousSha = - env.GITHUB_EVENT_BEFORE || - (await getRemoteBranchHeadSha({ - cwd: workingDirectory, - branch: currentBranch - })) + previousSha = env.GITHUB_EVENT_BEFORE if ( !previousSha || diff --git a/src/inputs.ts b/src/inputs.ts index 762dd07c..e5851602 100644 --- a/src/inputs.ts +++ b/src/inputs.ts @@ -30,6 +30,7 @@ export type Inputs = { sinceLastRemoteCommit: boolean writeOutputFiles: boolean outputDir: string + outputRenamedFilesAsDeletedAndAdded: boolean } export const getInputs = (): Inputs => { @@ -111,6 +112,10 @@ export const getInputs = (): Inputs => { required: false }) const outputDir = core.getInput('output_dir', {required: false}) + const outputRenamedFilesAsDeletedAndAdded = core.getBooleanInput( + 'output_renamed_files_as_deleted_and_added', + {required: false} + ) const inputs: Inputs = { files, @@ -139,7 +144,8 @@ export const getInputs = (): Inputs => { escapeJson, sinceLastRemoteCommit, writeOutputFiles, - outputDir + outputDir, + outputRenamedFilesAsDeletedAndAdded } if (fetchDepth) { diff --git a/src/main.ts b/src/main.ts index aa5897be..2d925a90 100644 --- a/src/main.ts +++ b/src/main.ts @@ -61,6 +61,8 @@ export async function run(): Promise { const hasSubmodule = await submoduleExists({cwd: workingDirectory}) let gitFetchExtraArgs = ['--no-tags', '--prune', '--recurse-submodules'] const isTag = env.GITHUB_REF?.startsWith('refs/tags/') + const outputRenamedFilesAsDeletedAndAdded = + inputs.outputRenamedFilesAsDeletedAndAdded let submodulePaths: string[] = [] if (hasSubmodule) { @@ -118,7 +120,8 @@ export async function run(): Promise { workingDirectory, hasSubmodule, diffResult, - submodulePaths + submodulePaths, + outputRenamedFilesAsDeletedAndAdded }) core.debug(`All diff files: ${JSON.stringify(allDiffFiles)}`) @@ -261,7 +264,8 @@ export async function run(): Promise { const otherChangedFiles = allOtherChangedFiles .split(inputs.separator) .filter( - filePath => !allChangedFiles.split(inputs.separator).includes(filePath) + (filePath: string) => + !allChangedFiles.split(inputs.separator).includes(filePath) ) const onlyChanged = @@ -320,7 +324,8 @@ export async function run(): Promise { const otherModifiedFiles = allOtherModifiedFiles .split(inputs.separator) .filter( - filePath => !allModifiedFiles.split(inputs.separator).includes(filePath) + (filePath: string) => + !allModifiedFiles.split(inputs.separator).includes(filePath) ) const onlyModified = diff --git a/src/utils.ts b/src/utils.ts index 30c31ddc..bf76707d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -410,7 +410,8 @@ export const getAllChangedFiles = async ({ sha2, diff, isSubmodule = false, - parentDir = '' + parentDir = '', + outputRenamedFilesAsDeletedAndAdded = false }: { cwd: string sha1: string @@ -418,6 +419,7 @@ export const getAllChangedFiles = async ({ diff: string isSubmodule?: boolean parentDir?: string + outputRenamedFilesAsDeletedAndAdded?: boolean }): Promise => { const {exitCode, stdout, stderr} = await exec.getExecOutput( 'git', @@ -466,13 +468,21 @@ export const getAllChangedFiles = async ({ const lines = stdout.split('\n').filter(Boolean) for (const line of lines) { - const [changeType, filePath] = line.split('\t') + const [changeType, filePath, newPath = ''] = line.split('\t') const normalizedFilePath = isSubmodule ? normalizePath(path.join(parentDir, filePath)) : normalizePath(filePath) + const normalizedNewPath = isSubmodule + ? normalizePath(path.join(parentDir, newPath)) + : normalizePath(newPath) if (changeType.startsWith('R')) { - changedFiles[ChangeTypeEnum.Renamed].push(normalizedFilePath) + if (outputRenamedFilesAsDeletedAndAdded) { + changedFiles[ChangeTypeEnum.Deleted].push(normalizedFilePath) + changedFiles[ChangeTypeEnum.Added].push(normalizedNewPath) + } else { + changedFiles[ChangeTypeEnum.Renamed].push(normalizedFilePath) + } } else { changedFiles[changeType as ChangeTypeEnum].push(normalizedFilePath) }