Merge pull request #837 from tj-actions/fix/wrong-changed-files-for-forked-prs

fix: wrong changed files for forked prs
This commit is contained in:
repo-ranger[bot] 2022-12-05 22:02:59 +00:00 committed by GitHub
commit 703a319222
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 10 deletions

View File

@ -173,9 +173,11 @@ runs:
GITHUB_BASE_REF: ${{ github.base_ref }} GITHUB_BASE_REF: ${{ github.base_ref }}
GITHUB_HEAD_REF: ${{ github.head_ref }} GITHUB_HEAD_REF: ${{ github.head_ref }}
GITHUB_WORKSPACE: ${{ github.workspace }} GITHUB_WORKSPACE: ${{ github.workspace }}
GITHUB_HEAD_REPO_FORK: ${{ github.event.pull_request.head.repo.fork }}
GITHUB_EVENT_PULL_REQUEST_BASE_SHA: ${{ github.event.pull_request.base.sha }} GITHUB_EVENT_PULL_REQUEST_BASE_SHA: ${{ github.event.pull_request.base.sha }}
GITHUB_EVENT_BEFORE: ${{ github.event.before }} GITHUB_EVENT_BEFORE: ${{ github.event.before }}
GITHUB_EVENT_FORCED: ${{ github.event.forced }} GITHUB_EVENT_FORCED: ${{ github.event.forced }}
GITHUB_REFNAME: ${{ github.ref_name }}
# INPUT_<VARIABLE_NAME> is not available in Composite run steps # INPUT_<VARIABLE_NAME> is not available in Composite run steps
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs # https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs
INPUT_SHA: ${{ inputs.sha }} INPUT_SHA: ${{ inputs.sha }}

View File

@ -7,11 +7,16 @@ GITHUB_OUTPUT=${GITHUB_OUTPUT:-""}
EXTRA_ARGS="--no-tags" EXTRA_ARGS="--no-tags"
PREVIOUS_SHA="" PREVIOUS_SHA=""
CURRENT_SHA="" CURRENT_SHA=""
MERGE_BASE_EXRTRA_ARGS=""
if [[ "$GITHUB_REF" == "refs/tags/"* ]]; then if [[ "$GITHUB_REF" == "refs/tags/"* ]]; then
EXTRA_ARGS="" EXTRA_ARGS=""
fi fi
if [[ "$GITHUB_HEAD_REPO_FORK" == "true" ]]; then
MERGE_BASE_EXRTRA_ARGS="--fork-point"
fi
echo "::group::changed-files-diff-sha" echo "::group::changed-files-diff-sha"
if [[ -n $INPUT_PATH ]]; then if [[ -n $INPUT_PATH ]]; then
@ -47,6 +52,8 @@ fi
if [[ -z $GITHUB_BASE_REF ]]; then if [[ -z $GITHUB_BASE_REF ]]; then
echo "Running on a push event..." echo "Running on a push event..."
TARGET_BRANCH=$GITHUB_REFNAME
CURRENT_BRANCH=$TARGET_BRANCH
echo "::debug::Getting HEAD SHA..." echo "::debug::Getting HEAD SHA..."
if [[ -n "$INPUT_UNTIL" ]]; then if [[ -n "$INPUT_UNTIL" ]]; then
@ -61,15 +68,12 @@ if [[ -z $GITHUB_BASE_REF ]]; then
if [[ -z $INPUT_SHA ]]; then if [[ -z $INPUT_SHA ]]; then
CURRENT_SHA=$(git rev-list -n 1 HEAD 2>&1) && exit_status=$? || exit_status=$? CURRENT_SHA=$(git rev-list -n 1 HEAD 2>&1) && exit_status=$? || exit_status=$?
else else
# shellcheck disable=SC2086
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH"
CURRENT_SHA=$INPUT_SHA; exit_status=$? CURRENT_SHA=$INPUT_SHA; exit_status=$?
# shellcheck disable=SC2086
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" 2>&1 && exit_status=$? || exit_status=$?
fi fi
fi fi
TARGET_BRANCH=$(git name-rev --name-only "$CURRENT_SHA" 2>&1) && exit_status=$? || exit_status=$?
CURRENT_BRANCH=$TARGET_BRANCH && exit_status=$? || exit_status=$?
echo "::debug::Verifying the current commit SHA: $CURRENT_SHA" echo "::debug::Verifying the current commit SHA: $CURRENT_SHA"
git rev-parse --quiet --verify "$CURRENT_SHA^{commit}" 1>/dev/null 2>&1 && exit_status=$? || exit_status=$? git rev-parse --quiet --verify "$CURRENT_SHA^{commit}" 1>/dev/null 2>&1 && exit_status=$? || exit_status=$?
@ -127,9 +131,9 @@ if [[ -z $GITHUB_BASE_REF ]]; then
fi fi
fi fi
else else
# shellcheck disable=SC2086
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH"
PREVIOUS_SHA=$INPUT_BASE_SHA PREVIOUS_SHA=$INPUT_BASE_SHA
# shellcheck disable=SC2086
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" 2>&1 && exit_status=$? || exit_status=$?
fi fi
echo "::debug::Target branch $TARGET_BRANCH..." echo "::debug::Target branch $TARGET_BRANCH..."
@ -200,7 +204,7 @@ else
PREVIOUS_SHA=$(git rev-parse origin/"$CURRENT_BRANCH" 2>&1) && exit_status=$? || exit_status=$? PREVIOUS_SHA=$(git rev-parse origin/"$CURRENT_BRANCH" 2>&1) && exit_status=$? || exit_status=$?
fi fi
else else
PREVIOUS_SHA=$(git merge-base "$TARGET_BRANCH" "$CURRENT_SHA" 2>&1) && exit_status=$? || exit_status=$? PREVIOUS_SHA=$(git merge-base $MERGE_BASE_EXRTRA_ARGS "$TARGET_BRANCH" "$CURRENT_SHA" 2>&1) && exit_status=$? || exit_status=$?
if [[ -z "$PREVIOUS_SHA" ]]; then if [[ -z "$PREVIOUS_SHA" ]]; then
PREVIOUS_SHA=$GITHUB_EVENT_PULL_REQUEST_BASE_SHA && exit_status=$? || exit_status=$? PREVIOUS_SHA=$GITHUB_EVENT_PULL_REQUEST_BASE_SHA && exit_status=$? || exit_status=$?
@ -220,13 +224,13 @@ else
depth=$INPUT_FETCH_DEPTH depth=$INPUT_FETCH_DEPTH
max_depth=$INPUT_MAX_FETCH_DEPTH max_depth=$INPUT_MAX_FETCH_DEPTH
while ! git diff --name-only --ignore-submodules=all "$PREVIOUS_SHA"..."$CURRENT_SHA"; do while ! git diff --name-only --ignore-submodules=all "$PREVIOUS_SHA"..."$CURRENT_SHA" 1>/dev/null 2>&1; do
echo "Fetching $depth commits..." echo "Fetching $depth commits..."
# shellcheck disable=SC2086 # shellcheck disable=SC2086
git fetch $EXTRA_ARGS -u --progress --deepen="$depth" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" git fetch $EXTRA_ARGS -u --progress --deepen="$depth" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH"
PREVIOUS_SHA=$(git merge-base origin/"$TARGET_BRANCH" "$CURRENT_SHA" 2>&1) && exit_status=$? || exit_status=$? PREVIOUS_SHA=$(git merge-base $MERGE_BASE_EXRTRA_ARGS "$TARGET_BRANCH" "$CURRENT_SHA" 2>&1) && exit_status=$? || exit_status=$?
if [[ $depth -gt $max_depth ]]; then if [[ $depth -gt $max_depth ]]; then
echo "::error::Unable to locate a common ancestor between $TARGET_BRANCH and $CURRENT_SHA" echo "::error::Unable to locate a common ancestor between $TARGET_BRANCH and $CURRENT_SHA"