Skip to content

Commit 1ec2fe7

Browse files
Report coverage in pull requests (#102)
1 parent 00ccdcd commit 1ec2fe7

File tree

1 file changed

+70
-5
lines changed

1 file changed

+70
-5
lines changed

.github/workflows/test.yaml

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# Copyright 2025 SAP SE
22
# SPDX-License-Identifier: Apache-2.0
33

4-
name: Test
4+
name: Test and Report Coverage
55

6-
on: [push]
6+
on:
7+
push:
8+
pull_request:
9+
branches:
10+
- '*'
711

812
jobs:
913
test-without-docker:
@@ -15,10 +19,12 @@ jobs:
1519
uses: actions/setup-go@v5
1620
with:
1721
go-version: 1.24.2
18-
- name: Test without Docker
22+
- name: Test quickly without Docker
1923
run: go test -v ./...
2024

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

0 commit comments

Comments
 (0)