-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeownerLocator.ts
47 lines (38 loc) · 1.41 KB
/
codeownerLocator.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
45
46
47
import * as octokit from '@octokit/rest';
import {RepoInfo} from '../types';
type ContentResponse = {
name: string;
path: string;
data: string;
};
export class CodeownerLocator {
repo: RepoInfo;
octo: octokit;
auth?: octokit.Auth;
constructor(repo: RepoInfo, auth?: octokit.Auth) {
this.repo = repo;
this.auth = auth;
this.octo = new octokit();
this.auth && this.octo.authenticate(this.auth);
}
private async locateInDir(path: string): Promise<string | null> {
try {
const search = await this.octo.repos.getContent({path, ...this.repo});
const codeOwnersLocation = search.data.filter((x: ContentResponse) => x.name === 'CODEOWNERS');
if (codeOwnersLocation) return codeOwnersLocation[0].path;
return null;
} catch (err) {
return null;
}
}
public async locateCodeownersFile(): Promise<string> {
const codeowners =
(await this.locateInDir('/')) || (await this.locateInDir('docs/')) || (await this.locateInDir('.github/'));
if (codeowners) return codeowners;
throw new Error('Could not find CODEOWNERS file in allowed paths');
}
public async getCodeownersFile(): Promise<octokit.AnyResponse> {
const path = await this.locateCodeownersFile();
return await this.octo.repos.getContent({path, ...this.repo});
}
}