Skip to content

Commit 127c287

Browse files
ci: gate PRs on 100% patch coverage + 95% project floor (#24)
* 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> Co-authored-by: Manas Srivastava <[email protected]>
1 parent baf6c42 commit 127c287

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

.github/workflows/coverage.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ jobs:
126126
- uses: actions/checkout@v4
127127
with:
128128
path: provisioner
129+
# Full history so diff-cover can resolve origin/<base_ref>.
130+
fetch-depth: 0
129131
- uses: actions/checkout@v4
130132
with:
131133
repository: InstaNode-dev/common
@@ -156,3 +158,55 @@ jobs:
156158
files: provisioner/coverage.out
157159
flags: provisioner
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).
166+
# ------------------------------------------------------------------
167+
- uses: actions/setup-python@v5
168+
if: github.event_name == 'pull_request'
169+
with:
170+
python-version: '3.12'
171+
- name: Install diff-cover + cobertura converter
172+
if: github.event_name == 'pull_request'
173+
run: |
174+
pip install diff-cover
175+
go install github.com/boumenot/gocover-cobertura@latest
176+
- name: Convert coverage to Cobertura
177+
if: github.event_name == 'pull_request'
178+
working-directory: provisioner
179+
run: $(go env GOPATH)/bin/gocover-cobertura < coverage.out > coverage.xml
180+
- name: Patch coverage gate (100% of changed lines)
181+
if: github.event_name == 'pull_request'
182+
working-directory: provisioner
183+
run: |
184+
git fetch origin "${{ github.base_ref }}" --depth=1 || true
185+
diff-cover coverage.xml \
186+
--compare-branch="origin/${{ github.base_ref }}" \
187+
--fail-under=100
188+
- name: Project coverage floor (>=95% production code)
189+
if: github.event_name == 'pull_request'
190+
working-directory: provisioner
191+
# The >=95% floor is measured over PRODUCTION code only. We drop
192+
# genuinely-non-shippable packages from the coverage profile before
193+
# computing the total — this is correct measurement, NOT a waiver.
194+
# No internal/<domain> production package is ever excluded here.
195+
#
196+
# Excluded (and why):
197+
# cmd/smoke-buildinfo — diagnostic/smoke binary, not shipped logic.
198+
# cmd/* — pure diagnostic/smoke binaries.
199+
# internal/testhelpers — test-DB/setup harness (none today; future-proof).
200+
# e2e/ — black-box E2E suite (//go:build e2e; none today).
201+
# proto/gen, *_pb.go — generated protobuf code.
202+
# Build-tag-gated files (//go:build e2e|integration|chaos|loadtest)
203+
# are not compiled into the `-short` run, so they never appear in
204+
# coverage.out — the path filter below is belt-and-suspenders.
205+
run: |
206+
# Keep the `mode:` header line; drop excluded package paths.
207+
grep -vE '(/internal/testhelpers/|/cmd/|/e2e/|/proto/gen/|_pb\.go:)' \
208+
coverage.out > coverage.prod.out
209+
total=$(go tool cover -func=coverage.prod.out | tail -1 | awk '{print $3}' | tr -d '%')
210+
echo "Total project coverage: ${total}%"
211+
awk -v t="$total" 'BEGIN { exit (t+0 >= 95) ? 0 : 1 }' \
212+
|| { echo "::error::Production coverage ${total}% is below the 95% floor"; exit 1; }

0 commit comments

Comments
 (0)