Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@

*.out
**/*ipa-collector-results-combined.log
**/*metric-collection-results.json
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
14 changes: 14 additions & 0 deletions tools/spectral/ipa/metrics/config.js
Original file line number Diff line number Diff line change
@@ -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'),
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give an example how we can set different configurations? For the GH action later on we'd like to point to the generated FOAS for example

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script currently supports providing a custom OAS file only, which must be in JSON format.

For example, you can run the script using:
node metricCollection.js "<path_to_oas_file>" to specify a custom OAS file.


export default config;
51 changes: 51 additions & 0 deletions tools/spectral/ipa/metrics/metricCollection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import spectral from '@stoplight/spectral-core';
import * as fs from 'node:fs';
import {
loadOpenAPIFile,
extractTeamOwnership,
loadRuleset,
loadCollectorResults,
getSeverityPerRule,
merge,
} from './utils.js';
import config from './config.js';
const { Spectral } = spectral;

async function runMetricCollectionJob(oasFilePath = config.defaultOasFilePath) {
try {
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 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);

console.log('Loading collector results...');
const collectorResults = loadCollectorResults(config.defaultCollectorResultsFilePath);

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;
}
}

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));
108 changes: 108 additions & 0 deletions tools/spectral/ipa/metrics/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
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;
}
Loading