From 3415b437807016c147990325ffd1c445deaa7a1d Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Mon, 2 Oct 2023 17:35:05 -0600 Subject: [PATCH] fix: incorrect branch name for issue_comment event (#1625) Co-authored-by: GitHub Action Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com> --- .github/workflows/issue-comment-test.yml | 108 ++++++++++++++++++++++- dist/index.js | Bin 1705996 -> 1706786 bytes dist/index.js.map | Bin 1712758 -> 1713662 bytes src/commitSha.ts | 13 ++- src/utils.ts | 22 +++++ 5 files changed, 141 insertions(+), 2 deletions(-) diff --git a/.github/workflows/issue-comment-test.yml b/.github/workflows/issue-comment-test.yml index d5eab243..b721fada 100644 --- a/.github/workflows/issue-comment-test.yml +++ b/.github/workflows/issue-comment-test.yml @@ -34,6 +34,59 @@ jobs: shell: bash + - name: Run changed-files for old new filenames test rename + id: changed-files-all-old-new-renamed-files + uses: ./ + with: + base_sha: d1c0ee4 + sha: 4d04215 + fetch_depth: 60000 + include_all_old_new_renamed_files: true + + - name: Show output + run: | + echo '${{ toJSON(steps.changed-files-all-old-new-renamed-files.outputs) }}' + shell: + bash + + - name: Show output + run: | + echo '${{ toJSON(steps.changed-files-all-old-new-renamed-files.outputs) }}' + shell: + bash + + - name: Check all_old_new_renamed_files output on non windows platform + if: "!contains(steps.changed-files-all-old-new-renamed-files.outputs.all_old_new_renamed_files, 'test/test rename 1.txt,test/test rename-1.txt') && runner.os != 'Windows'" + run: | + echo "Invalid output: Expected to include (test/test rename 1.txt,test/test rename-1.txt) got (${{ steps.changed-files-all-old-new-renamed-files.outputs.all_old_new_renamed_files }})" + exit 1 + shell: + bash + + - name: Check all_old_new_renamed_files output on windows platform + if: "!contains(steps.changed-files-all-old-new-renamed-files.outputs.all_old_new_renamed_files, 'test\\test rename 1.txt,test\\test rename-1.txt') && runner.os == 'Windows'" + run: | + echo "Invalid output: Expected to not include (test\\test rename 1.txt,test\\test rename-1.txt) got (${{ steps.changed-files-all-old-new-renamed-files.outputs.all_old_new_renamed_files }})" + exit 1 + shell: + bash + + - name: Check the renamed_files output on non windows platform + if: "!contains(steps.changed-files-all-old-new-renamed-files.outputs.renamed_files, 'test/test rename-1.txt') && runner.os != 'Windows'" + run: | + echo "Invalid output: Expected to include (test/test rename-1.txt) got (${{ steps.changed-files-all-old-new-renamed-files.outputs.renamed_files }})" + exit 1 + shell: + bash + + - name: Check the renamed_files output on windows platform + if: "!contains(steps.changed-files-all-old-new-renamed-files.outputs.renamed_files, 'test\\test rename-1.txt') && runner.os == 'Windows'" + run: | + echo "Invalid output: Expected to not include (test\\test rename-1.txt) got (${{ steps.changed-files-all-old-new-renamed-files.outputs.renamed_files }})" + exit 1 + shell: + bash + issue_commented: # This job only runs for issue comments name: Issue comment @@ -63,4 +116,57 @@ jobs: run: | echo '${{ toJSON(steps.changed-files.outputs) }}' shell: - bash \ No newline at end of file + bash + + - name: Run changed-files for old new filenames test rename + id: changed-files-all-old-new-renamed-files + uses: ./ + with: + base_sha: d1c0ee4 + sha: 4d04215 + fetch_depth: 60000 + include_all_old_new_renamed_files: true + + - name: Show output + run: | + echo '${{ toJSON(steps.changed-files-all-old-new-renamed-files.outputs) }}' + shell: + bash + + - name: Show output + run: | + echo '${{ toJSON(steps.changed-files-all-old-new-renamed-files.outputs) }}' + shell: + bash + + - name: Check all_old_new_renamed_files output on non windows platform + if: "!contains(steps.changed-files-all-old-new-renamed-files.outputs.all_old_new_renamed_files, 'test/test rename 1.txt,test/test rename-1.txt') && runner.os != 'Windows'" + run: | + echo "Invalid output: Expected to include (test/test rename 1.txt,test/test rename-1.txt) got (${{ steps.changed-files-all-old-new-renamed-files.outputs.all_old_new_renamed_files }})" + exit 1 + shell: + bash + + - name: Check all_old_new_renamed_files output on windows platform + if: "!contains(steps.changed-files-all-old-new-renamed-files.outputs.all_old_new_renamed_files, 'test\\test rename 1.txt,test\\test rename-1.txt') && runner.os == 'Windows'" + run: | + echo "Invalid output: Expected to not include (test\\test rename 1.txt,test\\test rename-1.txt) got (${{ steps.changed-files-all-old-new-renamed-files.outputs.all_old_new_renamed_files }})" + exit 1 + shell: + bash + + - name: Check the renamed_files output on non windows platform + if: "!contains(steps.changed-files-all-old-new-renamed-files.outputs.renamed_files, 'test/test rename-1.txt') && runner.os != 'Windows'" + run: | + echo "Invalid output: Expected to include (test/test rename-1.txt) got (${{ steps.changed-files-all-old-new-renamed-files.outputs.renamed_files }})" + exit 1 + shell: + bash + + - name: Check the renamed_files output on windows platform + if: "!contains(steps.changed-files-all-old-new-renamed-files.outputs.renamed_files, 'test\\test rename-1.txt') && runner.os == 'Windows'" + run: | + echo "Invalid output: Expected to not include (test\\test rename-1.txt) got (${{ steps.changed-files-all-old-new-renamed-files.outputs.renamed_files }})" + exit 1 + shell: + bash diff --git a/dist/index.js b/dist/index.js index b3fcbce577361a4c7b4593a96f007f5d24ace35d..85e6abbfd35594816d5f5bb0725349f4776b1efc 100644 GIT binary patch delta 482 zcmeB~kg;f5#)hDIOgX8W!{+&OO%9qT%;J}rn>v|MQcO6t#JRMnC^fIdsVFfoIRhj< z-A{p0Ng^{%K_eMmN5P@xv6Kz95$|cDhvoqx$r7(-?V0b#)Vyl8RExbc<5c)ODa9oIY_GWApR{qO6?L zJ!Ug%X<(QRbR5uo7{cxBvlxMx35c12m<5PgftU@5*@2h?h&h3n3y8Uamm_CI;Q&|=H}^AdD0UDY(Q+LDvM@8vvxrhuIo*0kyCmNTYe07Ggy?*IS* diff --git a/src/commitSha.ts b/src/commitSha.ts index fa9ec0fe..17ca5bcb 100644 --- a/src/commitSha.ts +++ b/src/commitSha.ts @@ -5,6 +5,7 @@ import {Env} from './env' import {Inputs} from './inputs' import { canDiffCommits, + getCurrentBranchName, getHeadSha, getParentSha, getPreviousGitTag, @@ -90,7 +91,7 @@ export const getSHAForNonPullRequestEvent = async ( isTag: boolean ): Promise => { let targetBranch = env.GITHUB_REF_NAME - const currentBranch = targetBranch + let currentBranch = targetBranch let initialCommit = false if (!inputs.skipInitialFetch) { @@ -163,6 +164,16 @@ export const getSHAForNonPullRequestEvent = async ( const currentSha = await getCurrentSHA({inputs, workingDirectory}) let previousSha = inputs.baseSha const diff = '..' + const currentBranchName = await getCurrentBranchName({cwd: workingDirectory}) + + if ( + currentBranchName && + currentBranchName !== 'HEAD' && + (currentBranchName !== targetBranch || currentBranchName !== currentBranch) + ) { + targetBranch = currentBranchName + currentBranch = currentBranchName + } if (previousSha && currentSha && currentBranch && targetBranch) { if (previousSha === currentSha) { diff --git a/src/utils.ts b/src/utils.ts index 2e01a4ee..9e3138cd 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -696,6 +696,28 @@ export const getRemoteBranchHeadSha = async ({ return stdout.trim() } +export const getCurrentBranchName = async ({ + cwd +}: { + cwd: string +}): Promise => { + const {stdout, exitCode} = await exec.getExecOutput( + 'git', + ['rev-parse', '--abbrev-ref', 'HEAD'], + { + cwd, + ignoreReturnCode: true, + silent: !core.isDebug() + } + ) + + if (exitCode !== 0) { + return '' + } + + return stdout.trim() +} + export const getParentSha = async ({cwd}: {cwd: string}): Promise => { const {stdout, exitCode} = await exec.getExecOutput( 'git',