/
code-owners.ts
44 lines (40 loc) · 1.28 KB
/
code-owners.ts
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
import ignore from 'ignore';
import { logger } from '../../logger';
import { Pr, platform } from '../../platform';
import { readLocalFile } from '../../util/fs';
export async function codeOwnersForPr(pr: Pr): Promise<string[]> {
try {
const codeOwnersFile =
(await readLocalFile('CODEOWNERS', 'utf8')) ||
(await readLocalFile('.github/CODEOWNERS', 'utf8')) ||
(await readLocalFile('.gitlab/CODEOWNERS', 'utf8')) ||
(await readLocalFile('docs/CODEOWNERS', 'utf8'));
if (!codeOwnersFile) {
return [];
}
const prFiles = await platform.getPrFiles(pr);
const rules = codeOwnersFile
.split('\n')
.map((line) => line.trim())
.filter((line) => line && !line.startsWith('#'))
.map((line) => {
const [pattern, ...usernames] = line.split(/\s+/);
return {
usernames,
match: (path: string) => {
const matcher = ignore().add(pattern);
return matcher.ignores(path);
},
};
})
.reverse();
const matchingRule = rules.find((rule) => prFiles?.every(rule.match));
if (!matchingRule) {
return [];
}
return matchingRule.usernames;
} catch (err) {
logger.warn({ err, pr }, 'Failed to determine code owners for PR.');
return [];
}
}