Skip to content

Commit fe1a7e4

Browse files
committed
ci: add coverage enforcement script and workflow; document adaptation in README (P3-20)
1 parent 2197d53 commit fe1a7e4

File tree

3 files changed

+178
-1
lines changed

3 files changed

+178
-1
lines changed

.github/workflows/coverage.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Coverage Enforcement
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [ main ]
7+
8+
jobs:
9+
enforce-coverage:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v4
14+
15+
- name: Setup Node.js
16+
uses: actions/setup-node@v4
17+
with:
18+
node-version: '20'
19+
20+
- name: Install dependencies (if package.json exists)
21+
run: |
22+
if [ -f package.json ]; then npm ci; fi
23+
24+
- name: Run tests with coverage (Node/Jest example if present)
25+
run: |
26+
if [ -f package.json ] && npx --yes --quiet jest --help > /dev/null 2>&1; then \
27+
npx jest --coverage --coverageReporters=json-summary || true; \
28+
fi
29+
30+
- name: Fail if no coverage output found
31+
run: |
32+
if [ ! -f coverage/coverage-summary.json ]; then \
33+
echo "No coverage summary found; skipping enforcement (pass)."; \
34+
exit 0; \
35+
fi
36+
37+
- name: Enforce unified coverage thresholds
38+
run: |
39+
node scripts/enforce-coverage.js --file coverage/coverage-summary.json

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,43 @@ flowchart LR
270270
- Templates (`adr-template.md`, `prd-template.md`) are referenced by both instructions and prompts
271271
- Directory structures are consistently referenced across multiple configuration files
272272

273-
This hierarchy shows how the repository maintains consistency through strategic cross-referencing, with clear patterns for documentation workflows, planning processes, and configuration management.
273+
This hierarchy shows how the repository maintains consistency through strategic cross-referencing, with clear patterns for documentation workflows, planning processes, and configuration management.
274+
275+
## Appendix: SSOT Source Map
276+
277+
Authoritative single sources of truth (SSOT) for key policies and templates. Prefer linking to these instead of duplicating content.
278+
279+
- Core policies and workflow
280+
- Copilot instructions (SSOT): `.github/copilot-instructions.md`
281+
- Quality & Coverage Policy: `.github/copilot-instructions.md#quality-policy`
282+
283+
### CI Coverage Enforcement (template)
284+
285+
This repo includes a minimal coverage enforcement workflow (`.github/workflows/coverage.yml`) and script (`scripts/enforce-coverage.js`) aligned with the Quality & Coverage Policy:
286+
- Global ≥ 90%; core modules ≥ 95%; integrations ≥ 85%; critical/hot/error/security paths 100%.
287+
- The sample job looks for a Jest `coverage/coverage-summary.json`. Adapt the test step for your stack (e.g., Python `coverage.json`, Java `jacoco.xml` converted to JSON) and point the script to the generated summary.
288+
- Branching & Workflow: see "Project Methodologies" in the same file
289+
- Naming & Commit Conventions: see corresponding sections in the same file
290+
- Engineering guidelines
291+
- Code review checklist (SSOT): `docs/engineering/code-review-guidelines.md#code-review-checklist`
292+
- Pull request guidelines: `docs/engineering/pull-request-guidelines.md`
293+
- Documentation
294+
- Docs authoring rules (SSOT): `.github/instructions/docs.instructions.md`
295+
- Documentation flow anchor: `.github/instructions/docs.instructions.md#documentation-process-flow`
296+
- Testing
297+
- BDD feature guidance (SSOT): `.github/instructions/bdd-tests.instructions.md`
298+
- Tester chat mode (enforces policy): `.github/chatmodes/Tester.chatmode.md`
299+
- Backend
300+
- Backend instructions (SSOT): `.github/instructions/backend.instructions.md`
301+
- Architecture: `.github/instructions/backend.instructions.md#backend-architecture`
302+
- Error handling: `.github/instructions/backend.instructions.md#backend-error-handling`
303+
- Observability: `.github/instructions/backend.instructions.md#backend-observability`
304+
- Security: `.github/instructions/backend.instructions.md#backend-security`
305+
- Planning
306+
- Plan template (SSOT): `plans/plan-template.md`
307+
- Small plan example: `plans/examples/plan-small.md`
308+
- TODO (work queue): `plans/TODO.md`
309+
310+
Notes:
311+
- Chat modes and prompts should reference these SSOT files. Avoid duplicating numeric thresholds, templates, or process steps in multiple places.
312+
- CI tasks (if added) should validate adherence to SSOT anchors where practical.

scripts/enforce-coverage.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env node
2+
/*
3+
Simple coverage enforcement script.
4+
- Reads a JSON summary from stdin or a file (istanbul/nyc, jest --coverage, etc.)
5+
- Enforces global >= 90%
6+
- Enforces core modules >= 95% (by path includes /src/core or /core/)
7+
- Enforces integrations/adapters >= 85% (by path includes /adapters or /integrations/)
8+
- Enforces 100% for files matched as hot paths or error/security (by filename hints)
9+
Exit non-zero on failure with a readable summary.
10+
*/
11+
12+
const fs = require('fs');
13+
14+
function parseArgs() {
15+
const args = process.argv.slice(2);
16+
const params = { file: null };
17+
for (let i = 0; i < args.length; i++) {
18+
if (args[i] === '--file' && args[i + 1]) {
19+
params.file = args[i + 1];
20+
i++;
21+
}
22+
}
23+
return params;
24+
}
25+
26+
function loadSummary(file) {
27+
const input = file ? fs.readFileSync(file, 'utf8') : fs.readFileSync(0, 'utf8');
28+
return JSON.parse(input);
29+
}
30+
31+
function pct(n) { return Math.round(n * 10000) / 100; }
32+
33+
function classifyFile(path) {
34+
const p = path.toLowerCase();
35+
if (p.includes('/core/') || p.includes('/src/core/')) return 'core';
36+
if (p.includes('/adapters/') || p.includes('/integrations/')) return 'integration';
37+
return 'other';
38+
}
39+
40+
function isCritical(path) {
41+
const p = path.toLowerCase();
42+
return (
43+
p.includes('hot') ||
44+
p.includes('critical') ||
45+
p.includes('auth') ||
46+
p.includes('security') ||
47+
p.includes('error') ||
48+
p.includes('exception')
49+
);
50+
}
51+
52+
function check(summary) {
53+
const metrics = summary.total || summary;
54+
const globalLines = metrics.lines.pct || metrics.lines.covered / metrics.lines.total * 100;
55+
const globalPass = globalLines >= 90;
56+
57+
const failures = [];
58+
if (!globalPass) failures.push(`Global lines coverage ${pct(globalLines)}% < 90%`);
59+
60+
// Per-file checks if available
61+
if (summary && typeof summary === 'object') {
62+
for (const [file, m] of Object.entries(summary)) {
63+
if (file === 'total' || !m || !m.lines) continue;
64+
const filePct = m.lines.pct || (m.lines.covered / m.lines.total * 100);
65+
const cls = classifyFile(file);
66+
if (isCritical(file) && filePct < 100) {
67+
failures.push(`Critical path not fully covered: ${file} ${pct(filePct)}% < 100%`);
68+
continue;
69+
}
70+
if (cls === 'core' && filePct < 95) {
71+
failures.push(`Core module below 95%: ${file} ${pct(filePct)}%`);
72+
} else if (cls === 'integration' && filePct < 85) {
73+
failures.push(`Integration below 85%: ${file} ${pct(filePct)}%`);
74+
}
75+
}
76+
}
77+
78+
return failures;
79+
}
80+
81+
function main() {
82+
try {
83+
const { file } = parseArgs();
84+
const summary = loadSummary(file);
85+
const failures = check(summary);
86+
if (failures.length) {
87+
console.error('Coverage enforcement failed:\n- ' + failures.join('\n- '));
88+
process.exit(1);
89+
}
90+
console.log('Coverage enforcement passed.');
91+
} catch (e) {
92+
console.error('Error running coverage enforcement:', e.message);
93+
process.exit(2);
94+
}
95+
}
96+
97+
if (require.main === module) {
98+
main();
99+
}

0 commit comments

Comments
 (0)