Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor bb notify and add inprogress #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 65 additions & 30 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 : '';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure on the keys..need testing.


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*/ });
}
}

Expand All @@ -360,3 +391,7 @@ function deleteCommentsByAuthor(client, jobInfo, err, data, res) {
});
});
}

function makeId() {
return Math.random().toString(36).substr(2, 18);
}