Skip to content

Commit 4bbd49b

Browse files
fix: ensure the fork remote doesn't exists before creating it (#2012)
Co-authored-by: GitHub Action <[email protected]>
1 parent c6557ed commit 4bbd49b

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

dist/index.js

+22-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ const getChangedFilesFromLocalGitHistory = async ({
8888
}
8989

9090
if (isFork) {
91-
await setForkRemote({cwd: workingDirectory})
92-
remoteName = 'fork'
91+
remoteName = await setForkRemote({cwd: workingDirectory})
9392
}
9493

9594
let diffResult: DiffResult

src/utils.ts

+33-3
Original file line numberDiff line numberDiff line change
@@ -744,15 +744,45 @@ export const getParentSha = async ({cwd}: {cwd: string}): Promise<string> => {
744744
return stdout.trim()
745745
}
746746

747-
export const setForkRemote = async ({cwd}: {cwd: string}): Promise<void> => {
748-
await exec.getExecOutput(
747+
const remoteExists = async (
748+
cwd: string,
749+
remoteName: string
750+
): Promise<boolean> => {
751+
const {exitCode} = await exec.getExecOutput(
749752
'git',
750-
['remote', 'add', 'fork', github.context.payload.repository?.clone_url],
753+
['remote', 'get-url', remoteName],
751754
{
752755
cwd,
756+
ignoreReturnCode: true,
753757
silent: !core.isDebug()
754758
}
755759
)
760+
761+
return exitCode === 0
762+
}
763+
764+
export const setForkRemote = async ({cwd}: {cwd: string}): Promise<string> => {
765+
const remoteName = 'changed-files-fork'
766+
767+
const remoteFound = await remoteExists(cwd, remoteName)
768+
769+
if (!remoteFound) {
770+
await exec.getExecOutput(
771+
'git',
772+
[
773+
'remote',
774+
'add',
775+
remoteName,
776+
github.context.payload.repository?.clone_url
777+
],
778+
{
779+
cwd,
780+
silent: !core.isDebug()
781+
}
782+
)
783+
}
784+
785+
return remoteName
756786
}
757787

758788
export const verifyCommitSha = async ({

0 commit comments

Comments
 (0)