-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapi.js
executable file
·380 lines (338 loc) · 11.2 KB
/
api.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
'use strict';
var async = require('async');
var crypto = require('crypto');
var debug = require('debug')('strider-bitbucket:api');
var gravatar = require('gravatar');
var API = 'https://bitbucket.org/api/1.0/';
var API2 = 'https://api.bitbucket.org/2.0/';
module.exports = {
parseRepo: parseRepo,
parseCommitData: parseCommitData,
parsePullRequestData: parsePullRequestData,
generateSecret: generateSecret,
setWebhooks: setWebhooks,
removeWebhooks: removeWebhooks,
startCommitJob: startCommitJob,
startPullrequestJob: startPullrequestJob,
makeJob: makeJob,
resultHandler: resultHandler
};
function generateSecret(callback) {
crypto.randomBytes(32, function (err, buf) {
callback(err, buf && buf.toString('hex'));
});
}
function parseRepo(repo) {
return {
id: `${repo.owner}/${repo.slug}`,
name: `${repo.owner}/${repo.slug}`,
display_name: `${repo.owner}/${repo.name}`,
display_url: `https://bitbucket.org/${repo.owner}/${repo.slug}`,
group: repo.owner,
private: repo.is_private,
config: {
auth: {type: 'ssh'},
scm: repo.scm,
url: `ssh://${repo.scm}@bitbucket.org/${repo.owner}/${repo.slug}`,
owner: repo.owner,
repo: repo.slug,
pull_requests: 'none',
whitelist: []
}
};
}
function removeWebhooks(client, hostname, project_name, done) {
var url = `${API}repositories/${project_name}/services`;
var hitbase = `${hostname}/${project_name}/api/bitbucket/`;
var tasks = [];
function remove(id, next) {
debug('removing', id);
client.del(`${url}/${id}/`, function (err, data) {
debug('removed response', id, err, data);
next(err);
});
}
debug('checking for services', url);
client.get(url, function (err, data) {
debug('response', err, JSON.stringify(data));
if (err) return done(err);
var hit;
for (var i = 0; i < data.length; i++) {
if (data[i].service.type.indexOf('POST') === -1) continue;
var fields = data[i].service.fields;
hit = false;
for (var j = 0; j < fields.length; j++) {
if (fields[j].name === 'URL' &&
fields[j].value.toLowerCase().indexOf(hitbase) === 0) {
debug('found a match!', data[i], fields[j]);
hit = true;
break;
}
}
if (hit) {
tasks.push(remove.bind(null, data[i].id));
}
}
debug(tasks.length, 'matches found');
if (!tasks.length) return done(null);
async.parallel(tasks, function (err) {
done(err, true);
});
});
}
function setWebhooks(client, hostname, project_name, done) {
var url = `${API}repositories/${project_name}/services`;
var hitbase = `${hostname}/${project_name}/api/bitbucket/`;
debug('checking for existing services', url);
client.get(url, function (err, data, res) {
debug('response', err, data, res.status);
if (err) return done(err);
var hit;
for (var i = 0; i < data.length; i++) {
if (data[i].service.type.indexOf('POST') === -1) continue;
var fields = data[i].service.fields;
hit = false;
// TODO check for the POST and PR hooks separately, and if their
// 'secret's differ, then reset one to match the other
for (var j = 0; j < fields.length; j++) {
if (fields[j].name === 'URL' &&
fields[j].value.toLowerCase().indexOf(hitbase) === 0) {
hit = fields[j].value;
break;
}
}
if (hit) {
debug('Found a matching service already there...', hit, data[i]);
return done(null, hit.slice(hitbase.length), true);
}
}
generateSecret(function (err, secret) {
debug('generated secret', err, secret);
if (err) return done(err);
debug('create POST service', url, hitbase, `${hitbase}commit/${secret}`);
client.post(`${url}/`, {
type: 'POST',
URL: `${hitbase}commit/${secret}`
}, function (err, data, req) {
debug('response:', err, data, req.status);
if (err) return done(err);
debug('create Pull Request POST service', url, hitbase, `${hitbase}pull-request/${secret}`);
client.post(`${url}/`, {
type: 'Pull Request POST',
'create/edit/merge/decline': 'on',
comments: 'on',
'approve/unapprove': 'on',
URL: `${hitbase}pull-request/${secret}`
}, function (err, data, req) {
debug('response:', err, data, req.status);
return done(null, secret);
});
});
});
});
}
function parseAuthor(raw) {
var match = raw.match(/([^<]+)<([^>]+)>/);
if (!match) {
return {
name: raw.trim(),
email: null
};
}
return {
name: match[1].trim(),
email: match[2].trim()
};
}
function parsePullRequestData(data) {
var pullrequest = data.pullrequest_created || data.pullrequest_updated;
var author = pullrequest.author;
if (!data.pullrequest_created && !data.pullrequest_updated) {
return {notInteresting: true};
}
if (pullrequest.description.indexOf('[skip ci]') > -1) {
return {skipCi: true};
}
return {
trigger: {
type: 'pullrequest',
author: {
name: author.display_name,
email: '',
image: author.links.avatar.href
},
url: `https://bitbucket.org/${pullrequest.source.repository.full_name}/pull-requests/${pullrequest.id}/diff`,
message: pullrequest.description,
timestamp: pullrequest.created_on,
source: {
type: 'plugin',
plugin: 'bitbucket'
}
},
deploy: true,
branch: pullrequest.source.branch.name,
ref: {
branch: pullrequest.source.branch.name,
id: pullrequest.source.commit.hash
}
};
}
function parseCommitData(data) {
var commit = data.commits[data.commits.length - 1];
var author = parseAuthor(commit.raw_author);
if (commit.message.indexOf('[skip ci]') > -1) {
return {skipCi: true};
}
if (!data.commits.length) {
return {notInteresting: true};
}
return {
trigger: {
type: 'commit',
author: {
name: author.name,
email: author.email,
image: author.email && gravatar.url(author.email, {}, true)
},
url: `${data.canon_url}${data.repository.absolute_url}commits/${commit.raw_node}`,
message: commit.message,
timestamp: commit.timestamp,
source: {
type: 'plugin',
plugin: 'bitbucket'
}
},
deploy: true,
branch: commit.branch,
ref: {
branch: commit.branch,
id: commit.raw_node
}
};
}
function startJob(data, project, emitter, done) {
if (data.skipCi) {
debug('Skipping job due to [skip ci] tag');
return done();
} else if (data.notInteresting) {
debug('Skipping job due to not interesting action');
return done();
}
var branch = project.branch(data.branch);
var job;
if (branch) {
job = makeJob(project, data);
if (job) {
emitter.emit('job.prepare', job);
return done();
}
}
debug('webhook received, but no branched matched or branch is not active');
return done(null);
}
//TODO add postcomit and postpull calling
function startCommitJob(payload, project, emitter, done) {
debug('starting commit job', payload, project.name);
var data = parseCommitData(payload);
return startJob(data, project, emitter, done);
}
function startPullrequestJob(payload, project, emitter, done) {
debug('starting pullrequest job', payload, project.name);
var data = parsePullRequestData(payload);
return startJob(data, project, emitter, done);
}
// 'TODO: this should be in a strider-lib module or something. Its
// needed by a lot of plugins (just copied from strider-github)
function makeJob(project, config) {
var now = new Date();
var deploy = false;
var branch = project.branch(config.branch) || {active: true, mirror_master: true, deploy_on_green: false};
if (!branch.active) return false;
if (config.branch !== 'master' && branch.mirror_master) {
// mirror_master branches don't deploy
deploy = false;
} else {
deploy = config.deploy && branch.deploy_on_green;
}
return {
type: deploy ? 'TEST_AND_DEPLOY' : 'TEST_ONLY',
trigger: config.trigger,
project: project.name,
ref: config.ref,
user_id: project.creator._id,
created: now
};
}
function resultHandler(client, emitter, jobInfo) {
function listener(data) {
emitter.removeListener('job.done', listener);
resultCommentor(client, jobInfo, data);
}
emitter.on('job.done', listener);
}
function resultCommentor(client, jobInfo, data) {
var urlTtoComment;
var phase = data.phases;
var key = data.id ? `job/${data.id}` : '';
var errorText = data.std.err.substr(data.std.err.length - 18);
var exitState = {
FAILED: 'FAILED',
SUCCESSFUL: 'SUCCESSFUL'
};
if (jobInfo.trigger === 'pullrequestJob') {
urlTtoComment = `${API}repositories/${jobInfo.repositoryFullName}/pullrequests/${jobInfo.id}/comments`;
if (phase.test.exitCode !== 0) {
client.post(urlTtoComment, {content: `:x: ${exitState.FAILED} ${jobInfo.projectUrl}${key}${errorText}`}, deleteCommentsByAuthor.bind(this, client, jobInfo));
} else if (phase.deploy.finished!==null && phase.deploy.exitCode !== 0) {
client.post(urlTtoComment, {content: `:x: ${exitState.FAILED} ${jobInfo.projectUrl}${key}${errorText}`}, deleteCommentsByAuthor.bind(this, client, jobInfo));
} else if (phase.test.exitCode === 0 && (phase.deploy.finished===null || phase.deploy.exitCode === 0)) {
client.post(urlTtoComment, {content: `:star: ${exitState.SUCCESSFUL} ${jobInfo.projectUrl}${key}`}, deleteCommentsByAuthor.bind(this, client, jobInfo));
}
} else if (jobInfo.trigger === 'commitJob') {
key = key || Math.random().toString(36).substr(2, 18);
urlTtoComment = `${API2}repositories${jobInfo.repositoryFullName}commit/${jobInfo.id}/statuses/build`;
if (phase.test.exitCode !== 0) {
client.post(urlTtoComment, {
key: key,
state: exitState.FAILED,
url: jobInfo.projectUrl + key,
description: exitState.FAILED + errorText
}, function () {
});
} else if (phase.deploy.finished!==null && phase.deploy.exitCode !== 0) {
client.post(urlTtoComment, {
key: key,
state: exitState.FAILED,
url: jobInfo.projectUrl + key,
description: exitState.FAILED + errorText
}, function () {
});
} else if (phase.test.exitCode === 0 && (phase.deploy.finished===null || phase.deploy.exitCode === 0)) {
client.post(urlTtoComment, {
key: key,
state: exitState.SUCCESSFUL,
url: jobInfo.projectUrl + key,
description: exitState.SUCCESSFUL
}, function () {
});
}
}
}
function deleteCommentsByAuthor(client, jobInfo, err, data) {
var author = JSON.parse(data).username;
client.get(`${API2}repositories/${jobInfo.repositoryFullName}/pullrequests/${jobInfo.id}/comments`, function (err, data) {
var commentArray = data.values;
commentArray
.filter(function (message) {
if (message.user.username === author) {
return Date.parse(message.created_on);
}
})
.forEach(function (item, i, arr) {
if (i < arr.length - 1) {
client.del(`${API}repositories/${jobInfo.repositoryFullName}/pullrequests/${jobInfo.id}/comments/${item.id}`, function () {
});
}
});
});
}