From d78a469ec76746518146bad2b4d91e820567c12b Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 4 Dec 2022 23:51:36 -0700 Subject: [PATCH 1/9] fix: wrong changed files for forked prs --- action.yml | 1 + diff-sha.sh | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/action.yml b/action.yml index a7bc6ff4..c542cd1f 100644 --- a/action.yml +++ b/action.yml @@ -173,6 +173,7 @@ runs: GITHUB_BASE_REF: ${{ github.base_ref }} GITHUB_HEAD_REF: ${{ github.head_ref }} 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_BEFORE: ${{ github.event.before }} GITHUB_EVENT_FORCED: ${{ github.event.forced }} diff --git a/diff-sha.sh b/diff-sha.sh index 7dba02fd..2cb6f498 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -7,11 +7,16 @@ GITHUB_OUTPUT=${GITHUB_OUTPUT:-""} EXTRA_ARGS="--no-tags" PREVIOUS_SHA="" CURRENT_SHA="" +MERGE_BASE_EXRTRA_ARGS="" if [[ "$GITHUB_REF" == "refs/tags/"* ]]; then EXTRA_ARGS="" fi +if [[ "$GITHUB_HEAD_REPO_FORK" == "true" ]]; then + MERGE_BASE_EXRTRA_ARGS="--fork-point" +fi + echo "::group::changed-files-diff-sha" if [[ -n $INPUT_PATH ]]; then @@ -200,7 +205,7 @@ else PREVIOUS_SHA=$(git rev-parse origin/"$CURRENT_BRANCH" 2>&1) && exit_status=$? || exit_status=$? fi 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 PREVIOUS_SHA=$GITHUB_EVENT_PULL_REQUEST_BASE_SHA && exit_status=$? || exit_status=$? @@ -220,13 +225,13 @@ else depth=$INPUT_FETCH_DEPTH max_depth=$INPUT_MAX_FETCH_DEPTH - while ! git merge-base "$PREVIOUS_SHA" "$CURRENT_SHA" 1>/dev/null; do - echo "Fetching $depth commits..." + while ! git merge-base $MERGE_BASE_EXRTRA_ARGS "$PREVIOUS_SHA" "$CURRENT_SHA" 1>/dev/null; do + echo "Fetching $depth commits..." # shellcheck disable=SC2086 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) + PREVIOUS_SHA=$(git merge-base $MERGE_BASE_EXRTRA_ARGS "$TARGET_BRANCH" "$CURRENT_SHA" 2>&1) if [[ $depth -gt $max_depth ]]; then echo "::error::Unable to locate a common ancestor between $TARGET_BRANCH and $CURRENT_SHA" From 3a46c048f740ad95ae07e504970cc82864ac4891 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Mon, 5 Dec 2022 00:02:15 -0700 Subject: [PATCH 2/9] Updated formatting --- diff-sha.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/diff-sha.sh b/diff-sha.sh index 2cb6f498..9c2be568 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -225,13 +225,13 @@ else depth=$INPUT_FETCH_DEPTH max_depth=$INPUT_MAX_FETCH_DEPTH - while ! git merge-base $MERGE_BASE_EXRTRA_ARGS "$PREVIOUS_SHA" "$CURRENT_SHA" 1>/dev/null; do - echo "Fetching $depth commits..." + while ! git merge-base $MERGE_BASE_EXRTRA_ARGS "$PREVIOUS_SHA" "$CURRENT_SHA" 1>/dev/null; do + echo "Fetching $depth commits..." # shellcheck disable=SC2086 git fetch $EXTRA_ARGS -u --progress --deepen="$depth" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" - PREVIOUS_SHA=$(git merge-base $MERGE_BASE_EXRTRA_ARGS "$TARGET_BRANCH" "$CURRENT_SHA" 2>&1) + 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 echo "::error::Unable to locate a common ancestor between $TARGET_BRANCH and $CURRENT_SHA" From a4d22acb209687e67dd7b2d83b963ac58dd0fb16 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Mon, 5 Dec 2022 00:12:48 -0700 Subject: [PATCH 3/9] Updated fetch branch --- diff-sha.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/diff-sha.sh b/diff-sha.sh index 68af10bf..9c5ff7b3 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -66,9 +66,9 @@ if [[ -z $GITHUB_BASE_REF ]]; then if [[ -z $INPUT_SHA ]]; then CURRENT_SHA=$(git rev-list -n 1 HEAD 2>&1) && exit_status=$? || exit_status=$? else - # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" CURRENT_SHA=$INPUT_SHA; exit_status=$? + # shellcheck disable=SC2086 + git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$CURRENT_SHA" 2>&1 && exit_status=$? || exit_status=$? fi fi @@ -132,9 +132,9 @@ if [[ -z $GITHUB_BASE_REF ]]; then fi fi else - # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" PREVIOUS_SHA=$INPUT_BASE_SHA + # shellcheck disable=SC2086 + git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$PREVIOUS_SHA" 2>&1 && exit_status=$? || exit_status=$? fi echo "::debug::Target branch $TARGET_BRANCH..." From c3642c4a1c9a0bcca6ba9e9f87c4b2aeaba69035 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Mon, 5 Dec 2022 13:19:24 -0700 Subject: [PATCH 4/9] Update diff-sha.sh --- diff-sha.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/diff-sha.sh b/diff-sha.sh index 9c5ff7b3..edc1f3d0 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -68,7 +68,7 @@ if [[ -z $GITHUB_BASE_REF ]]; then else CURRENT_SHA=$INPUT_SHA; exit_status=$? # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$CURRENT_SHA" 2>&1 && exit_status=$? || exit_status=$? + git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" 2>&1 && exit_status=$? || exit_status=$? fi fi @@ -134,7 +134,7 @@ if [[ -z $GITHUB_BASE_REF ]]; then else PREVIOUS_SHA=$INPUT_BASE_SHA # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$PREVIOUS_SHA" 2>&1 && exit_status=$? || exit_status=$? + git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" 2>&1 && exit_status=$? || exit_status=$? fi echo "::debug::Target branch $TARGET_BRANCH..." From aba4e5712baff31f62a76d7af30a3d2deb0db7cb Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Mon, 5 Dec 2022 14:21:07 -0700 Subject: [PATCH 5/9] Update diff-sha.sh --- diff-sha.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff-sha.sh b/diff-sha.sh index edc1f3d0..4644cfcf 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -225,7 +225,7 @@ else depth=$INPUT_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..." # shellcheck disable=SC2086 From 77fbd5a18ca9469ad9045e05ddebb145d71cd248 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Mon, 5 Dec 2022 14:29:20 -0700 Subject: [PATCH 6/9] Update diff-sha.sh --- diff-sha.sh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/diff-sha.sh b/diff-sha.sh index 4644cfcf..e47de82e 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -52,6 +52,8 @@ fi if [[ -z $GITHUB_BASE_REF ]]; then echo "Running on a push event..." + TARGET_BRANCH=$GITHUB_REFNAME + CURRENT_BRANCH=TARGET_BRANCH echo "::debug::Getting HEAD SHA..." if [[ -n "$INPUT_UNTIL" ]]; then @@ -68,13 +70,10 @@ if [[ -z $GITHUB_BASE_REF ]]; then else CURRENT_SHA=$INPUT_SHA; exit_status=$? # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" 2>&1 && exit_status=$? || exit_status=$? + git fetch $EXTRA_ARGS -u --progress --depth="$INPUT_FETCH_DEPTH" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" 2>&1 && exit_status=$? || exit_status=$? 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" git rev-parse --quiet --verify "$CURRENT_SHA^{commit}" 1>/dev/null 2>&1 && exit_status=$? || exit_status=$? @@ -134,7 +133,7 @@ if [[ -z $GITHUB_BASE_REF ]]; then else PREVIOUS_SHA=$INPUT_BASE_SHA # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" 2>&1 && exit_status=$? || exit_status=$? + git fetch $EXTRA_ARGS -u --progress --depth="$INPUT_FETCH_DEPTH" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" 2>&1 && exit_status=$? || exit_status=$? fi echo "::debug::Target branch $TARGET_BRANCH..." From fd5d6c3a2d815290237966aa24103f30b1876ac0 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Mon, 5 Dec 2022 14:30:14 -0700 Subject: [PATCH 7/9] Update action.yml --- action.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/action.yml b/action.yml index c542cd1f..f31d8ef4 100644 --- a/action.yml +++ b/action.yml @@ -177,6 +177,7 @@ runs: GITHUB_EVENT_PULL_REQUEST_BASE_SHA: ${{ github.event.pull_request.base.sha }} GITHUB_EVENT_BEFORE: ${{ github.event.before }} GITHUB_EVENT_FORCED: ${{ github.event.forced }} + GITHUB_REFNAME: ${{ github.ref_name }} # INPUT_ is not available in Composite run steps # https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs INPUT_SHA: ${{ inputs.sha }} From 860dc31320c1b45b3b06a846b7094fd51cb2fed5 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Mon, 5 Dec 2022 14:34:38 -0700 Subject: [PATCH 8/9] Update diff-sha.sh --- diff-sha.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff-sha.sh b/diff-sha.sh index e47de82e..40de9266 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -53,7 +53,7 @@ fi if [[ -z $GITHUB_BASE_REF ]]; then echo "Running on a push event..." TARGET_BRANCH=$GITHUB_REFNAME - CURRENT_BRANCH=TARGET_BRANCH + CURRENT_BRANCH=$TARGET_BRANCH echo "::debug::Getting HEAD SHA..." if [[ -n "$INPUT_UNTIL" ]]; then From e6fbdde201eb744799156ce81b7f53d3e1257ed7 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Mon, 5 Dec 2022 14:38:37 -0700 Subject: [PATCH 9/9] Update diff-sha.sh --- diff-sha.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/diff-sha.sh b/diff-sha.sh index 40de9266..01b41458 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -70,7 +70,7 @@ if [[ -z $GITHUB_BASE_REF ]]; then else CURRENT_SHA=$INPUT_SHA; exit_status=$? # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --depth="$INPUT_FETCH_DEPTH" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" 2>&1 && exit_status=$? || exit_status=$? + 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 @@ -133,7 +133,7 @@ if [[ -z $GITHUB_BASE_REF ]]; then else PREVIOUS_SHA=$INPUT_BASE_SHA # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --depth="$INPUT_FETCH_DEPTH" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" 2>&1 && exit_status=$? || exit_status=$? + 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 echo "::debug::Target branch $TARGET_BRANCH..."