Skip to content

Commit

Permalink
Create action.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
barrfalk committed Jul 19, 2024
1 parent edbdd69 commit 505aa54
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions .github/actions/get-pr-number/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Get PR Number
description: Get PR number for merge queues and squash merges
branding:
icon: package
color: blue

inputs:
token:
description: Specify token (GH or PAT), instead of inheriting one from the calling workflow
default: ${{ github.token }}

outputs:
pr:
description: "Associated pull request number"
value: ${{ steps.vars.outputs.pr }}

runs:
using: composite
steps:
- id: vars
shell: bash
run: |
# Get PR number based on the event type
if [ "${{ github.event_name }}" == 'pull_request' ]; then
echo "Event type: pull request"
pr=${{ github.event.number }}
elif [ "${{ github.event_name }}" == 'merge_group' ]; then
echo "Event type: merge queue"
pr=$(echo ${{ github.event.merge_group.head_ref }} | grep -Eo "queue/main/pr-[0-9]+" | cut -d '-' -f2)
elif [ "${{ github.event_name }}" == 'push' ]; then
echo "Event type: push"
pr=$(\
curl -sL -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ inputs.token }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }}/pulls \
| jq 'map(select(.base.ref == "${{ github.ref_name }}")) | .[0].number'
)
if [ -z "${pr}" ]; then
echo "No PR number found. Was this push triggered by a squashed PR merge?"
pr=""
fi
else
echo "Event type: unknown or unexpected"
pr=""
fi
# Validate PR number
if [[ ! "${pr}" =~ ^[0-9]+$ ]]; then
echo "PR number format incorrect: ${pr}"
exit 1
fi
# Output PR number
echo "Summary ---"
echo -e "\tPR: ${pr}"
echo "pr=${pr}" >> $GITHUB_OUTPUT

0 comments on commit 505aa54

Please sign in to comment.