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

Conversation

knownasilya
Copy link
Member

In response to #30
WIP.

Would like @nomean42 to test.

Ilya Radchenko added 2 commits May 5, 2016 08:14
}, 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.

@nomean42
Copy link
Contributor

nomean42 commented May 5, 2016

in a few days, if you dont mind

@knownasilya
Copy link
Member Author

No problem

@nomean42
Copy link
Contributor

nomean42 commented Jun 3, 2016

@knownasilya
"a few days" hmmmm, sorry :)
We really got some trouble with correct key cause there is no jobId in 'job.prepare' event data.
I think its unnecessary to look at unfinished job, so i suggest following code of prepareListener
from line 305 and down:

function resultHandler(client, emitter, jobInfo) {
  var key = makeId();
  var prepareListener = function (data) {
    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) {
    var id = data.id ? 'job/' + data.id : '';

    emitter.removeListener('job.done', doneListener);

    resultCommentor(client, jobInfo, data, key, id);
  };

  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, key, id) {
  var phase = data.phases;
  var message;
  var state;

  if (jobInfo.trigger === 'pullrequestJob') {
    if (phase.test.exitCode !== 0) {
      message = ':x: ' + buildState.FAILED + ' ' + jobInfo.projectUrl + id;
    } else if (phase.deploy.exitCode !== 0) {
      message = ':x: ' + buildState.FAILED + ' ' + jobInfo.projectUrl  + id;
    } else if (phase.test.exitCode === 0 && phase.deploy.exitCode === 0) {
      message = ':star: ' + buildState.SUCCESSFUL + ' ' + jobInfo.projectUrl + id;
    }

    notifyBitbucketBuild({
      content: message
    }, jobInfo, client, deleteCommentsByAuthor);
  } else if (jobInfo.trigger === 'commitJob') {

    if (phase.test.exitCode !== 0) {
      state = buildState.FAILED;
    } else if (phase.deploy.exitCode !== 0) {
      state = buildState.FAILED;
    } else if (phase.test.exitCode === 0 && phase.deploy.exitCode === 0) {
      state = buildState.SUCCESSFUL;
    }

    notifyBitbucketBuild({
      key: key,
      url: jobInfo.projectUrl + id,
      description: state,
      state: state
    }, jobInfo, client, function() { /*noop*/ });
  }
}

function deleteCommentsByAuthor(client, jobInfo, err, data, res) {
  var author = JSON.parse(data).username;

  client.get(API2 + 'repositories/' + jobInfo.repositoryFullName + '/pullrequests/' + jobInfo.id + '/comments', function(err, data, res) {
    var commentArray = data.values;
    commentArray.forEach(function(item, i) {
      if (item.user.username === author) {
        if (i < commentArray.length -1) {
          client.del(API + 'repositories/' + jobInfo.repositoryFullName + '/pullrequests/' + jobInfo.id + '/comments/' + item.id, function () {});
        }
      }
    });
  });
}

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

it works fine.

ps. sorry for that, i really dont want to make new pr

@knownasilya knownasilya self-assigned this Jun 3, 2016
@nomean42
Copy link
Contributor

@knownasilya please tell is there some progress with this stuff? looks like i got one more fix, but it would be better to do it after this refactor

@knownasilya
Copy link
Member Author

Sorry, I haven't had time to update the PR. I can make you a collaborator so you can work on this branch if you want?

@nomean42
Copy link
Contributor

I also have some troubles with time but lets try to do it together :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants