forked from ubiquity/ubiquibot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-linked-pull-requests.ts
More file actions
49 lines (43 loc) · 1.69 KB
/
Copy pathget-linked-pull-requests.ts
File metadata and controls
49 lines (43 loc) · 1.69 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
import axios from "axios";
import { HTMLElement, parse } from "node-html-parser";
import { GetLinkedParams } from "../handlers/assign/check-pull-requests";
import { Context } from "../types/context";
interface GetLinkedResults {
organization: string;
repository: string;
number: number;
href: string;
}
export async function getLinkedPullRequests(
context: Context,
{ owner, repository, issue }: GetLinkedParams
): Promise<GetLinkedResults[]> {
const logger = context.logger;
const collection = [] as GetLinkedResults[];
const { data } = await axios.get(`https://github.com/${owner}/${repository}/issues/${issue}`);
const dom = parse(data);
const devForm = dom.querySelector("[data-target='create-branch.developmentForm']") as HTMLElement;
const linkedList = devForm.querySelectorAll(".my-1");
if (linkedList.length === 0) {
context.logger.info(`No linked pull requests found`);
return [];
}
for (const linked of linkedList) {
const relativeHref = linked.querySelector("a")?.attrs?.href;
if (!relativeHref) continue;
const parts = relativeHref.split("/");
// check if array size is at least 4
if (parts.length < 4) continue;
// extract the organization name and repo name from the link:(e.g. "
const organization = parts[parts.length - 4];
const repository = parts[parts.length - 3];
const number = Number(parts[parts.length - 1]);
const href = `https://github.com${relativeHref}`;
if (`${organization}/${repository}` !== `${owner}/${repository}`) {
logger.info("Skipping linked pull request from another repository", href);
continue;
}
collection.push({ organization, repository, number, href });
}
return collection;
}