diff --git a/.github/workflows/pre_commit.yaml b/.github/workflows/pre_commit.yaml index 3f3f3ccb0330..c0aaf890efb3 100644 --- a/.github/workflows/pre_commit.yaml +++ b/.github/workflows/pre_commit.yaml @@ -6,9 +6,6 @@ name: pre-commit on: pull_request: - merge_group: - push: - branches: [trunk] jobs: pre-commit: @@ -17,3 +14,14 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-python@v4 - uses: pre-commit/action@v3.0.0 + - name: Collect pre-commit output + if: ${{ failure() }} + run: | + mkdir -p pre-commit-output + git diff > pre-commit-output/diff + cp $GITHUB_EVENT_PATH pre-commit-output/event + - uses: actions/upload-artifact@v4 + if: ${{ failure() }} + with: + name: pre-commit output + path: pre-commit-output/* diff --git a/.github/workflows/pre_commit_on_merge.yaml b/.github/workflows/pre_commit_on_merge.yaml new file mode 100644 index 000000000000..76102eed58e4 --- /dev/null +++ b/.github/workflows/pre_commit_on_merge.yaml @@ -0,0 +1,21 @@ +# Part of the Carbon Language project, under the Apache License v2.0 with LLVM +# Exceptions. See /LICENSE for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +# Run pre-commit checks after a commit is merged into trunk. This is like +# pre-commit, but does not trigger adding suggested edits. In principle, this +# should never fail. +name: pre-commit-on-merge + +on: + merge_group: + push: + branches: [trunk] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + - uses: pre-commit/action@v3.0.0 diff --git a/.github/workflows/pre_commit_suggestions.yaml b/.github/workflows/pre_commit_suggestions.yaml new file mode 100644 index 000000000000..96dbe828ecd2 --- /dev/null +++ b/.github/workflows/pre_commit_suggestions.yaml @@ -0,0 +1,57 @@ +# Part of the Carbon Language project, under the Apache License v2.0 with LLVM +# Exceptions. See /LICENSE for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +# Create PR suggestions based on problems found by pre-commit action. +name: pre-commit-suggestions + +on: + workflow_run: + workflows: [pre-commit] + types: + - completed + +permissions: + pull-requests: write + +jobs: + pull-request-suggestions: + if: ${{ github.event.workflow_run.conclusion == 'failure' }} + runs-on: ubuntu-latest + steps: + - name: Print event + run: cat $GITHUB_EVENT_PATH + + - name: Download pre-commit output + uses: actions/github-script@v6 + with: + script: | + let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: context.payload.workflow_run.id, + }); + + let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { + return artifact.name == "pre-commit output" + })[0]; + + let download = await github.rest.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: matchArtifact.id, + archive_format: 'zip', + }); + + let fs = require('fs'); + fs.writeFileSync(`${process.env.HOME}/output.zip`, Buffer.from(download.data)); + + - name: Unzip output + run: | + mkdir -p ~/output + unzip -d ~/output ~/output.zip diff event + + - name: Create suggestions + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: GITHUB_EVENT_PATH=~/output/event npx suggestion-bot ~/output/diff