|
| 1 | +name: Get merged pull request |
| 2 | +description: Given the current push event's commit SHA, finds the pull request that was merged to produce it. |
| 3 | + |
| 4 | +inputs: |
| 5 | + github_token: |
| 6 | + description: GitHub token used to call the API |
| 7 | + required: true |
| 8 | + |
| 9 | +outputs: |
| 10 | + number: |
| 11 | + description: The PR number |
| 12 | + value: ${{ steps.getMergedPR.outputs.number }} |
| 13 | + title: |
| 14 | + description: The PR title |
| 15 | + value: ${{ steps.getMergedPR.outputs.title }} |
| 16 | + body: |
| 17 | + description: The PR body |
| 18 | + value: ${{ steps.getMergedPR.outputs.body }} |
| 19 | + labels: |
| 20 | + description: Newline-separated label names |
| 21 | + value: ${{ steps.getMergedPR.outputs.labels }} |
| 22 | + assignees: |
| 23 | + description: Newline-separated assignee logins |
| 24 | + value: ${{ steps.getMergedPR.outputs.assignees }} |
| 25 | + |
| 26 | +runs: |
| 27 | + using: composite |
| 28 | + steps: |
| 29 | + - name: Find merged pull request for this commit |
| 30 | + id: getMergedPR |
| 31 | + shell: bash |
| 32 | + run: | |
| 33 | + RESP=$(gh api -H "Accept: application/vnd.github+json" "/repos/${GITHUB_REPOSITORY}/commits/${GITHUB_SHA}/pulls") |
| 34 | +
|
| 35 | + # Prefer the PR whose merge_commit_sha matches exactly (parity with the old action) |
| 36 | + PR=$(echo "$RESP" | jq -c "first(.[] | select(.merge_commit_sha == \"${GITHUB_SHA}\"))" 2>/dev/null) |
| 37 | +
|
| 38 | + # Fall back to the first merged PR associated with this commit |
| 39 | + if [[ -z "$PR" || "$PR" == "null" ]]; then |
| 40 | + PR=$(echo "$RESP" | jq -c 'first(.[] | select(.merged_at != null))' 2>/dev/null) |
| 41 | + fi |
| 42 | +
|
| 43 | + if [[ -z "$PR" || "$PR" == "null" ]]; then |
| 44 | + echo "::warning::No merged pull request found for commit ${GITHUB_SHA}" |
| 45 | + exit 0 |
| 46 | + fi |
| 47 | +
|
| 48 | + { |
| 49 | + echo "number=$(echo "$PR" | jq -r '.number')" |
| 50 | + echo "title=$(echo "$PR" | jq -r '.title')" |
| 51 | +
|
| 52 | + echo "body<<GETMPR_EOF" |
| 53 | + echo "$PR" | jq -r '.body // ""' |
| 54 | + echo "GETMPR_EOF" |
| 55 | +
|
| 56 | + echo "labels<<GETMPR_LABELS_EOF" |
| 57 | + echo "$PR" | jq -r '[.labels[].name] | join("\n")' |
| 58 | + echo "GETMPR_LABELS_EOF" |
| 59 | +
|
| 60 | + echo "assignees<<GETMPR_ASSIGNEES_EOF" |
| 61 | + echo "$PR" | jq -r '[.assignees[].login] | join("\n")' |
| 62 | + echo "GETMPR_ASSIGNEES_EOF" |
| 63 | +
|
| 64 | + } >> "$GITHUB_OUTPUT" |
| 65 | + env: |
| 66 | + GITHUB_TOKEN: ${{ inputs.github_token }} |
0 commit comments