3
0
Fork 0
mirror of https://github.com/tj-actions/changed-files synced 2024-12-17 13:47:20 +00:00

fix: bug retrieving diff when persist credentials is false (#1209)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Tonye Jack 2023-06-01 10:03:09 -06:00 committed by GitHub
parent ab45871481
commit 5978e5a2df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 17 deletions

View file

@ -343,6 +343,39 @@ jobs:
shell: shell:
bash bash
test-pull-request-without-persist-credentials:
name: Test changed-files with pull request without persist credentials
runs-on: ubuntu-latest
needs: build
if: github.event_name != 'push'
strategy:
fail-fast: false
max-parallel: 4
matrix:
fetch-depth: [1, 2, 0]
steps:
- name: Checkout to branch
uses: actions/checkout@v3
with:
fetch-depth: ${{ matrix.fetch-depth }}
persist-credentials: false
- name: Download build assets
uses: actions/download-artifact@v3
with:
name: build-assets
- name: Run changed-files
id: changed-files
uses: ./
- name: Show output
run: |
echo '${{ toJSON(steps.changed-files.outputs) }}'
shell:
bash
test-non-existent-base-sha: test-non-existent-base-sha:
name: Test changed-files non existent base sha name: Test changed-files non existent base sha
runs-on: ubuntu-latest runs-on: ubuntu-latest

BIN
dist/index.js generated vendored

Binary file not shown.

BIN
dist/index.js.map generated vendored

Binary file not shown.

View file

@ -7,10 +7,10 @@ import {
getHeadSha, getHeadSha,
getParentSha, getParentSha,
getPreviousGitTag, getPreviousGitTag,
getRemoteBranchHeadSha,
gitFetch, gitFetch,
gitFetchSubmodules, gitFetchSubmodules,
gitLog, gitLog,
gitLsRemote,
verifyCommitSha verifyCommitSha
} from './utils' } from './utils'
@ -261,7 +261,7 @@ export const getSHAForPullRequestEvent = async (
if (isShallow) { if (isShallow) {
core.info('Repository is shallow, fetching more history...') core.info('Repository is shallow, fetching more history...')
const prFetchExitCode = await gitFetch({ let prFetchExitCode = await gitFetch({
cwd: workingDirectory, cwd: workingDirectory,
args: [ args: [
...gitExtraArgs, ...gitExtraArgs,
@ -273,7 +273,7 @@ export const getSHAForPullRequestEvent = async (
}) })
if (prFetchExitCode !== 0) { if (prFetchExitCode !== 0) {
await gitFetch({ prFetchExitCode = await gitFetch({
cwd: workingDirectory, cwd: workingDirectory,
args: [ args: [
...gitExtraArgs, ...gitExtraArgs,
@ -286,6 +286,12 @@ export const getSHAForPullRequestEvent = async (
}) })
} }
if (prFetchExitCode !== 0) {
throw new Error(
'Failed to fetch pull request branch. Please ensure "persist-credentials" is set to "true" when checking out the repository. See: https://github.com/actions/checkout#usage'
)
}
if (!inputs.sinceLastRemoteCommit) { if (!inputs.sinceLastRemoteCommit) {
core.debug('Fetching target branch...') core.debug('Fetching target branch...')
await gitFetch({ await gitFetch({
@ -354,9 +360,9 @@ export const getSHAForPullRequestEvent = async (
previousSha = env.GITHUB_EVENT_BEFORE previousSha = env.GITHUB_EVENT_BEFORE
if (!previousSha) { if (!previousSha) {
previousSha = await gitLsRemote({ previousSha = await getRemoteBranchHeadSha({
cwd: workingDirectory, cwd: workingDirectory,
args: [currentBranch] branch: currentBranch
}) })
} }
@ -370,9 +376,9 @@ export const getSHAForPullRequestEvent = async (
previousSha = env.GITHUB_EVENT_PULL_REQUEST_BASE_SHA previousSha = env.GITHUB_EVENT_PULL_REQUEST_BASE_SHA
} }
} else { } else {
previousSha = await gitLsRemote({ previousSha = await getRemoteBranchHeadSha({
cwd: workingDirectory, cwd: workingDirectory,
args: [targetBranch] branch: targetBranch
}) })
if (!previousSha) { if (!previousSha) {

View file

@ -501,28 +501,23 @@ export const getHeadSha = async ({cwd}: {cwd: string}): Promise<string> => {
return stdout.trim() return stdout.trim()
} }
export const gitLsRemote = async ({ export const getRemoteBranchHeadSha = async ({
cwd, cwd,
args branch
}: { }: {
cwd: string cwd: string
args: string[] branch: string
}): Promise<string> => { }): Promise<string> => {
const {stdout} = await exec.getExecOutput( const {stdout} = await exec.getExecOutput(
'git', 'git',
['ls-remote', 'origin', ...args], ['rev-parse', `refs/remotes/origin/${branch}`],
{ {
cwd, cwd,
silent: process.env.RUNNER_DEBUG !== '1' silent: process.env.RUNNER_DEBUG !== '1'
} }
) )
const output = stdout.trim().split('\t')
if (output.length === 0) { return stdout.trim()
throw new Error('No output returned from git ls-remote')
}
return output[0]
} }
export const getParentSha = async ({cwd}: {cwd: string}): Promise<string> => { export const getParentSha = async ({cwd}: {cwd: string}): Promise<string> => {