From 748c5630caa13f926505a18829b123d40b9066c7 Mon Sep 17 00:00:00 2001 From: Connor Catlett Date: Wed, 27 Sep 2023 17:18:25 +0000 Subject: [PATCH] Add Github actions code coverage CI job Signed-off-by: Connor Catlett --- .github/workflows/code-coverage.yaml | 89 ++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 .github/workflows/code-coverage.yaml diff --git a/.github/workflows/code-coverage.yaml b/.github/workflows/code-coverage.yaml new file mode 100644 index 0000000000..8e3f1b4d5c --- /dev/null +++ b/.github/workflows/code-coverage.yaml @@ -0,0 +1,89 @@ +name: Code Coverage +on: [pull_request] + +jobs: + cover-base: + name: Generate Base Coverage + runs-on: ubuntu-latest + steps: + - name: Checkout base + uses: actions/checkout@v3 + with: + ref: ${{ github.event.pull_request.base.ref }} + + - name: Set up go + uses: actions/setup-go@v3 + with: + go-version: '^1.20.2' + + - name: Generate report + run: | + go test -coverprofile cover-base.out ./cmd/... ./pkg/... + + - name: Upload report + uses: actions/upload-artifact@v3 + with: + name: cover-base + path: cover-base.out + + cover-pr: + name: Generate PR Coverage + runs-on: ubuntu-latest + steps: + - name: Checkout PR + uses: actions/checkout@v3 + + - name: Set up go + uses: actions/setup-go@v3 + with: + go-version: '^1.20.2' + + - name: Generate report + run: | + go test -coverprofile cover-pr.out ./cmd/... ./pkg/... + + - name: Upload report + uses: actions/upload-artifact@v3 + with: + name: cover-pr + path: cover-pr.out + + code-coverage: + name: Output Code Coverage + runs-on: ubuntu-latest + needs: [cover-base, cover-pr] + steps: + - name: Download reports + uses: actions/download-artifact@v3 + + - name: Set up go + uses: actions/setup-go@v3 + with: + go-version: '^1.20.2' + + - name: Install copherage tool + run: go install k8s.io/test-infra/robots/coverage@latest + + - name: Generate comment + id: generate-comment + run: | + echo 'comment<> $GITHUB_OUTPUT + echo '' >> $GITHUB_OUTPUT + echo '## Code Coverage Diff' >> $GITHUB_OUTPUT + coverage diff cover-base/cover-base.out cover-pr/cover-pr.out + COVERAGE_DIFF=$(coverage diff cover-base/cover-base.out cover-pr/cover-pr.out | sed -e '1,5d') + if [[ -n "${COVERAGE_DIFF}" ]]; then + printf -- "%s\n" "${COVERAGE_DIFF}" >> $GITHUB_OUTPUT + else + echo 'This PR does not change the code coverage' >> $GITHUB_OUTPUT + fi + echo 'EOF' >> $GITHUB_OUTPUT + + - name: Create or update comment + uses: edumserrano/find-create-or-update-comment@v1 + with: + issue-number: ${{ github.event.pull_request.number }} + body-includes: '' + comment-author: 'github-actions[bot]' + body: ${{ steps.generate-comment.outputs.comment }} + edit-mode: replace