From 57b6157b6ca25dbda89f5f67d84cf363b911eecf Mon Sep 17 00:00:00 2001 From: Frank Lee Date: Tue, 10 Jan 2023 15:06:27 +0800 Subject: [PATCH] [workflow] auto comment if precommit check fails (#2417) --- .github/workflows/auto_example_check.yml | 3 -- .github/workflows/build.yml | 1 - .github/workflows/comment.yml | 67 ++++++++++++++++++++++++ .github/workflows/pre_commit.yml | 15 +++++- 4 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/comment.yml diff --git a/.github/workflows/auto_example_check.yml b/.github/workflows/auto_example_check.yml index 7f1e357e3..d9063bad9 100644 --- a/.github/workflows/auto_example_check.yml +++ b/.github/workflows/auto_example_check.yml @@ -28,9 +28,6 @@ jobs: - name: Get all changed example files id: changed-files uses: tj-actions/changed-files@v35 - # Using this can trigger action each time a PR is submitted. - with: - since_last_remote_commit: true - name: setup matrix id: setup-matrix run: | diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 62d6350d6..25c8a3957 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -25,7 +25,6 @@ jobs: id: find-changed-files uses: tj-actions/changed-files@v35 with: - since_last_remote_commit: true files: | op_builder/** colossalai/kernel/** diff --git a/.github/workflows/comment.yml b/.github/workflows/comment.yml new file mode 100644 index 000000000..9f873bad7 --- /dev/null +++ b/.github/workflows/comment.yml @@ -0,0 +1,67 @@ +name: Auto Workflow Comment + +on: + workflow_run: + workflows: [pre-commit] + types: + - completed + +jobs: + # comment with a message on how to do pre-commit + # if the pre-commit check was not passed + report-precommit-failure: + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.name }} == "pre-commit" && ${{ github.event.workflow_run.conclusion == 'failure' }} + steps: + - name: 'Download artifact' + 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 == "pr_number" + })[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.GITHUB_WORKSPACE}/pr_number.zip`, Buffer.from(download.data)); + + - name: 'Unzip artifact' + run: unzip pr_number.zip + + - name: 'Comment on PR' + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + let fs = require('fs'); + let issue_number = Number(fs.readFileSync('./pr_number')); + let owner = context.repo.owner; + let repo = context.repo.repo; + let run_id = context.payload.workflow_run.id; + let run_url = `https://github.com/${owner}/${repo}/actions/runs/${run_id}` + let body = ` + Your pre-commit check failed, follow the steps to run pre-commit on your file for code style consistency. + + 1. install pre-commit via "pip install pre-commit" + 2. install pre-commit hooks via "pre-commit install" + 3. run pre-commit on file with format error via "pre-commit run --files path" by replacing "path" with the actual file path + 4. commit and push to your branch + + View your job at ${run_url}. + Read our "CONTRIBUTING.md" for more reference to the code style. + `; + await github.rest.issues.createComment({ + owner: owner, + repo: repo, + issue_number: issue_number, + body: body + }); diff --git a/.github/workflows/pre_commit.yml b/.github/workflows/pre_commit.yml index 128802629..113f50ee0 100644 --- a/.github/workflows/pre_commit.yml +++ b/.github/workflows/pre_commit.yml @@ -15,8 +15,6 @@ jobs: - name: Find the changed files id: find-changed-files uses: tj-actions/changed-files@v35 - with: - since_last_remote_commit: true - name: List all changed files run: | @@ -44,3 +42,16 @@ jobs: echo "======= running pre-commit on ${file} =======" pre-commit run --files $file done + + - name: Save PR number + if: always() + env: + PR_NUMBER: ${{ github.event.number }} + run: | + mkdir -p ./pr + echo $PR_NUMBER > ./pr/pr_number + - uses: actions/upload-artifact@v3 + if: always() + with: + name: pr_number + path: pr/