From e33c043dec37d0e78b18f6df36fbc3890f0b53f8 Mon Sep 17 00:00:00 2001 From: Frank Lee Date: Fri, 24 Feb 2023 14:41:33 +0800 Subject: [PATCH] [workflow] moved pre-commit to post-commit (#2895) --- .github/workflows/README.md | 10 ++- .../{pre_commit.yml => post_commit.yml} | 50 +++++++++----- .../workflows/report_precommit_failure.yml | 67 ------------------- 3 files changed, 39 insertions(+), 88 deletions(-) rename .github/workflows/{pre_commit.yml => post_commit.yml} (50%) delete mode 100644 .github/workflows/report_precommit_failure.yml diff --git a/.github/workflows/README.md b/.github/workflows/README.md index 3bf535343..9634b84b8 100644 --- a/.github/workflows/README.md +++ b/.github/workflows/README.md @@ -35,10 +35,9 @@ I will provide the details of each workflow below. ### Code Style Check -| Workflow Name | File name | Description | -| --------------------------- | ------------------------------ | ---------------------------------------------------------------------------------------------------------- | -| `Pre-commit` | `pre_commit.yml` | This workflow runs pre-commit checks for code style consistency for PRs. | -| `Report pre-commit failure` | `report_precommit_failure.yml` | This PR will put up a comment in the PR to explain the precommit failure and remedy if `Pre-commit` fails. | +| Workflow Name | File name | Description | +| ------------- | ----------------- | -------------------------------------------------------------------------------------------------------------- | +| `post-commit` | `post_commit.yml` | This workflow runs pre-commit checks for changed files to achieve code style consistency after a PR is merged. | ### Unit Test @@ -130,8 +129,7 @@ This file controls which CUDA versions will be checked against CUDA extenson bui ## Progress Log - [x] Code style check - - [x] pre-commit check - - [x] pre-commit failure report + - [x] post-commit check - [x] unit testing - [x] test on PR - [x] report test coverage diff --git a/.github/workflows/pre_commit.yml b/.github/workflows/post_commit.yml similarity index 50% rename from .github/workflows/pre_commit.yml rename to .github/workflows/post_commit.yml index 3e71be2fc..765a4d428 100644 --- a/.github/workflows/pre_commit.yml +++ b/.github/workflows/post_commit.yml @@ -1,11 +1,17 @@ -name: pre-commit +name: post-commit on: pull_request: + types: + - closed jobs: + # this job will run after a PR is merged to run pre-commit on any changed file + # so that the user does not need to learn pre-commit and pre-commit can still + # be auto-executed by the workflow pre-commit: runs-on: ubuntu-latest + if: github.event.pull_request.merged == true && github.repository == 'hpcaitech/ColossalAI' steps: - uses: actions/checkout@v2 with: @@ -36,6 +42,11 @@ jobs: echo "$file was changed" done + # check out the main branch + - uses: actions/checkout@v2 + with: + ref: 'main' + - uses: actions/setup-python@v3 - name: Cache pre-commit hooks @@ -49,23 +60,32 @@ jobs: pip install pre-commit pre-commit install - - name: Run pre-commit on Changed Files - id: precommit + # run pre-commit on changed files + - name: Run Pre-commit run: | for file in ${{ steps.find-changed-files.outputs.all_changed_files }}; do - echo "======= running pre-commit on ${file} =======" - pre-commit run --files $file + pre-commit run --files $file || true done - - name: Save PR number - if: always() - env: - PR_NUMBER: ${{ github.event.number }} + # create commit for pre-commit + - name: Create commits run: | - mkdir -p ./pr - echo $PR_NUMBER > ./pr/pr_number - - uses: actions/upload-artifact@v3 - if: always() + git config --global user.name 'github-actions' + git config --global user.email 'github-actions@github.com' + git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }} + git add -A + git commit -am "[format] applied code formatting on changed files in pull request ${{ github.event.pull_request.number }}" + + # create pull request + - name: Create Pull Request + id: cpr + uses: peter-evans/create-pull-request@v4 with: - name: pr_number - path: pr/ + branch: pre-commit-${{ github.event.pull_request.number }} + title: "[format] applied code formatting on changed files in PR ${{ github.event.pull_request.number }}" + + - name: Enable Auto-merge for the New PR + uses: peter-evans/enable-pull-request-automerge@v2 + with: + pull-request-number: ${{ steps.cpr.outputs.pull-request-number }} + merge-method: squash diff --git a/.github/workflows/report_precommit_failure.yml b/.github/workflows/report_precommit_failure.yml deleted file mode 100644 index e6ca7b01b..000000000 --- a/.github/workflows/report_precommit_failure.yml +++ /dev/null @@ -1,67 +0,0 @@ -name: Report Precommit Failure - -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.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 - });