-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathlinks.js
123 lines (109 loc) · 2.96 KB
/
links.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import * as cheerio from 'cheerio';
const FIXES_RE = /(Close[ds]?|Fix(e[ds])?|Resolve[sd]?)\s*:\s*(\S+)/mgi;
const FIX_RE = /(Close[ds]?|Fix(e[ds])?|Resolve[sd]?)\s*:\s*(\S+)/i;
const REFS_RE = /Refs?\s*:\s*(\S+)/mgi;
const REF_RE = /Refs?\s*:\s*(\S+)/i;
const PR_RE = /PR-URL\s*:\s*(\S+)/i;
/**
* Most of this class is ported from node-review
*/
export class LinkParser {
constructor(owner, repo, html) {
this.owner = owner;
this.repo = repo;
this.$ = cheerio.load(html);
}
getFixesUrlsFromArray(arr) {
const result = new Set();
for (const item of arr) {
const m = item.match(FIX_RE);
if (!m) continue;
const ref = m[3];
const url = this.getUrlFromOP(ref);
if (url) result.add(url);
}
return Array.from(result);
}
getRefsUrlsFromArray(arr) {
const result = new Set();
for (const item of arr) {
const m = item.match(REF_RE);
if (!m) continue;
const ref = m[1];
const url = this.getUrlFromOP(ref);
if (url) result.add(url);
}
return Array.from(result);
}
getPRUrlsFromArray(arr) {
const result = new Set();
for (const item of arr) {
const m = item.match(PR_RE);
if (!m) continue;
const prUrl = m[1];
const url = this.getUrlFromOP(prUrl);
if (url) result.add(url);
}
return Array.from(result);
}
// Do this so we can reliably get the correct url.
// Otherwise, the number could reference a PR or an issue.
getUrlFromOP(ref) {
const as = this.$('a');
const links = as.map((i, el) => this.$(el)).get();
for (const link of links) {
const text = link.text();
if (text === ref) {
const href = link.attr('href');
if (href) return href;
}
}
}
getFixes() {
const text = this.$.text();
const fixes = text.match(FIXES_RE) || [];
return this.getFixesUrlsFromArray(fixes);
}
getRefs() {
const text = this.$.text();
const refs = text.match(REFS_RE) || [];
return this.getRefsUrlsFromArray(refs);
}
getAltPrUrl() {
const text = this.$.text();
const refs = text.match(PR_RE) || [];
return this.getPRUrlsFromArray(refs);
}
}
const GITHUB_PULL_REQUEST_URL = /github.com\/([^/]+)\/([^/]+)\/pull\/(\d+)/;
export function parsePRFromURL(url) {
if (typeof url !== 'string') {
return undefined;
}
const match = url.match(GITHUB_PULL_REQUEST_URL);
if (match) {
return {
owner: match[1],
repo: match[2],
prid: parseInt(match[3])
};
}
return undefined;
};
export function getPrURL({ owner, repo, prid }) {
return `https://github.com/${owner}/${repo}/pull/${prid}`;
};
export function getMachineUrl(machine) {
return `[${machine.hostname}](${machine.url})`;
};
const PR_URL_RE = /PR-URL: https:\/\/github.com\/.+/;
export function parsePrURL(text) {
if (typeof text !== 'string') {
return undefined;
}
const match = text.match(PR_URL_RE);
if (!match) {
return undefined;
}
return parsePRFromURL(match[0]);
};