|
| 1 | +import fs from 'node:fs'; |
| 2 | +import { bundleAndLoadRuleset } from '@stoplight/spectral-ruleset-bundler/with-loader'; |
| 3 | +import { EntryType } from './collector.js'; |
| 4 | + |
| 5 | +export function loadOpenAPIFile(filePath) { |
| 6 | + try { |
| 7 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 8 | + return JSON.parse(content); |
| 9 | + } catch (error) { |
| 10 | + throw new Error(`Failed to load OpenAPI file: ${error.message}`); |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +export function getSeverityPerRule(ruleset) { |
| 15 | + const rules = ruleset.rules || {}; |
| 16 | + const map = {}; |
| 17 | + for (const [name, ruleObject] of Object.entries(rules)) { |
| 18 | + map[name] = ruleObject.definition.severity; |
| 19 | + } |
| 20 | + return map; |
| 21 | +} |
| 22 | + |
| 23 | +export async function loadRuleset(rulesetPath, spectral) { |
| 24 | + try { |
| 25 | + const ruleset = await bundleAndLoadRuleset(rulesetPath, { fs, fetch }); |
| 26 | + await spectral.setRuleset(ruleset); |
| 27 | + return ruleset; |
| 28 | + } catch (error) { |
| 29 | + throw new Error(`Failed to load ruleset: ${error.message}`); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +export function extractTeamOwnership(oasContent) { |
| 34 | + const ownerTeams = {}; |
| 35 | + const paths = oasContent.paths || {}; |
| 36 | + |
| 37 | + for (const [path, pathItem] of Object.entries(paths)) { |
| 38 | + for (const [, operation] of Object.entries(pathItem)) { |
| 39 | + const ownerTeam = operation['x-xgen-owner-team']; |
| 40 | + |
| 41 | + if (ownerTeam) { |
| 42 | + if (!ownerTeams[path]) { |
| 43 | + ownerTeams[path] = ownerTeam; |
| 44 | + } else if (ownerTeams[path] !== ownerTeam) { |
| 45 | + console.warn(`Conflict on path ${path}: ${ownerTeams[path]} vs ${ownerTeam}`); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return ownerTeams; |
| 52 | +} |
| 53 | + |
| 54 | +export function loadCollectorResults(collectorResultsFilePath) { |
| 55 | + try { |
| 56 | + const content = fs.readFileSync(collectorResultsFilePath, 'utf8'); |
| 57 | + const contentParsed = JSON.parse(content); |
| 58 | + return { |
| 59 | + [EntryType.VIOLATION]: contentParsed[EntryType.VIOLATION], |
| 60 | + [EntryType.ADOPTION]: contentParsed[EntryType.ADOPTION], |
| 61 | + [EntryType.EXCEPTION]: contentParsed[EntryType.EXCEPTION], |
| 62 | + }; |
| 63 | + } catch (error) { |
| 64 | + throw new Error(`Failed to load Collector Results file: ${error.message}`); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function getIPAFromIPARule(ipaRule) { |
| 69 | + const pattern = /IPA-\d{3}/; |
| 70 | + const match = ipaRule.match(pattern); |
| 71 | + if (match) { |
| 72 | + return match[0]; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export function merge(ownershipData, collectorResults, ruleSeverityMap) { |
| 77 | + const results = []; |
| 78 | + |
| 79 | + function addEntry(entryType, adoptionStatus) { |
| 80 | + for (const entry of collectorResults[entryType]) { |
| 81 | + const existing = results.find( |
| 82 | + (result) => result.component_id === entry.componentId && result.ipa_rule === entry.ruleName |
| 83 | + ); |
| 84 | + |
| 85 | + if (existing) { |
| 86 | + console.warn('Duplicate entries found', existing); |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + let ownerTeam = null; |
| 91 | + if (entry.componentId.startsWith('paths')) { |
| 92 | + const pathParts = entry.componentId.split('.'); |
| 93 | + if (pathParts.length === 2) { |
| 94 | + const path = pathParts[1]; |
| 95 | + ownerTeam = ownershipData[path]; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + results.push({ |
| 100 | + component_id: entry.componentId, |
| 101 | + ipa_rule: entry.ruleName, |
| 102 | + ipa: getIPAFromIPARule(entry.ruleName), |
| 103 | + severity_level: ruleSeverityMap[entry.ruleName], |
| 104 | + adoption_status: adoptionStatus, |
| 105 | + exception_reason: entryType === EntryType.EXCEPTION ? entry.exceptionReason : null, |
| 106 | + owner_team: ownerTeam, |
| 107 | + timestamp: new Date().toISOString(), |
| 108 | + }); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + addEntry(EntryType.VIOLATION, 'violated'); |
| 113 | + addEntry(EntryType.ADOPTION, 'adopted'); |
| 114 | + addEntry(EntryType.EXCEPTION, 'exempted'); |
| 115 | + |
| 116 | + return results; |
| 117 | +} |
0 commit comments