Skip to content

Commit

Permalink
fix: bug with prs from forks returning incorrect set of changed files
Browse files Browse the repository at this point in the history
  • Loading branch information
jackton1 committed Mar 26, 2024
1 parent 0647424 commit 78c8612
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 41 deletions.
81 changes: 50 additions & 31 deletions src/commitSha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,27 @@ export interface DiffResult {
initialCommit?: boolean
}

export const getSHAForNonPullRequestEvent = async (
inputs: Inputs,
env: Env,
workingDirectory: string,
isShallow: boolean,
hasSubmodule: boolean,
gitFetchExtraArgs: string[],
interface SHAForNonPullRequestEvent {
inputs: Inputs
env: Env
workingDirectory: string
isShallow: boolean
hasSubmodule: boolean
gitFetchExtraArgs: string[]
isTag: boolean
): Promise<DiffResult> => {
remoteName: string
}

export const getSHAForNonPullRequestEvent = async ({
inputs,
env,
workingDirectory,
isShallow,
hasSubmodule,
gitFetchExtraArgs,
isTag,
remoteName
}: SHAForNonPullRequestEvent): Promise<DiffResult> => {
let targetBranch = env.GITHUB_REF_NAME
let currentBranch = targetBranch
let initialCommit = false
Expand Down Expand Up @@ -122,8 +134,8 @@ export const getSHAForNonPullRequestEvent = async (
'-u',
'--progress',
`--deepen=${inputs.fetchDepth}`,
'origin',
`+refs/heads/${sourceBranch}:refs/remotes/origin/${sourceBranch}`
remoteName,
`+refs/heads/${sourceBranch}:refs/remotes/${remoteName}/${sourceBranch}`
]
})
} else {
Expand All @@ -134,8 +146,8 @@ export const getSHAForNonPullRequestEvent = async (
'-u',
'--progress',
`--deepen=${inputs.fetchDepth}`,
'origin',
`+refs/heads/${targetBranch}:refs/remotes/origin/${targetBranch}`
remoteName,
`+refs/heads/${targetBranch}:refs/remotes/${remoteName}/${targetBranch}`
]
})
}
Expand Down Expand Up @@ -307,14 +319,23 @@ export const getSHAForNonPullRequestEvent = async (
}
}

export const getSHAForPullRequestEvent = async (
inputs: Inputs,
env: Env,
workingDirectory: string,
isShallow: boolean,
hasSubmodule: boolean,
interface SHAForPullRequestEvent {
inputs: Inputs
workingDirectory: string
isShallow: boolean
hasSubmodule: boolean
gitFetchExtraArgs: string[]
): Promise<DiffResult> => {
remoteName: string
}

export const getSHAForPullRequestEvent = async ({
inputs,
workingDirectory,
isShallow,
hasSubmodule,
gitFetchExtraArgs,
remoteName
}: SHAForPullRequestEvent): Promise<DiffResult> => {
let targetBranch = github.context.payload.pull_request?.base?.ref
const currentBranch = github.context.payload.pull_request?.head?.ref
if (inputs.sinceLastRemoteCommit) {
Expand All @@ -330,7 +351,7 @@ export const getSHAForPullRequestEvent = async (
...gitFetchExtraArgs,
'-u',
'--progress',
'origin',
remoteName,
`pull/${github.context.payload.pull_request?.number}/head:${currentBranch}`
]
})
Expand All @@ -343,8 +364,8 @@ export const getSHAForPullRequestEvent = async (
'-u',
'--progress',
`--deepen=${inputs.fetchDepth}`,
'origin',
`+refs/heads/${currentBranch}*:refs/remotes/origin/${currentBranch}*`
remoteName,
`+refs/heads/${currentBranch}*:refs/remotes/${remoteName}/${currentBranch}*`
]
})
}
Expand All @@ -364,8 +385,8 @@ export const getSHAForPullRequestEvent = async (
'-u',
'--progress',
`--deepen=${inputs.fetchDepth}`,
'origin',
`+refs/heads/${targetBranch}:refs/remotes/origin/${targetBranch}`
remoteName,
`+refs/heads/${targetBranch}:refs/remotes/${remoteName}/${targetBranch}`
]
})

Expand Down Expand Up @@ -427,10 +448,7 @@ export const getSHAForPullRequestEvent = async (
}
}

if (
!github.context.payload.pull_request?.base?.ref ||
github.context.payload.pull_request?.head?.repo?.fork === true
) {
if (!github.context.payload.pull_request?.base?.ref) {
diff = '..'
}

Expand Down Expand Up @@ -492,7 +510,8 @@ export const getSHAForPullRequestEvent = async (
} else {
previousSha = await getRemoteBranchHeadSha({
cwd: workingDirectory,
branch: targetBranch
branch: targetBranch,
remoteName
})

if (!previousSha) {
Expand Down Expand Up @@ -521,8 +540,8 @@ export const getSHAForPullRequestEvent = async (
'-u',
'--progress',
`--deepen=${inputs.fetchDepth}`,
'origin',
`+refs/heads/${targetBranch}:refs/remotes/origin/${targetBranch}`
remoteName,
`+refs/heads/${targetBranch}:refs/remotes/${remoteName}/${targetBranch}`
]
})

Expand Down
30 changes: 22 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
hasLocalGitDirectory,
isRepoShallow,
recoverDeletedFiles,
setForkRemote,
setOutput,
submoduleExists,
updateGitGlobalConfig,
Expand Down Expand Up @@ -65,8 +66,15 @@ const getChangedFilesFromLocalGitHistory = async ({

const isShallow = await isRepoShallow({cwd: workingDirectory})
const hasSubmodule = await submoduleExists({cwd: workingDirectory})
let gitFetchExtraArgs = ['--no-tags', '--prune', '--recurse-submodules']
let gitFetchExtraArgs = ['--no-tags', '--prune']

if (hasSubmodule) {
gitFetchExtraArgs.push('--recurse-submodules')
}

const isTag = env.GITHUB_REF?.startsWith('refs/tags/')
const isFork = github.context.payload.pull_request?.head.repo.fork || false
let remoteName = 'origin'
const outputRenamedFilesAsDeletedAndAdded =
inputs.outputRenamedFilesAsDeletedAndAdded
let submodulePaths: string[] = []
Expand All @@ -79,33 +87,39 @@ const getChangedFilesFromLocalGitHistory = async ({
gitFetchExtraArgs = ['--prune', '--no-recurse-submodules']
}

if (isFork) {
await setForkRemote({cwd: workingDirectory})
remoteName = 'fork'
}

let diffResult: DiffResult

if (!github.context.payload.pull_request?.base?.ref) {
core.info(`Running on a ${github.context.eventName || 'push'} event...`)
diffResult = await getSHAForNonPullRequestEvent(
diffResult = await getSHAForNonPullRequestEvent({
inputs,
env,
workingDirectory,
isShallow,
hasSubmodule,
gitFetchExtraArgs,
isTag
)
isTag,
remoteName
})
} else {
core.info(
`Running on a ${github.context.eventName || 'pull_request'} (${
github.context.payload.action
}) event...`
)
diffResult = await getSHAForPullRequestEvent(
diffResult = await getSHAForPullRequestEvent({
inputs,
env,
workingDirectory,
isShallow,
hasSubmodule,
gitFetchExtraArgs
)
gitFetchExtraArgs,
remoteName
})
}

if (diffResult.initialCommit) {
Expand Down
24 changes: 22 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,14 +685,16 @@ export const isInsideWorkTree = async ({

export const getRemoteBranchHeadSha = async ({
cwd,
branch
branch,
remoteName
}: {
cwd: string
branch: string
remoteName: string
}): Promise<string> => {
const {stdout} = await exec.getExecOutput(
'git',
['rev-parse', `origin/${branch}`],
['rev-parse', `${remoteName}/${branch}`],
{
cwd,
silent: !core.isDebug()
Expand Down Expand Up @@ -742,6 +744,24 @@ export const getParentSha = async ({cwd}: {cwd: string}): Promise<string> => {
return stdout.trim()
}

export const setForkRemote = async ({cwd}: {cwd: string}): Promise<void> => {
if (github.context.payload.repository?.fork) {
await exec.getExecOutput(
'git',
[
'remote',
'set-url',
'fork',
github.context.payload.repository?.clone_url
],
{
cwd,
silent: !core.isDebug()
}
)
}
}

export const verifyCommitSha = async ({
sha,
cwd,
Expand Down

0 comments on commit 78c8612

Please sign in to comment.