Skip to content

Commit

Permalink
fix: Added new github action to retrieve the PR associated with the b…
Browse files Browse the repository at this point in the history
…ranch… (#537)
  • Loading branch information
barrfalk authored Jul 18, 2024
1 parent 8889e49 commit 0ef3bb5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
56 changes: 56 additions & 0 deletions .github/actions/get-pr-number.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 == "release/${{ 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
2 changes: 1 addition & 1 deletion .github/workflows/merge-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
# Get PR number for squash merges to release
- name: PR Number
id: pr
uses: bcgov-nr/action-get-pr@v0.0.1
uses: ../actions/get-pr-number

# https://github.com/bcgov/quickstart-openshift-helpers
deploy-test:
Expand Down

0 comments on commit 0ef3bb5

Please sign in to comment.