From b3d0b3daab855e1360b994ca6c7c5a446c04b973 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Tue, 14 Jan 2025 17:53:10 +0000 Subject: [PATCH 01/10] CLOUDP-294461: [Product Metrics/Observability] Implement Metric Collection workflow --- package.json | 1 + .../spectral/ipa/metrics/metricCollection.js | 50 ++++++++ tools/spectral/ipa/metrics/utils.js | 110 ++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 tools/spectral/ipa/metrics/metricCollection.js create mode 100644 tools/spectral/ipa/metrics/utils.js diff --git a/package.json b/package.json index fd649f3d47..40e7797f57 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "name": "mongodb-openapi", "description": "MongoDB repository with OpenAPI specification", + "type": "module", "scripts": { "format": "npx prettier . --write", "format-check": "npx prettier . --check", diff --git a/tools/spectral/ipa/metrics/metricCollection.js b/tools/spectral/ipa/metrics/metricCollection.js new file mode 100644 index 0000000000..9777be527a --- /dev/null +++ b/tools/spectral/ipa/metrics/metricCollection.js @@ -0,0 +1,50 @@ +import spectral from '@stoplight/spectral-core'; +import { fileURLToPath } from 'node:url'; +import * as fs from 'node:fs'; +import * as path from 'node:path'; +import { loadOpenAPIFile, extractTeamOwnership, loadRuleset, loadCollectorResults, getSeverityPerRule, merge } from './utils.js'; +const { Spectral } = spectral; + +//TBD +const oasFile = '../../../../openapi/v2.json'; +const dirname = path.dirname(fileURLToPath(import.meta.url)); +const collectorResultsFile = path.join(dirname, '../ipa-collector-results-combined.log'); + + +async function runMetricCollectionJob() { + try { + console.log('Loading OpenAPI file...'); + const oasContent = loadOpenAPIFile(oasFile); + + console.log('Extracting team ownership data...'); + const ownershipData = extractTeamOwnership(oasContent); + + console.log('Initializing Spectral...'); + const spectral = new Spectral(); + const rulesetPath = path.join(dirname, '../ipa-spectral.yaml'); + const ruleset = await loadRuleset(rulesetPath, spectral); + + console.log('Getting rule severities...'); + const ruleSeverityMap = getSeverityPerRule(ruleset); + + console.log('Running Spectral analysis...'); + const spectralResults = await spectral.run(oasContent); + + console.log('Loading collector results...'); + const collectorResults = loadCollectorResults(collectorResultsFile); + + console.log('Merging results...'); + const mergedResults = merge(spectralResults, ownershipData, collectorResults, ruleSeverityMap); + + console.log('Metric collection job complete.'); + return mergedResults; + } catch (error) { + console.error('Error during metric collection:', error.message); + throw error; + } +} + + +runMetricCollectionJob() + .then((results) => fs.writeFileSync('results.log', JSON.stringify(results))) + .catch((error) => console.error(error.message)); diff --git a/tools/spectral/ipa/metrics/utils.js b/tools/spectral/ipa/metrics/utils.js new file mode 100644 index 0000000000..ef46285ccc --- /dev/null +++ b/tools/spectral/ipa/metrics/utils.js @@ -0,0 +1,110 @@ +import fs from 'node:fs'; +import { + bundleAndLoadRuleset +} from '@stoplight/spectral-ruleset-bundler/with-loader'; +import { EntryType } from './collector.js'; + +export function loadOpenAPIFile(filePath) { + try { + const content = fs.readFileSync(filePath, 'utf8'); + return JSON.parse(content); + } catch (error) { + throw new Error(`Failed to load OpenAPI file: ${error.message}`); + } +} + +export function getSeverityPerRule(ruleset) { + const rules = ruleset.rules || {}; + const map = {}; + for (const [name, ruleObject] of Object.entries(rules)) { + map[name] = ruleObject.definition.severity; + } + return map; +} + +export async function loadRuleset(rulesetPath, spectral) { + try { + const ruleset = await bundleAndLoadRuleset(rulesetPath, { fs, fetch }); + await spectral.setRuleset(ruleset); + return ruleset; + } catch (error) { + throw new Error(`Failed to load ruleset: ${error.message}`); + } +} + +export function extractTeamOwnership(oasContent) { + const ownerTeams = {}; + const paths = oasContent.paths || {}; + + for (const [path, pathItem] of Object.entries(paths)) { + for (const [, operation] of Object.entries(pathItem)) { + const ownerTeam = operation['x-xgen-owner-team']; + + if (ownerTeam) { + if (!ownerTeams[path]) { + ownerTeams[path] = ownerTeam; + } else if (ownerTeams[path] !== ownerTeam) { + console.warn(`Conflict on path ${path}: ${ownerTeams[path]} vs ${ownerTeam}`); + } + } + } + } + + return ownerTeams; +} + +export function loadCollectorResults(collectorResultsFilePath) { + try { + const content = fs.readFileSync(collectorResultsFilePath, 'utf8'); + const contentParsed = JSON.parse(content); + return { + [EntryType.VIOLATION]: contentParsed[EntryType.VIOLATION], + [EntryType.ADOPTION]: contentParsed[EntryType.ADOPTION], + [EntryType.EXCEPTION]: contentParsed[EntryType.EXCEPTION], + }; + } catch (error) { + throw new Error(`Failed to load Collector Results file: ${error.message}`); + } +} + +function getIPAFromIPARule(ipaRule) { + const pattern = /IPA-\d{3}/; + const match = ipaRule.match(pattern); + if (match) { + return match[0]; + } +} + +export function merge(spectralResults, ownershipData, collectorResults, ruleSeverityMap) { + const results = []; + + function addEntry(entryType, adoptionStatus) { + for (const entry of collectorResults[entryType]) { + const existing = results.find((result) => + result.component_id === entry.componentId && result.ipa_rule === entry.ruleName + ); + + if (existing) { + console.warn('Duplicate entries found', existing); + continue; + } + + results.push({ + component_id: entry.componentId, + ipa_rule: entry.ruleName, + ipa: getIPAFromIPARule(entry.ruleName), + severity_level: ruleSeverityMap[entry.ruleName], + adoption_status: adoptionStatus, + exception_reason: entryType === EntryType.EXCEPTION ? entry.exceptionReason : null, + owner_team: entry.ownerTeam || null, + timestamp: new Date().toISOString(), + }); + } + } + + addEntry(EntryType.VIOLATION, 'violated'); + addEntry(EntryType.ADOPTION, 'adopted'); + addEntry(EntryType.EXCEPTION, 'exempted'); + + return results; +} \ No newline at end of file From dc56c6a3555f0ee50d56f295b7fc47c28563a4ba Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Tue, 14 Jan 2025 17:58:12 +0000 Subject: [PATCH 02/10] prettier fix --- tools/spectral/ipa/metrics/metricCollection.js | 11 ++++++++--- tools/spectral/ipa/metrics/utils.js | 10 ++++------ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/tools/spectral/ipa/metrics/metricCollection.js b/tools/spectral/ipa/metrics/metricCollection.js index 9777be527a..8c83b61e29 100644 --- a/tools/spectral/ipa/metrics/metricCollection.js +++ b/tools/spectral/ipa/metrics/metricCollection.js @@ -2,7 +2,14 @@ import spectral from '@stoplight/spectral-core'; import { fileURLToPath } from 'node:url'; import * as fs from 'node:fs'; import * as path from 'node:path'; -import { loadOpenAPIFile, extractTeamOwnership, loadRuleset, loadCollectorResults, getSeverityPerRule, merge } from './utils.js'; +import { + loadOpenAPIFile, + extractTeamOwnership, + loadRuleset, + loadCollectorResults, + getSeverityPerRule, + merge, +} from './utils.js'; const { Spectral } = spectral; //TBD @@ -10,7 +17,6 @@ const oasFile = '../../../../openapi/v2.json'; const dirname = path.dirname(fileURLToPath(import.meta.url)); const collectorResultsFile = path.join(dirname, '../ipa-collector-results-combined.log'); - async function runMetricCollectionJob() { try { console.log('Loading OpenAPI file...'); @@ -44,7 +50,6 @@ async function runMetricCollectionJob() { } } - runMetricCollectionJob() .then((results) => fs.writeFileSync('results.log', JSON.stringify(results))) .catch((error) => console.error(error.message)); diff --git a/tools/spectral/ipa/metrics/utils.js b/tools/spectral/ipa/metrics/utils.js index ef46285ccc..6321fbd7d3 100644 --- a/tools/spectral/ipa/metrics/utils.js +++ b/tools/spectral/ipa/metrics/utils.js @@ -1,7 +1,5 @@ import fs from 'node:fs'; -import { - bundleAndLoadRuleset -} from '@stoplight/spectral-ruleset-bundler/with-loader'; +import { bundleAndLoadRuleset } from '@stoplight/spectral-ruleset-bundler/with-loader'; import { EntryType } from './collector.js'; export function loadOpenAPIFile(filePath) { @@ -80,8 +78,8 @@ export function merge(spectralResults, ownershipData, collectorResults, ruleSeve function addEntry(entryType, adoptionStatus) { for (const entry of collectorResults[entryType]) { - const existing = results.find((result) => - result.component_id === entry.componentId && result.ipa_rule === entry.ruleName + const existing = results.find( + (result) => result.component_id === entry.componentId && result.ipa_rule === entry.ruleName ); if (existing) { @@ -107,4 +105,4 @@ export function merge(spectralResults, ownershipData, collectorResults, ruleSeve addEntry(EntryType.EXCEPTION, 'exempted'); return results; -} \ No newline at end of file +} From f52313cbee1521e886002b3778787f96164af851 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 15 Jan 2025 12:24:50 +0000 Subject: [PATCH 03/10] fix: config in a separate file --- .gitignore | 1 + tools/spectral/ipa/metrics/config.js | 14 +++++++++++ .../spectral/ipa/metrics/metricCollection.js | 24 +++++++++---------- 3 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 tools/spectral/ipa/metrics/config.js diff --git a/.gitignore b/.gitignore index 87dae3c541..cc9ee88ab2 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ *.out **/*ipa-collector-results-combined.log +**/*metric-collection-results.json diff --git a/tools/spectral/ipa/metrics/config.js b/tools/spectral/ipa/metrics/config.js new file mode 100644 index 0000000000..75f52f20bc --- /dev/null +++ b/tools/spectral/ipa/metrics/config.js @@ -0,0 +1,14 @@ +import path from 'path'; +import { fileURLToPath } from 'url'; + +const dirname = path.dirname(fileURLToPath(import.meta.url)); +const rootDir = path.resolve(dirname, '../../../../'); + +const config = { + defaultOasFilePath: path.join(rootDir, 'openapi', 'v2.json'), + defaultCollectorResultsFilePath: path.join(dirname, '..', 'ipa-collector-results-combined.log'), + defaultRulesetFilePath: path.join(dirname, '..', 'ipa-spectral.yaml'), + defaultMetricCollectionResultsFilePath: path.join(dirname, 'metric-collection-results.json'), +}; + +export default config; diff --git a/tools/spectral/ipa/metrics/metricCollection.js b/tools/spectral/ipa/metrics/metricCollection.js index 8c83b61e29..632a4179b9 100644 --- a/tools/spectral/ipa/metrics/metricCollection.js +++ b/tools/spectral/ipa/metrics/metricCollection.js @@ -10,25 +10,20 @@ import { getSeverityPerRule, merge, } from './utils.js'; +import config from './config.js'; const { Spectral } = spectral; -//TBD -const oasFile = '../../../../openapi/v2.json'; -const dirname = path.dirname(fileURLToPath(import.meta.url)); -const collectorResultsFile = path.join(dirname, '../ipa-collector-results-combined.log'); - -async function runMetricCollectionJob() { +async function runMetricCollectionJob(oasFilePath = config.defaultOasFilePath) { try { - console.log('Loading OpenAPI file...'); - const oasContent = loadOpenAPIFile(oasFile); + console.log(`Loading OpenAPI file: ${oasFilePath}`); + const oasContent = loadOpenAPIFile(oasFilePath); console.log('Extracting team ownership data...'); const ownershipData = extractTeamOwnership(oasContent); console.log('Initializing Spectral...'); const spectral = new Spectral(); - const rulesetPath = path.join(dirname, '../ipa-spectral.yaml'); - const ruleset = await loadRuleset(rulesetPath, spectral); + const ruleset = await loadRuleset(config.defaultRulesetFilePath, spectral); console.log('Getting rule severities...'); const ruleSeverityMap = getSeverityPerRule(ruleset); @@ -37,7 +32,7 @@ async function runMetricCollectionJob() { const spectralResults = await spectral.run(oasContent); console.log('Loading collector results...'); - const collectorResults = loadCollectorResults(collectorResultsFile); + const collectorResults = loadCollectorResults(config.defaultCollectorResultsFilePath); console.log('Merging results...'); const mergedResults = merge(spectralResults, ownershipData, collectorResults, ruleSeverityMap); @@ -50,6 +45,9 @@ async function runMetricCollectionJob() { } } -runMetricCollectionJob() - .then((results) => fs.writeFileSync('results.log', JSON.stringify(results))) +const args = process.argv.slice(2); +const customOasFile = args[0]; + +runMetricCollectionJob(customOasFile) + .then((results) => fs.writeFileSync(config.defaultMetricCollectionResultsFilePath, JSON.stringify(results))) .catch((error) => console.error(error.message)); From caab2aeec725e58f3446d8d82ca3c34b2405df46 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 15 Jan 2025 12:31:27 +0000 Subject: [PATCH 04/10] fix:unused imports --- tools/spectral/ipa/metrics/metricCollection.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/spectral/ipa/metrics/metricCollection.js b/tools/spectral/ipa/metrics/metricCollection.js index 632a4179b9..984ed3c0a7 100644 --- a/tools/spectral/ipa/metrics/metricCollection.js +++ b/tools/spectral/ipa/metrics/metricCollection.js @@ -1,7 +1,5 @@ import spectral from '@stoplight/spectral-core'; -import { fileURLToPath } from 'node:url'; import * as fs from 'node:fs'; -import * as path from 'node:path'; import { loadOpenAPIFile, extractTeamOwnership, From d6aed8f3e816923572da409c0bffe9add16d52ed Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 15 Jan 2025 12:47:46 +0000 Subject: [PATCH 05/10] fix:file path --- tools/spectral/ipa/metrics/config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/spectral/ipa/metrics/config.js b/tools/spectral/ipa/metrics/config.js index 75f52f20bc..59f991d5ed 100644 --- a/tools/spectral/ipa/metrics/config.js +++ b/tools/spectral/ipa/metrics/config.js @@ -6,7 +6,7 @@ const rootDir = path.resolve(dirname, '../../../../'); const config = { defaultOasFilePath: path.join(rootDir, 'openapi', 'v2.json'), - defaultCollectorResultsFilePath: path.join(dirname, '..', 'ipa-collector-results-combined.log'), + defaultCollectorResultsFilePath: path.join(dirname, 'ipa-collector-results-combined.log'), defaultRulesetFilePath: path.join(dirname, '..', 'ipa-spectral.yaml'), defaultMetricCollectionResultsFilePath: path.join(dirname, 'metric-collection-results.json'), }; From 8f33f03c406db4222ffce0a2aa993d9a543a2f54 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 15 Jan 2025 14:35:13 +0000 Subject: [PATCH 06/10] fix: explicitly flush to file --- tools/spectral/ipa/metrics/metricCollection.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/spectral/ipa/metrics/metricCollection.js b/tools/spectral/ipa/metrics/metricCollection.js index 984ed3c0a7..ead964fef8 100644 --- a/tools/spectral/ipa/metrics/metricCollection.js +++ b/tools/spectral/ipa/metrics/metricCollection.js @@ -9,6 +9,7 @@ import { merge, } from './utils.js'; import config from './config.js'; +import collector from './collector.js'; const { Spectral } = spectral; async function runMetricCollectionJob(oasFilePath = config.defaultOasFilePath) { @@ -28,6 +29,7 @@ async function runMetricCollectionJob(oasFilePath = config.defaultOasFilePath) { console.log('Running Spectral analysis...'); const spectralResults = await spectral.run(oasContent); + collector.flushToFile(); console.log('Loading collector results...'); const collectorResults = loadCollectorResults(config.defaultCollectorResultsFilePath); From 5386c00639a679402c4a7fcfd492d5daa95090f4 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 15 Jan 2025 17:34:33 +0000 Subject: [PATCH 07/10] fix --- .gitignore | 2 + tools/spectral/ipa/metrics/config.js | 7 +++- .../spectral/ipa/metrics/metricCollection.js | 40 ++++++++++++++----- tools/spectral/ipa/metrics/utils.js | 2 +- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index cc9ee88ab2..9dce256624 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ *.out **/*ipa-collector-results-combined.log **/*metric-collection-results.json +**/*spectral-output.txt +**/*spectral-report.xml diff --git a/tools/spectral/ipa/metrics/config.js b/tools/spectral/ipa/metrics/config.js index 59f991d5ed..a046a582c4 100644 --- a/tools/spectral/ipa/metrics/config.js +++ b/tools/spectral/ipa/metrics/config.js @@ -2,13 +2,16 @@ import path from 'path'; import { fileURLToPath } from 'url'; const dirname = path.dirname(fileURLToPath(import.meta.url)); +const outputDir = path.join(dirname, 'outputs'); const rootDir = path.resolve(dirname, '../../../../'); const config = { defaultOasFilePath: path.join(rootDir, 'openapi', 'v2.json'), - defaultCollectorResultsFilePath: path.join(dirname, 'ipa-collector-results-combined.log'), defaultRulesetFilePath: path.join(dirname, '..', 'ipa-spectral.yaml'), - defaultMetricCollectionResultsFilePath: path.join(dirname, 'metric-collection-results.json'), + defaultCollectorResultsFilePath: path.join(dirname, 'ipa-collector-results-combined.log'), + defaultMetricCollectionResultsFilePath: path.join(outputDir, 'metric-collection-results.json'), + defaultSpectralReportFile: path.join(outputDir, 'spectral-report.xml'), + defaultSpectralOutputFile: path.join(outputDir, 'spectral-output.txt'), }; export default config; diff --git a/tools/spectral/ipa/metrics/metricCollection.js b/tools/spectral/ipa/metrics/metricCollection.js index ead964fef8..1a2e07a51b 100644 --- a/tools/spectral/ipa/metrics/metricCollection.js +++ b/tools/spectral/ipa/metrics/metricCollection.js @@ -1,5 +1,6 @@ import spectral from '@stoplight/spectral-core'; import * as fs from 'node:fs'; +import { spawnSync } from 'child_process'; import { loadOpenAPIFile, extractTeamOwnership, @@ -9,7 +10,6 @@ import { merge, } from './utils.js'; import config from './config.js'; -import collector from './collector.js'; const { Spectral } = spectral; async function runMetricCollectionJob(oasFilePath = config.defaultOasFilePath) { @@ -20,22 +20,16 @@ async function runMetricCollectionJob(oasFilePath = config.defaultOasFilePath) { console.log('Extracting team ownership data...'); const ownershipData = extractTeamOwnership(oasContent); - console.log('Initializing Spectral...'); + console.log('Getting rule severities...'); const spectral = new Spectral(); const ruleset = await loadRuleset(config.defaultRulesetFilePath, spectral); - - console.log('Getting rule severities...'); const ruleSeverityMap = getSeverityPerRule(ruleset); - console.log('Running Spectral analysis...'); - const spectralResults = await spectral.run(oasContent); - collector.flushToFile(); - console.log('Loading collector results...'); const collectorResults = loadCollectorResults(config.defaultCollectorResultsFilePath); console.log('Merging results...'); - const mergedResults = merge(spectralResults, ownershipData, collectorResults, ruleSeverityMap); + const mergedResults = merge(ownershipData, collectorResults, ruleSeverityMap); console.log('Metric collection job complete.'); return mergedResults; @@ -48,6 +42,34 @@ async function runMetricCollectionJob(oasFilePath = config.defaultOasFilePath) { const args = process.argv.slice(2); const customOasFile = args[0]; +const result = spawnSync( + 'spectral', + [ + 'lint', + '--ruleset', + config.defaultRulesetFilePath, + '--format', + 'stylish', + '--verbose', + '--format', + 'junit', + '--output.junit', + config.defaultSpectralReportFile, + config.defaultOasFilePath, + ], + { + encoding: 'utf-8', + } +); + +if (result.error) { + console.error('Error running Spectral lint:', result.error); + process.exit(1); +} + +console.log('Spectral lint completed successfully.'); +fs.writeFileSync(config.defaultSpectralOutputFile, result.stdout); + runMetricCollectionJob(customOasFile) .then((results) => fs.writeFileSync(config.defaultMetricCollectionResultsFilePath, JSON.stringify(results))) .catch((error) => console.error(error.message)); diff --git a/tools/spectral/ipa/metrics/utils.js b/tools/spectral/ipa/metrics/utils.js index 6321fbd7d3..053971b5e9 100644 --- a/tools/spectral/ipa/metrics/utils.js +++ b/tools/spectral/ipa/metrics/utils.js @@ -73,7 +73,7 @@ function getIPAFromIPARule(ipaRule) { } } -export function merge(spectralResults, ownershipData, collectorResults, ruleSeverityMap) { +export function merge(ownershipData, collectorResults, ruleSeverityMap) { const results = []; function addEntry(entryType, adoptionStatus) { From 0924673104417dd6ad1d280b21e4a3e20b34d250 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 15 Jan 2025 18:01:18 +0000 Subject: [PATCH 08/10] fix: owner_team --- tools/spectral/ipa/metrics/utils.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tools/spectral/ipa/metrics/utils.js b/tools/spectral/ipa/metrics/utils.js index 053971b5e9..167a5ad233 100644 --- a/tools/spectral/ipa/metrics/utils.js +++ b/tools/spectral/ipa/metrics/utils.js @@ -87,6 +87,15 @@ export function merge(ownershipData, collectorResults, ruleSeverityMap) { continue; } + let ownerTeam = null; + if (entry.componentId.startsWith('paths')) { + const pathParts = entry.componentId.split('.'); + if (pathParts.length === 2) { + const path = pathParts[1]; + ownerTeam = ownershipData[path]; + } + } + results.push({ component_id: entry.componentId, ipa_rule: entry.ruleName, @@ -94,7 +103,7 @@ export function merge(ownershipData, collectorResults, ruleSeverityMap) { severity_level: ruleSeverityMap[entry.ruleName], adoption_status: adoptionStatus, exception_reason: entryType === EntryType.EXCEPTION ? entry.exceptionReason : null, - owner_team: entry.ownerTeam || null, + owner_team: ownerTeam, timestamp: new Date().toISOString(), }); } From 7ed962f130f43bddaf59a26211b100c0ac6443d5 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Thu, 16 Jan 2025 06:59:19 +0000 Subject: [PATCH 09/10] fix: create outputs dir --- tools/spectral/ipa/metrics/config.js | 9 +++++---- tools/spectral/ipa/metrics/metricCollection.js | 5 +++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/tools/spectral/ipa/metrics/config.js b/tools/spectral/ipa/metrics/config.js index a046a582c4..6c2fbde0fe 100644 --- a/tools/spectral/ipa/metrics/config.js +++ b/tools/spectral/ipa/metrics/config.js @@ -2,16 +2,17 @@ import path from 'path'; import { fileURLToPath } from 'url'; const dirname = path.dirname(fileURLToPath(import.meta.url)); -const outputDir = path.join(dirname, 'outputs'); const rootDir = path.resolve(dirname, '../../../../'); const config = { defaultOasFilePath: path.join(rootDir, 'openapi', 'v2.json'), defaultRulesetFilePath: path.join(dirname, '..', 'ipa-spectral.yaml'), defaultCollectorResultsFilePath: path.join(dirname, 'ipa-collector-results-combined.log'), - defaultMetricCollectionResultsFilePath: path.join(outputDir, 'metric-collection-results.json'), - defaultSpectralReportFile: path.join(outputDir, 'spectral-report.xml'), - defaultSpectralOutputFile: path.join(outputDir, 'spectral-output.txt'), + defaultOutputsDir: path.join(dirname, 'outputs'), }; +config.defaultMetricCollectionResultsFilePath = path.join(config.defaultOutputsDir, 'metric-collection-results.json'); +config.defaultSpectralReportFile = path.join(config.defaultOutputsDir, 'spectral-report.xml'); +config.defaultSpectralOutputFile = path.join(config.defaultOutputsDir, 'spectral-output.txt'); + export default config; diff --git a/tools/spectral/ipa/metrics/metricCollection.js b/tools/spectral/ipa/metrics/metricCollection.js index 1a2e07a51b..d94e9e3dc0 100644 --- a/tools/spectral/ipa/metrics/metricCollection.js +++ b/tools/spectral/ipa/metrics/metricCollection.js @@ -42,6 +42,11 @@ async function runMetricCollectionJob(oasFilePath = config.defaultOasFilePath) { const args = process.argv.slice(2); const customOasFile = args[0]; +if(!fs.existsSync(config.defaultOutputsDir)){ + fs.mkdirSync('outputs'); + console.log(`Output directory created successfully`); +} + const result = spawnSync( 'spectral', [ From 72dcbb250ecf981abddb9d1eaf0306a0aaa2a040 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Thu, 16 Jan 2025 07:00:32 +0000 Subject: [PATCH 10/10] prettier fix --- tools/spectral/ipa/metrics/metricCollection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/spectral/ipa/metrics/metricCollection.js b/tools/spectral/ipa/metrics/metricCollection.js index d94e9e3dc0..80b29a6945 100644 --- a/tools/spectral/ipa/metrics/metricCollection.js +++ b/tools/spectral/ipa/metrics/metricCollection.js @@ -42,7 +42,7 @@ async function runMetricCollectionJob(oasFilePath = config.defaultOasFilePath) { const args = process.argv.slice(2); const customOasFile = args[0]; -if(!fs.existsSync(config.defaultOutputsDir)){ +if (!fs.existsSync(config.defaultOutputsDir)) { fs.mkdirSync('outputs'); console.log(`Output directory created successfully`); }