Skip to content

Commit f28aae3

Browse files
ci: gate PRs on 100% patch coverage + 95% project floor (#53)
* ci(coverage): gate PRs on 100% patch coverage + 95% project floor Adds diff-cover patch-coverage enforcement to the coverage workflow: every changed line in a PR must be covered by a test (--fail-under=100), and total project coverage must stay >=95%. Go coverage is converted to Cobertura via gocover-cobertura so diff-cover can read it. fetch-depth: 0 lets diff-cover resolve origin/<base_ref>; gated to pull_request events. New org mandate. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * ci: measure 95% project floor over production code only The >=95% project floor was computed over the full coverage.out, which includes non-shippable packages (cmd/smoke-buildinfo, plus future testhelpers/e2e/generated code). That dilutes the denominator and turns a coverage gate into noise from diagnostic binaries. Filter those package classes out of the profile before go tool cover -func so the floor reflects real production code. Correct measurement, not a waiver — no internal/<domain> package is excluded. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 088f61c commit f28aae3

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

.github/workflows/coverage.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ jobs:
7474
- uses: actions/checkout@v4
7575
with:
7676
path: worker
77+
# Full history so diff-cover can resolve origin/<base_ref>.
78+
fetch-depth: 0
7779

7880
- name: Checkout proto sibling (for go.mod replace ../proto)
7981
uses: actions/checkout@v4
@@ -156,3 +158,57 @@ jobs:
156158
files: worker/coverage.out
157159
flags: worker
158160
fail_ci_if_error: false
161+
162+
# ------------------------------------------------------------------
163+
# Org patch-coverage mandate: every changed line in a PR diff must be
164+
# covered by a test (100%), and the project floor stays >=95%.
165+
# Tool: diff-cover (https://github.com/Bachmann1234/diff-cover). The
166+
# "Generate coverage" step is `|| true` so it still produces
167+
# coverage.out even on a flaky test — the gate reads that file.
168+
# ------------------------------------------------------------------
169+
- uses: actions/setup-python@v5
170+
if: github.event_name == 'pull_request'
171+
with:
172+
python-version: '3.12'
173+
- name: Install diff-cover + cobertura converter
174+
if: github.event_name == 'pull_request'
175+
run: |
176+
pip install diff-cover
177+
go install github.com/boumenot/gocover-cobertura@latest
178+
- name: Convert coverage to Cobertura
179+
if: github.event_name == 'pull_request'
180+
working-directory: worker
181+
run: $(go env GOPATH)/bin/gocover-cobertura < coverage.out > coverage.xml
182+
- name: Patch coverage gate (100% of changed lines)
183+
if: github.event_name == 'pull_request'
184+
working-directory: worker
185+
run: |
186+
git fetch origin "${{ github.base_ref }}" --depth=1 || true
187+
diff-cover coverage.xml \
188+
--compare-branch="origin/${{ github.base_ref }}" \
189+
--fail-under=100
190+
- name: Project coverage floor (>=95% production code)
191+
if: github.event_name == 'pull_request'
192+
working-directory: worker
193+
# The >=95% floor is measured over PRODUCTION code only. We drop
194+
# genuinely-non-shippable packages from the coverage profile before
195+
# computing the total — this is correct measurement, NOT a waiver.
196+
# No internal/<domain> production package is ever excluded here.
197+
#
198+
# Excluded (and why):
199+
# cmd/smoke-buildinfo — diagnostic/smoke binary, not shipped logic.
200+
# cmd/* — pure diagnostic/smoke binaries.
201+
# internal/testhelpers — test-DB/setup harness (none today; future-proof).
202+
# e2e/ — black-box E2E suite (//go:build e2e; none today).
203+
# proto/gen, *_pb.go — generated protobuf code.
204+
# Build-tag-gated files (//go:build e2e|integration|chaos|loadtest)
205+
# are not compiled into the `-short` run, so they never appear in
206+
# coverage.out — the path filter below is belt-and-suspenders.
207+
run: |
208+
# Keep the `mode:` header line; drop excluded package paths.
209+
grep -vE '(/internal/testhelpers/|/cmd/|/e2e/|/proto/gen/|_pb\.go:)' \
210+
coverage.out > coverage.prod.out
211+
total=$(go tool cover -func=coverage.prod.out | tail -1 | awk '{print $3}' | tr -d '%')
212+
echo "Total project coverage: ${total}%"
213+
awk -v t="$total" 'BEGIN { exit (t+0 >= 95) ? 0 : 1 }' \
214+
|| { echo "::error::Production coverage ${total}% is below the 95% floor"; exit 1; }

0 commit comments

Comments
 (0)