-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathpullRequestKey.ts
More file actions
25 lines (21 loc) · 854 Bytes
/
pullRequestKey.ts
File metadata and controls
25 lines (21 loc) · 854 Bytes
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
/**
* Helper types to identify key fields of pull request related APIs.
*
* Two functions are provided to extract the key fields from a formatted URL.
*/
export type pullRequestKey = {
owner: string;
repo: string;
pull_number: number;
};
export type pullRequestKeyInfo = string | pullRequestKey;
export function getPullRequestKey(pullRequest: pullRequestKeyInfo): pullRequestKey {
return typeof pullRequest === "string" ? getPullRequestKeyFromURL(pullRequest) : pullRequest;
}
export function getPullRequestKeyFromURL(pullRequestURL: string): pullRequestKey {
const match = pullRequestURL.match(/^https:\/\/github.com\/(.*)\/(.*)\/pull\/(\d+)$/);
if (!match) {
throw new Error(`Unrecognized PR URL: "${pullRequestURL}`);
}
return { owner: match[1], repo: match[2], pull_number: parseInt(match[3], 10) };
}