-
Notifications
You must be signed in to change notification settings - Fork 86
/
github.ts
124 lines (118 loc) · 2.93 KB
/
github.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
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
124
import * as path from 'path'
import * as fs from 'fs'
import _request from 'request'
function getErrorFromBody(body: string | any, opts: any) {
if (typeof body === 'string') {
try {
body = JSON.parse(body)
} catch (e) {}
}
// hide token from logs
opts.headers.Authorization = 'Token **********'
// log the request options to help debugging
body.request = opts
return new Error(JSON.stringify(body, null, ' '))
}
function request<T>(opts: any): Promise<T> {
return new Promise(function (resolve, reject) {
_request(opts, (error: any, response: _request.Response, body: any) => {
if (error) {
return reject(error)
}
const is2xx = !error && /^2/.test('' + response.statusCode)
if (!is2xx) {
return reject(getErrorFromBody(body, opts))
}
resolve(body)
})
})
}
function options(token: string, url: string, method?: string): any {
return {
method: method || 'GET',
url: url,
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: 'Token ' + token,
'User-Agent': 'SKPM-Release-Agent',
},
}
}
export function getOrCreateDraftRelease(
token: string,
repo: string,
tag: string,
branch: string,
changelog: string[]
): Promise<{ id: string }> {
return request<{ id: string }>(
options(
token,
'https://api.github.com/repos/' + repo + '/releases/tags/' + tag
)
)
.then(function (res) {
return res
})
.catch(function () {
const opts = options(
token,
'https://api.github.com/repos/' + repo + '/releases',
'POST'
)
opts.json = {
tag_name: tag,
target_commitish: branch,
body: '* ' + changelog.join('\n* '),
name: tag,
draft: true,
}
return request<{ id: string }>(opts)
})
}
export function updateAsset(
token: string,
repo: string,
releaseId: string,
asset: string
): Promise<{ name: string }> {
const name = path.basename(asset)
const opts = options(
token,
'https://uploads.github.com/repos/' +
repo +
'/releases/' +
releaseId +
'/assets?name=' +
encodeURIComponent(name),
'POST'
)
const stat = fs.statSync(asset) // eslint-disable-line no-sync
const rd = fs.createReadStream(asset)
opts.headers['Content-Type'] = 'application/zip'
opts.headers['Content-Length'] = stat.size
const us = _request(opts)
return new Promise(function (resolve, reject) {
rd.on('error', function (err) {
return reject(err)
})
us.on('error', function (err) {
return reject(err)
})
us.on('end', function () {
resolve({ name: name })
})
rd.pipe(us)
})
}
export function publishRelease(token: string, repo: string, releaseId: string) {
const opts = options(
token,
'https://api.github.com/repos/' + repo + '/releases/' + releaseId,
'PATCH'
)
opts.json = {
draft: false,
}
return request(opts)
}