-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathowners.js
More file actions
68 lines (63 loc) · 1.94 KB
/
owners.js
File metadata and controls
68 lines (63 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const fs = require("fs");
/**
* Parse an OWNERS file (same format as CODEOWNERS).
* Returns array of { pattern, owners } rules.
*
* By default, team refs (org/team) are filtered out and @ is stripped.
* Pass { includeTeams: true } to keep team refs (with @ stripped).
*
* @param {string} filePath - absolute path to the OWNERS file
* @param {{ includeTeams?: boolean }} [opts]
* @returns {Array<{pattern: string, owners: string[]}>}
*/
function parseOwnersFile(filePath, opts) {
const includeTeams = opts && opts.includeTeams;
const lines = fs.readFileSync(filePath, "utf-8").split("\n");
const rules = [];
for (const raw of lines) {
const line = raw.trim();
if (!line || line.startsWith("#")) continue;
const parts = line.split(/\s+/);
if (parts.length < 2) continue;
const pattern = parts[0];
const owners = parts
.slice(1)
.filter((p) => p.startsWith("@") && (includeTeams || !p.includes("/")))
.map((p) => p.slice(1));
rules.push({ pattern, owners });
}
return rules;
}
/**
* Match a filepath against an OWNERS pattern.
* Supports: "*" (catch-all), "/dir/" (prefix), "/path/file" (exact).
*/
function ownersMatch(pattern, filepath) {
if (pattern === "*") return true;
let p = pattern;
if (p.startsWith("/")) p = p.slice(1);
if (p.endsWith("/")) return filepath.startsWith(p);
return filepath === p;
}
/**
* Find owners for a file. Last match wins, like CODEOWNERS.
* @returns {string[]} owner logins
*/
function findOwners(filepath, rules) {
let matched = [];
for (const rule of rules) {
if (ownersMatch(rule.pattern, filepath)) {
matched = rule.owners;
}
}
return matched;
}
/**
* Get maintainers from the * catch-all rule.
* @returns {string[]} logins
*/
function getMaintainers(rules) {
const catchAll = rules.find((r) => r.pattern === "*");
return catchAll ? catchAll.owners : [];
}
module.exports = { parseOwnersFile, ownersMatch, findOwners, getMaintainers };