forked from wechaty/docusaurus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpr-number-to-title.ts
41 lines (38 loc) · 1.04 KB
/
pr-number-to-title.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
import { FileBox } from 'file-box'
export async function prNumberToTitleByApi (
org: string,
repo: string,
pr: number,
): Promise<string> {
const fileBox = FileBox.fromUrl(
// https://stackoverflow.com/a/34601082/1123955
`https://api.github.com/repos/${org}/${repo}/pulls/${pr}`,
'pr.json',
{
'User-Agent': 'FileBox',
}
)
const prJsonText = (await fileBox.toBuffer()).toString()
// console.log(prJsonText)
const prJson = JSON.parse(prJsonText)
const prTitle = prJson.title as string
return prTitle
}
export async function prNumberToTitle (
org: string,
repo: string,
pr: number,
): Promise<string> {
const fileBox = FileBox.fromUrl(
`https://github.com/${org}/${repo}/pull/${pr}`,
'pr.json',
)
const prHtml = (await fileBox.toBuffer()).toString()
// console.log(prHtml)
const matches = prHtml.match(/<html.+?<head>.+?<title>(.+?) by .+? · Pull Request #\d+.+?<\/title>/si)
if (!matches) {
throw new Error('no matches for pr title')
}
const prTitle = matches[1]
return prTitle
}