-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.ts
60 lines (53 loc) · 1.23 KB
/
api.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as HTTPS from 'https'
export interface IAPIPR {
readonly title: string
readonly body: string
}
type GraphQLResponse = {
readonly data: {
readonly repository: {
readonly pullRequest: IAPIPR
}
}
}
export function fetchPR(id: number): Promise<IAPIPR | null> {
return new Promise((resolve, reject) => {
const options: HTTPS.RequestOptions = {
host: 'api.github.com',
protocol: 'https:',
path: '/graphql',
method: 'POST',
headers: {
Authorization: `bearer ${process.env.GITHUB_ACCESS_TOKEN}`,
'User-Agent': 'what-the-changelog',
},
}
const request = HTTPS.request(options, response => {
let received = ''
response.on('data', chunk => {
received += chunk
})
response.on('end', () => {
try {
const json: GraphQLResponse = JSON.parse(received)
const pr = json.data.repository.pullRequest
resolve(pr)
} catch (e) {
resolve(null)
}
})
})
const graphql = `
{
repository(owner: "desktop", name: "desktop") {
pullRequest(number: ${id}) {
title
body
}
}
}
`
request.write(JSON.stringify({ query: graphql }))
request.end()
})
}