| 1 | // Code ownership: parsing de ficheros CODEOWNERS y de metadatos |
| 2 | // `package.yml` (metadata.owner), según las convenciones descritas en |
| 3 | // code_ownership_files.md. Compartido por scan.mjs y fsScan.js. |
| 4 | import { globToRegex } from './ignore.js'; |
| 5 | |
| 6 | // nombres de fichero que aportan reglas de ownership |
| 7 | export function isCodeownersFile(name) { return name === 'CODEOWNERS'; } |
| 8 | export function isPackageMeta(name) { return name === 'package.yml' || name === 'package.yaml'; } |
| 9 | |
| 10 | // CODEOWNERS: "patrón @owner1 [@owner2 …]" → [patrón, primer owner] |
| 11 | export function parseCodeowners(text) { |
| 12 | const rules = []; |
| 13 | for (let line of text.split('\n')) { |
| 14 | line = line.trim(); |
| 15 | if (!line || line.startsWith('#')) continue; |
| 16 | const parts = line.split(/\s+/); |
| 17 | if (parts.length < 2 || !parts[1].startsWith('@')) continue; |
| 18 | rules.push([parts[0], parts[1]]); |
| 19 | } |
| 20 | return rules; |
| 21 | } |
| 22 | |
| 23 | // package.yml: metadata.owner: '@team' → regla "dir/ → owner" |
| 24 | export function parsePackageMeta(text, dirRel) { |
| 25 | const m = text.match(/owner:\s*['"]?(@[\w/@.-]+)/); |
| 26 | if (!m) return null; |
| 27 | return [`/${dirRel}/`, m[1]]; |
| 28 | } |
| 29 | |
| 30 | // compila reglas (semántica CODEOWNERS: la última que casa gana) |
| 31 | export function makeOwnerResolver(rules) { |
| 32 | const compiled = []; |
| 33 | for (const [pat0, owner] of rules ?? []) { |
| 34 | let pat = pat0; |
| 35 | if (pat.endsWith('/')) pat = pat.slice(0, -1); |
| 36 | let prefix = ''; |
| 37 | if (pat.startsWith('/')) pat = pat.slice(1); |
| 38 | else if (!pat.includes('/')) prefix = '(?:[^/]+/)*'; |
| 39 | try { |
| 40 | // el patrón casa con la ruta exacta o como prefijo de directorio |
| 41 | compiled.push({ re: new RegExp(`^${prefix}${globToRegex(pat)}(?:/.*)?$`), owner }); |
| 42 | } catch { /* patrón inválido: se omite */ } |
| 43 | } |
| 44 | return (relPath) => { |
| 45 | let owner = null; |
| 46 | for (const r of compiled) if (r.re.test(relPath)) owner = r.owner; |
| 47 | return owner; |
| 48 | }; |
| 49 | } |