Skip to content

Commit e941698

Browse files
committed
task: filter violations to add to the report
1 parent b138901 commit e941698

File tree

2 files changed

+90
-1
lines changed

2 files changed

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

0 commit comments

Comments
 (0)