Skip to content

Resolved Race Conditions in GitHub PR Labeling #32

Resolved Race Conditions in GitHub PR Labeling

Resolved Race Conditions in GitHub PR Labeling #32

name: Check Label and Workflow
on:
pull_request:
types: [opened, reopened, synchronize, ready_for_review, converted_to_draft, review_requested, review_request_removed]
workflow_run:
workflows: ["Code Checks", "Check CODEOWNERS Entries", "Pull Request Ready for Review", "Audit Service Tags", "Code Health Report"]
types: [completed]
jobs:
check:
if: ${{ github.ref != 'refs/heads/master' }}
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
pull-requests: read
steps:
- uses: actions/checkout@v4
- run: echo "${{ github.event_name }}"
- name: Check if workflows have completed successfully
id: check_workflow
run: |
if [ ${{ github.event.pull_request.draft }} = true ]; then
echo "PR is still in draft mode."
ALL_COMPLETED=false
else
WORKFLOWS=("Check CODEOWNERS Entries" "Audit Service Tags" "Code Checks")
ALL_COMPLETED=true
for WORKFLOW in "${WORKFLOWS[@]}"; do
# Check if there's any in-progress run for this workflow
IN_PROGRESS=$(gh run list --workflow="$WORKFLOW" --status=in_progress --json headBranch,event -q \
'.[] | select(.headBranch == "${{ github.head_ref }}" and .event == "pull_request")')
if [ -n "$IN_PROGRESS" ]; then
echo "$WORKFLOW is still in progress."
ALL_COMPLETED=false
else
# Check if the latest completed run was successful
SUCCESSFUL=$(gh run list --workflow="$WORKFLOW" --status=completed --json headBranch,event,conclusion -q \
'.[] | select(.headBranch == "${{ github.head_ref }}" and .event == "pull_request") | select(.conclusion == "success")' | head -n 1)
if [ -z "$SUCCESSFUL" ]; then
echo "$WORKFLOW did not complete successfully."
ALL_COMPLETED=false
# break
else
echo "$WORKFLOW completed successfully."
fi
fi
done
fi
if [ "$ALL_COMPLETED" = true ]; then
echo "ci_ready=true" >> $GITHUB_OUTPUT
else
echo "ci_ready=false" >> $GITHUB_OUTPUT
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Add ready-for-review label
if: steps.check_workflow.outputs.ci_ready == 'true'
uses: actions-ecosystem/action-add-labels@v1
with:
number: ${{ env.pr_number }}
labels: ready-for-review
- name: Remove ready-for-review label
if: steps.check_workflow.outputs.ci_ready == 'false'
uses: actions-ecosystem/action-remove-labels@v1
with:
number: ${{ env.pr_number }}
labels: ready-for-review