Skip to content

Instantly share code, notes, and snippets.

@aurbano
Created October 6, 2023 12:44
Show Gist options
  • Save aurbano/858b9778103cad787db4b2efb5b7387e to your computer and use it in GitHub Desktop.
Save aurbano/858b9778103cad787db4b2efb5b7387e to your computer and use it in GitHub Desktop.
Github Action: Ensure that "Required PRs" are merged before merging the current one
name: Check Required PRs
on:
pull_request:
types: [opened, synchronize, reopened, edited]
jobs:
check-linked-prs:
runs-on: ubuntu-latest
steps:
- name: Check Required PRs
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_BODY=$(jq --raw-output .pull_request.body "$GITHUB_EVENT_PATH")
PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
# Extract content under "Linked PRs" heading
SECTION_CONTENT=$(echo "$PR_BODY" | awk '/# Required PRs/ {flag=1; next} /# / {flag=0} flag')
# Extract PR numbers from URLs in the specific section
LINKED_PRS=$(echo "$SECTION_CONTENT" | grep -oP 'https://github\.com/[^/]+/[^/]+/pull/\K\d+')
for LINKED_PR_NUMBER in $LINKED_PRS; do
PR_STATUS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$GITHUB_REPOSITORY/pulls/$LINKED_PR_NUMBER" | jq --raw-output .merged)
if [ "$PR_STATUS" != "true" ]; then
echo "Error: Required PR #$LINKED_PR_NUMBER is not merged!"
exit 1
fi
done
# Comment that all Required PRs are merged
echo "All Required PRs have been merged!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment