generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |