From b3472d32e03ced200f1591aca5a50201ab44d274 Mon Sep 17 00:00:00 2001
From: Frank Lee <somerlee.9@gmail.com>
Date: Tue, 10 Jan 2023 22:30:16 +0800
Subject: [PATCH] [workflow]auto comment with test coverage report (#2419)

* [workflow]auto comment with test coverage report

* polish code

* polish yaml
---
 .github/workflows/build.yml                   | 16 ++++-
 ...mment.yml => report_precommit_failure.yml} |  4 +-
 .github/workflows/report_test_coverage.yml    | 69 +++++++++++++++++++
 .gitignore                                    |  3 +-
 4 files changed, 88 insertions(+), 4 deletions(-)
 rename .github/workflows/{comment.yml => report_precommit_failure.yml} (94%)
 create mode 100644 .github/workflows/report_test_coverage.yml

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 25c8a3957..30b932729 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -75,12 +75,26 @@ jobs:
 
       - name: Unit Testing
         run: |
-          PYTHONPATH=$PWD pytest --cov=. --cov-report lcov tests
+          PYTHONPATH=$PWD pytest --cov=. --cov-report xml tests
         env:
           DATA: /data/scratch/cifar-10
           NCCL_SHM_DISABLE: 1
           LD_LIBRARY_PATH: /github/home/.tensornvme/lib:/usr/local/nvidia/lib:/usr/local/nvidia/lib64
 
+      - name: Collate artifact
+        env:
+          PR_NUMBER: ${{ github.event.number }}
+        run: |
+          mkdir report
+          echo $PR_NUMBER > ./report/pr_number
+          mv coverage.xml ./report
+
+      - name: Upload test coverage artifact
+        uses: actions/upload-artifact@v3
+        with:
+          name: report
+          path: report/
+
       - name: Store Cache
         run: |
           # -p flag is required to preserve the file timestamp to avoid ninja rebuild
diff --git a/.github/workflows/comment.yml b/.github/workflows/report_precommit_failure.yml
similarity index 94%
rename from .github/workflows/comment.yml
rename to .github/workflows/report_precommit_failure.yml
index 9f873bad7..e6ca7b01b 100644
--- a/.github/workflows/comment.yml
+++ b/.github/workflows/report_precommit_failure.yml
@@ -1,4 +1,4 @@
-name: Auto Workflow Comment
+name: Report Precommit Failure
 
 on:
   workflow_run:
@@ -11,7 +11,7 @@ jobs:
   # 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' }}
+    if: ${{ github.event.workflow_run.conclusion == 'failure' }}
     steps:
       - name: 'Download artifact'
         uses: actions/github-script@v6
diff --git a/.github/workflows/report_test_coverage.yml b/.github/workflows/report_test_coverage.yml
new file mode 100644
index 000000000..167aa28b6
--- /dev/null
+++ b/.github/workflows/report_test_coverage.yml
@@ -0,0 +1,69 @@
+name: Report Test Coverage
+
+on:
+  workflow_run:
+    workflows: [Build]
+    types:
+      - completed
+
+jobs:
+  report-test-coverage:
+    runs-on: ubuntu-latest
+    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 == "report"
+            })[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}/report.zip`, Buffer.from(download.data));
+
+      - name: 'Unzip artifact'
+        run: |
+          unzip report.zip
+
+      - name: Code Coverage Report
+        uses: irongut/CodeCoverageSummary@v1.3.0
+        with:
+          filename: coverage.xml
+          badge: true
+          fail_below_min: true
+          format: markdown
+          hide_branch_rate: false
+          hide_complexity: false
+          indicators: true
+          output: both
+          thresholds: '80 90'
+
+      - 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 = fs.readFileSync('./code-coverage-results.md', {encoding:'utf8', flag:'r'})
+
+            await github.rest.issues.createComment({
+              owner: owner,
+              repo: repo,
+              issue_number: issue_number,
+              body: body
+            });
diff --git a/.gitignore b/.gitignore
index 8e345eeb8..bf74a7538 100644
--- a/.gitignore
+++ b/.gitignore
@@ -153,4 +153,5 @@ colossalai/version.py
 .pyi
 
 # ignore coverage test file
-converage.lcov
+coverage.lcov
+coverage.xml