From 5ac5dc4c95a54eb860caef6a66a2f2f81243153d Mon Sep 17 00:00:00 2001 From: Powei Feng Date: Tue, 3 Mar 2026 09:24:00 -0800 Subject: [PATCH] ci: fetch PR ref explicitly to extract commit message for forks (#9756) --- .github/actions/get-commit-msg/action.yml | 32 ++++++++++++----------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/.github/actions/get-commit-msg/action.yml b/.github/actions/get-commit-msg/action.yml index 88e20ec84d..bc5e1bda32 100644 --- a/.github/actions/get-commit-msg/action.yml +++ b/.github/actions/get-commit-msg/action.yml @@ -27,17 +27,24 @@ runs: echo "$HASH" > /tmp/commit_hash.txt - name: Find commit message (PR) shell: bash - id: checkout_code if: github.event_name == 'pull_request' run: | - BEFORE_HASH=$(git rev-parse HEAD) - echo "hash=$BEFORE_HASH" >> "$GITHUB_OUTPUT" - # Next we will checkout the actual head (not the merge commits) of the PR - AFTER_HASH="${{ github.event.pull_request.head.sha }}" - git checkout $AFTER_HASH - COMMIT_MESSAGE=$(git log -1 --no-merges) - echo "$COMMIT_MESSAGE" > /tmp/commit_msg.txt - echo "$AFTER_HASH" > /tmp/commit_hash.txt + PR_NUMBER="${{ github.event.pull_request.number }}" + # Fetch the head of the PR explicitly to handle forks. Depth 50 ensures we can traverse past recent merge commits. + git fetch --depth=50 origin "pull/$PR_NUMBER/head:pr-head" + + COMMIT_HASH=$(git log -1 --no-merges pr-head --format=%H) + AUTHOR_NAME=$(git log -1 --no-merges pr-head --format=%an) + AUTHOR_EMAIL=$(git log -1 --no-merges pr-head --format=%ae) + TSTAMP=$(git log -1 --no-merges pr-head --format=%aI) + COMMIT_MESSAGE=$(git log -1 --no-merges pr-head --format=%B) + + echo "commit $COMMIT_HASH" > /tmp/commit_msg.txt + echo "Author: ${AUTHOR_NAME}<${AUTHOR_EMAIL}>" >> /tmp/commit_msg.txt + echo "Date: ${TSTAMP}" >> /tmp/commit_msg.txt + echo "" >> /tmp/commit_msg.txt + echo "$COMMIT_MESSAGE" >> /tmp/commit_msg.txt + echo "$COMMIT_HASH" > /tmp/commit_hash.txt - shell: bash id: action_output run: | @@ -47,9 +54,4 @@ runs: cat /tmp/commit_msg.txt >> "$GITHUB_OUTPUT" echo "$DELIMITER" >> "$GITHUB_OUTPUT" # Get the commit hash - echo "hash=$(cat /tmp/commit_hash.txt)" >> "$GITHUB_OUTPUT" - - name: Cleanup Find commit message (PR) - shell: bash - if: github.event_name == 'pull_request' - run: | - git checkout ${{ steps.checkout_code.outputs.hash }} \ No newline at end of file + echo "hash=$(cat /tmp/commit_hash.txt)" >> "$GITHUB_OUTPUT" \ No newline at end of file