Skip to content

Commit

Permalink
More progress and remove stuff we don't need anymore.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kami committed Oct 20, 2011
1 parent 2cc7282 commit 05448bd
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 115 deletions.
198 changes: 83 additions & 115 deletions lib/core.js
Expand Up @@ -26,8 +26,8 @@ util.inherits(BuildbotGithub, EventEmitter);

BuildbotGithub.prototype._initialize = function() {
this._github = new GitHubApi(true);
this._github.authenticate(this._options['github']['username'],
this._options['github']['token']);
this._github.authenticateToken(this._options['github']['username'],
this._options['github']['token']);

// Set up server which listens for webhook events
this._server = new server.WebServer(this._options['webserver']['ip'],
Expand All @@ -37,7 +37,7 @@ BuildbotGithub.prototype._initialize = function() {

// Set up poller
this._buildbotPoller = new pollers.BuildbotPoller(this._options['buildbot'],
this._options['general']['buildbot_poll_interval']);
this._options['buildbot']['poll_interval']);

// Set up Buildbot instance
this._buildbot = new BuildBot(this._options['buildbot']['host'],
Expand All @@ -49,8 +49,8 @@ BuildbotGithub.prototype._initialize = function() {
this._options['buildbot']['builder_name']);

// Register change handlers
this._buildbotPoller.on('new_build', this._handleNewBuild);
this._server.on('trigger_comment', this._handleTriggerComment);
this._buildbotPoller.on('new_build', this._handleNewBuild.bind(this));
this._server.on('trigger_comment', this._handleTriggerComment.bind(this));
};

BuildbotGithub.prototype.start = function() {
Expand All @@ -65,103 +65,89 @@ BuildbotGithub.prototype.stop = function() {
this._buildbotPoller.stop();
};

BuildbotGithub.prototype._handleNewPullRequest = function(pullRequest) {
// Check if we need to trigger force build for this request
var self = this;
var id = pullRequest['number'];

function handleGotPullRequestDiscussion(err, pull) {
var discussion, forceBuildNeeded;

if (err) {
log.error('Error while retrieving pull request #${id} discussions: ${err}',
{'id': id, 'err': err.message});
return;
}
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');

discussion = pull.discussion;
forceBuildNeeded = self._forceBuildNeeded(discussion);
key = sprintf('%s-%s', pullId, revision);

if (!forceBuildNeeded) {
log.info('Force build for request #${id} is not needed', {'id': id});
return;
}
// Check if there are any pending builds with this key
requestCache = this._pullCache[key];

log.info('New pull request has been found (#${id})', {'id': id});
self._handleGotNewPullRequest(pull);
if (!requestCache) {
log.info('No pending builds for pull request #${id}', {'id': pullId,
'key': key});
return;
}

this._github.getPullApi().getDiscussion(this._options['github']['user'],
this._options['github']['project'],
id, handleGotPullRequestDiscussion);
requestCache['build_pending'] = false;
this._postPullRequestComment(build, pullId, key);
};

BuildbotGithub.prototype._handleGotNewPullRequest = function(pullRequest) {
var requestCache;
var pullId = pullRequest['number'];
var pullHash = utils.getPullRequestHash(pullId, pullRequest['head']['repository']['sha']);
var user = pullRequest['user']['login'];
var branch = pullRequest['head']['repository']['ref'];

requestCache = this._pullCache[pullHash];

if (!requestCache) {
this._pullCache[pullHash] = {
'id': pullId,
'build_pending': true,
'build_forced': false,
'comment_posted': false
};
}

if (requestCache['build_pending'] || requestCache['build_forced']) {
log.info('Build has already been forced for pull request #${id}', {'id': pullId});
return;
}
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 user = data.head.user.name.replace(/[^a-zA-Z0-9-._~\s]/g, ''), // Buildbot doesn't like unicode
who = sprintf('%s <%s>', user, data.head.user.email), // Person who opened a pull request
id = data.number,
revision = data.head.sha,
branch = data.head.ref;

log.info('Sending changes to buildbot', {'id': id, 'revision': revision,
'who': who,
'branch': branch});
self._buildbot.sendChanges(id, revision, who,
self._options['github']['project'],
self._options['github']['repository'],
branch, callback);
}
],

function onEnd(err, body) {
function(err) {
if (err) {
log.error('Failed to notify buildbot about new pull request: ${err}',
{'err': err.message});
requestCache['build_pending'] = false;
log.error('Sending changes failed: ${err}', {'err': err.toString()});
return;
}

requestCache['build_forced'] = true;
}

this._buildbot.sendChanges(pullId, pullHash, user,
this._options['github']['project'],
this._options['github']['repository'],
branch,
onEnd);
};

BuildbotGithub.prototype._handleNewBuild = function(build) {
var properties, pullId, builderPullHash, requestCache;
properties = build.properties;
pullId = utils.getPropertyValue(properties, 'pull-id');
builderPullHash = utils.getPropertyValue(properties, 'pull-hash');

// Check if there are any pending builds with this pull hash
requestCache = this._pullCache[builderPullHash];

if (!requestCache) {
log.info('No pending builds for pull request #${id}', {'pull-id': pullId});
return;
}

requestCache['build_pending'] = false;
this._postPullRequestComment(build, builderPullHash);
};

BuildbotGithub.prototype._handleTriggerComment = function() {
log.info('Successfully sent changes to buildbot');
});
};

BuildbotGithub.prototype._postPullRequestComment = function(build, pullId,
pullHash) {
var requestCache, commentString, commentBody;
requestCache = this._pullCache[pullHash];
key) {
var self = this,
requestCache, commentString, commentBody;
requestCache = this._pullCache[key];

function getTemplateObj() {
var text, buildNumber, buildStatus;
Expand All @@ -177,22 +163,24 @@ BuildbotGithub.prototype._postPullRequestComment = function(build, pullId,
}

var urlObj = {
protocol: (this._options['buildbot']['secure']) ? 'https:' : 'http',
host: this._options['buildbot']['host'],
port: this._options['buildbot']['port'],
protocol: (self._options['buildbot']['secure']) ? 'https:' : 'http',
host: self._options['buildbot']['host'],
port: self._options['buildbot']['port'],
query: '',
pathname: sprintf('/builders/%s/builds/%s',
this._options['buildbot']['builder_name'], buildNumber)
self._options['buildbot']['builder_name'], buildNumber)
};

var templateObj = {
'branch': build['sourceStamp']['branch'],
'blame': build['blame'],
'blame': build['blame'][0],
'number': buildNumber,
'builder_name': build['builderName'],
'status': buildStatus,
'build_url': url.format(urlObj)
};

return templateObj;
}

log.info('Posting comment with build results to pull request #${id}',
Expand All @@ -207,40 +195,20 @@ BuildbotGithub.prototype._postPullRequestComment = function(build, pullId,
}

commentBody = utils.applyFormatting(commentString, templateObj);
this._github.getIssueApi().addComment(this._options['github']['user'],
this._options['github']['project'],
this._github.getIssueApi().addComment(self._options['github']['user'],
self._options['github']['project'],
pullId, commentBody,
function(err, body) {
if (err) {
log.error('Posting comment to pull request #${id} failed: ${err}',
{'id': pullId, 'err': err.message});
{'id': pullId, 'err': err.toString()});
return;
}

// Mark as completed
log.info('Successfully posted comment to pull request #${id}: ${body}',
{'id': pullId, 'body': commentBody});
requestCache['comment_posted'] = true;
});
};

BuildbotGithub.prototype._forceBuildNeeded = function(discussion) {
// Goes through all the pull request comments and try to find out if a build
// request for this pull request is needed.
var i, discussionLen, entry;

discussionLen = discussion.length;
for (i = 0; i < discussionLen; i++) {
entry = discussion[i];

if (entry['type'] === 'IssueComment' &&
(entry['user']['login'] === this._options['github']['username'])) {
// Found my comment, build has already been forced, bailing out
return false;
}

return true;
}
};

exports.BuildbotGithub = BuildbotGithub;
2 changes: 2 additions & 0 deletions lib/pollers.js
Expand Up @@ -95,6 +95,8 @@ BuildbotPoller.prototype._pollForChanges = function() {
body = JSON.parse(body);
}
catch (err) {
log.error('Failed to parse buildbot response: ${err}',
{'err': err.toString()});
return;
}

Expand Down

0 comments on commit 05448bd

Please sign in to comment.