-
Notifications
You must be signed in to change notification settings - Fork 9
/
core.js
217 lines (176 loc) · 6.99 KB
/
core.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
var util = require('util');
var url = require('url');
var EventEmitter = require('events').EventEmitter;
var GitHubApi = require('./extern/node-github').GitHubApi;
var async = require('async');
var sprintf = require('sprintf').sprintf;
var log = require('logmagic').local('buildbot-github.core');
var BuildBot = require('./buildbot').BuildBot;
var utils = require('./utils');
var pollers = require('./pollers');
var server = require('./server');
function BuildbotGithub(options) {
this._options = options;
this._buildbotPoller = null;
this._server = null;
this._pullCache = {};
}
util.inherits(BuildbotGithub, EventEmitter);
BuildbotGithub.prototype._initialize = function() {
var options;
this._github = new GitHubApi(false);
this._github.authenticateToken(this._options['github']['username'],
this._options['github']['token']);
// Set up server which listens for webhook events
options = utils.merge({'username': this._options['github']['username'],
'trigger_string': this._options['general']['trigger_string']},
this._options['webserver']);
this._server = new server.WebServer(options);
// Set up poller
this._buildbotPoller = new pollers.BuildbotPoller(this._options['buildbot'],
this._options['buildbot']['poll_interval']);
// Set up Buildbot instance
this._buildbot = new BuildBot(this._options['buildbot']);
// Register change handlers
this._buildbotPoller.on('new_build', this._handleNewBuild.bind(this));
this._server.on('trigger_comment', this._handleTriggerComment.bind(this));
};
BuildbotGithub.prototype.start = function() {
this._initialize();
this._server.start();
this._buildbotPoller.start();
};
BuildbotGithub.prototype.stop = function() {
this._server.stop();
this._buildbotPoller.stop();
};
BuildbotGithub.prototype._handleNewBuild = function(build) {
var properties, pullId, revision, key, requestCache;
properties = build.properties || [];
pullId = utils.getPropertyValue(properties, 'pull-request-id');
revision = utils.getPropertyValue(properties, 'revision');
key = sprintf('%s-%s', pullId, revision);
if (!pullId || !revision) {
// Not triggered by us.
return;
}
// Check if there are any pending builds with this key
requestCache = this._pullCache[key];
if (!requestCache || !requestCache['build_pending']) {
log.infof('No pending builds for pull request #${id}', {'id': pullId,
'key': key});
return;
}
requestCache['build_pending'] = false;
this._postPullRequestComment(build, pullId, key);
};
BuildbotGithub.prototype._handleTriggerComment = function(payload) {
var self = this,
pullId = payload.issue.number;
async.waterfall([
function getDiscussion(callback) {
// Pretty lame way to retrieve pull request info but atm I don't want t
// deal with v3 api.
self._github.getPullApi().getDiscussion(self._options['github']['user'],
self._options['github']['project'],
pullId, callback);
},
function populateCache(data, callback) {
// TODO: Redis, kktnxbye
var revision = data.head.sha, requestCache,
key = sprintf('%s-%s', pullId, revision);
if (!self._pullCache.hasOwnProperty(key)) {
self._pullCache[key] = {
'id': pullId,
'build_pending': true
};
}
requestCache = self._pullCache[key];
requestCache['updated'] = parseInt(new Date().getTime() / 1000, 10);
callback(null, data);
},
function sendChanges(data, callback) {
var id = data.number,
user = data.user,
revision = data.head.sha,
branch = data.head.ref;
log.infof('Sending changes to buildbot', {'id': id, 'revision': revision,
'branch': branch});
self._buildbot.sendChanges(id, revision, user,
self._options['github']['project'],
self._options['github']['repository'],
branch, callback);
}
],
function(err) {
if (err) {
log.errorf('Sending changes failed: ${err}', {'err': err)});
return;
}
log.infof('Successfully sent changes to buildbot');
});
};
BuildbotGithub.prototype._postPullRequestComment = function(build, pullId,
key) {
var self = this,
requestCache, commentString, commentBody;
requestCache = this._pullCache[key];
function getTemplateObj() {
var text, buildNumber, buildStatus, rand, image;
buildNumber = build['number'];
text = build['text'].join('');
if (text.toLowerCase().indexOf('failed') !== -1) {
buildStatus = 'failure';
rand = utils.getRandomInt(0, self._options['templates']['images_failure'].length - 1);
image = self._options['templates']['images_failure'][rand];
}
else {
buildStatus = 'success';
rand = utils.getRandomInt(0, self._options['templates']['images_success'].length - 1);
image = self._options['templates']['images_success'][rand];
}
var urlObj = {
protocol: (self._options['buildbot']['secure']) ? 'https:' : 'http',
host: self._options['buildbot']['host'],
port: self._options['buildbot']['port'],
query: '',
pathname: sprintf('/builders/%s/builds/%s',
self._options['buildbot']['builder_name'], buildNumber)
};
var templateObj = {
'branch': build['sourceStamp']['branch'],
'blame': build['blame'][0],
'nickname': utils.getPropertyValue(build['properties'],
'github-nickname'),
'number': buildNumber,
'builder_name': build['builderName'],
'status': buildStatus,
'build_url': url.format(urlObj),
'image': image
};
return templateObj;
}
log.infof('Posting comment with build results to pull request #${id}',
{'id': pullId});
var templateObj = getTemplateObj();
if (templateObj['status'] === 'success') {
commentString = this._options['templates']['comment_success'];
}
else {
commentString = this._options['templates']['comment_failure'];
}
commentBody = utils.applyFormatting(commentString, templateObj);
this._github.getIssueApi().addComment(self._options['github']['user'],
self._options['github']['project'],
pullId, commentBody,
function(err, body) {
if (err) {
log.errorf('Posting comment to pull request #${id} failed: ${err}',
{'id': pullId, 'err': err.toString()});
return;
}
log.infof('Successfully posted comment to pull request #${id}: ${body}',
{'id': pullId, 'body': commentBody});
});
};
exports.BuildbotGithub = BuildbotGithub;