diff --git a/.github/workflows/pre-commit-status.yml b/.github/workflows/pre-commit-status.yml index c7be9f8d352..194ab1c8255 100644 --- a/.github/workflows/pre-commit-status.yml +++ b/.github/workflows/pre-commit-status.yml @@ -9,6 +9,7 @@ on: permissions: statuses: write + pull-requests: write jobs: report-success: @@ -62,3 +63,71 @@ jobs: target_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}' })).data; core.info(`${name} is ${state}`); + + manage-labels: + name: Manage PR labels + if: github.event.workflow_run.event == 'pull_request' + runs-on: ubuntu-latest + steps: + - name: Download and Extract Artifacts + uses: dawidd6/action-download-artifact@07ab29fd4a977ae4d2b275087cf67563dfdf0295 # v9 + continue-on-error: true + with: + run_id: ${{ github.event.workflow_run.id }} + name: pr-artifacts + path: ./pr-artifacts + + - name: Get PR information + id: pr-info + run: | + if [ -f "./pr-artifacts/pr_number.txt" ]; then + pr_number=$(cat ./pr-artifacts/pr_number.txt | tr -cd '[:digit:]') + pre_commit_outcome=$(cat ./pr-artifacts/pre_commit_outcome.txt | tr -cd '[:alpha:]_') + pending_commit=$(cat ./pr-artifacts/pending_commit.txt | tr -cd '[:digit:]') + has_retrigger_label=$(cat ./pr-artifacts/has_retrigger_label.txt | tr -cd '[:alpha:]') + + echo "pr_number=$pr_number" >> $GITHUB_OUTPUT + echo "pre_commit_outcome=$pre_commit_outcome" >> $GITHUB_OUTPUT + echo "pending_commit=$pending_commit" >> $GITHUB_OUTPUT + echo "has_retrigger_label=$has_retrigger_label" >> $GITHUB_OUTPUT + echo "artifacts_found=true" >> $GITHUB_OUTPUT + + echo "PR number: $pr_number" + echo "Pre-commit outcome: $pre_commit_outcome" + echo "Pending commit: $pending_commit" + echo "Has retrigger label: $has_retrigger_label" + else + echo "No PR artifacts found" + echo "artifacts_found=false" >> $GITHUB_OUTPUT + fi + + - name: Remove re-trigger label if it was present + if: | + steps.pr-info.outputs.artifacts_found == 'true' && + steps.pr-info.outputs.has_retrigger_label == 'true' + continue-on-error: true + run: | + gh pr edit ${{ steps.pr-info.outputs.pr_number }} --repo ${{ github.repository }} --remove-label 'Re-trigger Pre-commit Hooks' + env: + GH_TOKEN: ${{ github.token }} + + - name: Add label if pre-commit fixes are required + if: | + steps.pr-info.outputs.artifacts_found == 'true' && + steps.pr-info.outputs.pre_commit_outcome == 'failure' && + steps.pr-info.outputs.pending_commit == '0' + continue-on-error: true + run: | + gh pr edit ${{ steps.pr-info.outputs.pr_number }} --repo ${{ github.repository }} --add-label 'Status: Pre-commit fixes required ⚠️' + env: + GH_TOKEN: ${{ github.token }} + + - name: Remove label if pre-commit was successful + if: | + steps.pr-info.outputs.artifacts_found == 'true' && + steps.pr-info.outputs.pre_commit_outcome == 'success' + continue-on-error: true + run: | + gh pr edit ${{ steps.pr-info.outputs.pr_number }} --repo ${{ github.repository }} --remove-label 'Status: Pre-commit fixes required ⚠️' + env: + GH_TOKEN: ${{ github.token }} diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index 5f17b683e19..30bbc3c3e41 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -31,13 +31,6 @@ jobs: with: fetch-depth: 2 - - name: Remove Label - if: contains(github.event.pull_request.labels.*.name, 'Re-trigger Pre-commit Hooks') - run: gh pr edit ${{ github.event.number }} --remove-label 'Re-trigger Pre-commit Hooks' - continue-on-error: true - env: - GH_TOKEN: ${{ github.token }} - - name: Set up Python 3 uses: actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5.0.4 with: @@ -85,19 +78,20 @@ jobs: with: msg: "ci(pre-commit): Apply automatic fixes" - - name: Add label if no commits are pending - if: ${{ failure() && steps.pre-commit.outcome == 'failure' && steps.pre-commit.outputs.pending_commit == '0' && github.event_name == 'pull_request' }} - continue-on-error: true + - name: Save workflow information for labeling + if: ${{ always() && github.event_name == 'pull_request' }} run: | - gh pr edit ${{ github.event.number }} --add-label 'Status: Pre-commit fixes required ⚠️' - env: - GH_TOKEN: ${{ github.token }} + mkdir -p ./pr-artifacts + echo "${{ github.event.number }}" > ./pr-artifacts/pr_number.txt + echo "${{ steps.pre-commit.outcome }}" > ./pr-artifacts/pre_commit_outcome.txt + echo "${{ steps.pre-commit.outputs.pending_commit }}" > ./pr-artifacts/pending_commit.txt + echo "${{ contains(github.event.pull_request.labels.*.name, 'Re-trigger Pre-commit Hooks') }}" > ./pr-artifacts/has_retrigger_label.txt - - name: Remove label if everything was fixed - if: ${{ success() && github.event_name == 'pull_request' }} - continue-on-error: true - run: | - gh pr edit ${{ github.event.number }} --remove-label 'Status: Pre-commit fixes required ⚠️' - env: - GH_TOKEN: ${{ github.token }} + - name: Upload PR artifacts + if: ${{ always() && github.event_name == 'pull_request' }} + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: pr-artifacts + path: ./pr-artifacts/ + retention-days: 1