Skip to content

Commit f2840dc

Browse files
custom formatter
1 parent 61befe6 commit f2840dc

File tree

6 files changed

+186
-4
lines changed

6 files changed

+186
-4
lines changed

package-lock.json

Lines changed: 109 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "mongodb-openapi",
33
"description": "MongoDB repository with OpenAPI specification",
4+
"type": "module",
45
"scripts": {
56
"format": "npx prettier . --write",
67
"format-check": "npx prettier . --check",
@@ -9,7 +10,11 @@
910
},
1011
"dependencies": {
1112
"@stoplight/spectral-cli": "^6.14.2",
12-
"openapi-to-postmanv2": "4.24.0"
13+
"@stoplight/spectral-core": "^1.19.4",
14+
"@stoplight/spectral-ruleset-bundler": "^1.6.1",
15+
"@stoplight/spectral-runtime": "^1.1.3",
16+
"openapi-to-postmanv2": "4.24.0",
17+
"xmlbuilder2": "^3.1.1"
1318
},
1419
"devDependencies": {
1520
"@eslint/js": "^9.15.0",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { create } from 'xmlbuilder2';
2+
3+
4+
export default async function customJUnitFormatter(results, document, spectral) {
5+
const allRules = spectral.ruleset.rules;
6+
const failedRules = new Set(results.map((result) => result.code));
7+
8+
// Create a new XML document
9+
const xml = create({ version: '1.0' }) // Specify XML version
10+
.ele('testsuite', {
11+
name: 'SpectralLint',
12+
tests: Object.keys(allRules).length,
13+
});
14+
15+
// Add test cases for each rule
16+
for (const ruleName of Object.keys(allRules)) {
17+
const rule = allRules[ruleName];
18+
const testCase = xml.ele('testcase', {
19+
classname: ruleName,
20+
name: rule.description || 'No description available',
21+
time: '0', // Default time
22+
});
23+
24+
if (failedRules.has(ruleName)) {
25+
const failure = results.find((result) => result.code === ruleName);
26+
testCase.ele('failure', { type: ruleName }, failure.message);
27+
} else {
28+
testCase.ele('success'); // Mark as success for passed rules
29+
}
30+
}
31+
32+
// Convert the XML structure to a string
33+
return xml.end({ prettyPrint: true });
34+
}

tools/ipa/ipa-spectral.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
extends:
2-
- ./rulesets/IPA-104.yaml
2+
- ./rulesets/IPA-104.yaml

tools/ipa/rulesets/IPA-104.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
functions:
55
- "eachResourceHasGetMethod"
66

7-
87
rules:
98
xgen-IPA-104-resource-has-GET:
109
description: "APIs must provide a get method for resources. http://go/ipa/104"

tools/ipa/run-spectral.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import spectral from "@stoplight/spectral-core";
2+
import { bundleAndLoadRuleset } from "@stoplight/spectral-ruleset-bundler/with-loader";
3+
import { fileURLToPath } from "node:url";
4+
import * as fs from "node:fs";
5+
import * as path from "node:path";
6+
import customJUnitFormatter from "./formatters/custom-formatter.js";
7+
const { Spectral } = spectral;
8+
9+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
10+
11+
(async () => {
12+
const spectral = new Spectral();
13+
14+
try {
15+
// Load the ruleset
16+
const rulesetPath = path.join(__dirname, "./ipa-spectral.yaml");
17+
const ruleset = await bundleAndLoadRuleset(rulesetPath, { fs, fetch });
18+
await spectral.setRuleset(ruleset);
19+
20+
// Read the OpenAPI file and run Spectral
21+
const { readFile, writeFile } = fs.promises;
22+
const openApiContent = await readFile('../../openapi/v2.json', 'utf8');
23+
const results = await spectral.run(openApiContent);
24+
25+
// Use the custom formatter
26+
const formattedOutput = await customJUnitFormatter(results, '../../openapi/v2.json', spectral);
27+
28+
// Write the output to a file
29+
console.log(formattedOutput);
30+
await writeFile('./spectral-report.xml', formattedOutput);
31+
32+
} catch (err) {
33+
console.error('Error running Spectral:', err.message);
34+
process.exit(1);
35+
}
36+
})();

0 commit comments

Comments
 (0)