diff --git a/lib/api.js b/lib/api.js index e37b84a..477ad53 100755 --- a/lib/api.js +++ b/lib/api.js @@ -1,12 +1,17 @@ -var superagent = require('superagent') - , gravatar = require('gravatar') - , crypto = require('crypto') - , async = require('async') - , debug = require('debug')('strider-bitbucket:api') +var superagent = require('superagent'); +var gravatar = require('gravatar'); +var crypto = require('crypto'); +var async = require('async'); +var debug = require('debug')('strider-bitbucket:api'); var API = 'https://bitbucket.org/api/1.0/'; var API2 = 'https://api.bitbucket.org/2.0/'; +var buildState = { + FAILED: 'FAILED', + SUCCESSFUL: 'SUCCESSFUL', + INPROGRESS: 'INPROGRESS' +}; module.exports = { parseRepo: parseRepo, @@ -298,51 +303,77 @@ function makeJob(project, config) { } function resultHandler(client, emitter, jobInfo) { - var listener = function(data) { - emitter.removeListener('job.done', listener); + var key; + var prepareListener = function (data) { + key = data.id ? 'job/' + data.id : ''; + emitter.removeListener('job.prepare', prepareListener); + + notifyBitbucketBuild({ + key: key, + url: jobInfo.projectUrl + key, + description: buildState.INPROGRESS, + state: buildState.INPROGRESS + }, jobInfo, client, function() { /*noop*/ }); + }; + var doneListener = function(data) { + key = data.id ? 'job/' + data.id : ''; + + emitter.removeListener('job.done', doneListener); - resultCommentor(client, jobInfo, data); + resultCommentor(client, jobInfo, data, key); }; - emitter.on('job.done', listener); + emitter.on('job.prepare', prepareListener); + emitter.on('job.done', doneListener); +} + +function notifyBitbucketBuild(content, jobInfo, client, cb) { + var buildUrl; + + if (jobInfo.trigger === 'pullrequestJob') { + buildUrl = API + 'repositories/' + jobInfo.repositoryFullName + '/pullrequests/' + jobInfo.id + '/comments'; + } else if (jobInfo.trigger === 'commitJob') { + buildUrl = API2 + 'repositories' + jobInfo.repositoryFullName + 'commit/' + jobInfo.id + '/statuses/build'; + } + + client.post(buildUrl, content, cb.bind(this, client, jobInfo)); } -function resultCommentor(client, jobInfo, data) { - var urlTtoComment; +function resultCommentor(client, jobInfo, data, key) { 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' - }; + var message; + var state; 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)); - + message = ':x: ' + buildState.FAILED + ' ' + jobInfo.projectUrl + key + errorText; } else if (phase.deploy.exitCode !== 0) { - client.post(urlTtoComment, {content: ':x: ' + exitState.FAILED + ' ' + jobInfo.projectUrl + key + errorText}, deleteCommentsByAuthor.bind(this, client, jobInfo)); - + message = ':x: ' + buildState.FAILED + ' ' + jobInfo.projectUrl + key + errorText; } else if (phase.test.exitCode === 0 && phase.deploy.exitCode === 0) { - client.post(urlTtoComment, {content: ':star: ' + exitState.SUCCESSFUL + ' ' + jobInfo.projectUrl + key}, deleteCommentsByAuthor.bind(this, client, jobInfo)); + message = ':star: ' + buildState.SUCCESSFUL + ' ' + jobInfo.projectUrl + key; } + notifyBitbucketBuild({ + content: message + }, jobInfo, client, deleteCommentsByAuthor); } else if (jobInfo.trigger === 'commitJob') { - key = key || Math.random().toString(36).substr(2, 18); - urlTtoComment = API2 + 'repositories' + jobInfo.repositoryFullName + 'commit/' + jobInfo.id + '/statuses/build'; + key = key || makeId(); if (phase.test.exitCode !== 0) { - client.post(urlTtoComment, {key: key, state: exitState.FAILED, url: jobInfo.projectUrl + key, description: exitState.FAILED + errorText}, function() {}); - + state = buildState.FAILED; } else if (phase.deploy.exitCode !== 0) { - client.post(urlTtoComment, {key: key, state: exitState.FAILED, url: jobInfo.projectUrl + key, description: exitState.FAILED + errorText}, function() {}); - + state = buildState.FAILED; } else if (phase.test.exitCode === 0 && phase.deploy.exitCode === 0) { - client.post(urlTtoComment, {key: key, state: exitState.SUCCESSFUL, url: jobInfo.projectUrl + key, description: exitState.SUCCESSFUL}, function() {}); + state = buildState.SUCCESSFUL; } + + notifyBitbucketBuild({ + key: key, + url: jobInfo.projectUrl + key, + description: errorText ? state + errorText : state, + state: state + }, jobInfo, client, function() { /*noop*/ }); } } @@ -360,3 +391,7 @@ function deleteCommentsByAuthor(client, jobInfo, err, data, res) { }); }); } + +function makeId() { + return Math.random().toString(36).substr(2, 18); +}