11# Copyright 2025 SAP SE
22# SPDX-License-Identifier: Apache-2.0
33
4- name : Test
4+ name : Test and Report Coverage
55
66on : [push]
77
@@ -15,10 +15,12 @@ jobs:
1515 uses : actions/setup-go@v5
1616 with :
1717 go-version : 1.24.2
18- - name : Test without Docker
18+ - name : Test quickly without Docker
1919 run : go test -v ./...
2020
2121 test-with-docker :
22+ # We don't need to run this longer test if the previous one already failed.
23+ needs : test-without-docker
2224 runs-on : ubuntu-latest
2325 services :
2426 dind :
3234 uses : actions/setup-go@v5
3335 with :
3436 go-version : 1.24.2
35- - name : Test with Docker
36- run : POSTGRES_CONTAINER=1 VERNEMQ_CONTAINER=1 go test -v ./...
37+ - name : Run tests with Docker and calculate coverage
38+ run : |
39+ POSTGRES_CONTAINER=1 VERNEMQ_CONTAINER=1 go test -v -coverpkg=./internal/... -coverprofile=pr_profile.cov ./internal/...
40+ CURRENT_COVERAGE=$(go tool cover -func pr_profile.cov | grep total | awk '{print $3}' | tr -d '%')
41+ echo "CURRENT_COVERAGE=$CURRENT_COVERAGE" >> $GITHUB_ENV
42+
43+ # Steps below are only executed if the workflow is triggered by a pull request
44+ - name : Checkout base commit (PR only)
45+ if : ${{ github.event_name == 'pull_request' }}
46+ uses : actions/checkout@v4
47+ with :
48+ ref : ${{ github.event.pull_request.base.sha }}
49+ - name : Run tests on base commit and calculate previous coverage (PR only)
50+ if : ${{ github.event_name == 'pull_request' }}
51+ run : |
52+ POSTGRES_CONTAINER=1 VERNEMQ_CONTAINER=1 go test -v -coverpkg=./internal/... -coverprofile=base_profile.cov ./internal/...
53+ PREVIOUS_COVERAGE=$(go tool cover -func base_profile.cov | grep total | awk '{print $3}' | tr -d '%')
54+ echo "PREVIOUS_COVERAGE=$PREVIOUS_COVERAGE" >> $GITHUB_ENV
55+ - name : Calculate coverage change (PR only)
56+ if : ${{ github.event_name == 'pull_request' }}
57+ run : |
58+ CHANGE=$(echo "$CURRENT_COVERAGE - $PREVIOUS_COVERAGE" | bc)
59+ echo "CHANGE=$CHANGE" >> $GITHUB_ENV
60+ - name : Delete old coverage comments (PR only)
61+ if : ${{ github.event_name == 'pull_request' }}
62+ uses : actions/github-script@v7
63+ with :
64+ script : |
65+ const { data: comments } = await github.rest.issues.listComments({
66+ owner: context.repo.owner,
67+ repo: context.repo.repo,
68+ issue_number: context.issue.number,
69+ });
70+ const coverageCommentTag = '<!-- coverage-comment -->';
71+ for (const comment of comments) {
72+ if (comment.body.includes(coverageCommentTag)) {
73+ await github.rest.issues.deleteComment({
74+ owner: context.repo.owner,
75+ repo: context.repo.repo,
76+ comment_id: comment.id,
77+ });
78+ }
79+ }
80+ - name : Post coverage comment (PR only)
81+ if : ${{ github.event_name == 'pull_request' }}
82+ uses : actions/github-script@v7
83+ with :
84+ script : |
85+ const currentCoverage = process.env.CURRENT_COVERAGE || 'unknown';
86+ const previousCoverage = process.env.PREVIOUS_COVERAGE || 'unknown';
87+ const change = process.env.CHANGE || 'unknown';
88+ const commentBody = `
89+ <!-- coverage-comment -->
90+ Coverage in go module **internal/**: **${currentCoverage}%** (${change >= 0 ? '+' : ''}${change}%)
91+ `;
92+ await github.rest.issues.createComment({
93+ issue_number: context.issue.number,
94+ owner: context.repo.owner,
95+ repo: context.repo.repo,
96+ body: commentBody,
97+ });
0 commit comments