Skip to content

Commit 5386c00

Browse files
fix
1 parent 8f33f03 commit 5386c00

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@
2222
*.out
2323
**/*ipa-collector-results-combined.log
2424
**/*metric-collection-results.json
25+
**/*spectral-output.txt
26+
**/*spectral-report.xml

tools/spectral/ipa/metrics/config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import path from 'path';
22
import { fileURLToPath } from 'url';
33

44
const dirname = path.dirname(fileURLToPath(import.meta.url));
5+
const outputDir = path.join(dirname, 'outputs');
56
const rootDir = path.resolve(dirname, '../../../../');
67

78
const config = {
89
defaultOasFilePath: path.join(rootDir, 'openapi', 'v2.json'),
9-
defaultCollectorResultsFilePath: path.join(dirname, 'ipa-collector-results-combined.log'),
1010
defaultRulesetFilePath: path.join(dirname, '..', 'ipa-spectral.yaml'),
11-
defaultMetricCollectionResultsFilePath: path.join(dirname, 'metric-collection-results.json'),
11+
defaultCollectorResultsFilePath: path.join(dirname, 'ipa-collector-results-combined.log'),
12+
defaultMetricCollectionResultsFilePath: path.join(outputDir, 'metric-collection-results.json'),
13+
defaultSpectralReportFile: path.join(outputDir, 'spectral-report.xml'),
14+
defaultSpectralOutputFile: path.join(outputDir, 'spectral-output.txt'),
1215
};
1316

1417
export default config;

tools/spectral/ipa/metrics/metricCollection.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import spectral from '@stoplight/spectral-core';
22
import * as fs from 'node:fs';
3+
import { spawnSync } from 'child_process';
34
import {
45
loadOpenAPIFile,
56
extractTeamOwnership,
@@ -9,7 +10,6 @@ import {
910
merge,
1011
} from './utils.js';
1112
import config from './config.js';
12-
import collector from './collector.js';
1313
const { Spectral } = spectral;
1414

1515
async function runMetricCollectionJob(oasFilePath = config.defaultOasFilePath) {
@@ -20,22 +20,16 @@ async function runMetricCollectionJob(oasFilePath = config.defaultOasFilePath) {
2020
console.log('Extracting team ownership data...');
2121
const ownershipData = extractTeamOwnership(oasContent);
2222

23-
console.log('Initializing Spectral...');
23+
console.log('Getting rule severities...');
2424
const spectral = new Spectral();
2525
const ruleset = await loadRuleset(config.defaultRulesetFilePath, spectral);
26-
27-
console.log('Getting rule severities...');
2826
const ruleSeverityMap = getSeverityPerRule(ruleset);
2927

30-
console.log('Running Spectral analysis...');
31-
const spectralResults = await spectral.run(oasContent);
32-
collector.flushToFile();
33-
3428
console.log('Loading collector results...');
3529
const collectorResults = loadCollectorResults(config.defaultCollectorResultsFilePath);
3630

3731
console.log('Merging results...');
38-
const mergedResults = merge(spectralResults, ownershipData, collectorResults, ruleSeverityMap);
32+
const mergedResults = merge(ownershipData, collectorResults, ruleSeverityMap);
3933

4034
console.log('Metric collection job complete.');
4135
return mergedResults;
@@ -48,6 +42,34 @@ async function runMetricCollectionJob(oasFilePath = config.defaultOasFilePath) {
4842
const args = process.argv.slice(2);
4943
const customOasFile = args[0];
5044

45+
const result = spawnSync(
46+
'spectral',
47+
[
48+
'lint',
49+
'--ruleset',
50+
config.defaultRulesetFilePath,
51+
'--format',
52+
'stylish',
53+
'--verbose',
54+
'--format',
55+
'junit',
56+
'--output.junit',
57+
config.defaultSpectralReportFile,
58+
config.defaultOasFilePath,
59+
],
60+
{
61+
encoding: 'utf-8',
62+
}
63+
);
64+
65+
if (result.error) {
66+
console.error('Error running Spectral lint:', result.error);
67+
process.exit(1);
68+
}
69+
70+
console.log('Spectral lint completed successfully.');
71+
fs.writeFileSync(config.defaultSpectralOutputFile, result.stdout);
72+
5173
runMetricCollectionJob(customOasFile)
5274
.then((results) => fs.writeFileSync(config.defaultMetricCollectionResultsFilePath, JSON.stringify(results)))
5375
.catch((error) => console.error(error.message));

tools/spectral/ipa/metrics/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function getIPAFromIPARule(ipaRule) {
7373
}
7474
}
7575

76-
export function merge(spectralResults, ownershipData, collectorResults, ruleSeverityMap) {
76+
export function merge(ownershipData, collectorResults, ruleSeverityMap) {
7777
const results = [];
7878

7979
function addEntry(entryType, adoptionStatus) {

0 commit comments

Comments
 (0)