From 8dafa4bfe883954424790ff3486eabab57792dd0 Mon Sep 17 00:00:00 2001 From: Marcel Jacek <72880145+marceljk@users.noreply.github.com> Date: Mon, 23 Jun 2025 09:48:00 +0200 Subject: [PATCH] chore: add test coverage in PRs as comment (#884) * generate test coverage in CI pipeline * add pipeline which adds comments in PR with test coverage --- .github/workflows/ci.yaml | 35 ++++++++++++++++++++++++++++++++++- Makefile | 2 +- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ac13eea5..52ea2b9d 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1,9 +1,16 @@ name: CI Workflow -on: [pull_request, workflow_dispatch] +on: + pull_request: + workflow_dispatch: + push: + branches: + - main env: GO_VERSION: "1.24" + CODE_COVERAGE_FILE_NAME: "coverage.out" # must be the same as in Makefile + CODE_COVERAGE_ARTIFACT_NAME: "code-coverage" jobs: main: @@ -33,8 +40,17 @@ jobs: - name: Test run: make test + + - name: Archive code coverage results + uses: actions/upload-artifact@v4 + with: + name: ${{ env.CODE_COVERAGE_ARTIFACT_NAME }} + path: "stackit/${{ env.CODE_COVERAGE_FILE_NAME }}" + + config: name: Check GoReleaser config + if: github.event_name == 'pull_request' runs-on: ubuntu-latest steps: - name: Checkout @@ -44,3 +60,20 @@ jobs: uses: goreleaser/goreleaser-action@v6 with: args: check + + code_coverage: + name: "Code coverage report" + if: github.event_name == 'pull_request' # Do not run when workflow is triggered by push to main branch + runs-on: ubuntu-latest + needs: main + permissions: + contents: read + actions: read # to download code coverage results from "main" job + pull-requests: write # write permission needed to comment on PR + steps: + - name: Check new code coverage + uses: fgrosse/go-coverage-report@v1.2.0 + with: + coverage-artifact-name: ${{ env.CODE_COVERAGE_ARTIFACT_NAME }} + coverage-file-name: ${{ env.CODE_COVERAGE_FILE_NAME }} + root-package: 'github.com/stackitcloud/terraform-provider-stackit' \ No newline at end of file diff --git a/Makefile b/Makefile index ce6a3179..72d1ab47 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ fmt: # TEST test: @echo "Running tests for the terraform provider" - @cd $(ROOT_DIR)/stackit && go test ./... -count=1 && cd $(ROOT_DIR) + @cd $(ROOT_DIR)/stackit && go test ./... -count=1 -coverprofile=coverage.out && cd $(ROOT_DIR) # Test coverage coverage: