/
github.js
278 lines (231 loc) · 8.5 KB
/
github.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
var async = require('async')
var utils = require('../utils')
var config = require('../utils/config')
var log = require('../utils/log')
var USER_AGENT = config.STACK
var STATUS_CONTEXT = `continuous-integration/${config.STACK}`
exports.createClient = function(build) {
return new GithubClient(build)
}
exports.GithubClient = GithubClient
function GithubClient(build) {
this.token = build.token
this.repo = build.repo
this.commit = build.commit
this.logUrl = build.logUrl
this.statusQueue = async.queue(this.updateStatus.bind(this), 1)
if (!build.statusEmitter) return
build.statusEmitter.on('start', (build) => {
var status = {
state: 'pending',
description: `Build #${build.buildNum} started...`,
}
this.statusQueue.push(status, log.logIfErr)
})
build.statusEmitter.finishTasks.push((build, cb) => {
var status = {
state: build.error ? 'failure' : 'success',
description: build.error ? build.error.message : `Build #${build.buildNum} successful!`,
}
this.statusQueue.push(status, function(err) {
log.logIfErr(err)
cb()
})
})
}
GithubClient.prototype.updateStatus = function(status, cb) {
status = status || {}
status.state = status.state || 'pending'
status.description = status.description || ''
status.target_url = status.target_url || this.logUrl
status.context = status.context || STATUS_CONTEXT
this.request({path: `/repos/${this.repo}/statuses/${this.commit}`, body: status}, cb)
}
// By default this will just ensure that the SNS hook listens for push & pull_request
GithubClient.prototype.updateSnsHook = function(options, cb) {
options = options || {events: ['push', 'pull_request']}
var getHookId = cb => options.id ? cb(null, options.id) : this.getSnsHook((err, hook) => cb(err, hook && hook.id))
getHookId((err, id) => {
if (err) return cb(err)
this.updateHook(id, options, cb)
})
}
GithubClient.prototype.deleteSnsHook = function(id, cb) {
var getHookId = cb => id ? cb(null, id) : this.getSnsHook((err, hook) => cb(err, hook && hook.id))
log.info(`Deleting SNS hook for ${this.repo}`)
getHookId((err, id) => {
if (err) return cb(err)
if (!id) return cb()
this.deleteHook(id, cb)
})
}
GithubClient.prototype.getSnsHook = function(cb) {
this.listHooks((err, data) => cb(err, data && data.find(hook => hook.name == 'amazonsns')))
}
GithubClient.prototype.createOrUpdateSnsHook = function(awsKey, awsSecret, snsTopic, cb) {
var hook = {
name: 'amazonsns',
active: true,
events: ['push', 'pull_request'],
// https://github.com/github/github-services/blob/master/lib/services/amazon_sns.rb
config: {
aws_key: awsKey,
aws_secret: awsSecret,
sns_topic: snsTopic,
sns_region: process.env.AWS_REGION,
},
}
log.info(`Updating SNS hook for ${this.repo}`)
this.createHook(hook, cb)
}
GithubClient.prototype.listHooks = function(cb) {
this.request({path: `/repos/${this.repo}/hooks`}, cb)
}
// See: https://developer.github.com/v3/repos/hooks/#create-a-hook
// hook can be: {name: '', config: {}, events: [], active: true}
GithubClient.prototype.createHook = function(hook, cb) {
this.request({path: `/repos/${this.repo}/hooks`, body: hook}, cb)
}
// See: https://developer.github.com/v3/repos/hooks/#edit-a-hook
// updates can be: {config: {}, events: [], add_events: [], remove_events: [], active: true}
GithubClient.prototype.updateHook = function(id, updates, cb) {
this.request({method: 'PATCH', path: `/repos/${this.repo}/hooks/${id}`, body: updates}, cb)
}
// See: https://developer.github.com/v3/repos/hooks/#delete-a-hook
GithubClient.prototype.deleteHook = function(id, cb) {
this.request({method: 'DELETE', path: `/repos/${this.repo}/hooks/${id}`}, cb)
}
GithubClient.prototype.request = function(options, cb) {
/* eslint dot-notation:0 */
options.host = 'api.github.com'
options.headers = options.headers || {}
options.headers['Accept'] = 'application/vnd.github.v3+json'
options.headers['User-Agent'] = USER_AGENT
if (options.body) {
options.headers['Content-Type'] = 'application/vnd.github.v3+json'
}
if (this.token) {
options.headers['Authorization'] = `token ${this.token}`
}
utils.request(options, function(err, res, data) {
if (err) return cb(err)
if (!data && res.statusCode < 400) return cb(null, {})
var json
try {
json = JSON.parse(data)
} catch (e) {
err = new Error(data ? `Could not parse response: ${data}` : res.statusCode)
err.statusCode = res.statusCode
err.body = data
return cb(err)
}
if (res.statusCode >= 400) {
var errMsg = json.message || data
if (res.statusCode == 401) {
errMsg = 'GitHub token is invalid'
} else if (res.statusCode == 404) {
errMsg = 'GitHub token has insufficient privileges or repository does not exist'
}
err = new Error(errMsg)
err.statusCode = res.statusCode
err.body = json
return cb(err)
}
cb(null, json)
})
}
exports.parseEvent = function(event, eventType) {
// Remove redundant fields like urls
exports.trimEvent(event)
// Log the git event for debugging
log.raw(event)
if (!~['push', 'pull_request'].indexOf(eventType)) {
if (event.pull_request) {
eventType = 'pull_request'
} else if (event.pusher) {
eventType = 'push'
} else {
throw new Error(`Unknown GitHub event type: ${eventType}`)
}
}
if (!event.repository) {
throw new Error('repository field is missing from GitHub event')
}
var build = {
event,
eventType,
repo: event.repository.full_name,
project: `gh/${event.repository.full_name}`,
isPrivate: event.repository.private,
}
if (eventType == 'pull_request') {
// https://developer.github.com/v3/activity/events/types/#pullrequestevent
var prNum = event.number
if (!event.pull_request) {
throw new Error('pull_request field is missing from GitHub event')
}
if (event.pull_request.state == 'closed') {
return {ignore: `Pull request #${prNum} is closed`}
}
if (!~['opened', 'reopened', 'synchronize'].indexOf(event.action)) {
return {ignore: `Ignoring pull request #${prNum} action "${event.action}"`}
}
var base = event.pull_request.base || {}
var head = event.pull_request.head || {}
var baseRepo = base.repo || {}
var headRepo = head.repo || {}
// This should never happen
if (baseRepo.full_name != build.repo) {
throw new Error(`base repo ${baseRepo.full_name} is different from event repo ${build.repo}`)
}
build.branch = (base.ref || '').replace(/^refs\/heads\//, '')
build.cloneRepo = headRepo.full_name
build.checkoutBranch = (head.ref || '').replace(/^refs\/heads\//, '')
build.commit = head.sha
build.baseCommit = base.sha
build.comment = event.pull_request.title || ''
build.user = (event.pull_request.user || {}).login
build.isFork = build.cloneRepo != build.repo
build.prNum = prNum
} else {
// https://developer.github.com/v3/activity/events/types/#pushevent
var branchMatch = (event.ref || '').match(/^refs\/heads\/(.+)$/)
if (!branchMatch) {
return {ignore: `Ref does not match any branches: ${event.ref}`}
}
var branch = branchMatch[1]
if (event.deleted) {
return {ignore: `Branch ${branch} was deleted`}
}
build.branch = branch
build.cloneRepo = build.repo
build.checkoutBranch = branch
build.commit = (event.head_commit || {}).id
build.baseCommit = event.before
build.comment = (event.head_commit || {}).message || ''
build.user = (event.pusher || {}).name
build.committers = new Set((event.commits || []).concat(event.head_commit || {}).reduce((committers, commit) => {
if ((commit.author || {}).username) committers.push(commit.author.username)
if ((commit.committer || {}).username) committers.push(commit.committer.username)
return committers
}, []))
}
if (!/^[a-zA-Z0-9-_.]+\/[a-zA-Z0-9-_.]+$/.test(build.repo)) {
throw new Error(`Repository is invalid: ${build.repo}`)
}
if (!/^[0-9a-f]+$/.test(build.commit) || /^0+$/.test(build.commit)) {
throw new Error(`Commit is invalid: ${build.commit}`)
}
return build
}
exports.trimEvent = function(event) {
var deleteIfMatches = /^(url|_links)$|_url$/
var whitelist = ['clone_url', 'avatar_url']
Object.keys(event || {}).forEach(key => {
if (deleteIfMatches.test(key) && !~whitelist.indexOf(key)) {
delete event[key]
} else if (event[key] && typeof event[key] == 'object') {
exports.trimEvent(event[key])
}
})
}