Skip to content

Resolved Race Conditions in GitHub PR Labeling #8

Resolved Race Conditions in GitHub PR Labeling

Resolved Race Conditions in GitHub PR Labeling #8

name: Check Label and Workflow
on:
pull_request:
types: [opened, reopened, synchronize]
workflow_run:
workflows: ["Code Checks", "Audit Service Tags", "Check CODEOWNERS Entries"]
branches: ["*"]
types: [completed]
jobs:
check:
# if: ${{ github.ref != 'refs/heads/master' && github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
pull-requests: read
steps:
- uses: actions/checkout@v4
- name: Check for required label
id: check_label
run: |
LABELS=$(gh pr view ${{ github.event.pull_request.number }} --json labels -q '.labels[].name')
echo "$LABELS" | grep -q "run-checks" && echo "label_found=true" >> $GITHUB_OUTPUT || echo "label_found=false" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# - name: Check if another workflow is running
# id: check_workflows
# run: |
# CODE_CHECKS=$(gh run list --workflow=code_checks.yml --status=in_progress --json headBranch,event -q \
# '.[] | select(.headBranch == "${{ github.head_ref }}" and .event == "pull_request")')
# AUDIT_TAGS=$(gh run list --workflow=audit_service_tags.yml --status=in_progress --json headBranch,event -q \
# '.[] | select(.headBranch == "${{ github.head_ref }}" and .event == "pull_request")')
# if [ -n "$CODE_CHECKS" ] || [ -n "$AUDIT_TAGS" ]; then
# echo "ci_running=true" >> $GITHUB_OUTPUT
# else
# echo "ci_running=false" >> $GITHUB_OUTPUT
# fi
# env:
# GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Check if code_checks and audit_service_tags have completed successfully
id: check_workflow
run: |
WORKFLOWS=("code_checks.yml" "audit_service_tags.yml" "check_codeowners.yml")
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
break
fi
# 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
fi
done
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: Proceed if label exists and code_checks.yml is not running
if: steps.check_label.outputs.label_found == 'true' && steps.check_workflow.outputs.ci_running == 'false'
run: echo "Label is present and code_checks.yml is NOT running. Proceeding..."