Resolved Race Conditions in GitHub PR Labeling #26
Workflow file for this run
This file contains hidden or 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
| name: Check Label and Workflow | |
| on: | |
| pull_request: | |
| types: [opened, reopened, synchronize] | |
| workflow_run: | |
| workflows: ["Code Checks", "Check CODEOWNERS Entries", "Pull Request Ready for Review", "Audit Service Tags"] | |
| types: [completed] | |
| jobs: | |
| check: | |
| 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 workflows have completed successfully | |
| id: check_workflow | |
| run: | | |
| 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")') | |
| # 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 [ -n "$IN_PROGRESS" ]; then | |
| echo "$WORKFLOW is still in progress." | |
| ALL_COMPLETED=false | |
| # break | |
| elif [ -z "$SUCCESSFUL" ]; then | |
| echo "$WORKFLOW did not complete successfully." | |
| ALL_COMPLETED=false | |
| # break | |
| else | |
| echo "$WORKFLOW completed successfully." | |
| 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: 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 |