forked from ubiquity/ubiquibot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.ts
More file actions
93 lines (76 loc) · 2.88 KB
/
Copy pathparser.ts
File metadata and controls
93 lines (76 loc) · 2.88 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import axios from "axios";
import { HTMLElement, parse } from "node-html-parser";
import { getPullByNumber } from "./issue";
import { getBotContext, getLogger } from "../bindings";
interface GitParser {
owner: string;
repo: string;
issue_number?: number;
pull_number?: number;
}
export interface LinkedPR {
prOrganization: string;
prRepository: string;
prNumber: number;
prHref: string;
}
export const gitLinkedIssueParser = async ({ owner, repo, pull_number }: GitParser) => {
const logger = getLogger();
try {
const { data } = await axios.get(`https://github.com/${owner}/${repo}/pull/${pull_number}`);
const dom = parse(data);
const devForm = dom.querySelector("[data-target='create-branch.developmentForm']") as HTMLElement;
const linkedIssues = devForm.querySelectorAll(".my-1");
if (linkedIssues.length === 0) {
return null;
}
const issueUrl = linkedIssues[0].querySelector("a")?.attrs?.href || "";
return issueUrl;
} catch (error) {
logger.error(`${JSON.stringify(error)}`);
return null;
}
};
export const gitLinkedPrParser = async ({ owner, repo, issue_number }: GitParser): Promise<LinkedPR[]> => {
const logger = getLogger();
try {
const prData = [];
const { data } = await axios.get(`https://github.com/${owner}/${repo}/issues/${issue_number}`);
const dom = parse(data);
const devForm = dom.querySelector("[data-target='create-branch.developmentForm']") as HTMLElement;
const linkedPRs = devForm.querySelectorAll(".my-1");
if (linkedPRs.length === 0) return [];
for (const linkedPr of linkedPRs) {
const prUrl = linkedPr.querySelector("a")?.attrs?.href;
if (!prUrl) continue;
const parts = prUrl.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 prOrganization = parts[parts.length - 4];
const prRepository = parts[parts.length - 3];
const prNumber = Number(parts[parts.length - 1]);
const prHref = `https://github.com${prUrl}`;
if (`${prOrganization}/${prRepository}` !== `${owner}/${repo}`) continue;
prData.push({ prOrganization, prRepository, prNumber, prHref });
}
return prData;
} catch (error) {
logger.error(`${JSON.stringify(error)}`);
return [];
}
};
export const getLatestPullRequest = async (prs: LinkedPR[]) => {
const context = getBotContext();
let linkedPullRequest = null;
for (const _pr of prs) {
if (Number.isNaN(_pr.prNumber)) return null;
const pr = await getPullByNumber(context, _pr.prNumber);
if (!pr || !pr.merged) continue;
if (!linkedPullRequest) linkedPullRequest = pr;
else if (linkedPullRequest.merged_at && pr.merged_at && new Date(linkedPullRequest.merged_at) < new Date(pr.merged_at)) {
linkedPullRequest = pr;
}
}
return linkedPullRequest;
};