forked from ungoldman/gh-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
218 lines (189 loc) · 5.6 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
var extend = require('deep-extend')
var request = require('request')
var format = require('util').format
var path = require('path')
var ghReleaseAssets = require('gh-release-assets')
var getDefaults = require('./bin/lib/get-defaults')
var Emitter = require('events').EventEmitter
var OPTIONS = {
required: [
'auth',
'owner',
'repo',
'body',
'target_commitish',
'tag_name',
'name'
],
valid: [],
defaults: {
'dryRun': false,
'yes': false,
'draft': false,
'endpoint': 'https://api.github.com',
'prerelease': false,
'workpath': process.cwd()
},
whitelist: [
'auth',
'owner',
'repo',
'body',
'target_commitish',
'tag_name',
'name',
'dryRun',
'yes',
'draft',
'endpoint',
'prerelease',
'workpath',
'assets'
]
}
function Release (options, callback) {
var emitter = new Emitter()
if (options.cli) {
_Release(options, emitter, callback)
return emitter
}
var workpath = options.workpath || OPTIONS.defaults.workpath
var isEnterprise = !!options.endpoint && options.endpoint !== OPTIONS.defaults.endpoint
getDefaults(workpath, isEnterprise, function (err, defaults) {
options = extend(defaults, options)
if (err) {
return callback(err)
}
return _Release(options, emitter, callback)
})
return emitter
}
// attempt to create release
// err if release already exists
// return release url
function _Release (options, emitter, callback) {
// validate release options
var errors = validate(options)
var err
if (errors.missing.length) {
err = new Error('missing required options: ' + errors.missing.join(', '))
return callback(err)
}
if (errors.invalid.length) {
err = new Error('invalid options: ' + errors.invalid.join(', '))
return callback(err)
}
// err if auth info not provided (token or user/pass)
if (!getAuth(options)) return callback(new Error('missing auth info'))
var commitOptsPath = format('/repos/%s/%s/commits/%s', options.owner, options.repo, options.target_commitish)
var commitOptsUri = options.endpoint.replace(/\/+$/, '') + commitOptsPath
// check if commit exists on remote
var commitOpts = extend(getAuth(options), {
method: 'GET',
uri: commitOptsUri
})
request(commitOpts, function (err, res, body) {
if (err || res.statusCode === 404) {
var errorMessage = format('Target commitish %s not found in %s/%s', options.target_commitish, options.owner, options.repo)
return callback(new Error(errorMessage))
}
var releaseOptsPath = format('/repos/%s/%s/releases', options.owner, options.repo)
var releaseOptsUri = options.endpoint.replace(/\/+$/, '') + releaseOptsPath
var releaseOpts = extend(getAuth(options), {
uri: releaseOptsUri,
body: {
tag_name: options.tag_name,
target_commitish: options.target_commitish,
name: options.name,
body: options.body,
draft: options.draft,
prerelease: options.prerelease,
repo: options.repo,
owner: options.owner
}
})
if (options.dryRun) return callback(null, options)
request(releaseOpts, function (err, res, body) {
if (err) {
return callback(err)
}
if (body.errors) {
if (body.errors[0].code !== 'already_exists') {
return callback(body.errors)
}
var errorMessage = format('Release already exists for tag %s in %s/%s', options.tag_name, options.owner, options.repo)
return callback(new Error(errorMessage))
}
if (body.message === 'Bad credentials') {
return callback(new Error('GitHub says password is no beuno. please clear your cache manually. https://github.com/hypermodules/gh-release#config-location'))
}
if (options.assets) {
var assets = options.assets.map(function (asset) {
return path.join(options.workpath, asset)
})
var assetOptions = {
url: body.upload_url,
assets: assets
}
if (options.auth.token) {
assetOptions.token = options.auth.token
} else {
assetOptions.auth = options.auth
}
var assetUpload = ghReleaseAssets(assetOptions, function (err) {
if (err) return callback(err)
return callback(null, body)
})
assetUpload.on('upload-asset', function (name) {
emitter.emit('upload-asset', name)
})
assetUpload.on('upload-progress', function (name, progress) {
emitter.emit('upload-progress', name, progress)
})
assetUpload.on('uploaded-asset', function (name) {
emitter.emit('uploaded-asset', name)
})
} else {
callback(null, body)
}
})
})
}
function validate (options) {
var missing = []
var invalid = []
function checkMissing (opt) {
if (!options[opt]) missing.push(opt)
}
function validateInput (opt) {
if (OPTIONS.valid[opt].indexOf(options[opt]) === -1) {
invalid.push(opt)
}
}
OPTIONS.required.forEach(checkMissing)
Object.keys(OPTIONS.valid).forEach(validateInput)
return {
invalid: invalid,
missing: missing
}
}
function getAuth (options) {
var defaultRequest = {
method: 'POST',
json: true,
headers: {
'User-Agent': 'gh-release'
}
}
if (options.auth.token) {
defaultRequest.headers.Authorization = 'token ' + options.auth.token
} else if (options.auth.username && options.auth.password) {
defaultRequest.auth = options.auth
} else {
return false
}
return defaultRequest
}
Release.OPTIONS = OPTIONS
Release.validate = validate
module.exports = Release