Skip to content

Commit 8771dfc

Browse files
authored
task: filter violations to add to the report (#538)
1 parent 4f349b6 commit 8771dfc

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"format-check": "npx prettier . --check",
88
"lint-js": "npx eslint **/*.js",
99
"gen-ipa-docs": "node tools/spectral/ipa/scripts/generateRulesetReadme.js",
10-
"ipa-validation": "spectral lint ./openapi/.raw/v2.yaml --ruleset=./tools/spectral/ipa/ipa-spectral.yaml",
10+
"ipa-validation": "spectral lint ./openapi/.raw/v2.yaml --ruleset=./tools/spectral/ipa/ipa-spectral.yaml",
11+
"ipa-filter-violations": "node tools/spectral/ipa/scripts/filter-ipa-violations.js",
1112
"spectral-validation": "spectral lint ./openapi/.raw/v2.yaml --ruleset=./tools/spectral/.spectral.yaml",
1213
"test": "jest",
1314
"precommit": "husky install"
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import fs from 'node:fs/promises';
2+
import { execSync } from 'child_process';
3+
import path from 'path';
4+
5+
async function filterIpaViolations() {
6+
try {
7+
// Check if rule ID is provided
8+
const ruleId = process.argv[2];
9+
if (!ruleId) {
10+
console.error('Usage: node filter-ipa-violations.js <rule-id>');
11+
console.error('Example: node filter-ipa-violations.js xgen-IPA-102-collection-identifier-camelCase');
12+
process.exit(1);
13+
}
14+
15+
const outputFile = path.join(process.cwd(), `${ruleId}-violations.md`);
16+
17+
console.log(`Filtering violations for rule ID: ${ruleId}`);
18+
console.log('Running IPA validation...');
19+
20+
let validationOutput;
21+
try {
22+
// Run IPA validation and get output as JSON
23+
execSync(
24+
'spectral lint --format=json -o results.json ./openapi/.raw/v2.yaml --ruleset=./tools/spectral/ipa/ipa-spectral.yaml',
25+
{
26+
encoding: 'utf-8',
27+
timeout: 4000,
28+
maxBuffer: 10 * 1024 * 1024,
29+
}
30+
);
31+
} catch (error) {
32+
console.error('Error (expected):', error.message);
33+
}
34+
35+
// Read the JSON output
36+
validationOutput = await fs.readFile('results.json', 'utf-8');
37+
console.log('Filtering results...');
38+
39+
// Parse the JSON output
40+
const validationResults = JSON.parse(validationOutput);
41+
42+
// Filter results for the exact specified rule ID
43+
const filteredResults = validationResults.filter((violation) => violation.code === ruleId);
44+
45+
// Group by source file
46+
const groupedBySource = filteredResults.reduce((acc, violation) => {
47+
const source = violation.source;
48+
if (!acc[source]) {
49+
acc[source] = [];
50+
}
51+
acc[source].push({
52+
path: violation.path,
53+
message: violation.message,
54+
source: violation.source,
55+
});
56+
return acc;
57+
}, {});
58+
59+
// Generate markdown content
60+
let markdownContent = `# ${ruleId} Violations Checklist\n\n`;
61+
markdownContent += `Generated on: ${new Date().toLocaleString()}\n\n`;
62+
63+
Object.keys(groupedBySource).forEach((source) => {
64+
const violations = groupedBySource[source];
65+
66+
violations.forEach((violation) => {
67+
markdownContent += `## ${violation.source}\n\n`;
68+
markdownContent += `Path: \`${violation.path.join('/')}\`\n\n`;
69+
markdownContent += `- [ ] Fixed\n\n`;
70+
});
71+
});
72+
73+
// Write the markdown file
74+
await fs.writeFile(outputFile, markdownContent, 'utf-8');
75+
76+
const violationCount = filteredResults.length;
77+
console.log(`Results saved to ${outputFile}`);
78+
console.log(`Found ${violationCount} violations to fix`);
79+
} catch (error) {
80+
console.error('Error:', error.message);
81+
process.exit(1);
82+
}
83+
}
84+
85+
filterIpaViolations();

0 commit comments

Comments
 (0)