@@ -13,20 +13,63 @@ concurrency:
1313jobs :
1414 validate :
1515 runs-on : ubuntu-latest
16- if : >
17- github.event.workflow_run.event == 'pull_request' &&
18- github.event.workflow_run.conclusion == 'success' &&
19- fromJson(github.event.workflow_run.payload).pull_request.state == 'closed' &&
20- fromJson(github.event.workflow_run.payload).pull_request.base.ref == 'main'
21- env :
22- PULL_REQUEST_MERGED : ${{ github.event.pull_request.merged }}
16+ if : github.event.workflow_run.conclusion == 'success'
2317 outputs :
2418 result : ${{ steps.validate.outputs.result }}
19+ pull_request_merged : ${{ steps.initialize.outputs.pr_merged }}
20+ pull_request_head_ref : ${{ steps.initialize.outputs.pr_head_ref }}
2521 steps :
2622 - uses : actions/checkout@v4
2723
24+ - name : Initialize
25+ id : initialize
26+ env :
27+ GH_TOKEN : ${{ secrets.PAT }}
28+ WORKFLOW_EVENT : ${{ github.event.workflow_run.event }}
29+ run : |
30+ if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
31+ echo "[skip] workflow_dispatch"
32+ exit 0
33+ fi
34+
35+ if [ "$WORKFLOW_EVENT" != "pull_request" ]; then
36+ echo "[skip] Not a pull_request event"
37+ echo "pull_request_merged=false" >> $GITHUB_OUTPUT
38+ echo "pull_request_head_ref=" >> $GITHUB_OUTPUT
39+ exit 0
40+ fi
41+
42+ PR_NUMBER=$(echo '${{ toJson(github.event.workflow_run.pull_requests) }}' | jq -r '.[0].number // empty')
43+ if [ -z "$PR_NUMBER" ]; then
44+ echo "[skip] No PR number found"
45+ echo "pull_request_merged=false" >> $GITHUB_OUTPUT
46+ echo "pull_request_head_ref=" >> $GITHUB_OUTPUT
47+ exit 0
48+ fi
49+
50+ # Fetch PR details
51+ PR_JSON=$(gh pr view "$PR_NUMBER" --repo ${{ github.repository }} --json merged,headRefName,baseRefName,state)
52+
53+ PR_MERGED=$(echo "$PR_JSON" | jq -r '.merged')
54+ PR_STATE=$(echo "$PR_JSON" | jq -r '.state')
55+
56+ # Validate PR is closed and targeting main
57+ if [ "$PR_STATE" != "CLOSED" ] || [ "$PR_BASE" != "main" ]; then
58+ echo "[skip] PR not valid: state=$PR_STATE base=$PR_BASE"
59+ exit 0
60+ fi
61+
62+ PR_HEAD_REF=$(echo "$PR_JSON" | jq -r '.headRefName')
63+ PR_BASE=$(echo "$PR_JSON" | jq -r '.baseRefName')
64+
65+ echo "pull_request_merged=$PR_MERGED" >> $GITHUB_OUTPUT
66+ echo "pull_request_head_ref=$PR_HEAD_REF" >> $GITHUB_OUTPUT
67+
2868 - name : Validate
2969 id : validate
70+ env :
71+ PULL_REQUEST_MERGED : ${{ steps.initialize.outputs.pull_request_merged }}
72+ GITHUB_HEAD_REF : ${{ steps.initialize.outputs.pull_request_head_ref }}
3073 run : |
3174 if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
3275 echo "result=true" >> $GITHUB_OUTPUT
0 commit comments