-
-
Notifications
You must be signed in to change notification settings - Fork 5
107 lines (93 loc) · 3.42 KB
/
Copy pathtest.yml
File metadata and controls
107 lines (93 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
name: Tests
on:
pull_request:
workflow_dispatch:
permissions:
contents: read
pull-requests: write
# required by actions/upload-code-coverage (GitHub code-coverage merge protection)
code-quality: write
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- name: Install dependencies
run: npm ci
- name: Type-check
run: npm run check
- name: Unit tests (100% coverage enforced)
run: npm run test:coverage
- name: Upload coverage report
if: always()
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: coverage/
retention-days: 14
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
- name: Comment coverage table on PR
if: always() && github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const path = 'coverage/coverage-summary.json';
if (!fs.existsSync(path)) {
core.info('No coverage summary found — skipping PR comment.');
return;
}
const summary = JSON.parse(fs.readFileSync(path, 'utf8'));
const prefix = `${process.cwd()}/`;
const rows = Object.entries(summary)
.filter(([file]) => file !== 'total')
.map(([file, m]) => [
file.replace(prefix, ''),
`${m.statements.pct}`,
`${m.branches.pct}`,
`${m.functions.pct}`,
`${m.lines.pct}`
])
.sort((a, b) => a[0].localeCompare(b[0]));
const t = summary.total;
rows.push(['TOTAL', `${t.statements.pct}`, `${t.branches.pct}`, `${t.functions.pct}`, `${t.lines.pct}`]);
const headers = ['File', 'Stmts %', 'Branch %', 'Funcs %', 'Lines %'];
const all = [headers, ...rows];
const widths = headers.map((_, i) => Math.max(...all.map((r) => r[i].length)));
const fmt = (r) =>
r.map((c, i) => (i === 0 ? c.padEnd(widths[i]) : c.padStart(widths[i]))).join(' ');
const sep = widths.map((w) => '-'.repeat(w)).join(' ');
const marker = '<!-- discussion-kit-coverage -->';
const body = [
marker,
'### 🧪 Test coverage',
'',
'```',
fmt(headers),
sep,
...rows.map(fmt),
'```'
].join('\n');
const { owner, repo } = context.repo;
const issue_number = context.payload.pull_request.number;
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number,
per_page: 100
});
const existing = comments.find((c) => c.body && c.body.includes(marker));
if (existing) {
await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body });
} else {
await github.rest.issues.createComment({ owner, repo, issue_number, body });
}