forked from remixz/publish-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
324 lines (288 loc) · 11.2 KB
/
index.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* publish-release
* Create GitHub releases with assets
*
* @author Zach Bruggeman <[email protected]>
*/
var request = require('request')
var async = require('async')
var mime = require('mime')
var progress = require('progress-stream')
var util = require('util')
var fs = require('fs')
var path = require('path')
var EventEmitter = require('events').EventEmitter
var pkg = require('./package.json')
var DEFAULT_API_ROOT = 'https://api.github.com'
function PublishRelease (opts, cb) {
if (!(this instanceof PublishRelease)) return new PublishRelease(opts, cb)
this.opts = (opts || {})
this.cb = (cb || function noop () {})
this.publish()
}
util.inherits(PublishRelease, EventEmitter)
PublishRelease.prototype.publish = function publish () {
var self = this
var opts = this.opts
var cb = this.cb
// validate opts
var missing = []
;['token', 'repo', 'owner', 'tag'].forEach(function validateOpts (opt) {
if (!opts[opt]) {
missing.push(opt)
}
})
if (missing.length > 0) {
return cb(new Error('missing required options: ' + missing.join(', ')))
}
if (!opts.skipAssetsCheck && opts.assets && opts.assets.length > 0) {
try {
opts.assets.forEach(function (f) {
fs.accessSync(path.resolve(f))
})
} catch (err) {
cb(new Error('missing asset ' + err.path))
}
}
async.auto({
createRelease: function createRelease (callback) {
var ghReleaseUri = util.format((opts.apiUrl || DEFAULT_API_ROOT) + '/repos/%s/%s/releases', opts.owner, opts.repo)
function requestCreateRelease () {
self.emit('create-release')
var reqDetails = {
uri: ghReleaseUri,
method: 'POST',
json: true,
body: {
tag_name: opts.tag,
target_commitish: opts.target_commitish,
name: opts.name,
body: opts.notes,
draft: !!opts.draft,
prerelease: !!opts.prerelease
},
headers: {
'Authorization': 'token ' + opts.token,
'User-Agent': 'publish-release ' + pkg.version + ' (https://github.com/remixz/publish-release)'
}
}
request(reqDetails, function (err, res, body) {
if (err) {
// handle a real error, eg network fail
// will be handled by asyncAutoCallback
return callback(err)
}
var errorStatus = res.statusCode >= 400 && res.statusCode < 600
if (errorStatus) {
// handle an http error status
// will be handled by asyncAutoCallback
var e = new Error('Error status: ' + res.statusCode + ' response body:' + JSON.stringify(body) + '\n request details:' + JSON.stringify(reqDetails, null, 2))
return callback(e)
}
self.emit('created-release')
callback(null, body)
})
}
if (opts.reuseRelease) {
/**
* https://github.com/remixz/publish-release/issues/31
* We don't use "Get a release by tag name" because "tag name" means existing git tag,
* but we can draft release and don't create git tag
*/
request({
uri: ghReleaseUri,
method: 'GET',
json: true,
headers: {
'Authorization': 'token ' + opts.token,
'User-Agent': 'publish-release ' + pkg.version + ' (https://github.com/remixz/publish-release)'
}
}, function (err, res, body) {
if (err) return callback(err) // will be handled by asyncAutoCallback
var bodyReturn = null
async.eachSeries(body, function (el, callback) {
if (el.tag_name === opts.tag) {
bodyReturn = el
return
}
callback()
})
var statusOk = res.statusCode >= 200 && res.statusCode < 300
var hasReleaseMatchingTag = bodyReturn && bodyReturn.tag_name === opts.tag
var canReuse = !opts.reuseDraftOnly || (bodyReturn && bodyReturn.draft)
if (statusOk && hasReleaseMatchingTag && canReuse) {
self.emit('reuse-release')
bodyReturn.allowReuse = true // allow to editRelease
callback(null, bodyReturn)
} else if (!hasReleaseMatchingTag || hasReleaseMatchingTag && !opts.skipIfPublished) {
requestCreateRelease()
}
})
} else {
requestCreateRelease()
}
},
editRelease: ['createRelease', function editRelease (callback, obj) {
if (obj.createRelease.errors || !obj.createRelease.url) return callback()
if (opts.editRelease && obj.createRelease.allowReuse) {
self.emit('edit-release', obj.createRelease)
const editUri = obj.createRelease.url
const reqDetails = {
uri: editUri,
method: 'PATCH',
json: true,
body: {
tag_name: opts.tag,
target_commitish: opts.target_commitish,
name: opts.name,
body: opts.notes,
draft: !!opts.draft,
prerelease: !!opts.prerelease
},
headers: {
'Authorization': 'token ' + opts.token,
'User-Agent': 'publish-release ' + pkg.version + ' (https://github.com/remixz/publish-release)'
}
}
request(reqDetails, function (err, res, body) {
if (err) {
// handle a real error, eg network fail
// will be handled by asyncAutoCallback
return callback(err)
}
var errorStatus = res.statusCode >= 400 && res.statusCode < 600
if (errorStatus) {
// handle an http error status
// will be handled by asyncAutoCallback
var e = new Error('Error status: ' + res.statusCode + ' response body:' + JSON.stringify(body) + '\n request details:' + JSON.stringify(reqDetails, null, 2))
return callback(e)
}
self.emit('edited-release', body)
callback(null, body)
})
} else {
callback()
}
}],
deleteEmptyTag: ['createRelease', 'editRelease', function deleteEmptyTag (callback, obj) {
if (!obj.editRelease) return callback()
/**
* Compare if it's going from release/prerelease to tag
* to delete empty unused tag, checking if it's now draft and was not draft
*/
if (opts.deleteEmptyTag && obj.editRelease.draft && !obj.createRelease.draft) {
var deleteTagUri = util.format((opts.apiUrl || DEFAULT_API_ROOT) + '/repos/%s/%s/git/refs/tags/%s', opts.owner, opts.repo, obj.createRelease.tag_name)
const reqDetails = {
uri: deleteTagUri,
method: 'DELETE',
json: true,
headers: {
'Authorization': 'token ' + opts.token,
'User-Agent': 'publish-release ' + pkg.version + ' (https://github.com/remixz/publish-release)'
}
}
request(reqDetails, function (err, res, body) {
if (err) {
// handle a real error, eg network fail
// will be handled by asyncAutoCallback
return callback(err)
}
var errorStatus = res.statusCode >= 400 && res.statusCode < 600
if (errorStatus) {
// handle an http error status
// will be handled by asyncAutoCallback
var e = new Error('Error status: ' + res.statusCode + ' response body:' + JSON.stringify(body) + '\n request details:' + JSON.stringify(reqDetails, null, 2))
return callback(e)
}
self.emit('deleted-tag-release', obj.createRelease.tag_name)
callback(null, body)
})
} else {
callback()
}
}],
uploadAssets: ['createRelease', 'editRelease', 'deleteEmptyTag', function uploadAssets (callback, obj) {
if (!opts.assets || opts.assets.length === 0) return callback()
if (obj.createRelease.errors || !obj.createRelease.upload_url) return callback(obj.createRelease)
async.eachSeries(opts.assets, function (asset, callback) {
var fileName = path.basename(asset)
var uploadUri = obj.createRelease.upload_url.split('{')[0] + '?name=' + fileName
requestUploadAsset()
function requestUploadAsset () {
self.emit('upload-asset', fileName)
var stat = fs.statSync(asset)
var rd = fs.createReadStream(asset)
var us = request({
method: 'POST',
uri: uploadUri,
headers: {
'Authorization': 'token ' + opts.token,
'Content-Type': mime.lookup(fileName),
'Content-Length': stat.size,
'User-Agent': 'publish-release ' + pkg.version + ' (https://github.com/remixz/publish-release)'
}
}, function (err, res, body) {
if (err) return callback(err)
const bodyJson = JSON.parse(body)
if (res.statusCode === 422 && bodyJson.errors && bodyJson.errors[0].code === 'already_exists') {
self.emit('duplicated-asset', fileName)
if (!opts.skipDuplicatedAssets) {
async.eachSeries(obj.createRelease.assets, function (el, callback) {
if (fileName === el.name) {
const deleteAssetUri = obj.createRelease.url.split('/').slice(0, -1).join('/') + '/assets/' + el.id
request({
method: 'DELETE',
uri: deleteAssetUri,
headers: {
'Authorization': 'token ' + opts.token,
'User-Agent': 'publish-release ' + pkg.version + ' (https://github.com/remixz/publish-release)'
}
}, function (err, res, body) {
if (err) return callback(err)
self.emit('duplicated-asset-deleted', fileName)
requestUploadAsset()
callback()
})
} else {
callback()
}
})
} else {
callback()
}
} else {
self.emit('uploaded-asset', fileName)
callback()
}
})
var prog = progress({
length: stat.size,
time: 100
}, function (p) {
self.emit('upload-progress', fileName, p)
})
rd.on('error', function (err) {
return callback(err) // will be handled by asyncAutoCallback
})
rd.pipe(prog).pipe(us)
}
}, function (err) {
return callback(err) // will be handled by asyncAutoCallback
})
}]
}, function asyncAutoCallback (err, obj) {
if (err) {
// make sure we do not leak the Github auth token
err.message = err.message.replace(new RegExp(opts.token, 'g'), '****')
// we are an EventEmitter so emit the 'error' event so the caller knows we failed.
self.emit('error', err)
// just run the callback with no info. dont run cb(err) beacuse as an EventEmitter this creates a
// throw Error('Uncaught, unspecified "error" event.')
// and that error message isn't helpful to anyone
return cb()
}
// otherwise
cb(null, obj.createRelease)
})
}
module.exports = PublishRelease