-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathextends-to-get-content-params.ts
53 lines (48 loc) · 1.16 KB
/
extends-to-get-content-params.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
48
49
50
51
52
53
type Options = {
owner: string;
path: string;
url: string;
extendsValue: string;
};
const EXTENDS_REGEX = new RegExp(
"^" +
"(?:([a-z\\d](?:[a-z\\d]|-(?=[a-z\\d])){0,38})/)?" + // org
"([-_.\\w\\d]+)" + // project
"(?::([-_./\\w\\d]+\\.ya?ml))?" + // filename
"$",
"i"
);
/**
* Computes parameters to retrieve the configuration file specified in _extends
*
* Base can either be the name of a repository in the same organization or
* a full slug "organization/repo".
*
* @param options
* @return The params needed to retrieve a configuration file
*/
export function extendsToGetContentParams({
owner,
path,
url,
extendsValue,
}: Options) {
if (typeof extendsValue !== "string") {
throw new Error(
`[@probot/octokit-plugin-config] Invalid value ${JSON.stringify(
extendsValue
)} for _extends in ${url}`
);
}
const match = extendsValue.match(EXTENDS_REGEX);
if (match === null) {
throw new Error(
`[@probot/octokit-plugin-config] Invalid value "${extendsValue}" for _extends in ${url}`
);
}
return {
owner: match[1] || owner,
repo: match[2],
path: match[3] || path,
};
}