-
Notifications
You must be signed in to change notification settings - Fork 0
188 lines (161 loc) · 6.29 KB
/
code-quality.yml
File metadata and controls
188 lines (161 loc) · 6.29 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# ============================================================================
# Code Quality Workflow
# ============================================================================
# This workflow enforces code quality standards on every pull request:
# - Lint: runs ESLint to catch style and correctness issues.
# - Type Check: runs the TypeScript compiler in check-only mode.
# - Test & Coverage: runs Jest tests and compares line coverage against the
# configured threshold. Low-coverage files are reported as SARIF findings.
#
# The coverage SARIF output appears in the GitHub Security tab under the
# "code-quality/coverage/" category.
# ============================================================================
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: Code Quality
on:
workflow_dispatch:
pull_request:
branches: [main]
permissions:
security-events: write
contents: read
env:
COVERAGE_THRESHOLD: 80
jobs:
lint:
name: Lint
runs-on: ubuntu-latest
defaults:
run:
working-directory: sample-app
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
- name: Install dependencies
run: npm install
- name: Run linter
run: npm run lint
type-check:
name: Type Check
runs-on: ubuntu-latest
defaults:
run:
working-directory: sample-app
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
- name: Install dependencies
run: npm install
- name: Run type check
run: npx tsc --noEmit
test:
name: Test & Coverage
runs-on: ubuntu-latest
defaults:
run:
working-directory: sample-app
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
- name: Install dependencies
run: npm install
- name: Run tests with coverage
run: npm run test:ci
- name: Coverage threshold check
id: coverage
run: |
COVERAGE_FILE="coverage/coverage-summary.json"
if [ ! -f "$COVERAGE_FILE" ]; then
echo "::warning::Coverage summary not found at $COVERAGE_FILE"
exit 0
fi
TOTAL_PCT=$(jq '.total.lines.pct' "$COVERAGE_FILE")
echo "coverage=$TOTAL_PCT" >> "$GITHUB_OUTPUT"
echo "Line coverage: ${TOTAL_PCT}%"
if [ "$(echo "$TOTAL_PCT < ${{ env.COVERAGE_THRESHOLD }}" | bc -l)" -eq 1 ]; then
echo "::error::Line coverage ${TOTAL_PCT}% is below threshold ${{ env.COVERAGE_THRESHOLD }}%"
exit 1
fi
- name: Convert coverage to SARIF
if: always()
run: |
COVERAGE_FILE="coverage/coverage-summary.json"
SARIF_FILE="coverage-results.sarif"
cat > convert-coverage.js << 'SCRIPT'
const fs = require('fs');
const coverageFile = process.argv[2];
const threshold = parseInt(process.argv[3], 10);
const sarifFile = process.argv[4];
if (!fs.existsSync(coverageFile)) {
console.log('No coverage file found, skipping SARIF conversion');
process.exit(0);
}
const coverage = JSON.parse(fs.readFileSync(coverageFile, 'utf8'));
const results = [];
const rules = [
{
id: 'coverage-threshold-violation',
shortDescription: { text: 'File coverage below threshold' },
fullDescription: { text: `Line coverage is below the ${threshold}% threshold` },
help: { text: `Increase test coverage to at least ${threshold}%`, markdown: `Increase test coverage to at least **${threshold}%** by adding unit tests for uncovered code paths.` },
defaultConfiguration: { level: 'warning' },
properties: { tags: ['code-quality', 'coverage'] }
},
{
id: 'uncovered-function',
shortDescription: { text: 'Function has zero coverage' },
fullDescription: { text: 'Function is not covered by any test' },
help: { text: 'Add tests for this function', markdown: 'Add **unit tests** covering happy path and error cases for this function.' },
defaultConfiguration: { level: 'warning' },
properties: { tags: ['code-quality', 'coverage'] }
}
];
for (const [filePath, data] of Object.entries(coverage)) {
if (filePath === 'total') continue;
if (data.lines && data.lines.pct < threshold) {
results.push({
ruleId: 'coverage-threshold-violation',
level: 'warning',
message: { text: `Line coverage is ${data.lines.pct}% (threshold: ${threshold}%)` },
locations: [{
physicalLocation: {
artifactLocation: { uri: filePath.replace(/^\//, '') },
region: { startLine: 1 }
}
}],
partialFingerprints: { primaryLocationLineHash: filePath }
});
}
}
const sarif = {
'$schema': 'https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json',
version: '2.1.0',
runs: [{
tool: { driver: { name: 'coverage-analyzer', rules } },
results,
automationDetails: { id: 'code-quality/coverage/' }
}]
};
fs.writeFileSync(sarifFile, JSON.stringify(sarif, null, 2));
console.log(`Wrote ${results.length} findings to ${sarifFile}`);
SCRIPT
node convert-coverage.js "$COVERAGE_FILE" "${{ env.COVERAGE_THRESHOLD }}" "$SARIF_FILE"
- name: Upload coverage SARIF
uses: github/codeql-action/upload-sarif@v4
if: always() && hashFiles('coverage-results.sarif') != ''
continue-on-error: true
with:
sarif_file: coverage-results.sarif
category: code-quality/coverage/