-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
212 lines (190 loc) Β· 5.44 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
const extend = require('deep-extend')
const format = require('util').format
const path = require('path')
const ghReleaseAssets = require('gh-release-assets')
const getDefaults = require('./bin/lib/get-defaults')
const Emitter = require('events').EventEmitter
const { Octokit } = require('@octokit/rest')
const clientId = '04dac3c40b7e49b11f38'
const 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) {
const emitter = new Emitter()
if (options.cli) {
_Release(options, emitter, callback)
return emitter
}
const workpath = options.workpath || OPTIONS.defaults.workpath
const 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
const errors = validate(options)
let 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 (!getToken(options)) return callback(new Error('missing auth info'))
const octokit = new Octokit({
auth: getToken(options),
baseUrl: options.endpoint
})
octokit.repos.getCommit({
owner: options.owner,
repo: options.repo,
ref: options.target_commitish
}).then(results => {
if (options.dryRun) return callback(null, options)
octokit.repos.createRelease({
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
}).then(results => {
if (options.assets) {
const assets = options.assets.map(function (asset) {
if (typeof asset === 'object') {
return {
name: asset.name,
path: path.join(options.workpath, asset.path)
}
}
return path.join(options.workpath, asset)
})
const assetOptions = {
url: results.data.upload_url,
assets
}
if (options.auth.token) {
assetOptions.token = options.auth.token
} else {
assetOptions.auth = options.auth
}
const assetUpload = ghReleaseAssets(assetOptions, function (err) {
if (err) return callback(err)
return callback(null, results.data)
})
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, results.data)
}
}).catch(err => {
// Create Release error handling
if (err.status === 404) {
const notFoundError = new Error(format('404 Not Found. Review gh-release oAuth Organization access: https://github.com/settings/connections/applications/%s', clientId))
notFoundError.wrapped = err
return callback(notFoundError)
}
if (err.errors) {
if (err.errors[0].code !== 'already_exists') {
return callback(err)
}
const errorMessage = format('Release already exists for tag %s in %s/%s', options.tag_name, options.owner, options.repo)
return callback(new Error(errorMessage))
}
return callback(err)
})
}).catch(err => {
// Check target error handling
if (err.status === 404) {
const errorMessage = format('Target commitish %s not found in %s/%s', options.target_commitish, options.owner, options.repo)
return callback(new Error(errorMessage))
} else {
return callback(err)
}
})
}
function validate (options) {
const missing = []
const 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,
missing
}
}
function getToken (options) {
if (options.auth.token) {
return options.auth.token
} else if (options.auth.username && options.auth.password) {
return options.auth
} else {
return false
}
}
Release.OPTIONS = OPTIONS
Release.validate = validate
Release.clientId = clientId
module.exports = Release