Skip to content

Commit

Permalink
Merge pull request #32 from totem/feature_slack-notifications
Browse files Browse the repository at this point in the history
Slack Notifications
Verified success and failed conditions. Looks good.
  • Loading branch information
sukrit007 committed Jul 20, 2016
2 parents 2371baf + dc14e19 commit efd3d9d
Show file tree
Hide file tree
Showing 5 changed files with 479 additions and 17 deletions.
15 changes: 11 additions & 4 deletions lib/factory.js
Expand Up @@ -23,6 +23,7 @@ var winston = require('winston'),
path = require('path'),
PostHookNotifier = require('./notification/posthook'),
HipchatNotifier = require('./notification/hipchat'),
SlackNotifier = require('./notification/slack'),
nunjucks = require('nunjucks'),
_ = require('lodash');

Expand Down Expand Up @@ -72,6 +73,10 @@ var DEFAULT_CONFIG = {
token: process.env.HIPCHAT_TOKEN,
room: process.env.HIPCHAT_ROOM
},
slack: {
url: process.env.SLACK_URL,
room: process.env.SLACK_ROOM
},
baseUrl: process.env.BASE_URL || 'http://localhost:8080',
env: process.env.TOTEM_ENV || 'local'
};
Expand All @@ -86,7 +91,7 @@ function Factory(config) {

// Merge in custom config with defaults
_.defaults(config, DEFAULT_CONFIG);

// Store reference to current object
var _this = this;

Expand Down Expand Up @@ -115,17 +120,20 @@ function Factory(config) {
env: _this.config.env,
id: job.id
});
notifyCtx.shortCommit = notifyCtx.commit.substring(0, 7);

_.forEach(_this.notifiers, function(notifier) {
notifier.notify(isSuccessful, notifyCtx, image);
});

});
}, this.config.concurrentJobs); // Limit the number of concurrent jobs

// Notification for job
this.notifiers = [
new PostHookNotifier(config.hook),
new HipchatNotifier(config.hipchat)
new HipchatNotifier(config.hipchat),
new SlackNotifier(config.slack)
];

// Initialize the API
Expand Down Expand Up @@ -424,4 +432,3 @@ Factory.prototype.createJob = function createJob(req, res, context, next) {
return next(new restify.InternalError(e.message));
}
};

59 changes: 59 additions & 0 deletions lib/notification/slack.js
@@ -0,0 +1,59 @@
/*!
* Docker Image Factory - Notification module
*
*/

'use strict';

var winston = require('winston'),
Slack = require('node-slack'),
nunjucks = require('nunjucks');

module.exports = SlackNotifier;

function SlackNotifier(cfg) {
this.cfg = cfg || {};
this.slack = new Slack(cfg.url);
}

/**
* Returns true if notification is enabled
*/
SlackNotifier.prototype.isEnabled = function () {
return (this.cfg.url) ? true : false;
};

SlackNotifier.prototype.notify = function (isSuccessful, ctx) {
var cfg = this.cfg;
if (this.isEnabled() && !isSuccessful) {
var channel = cfg.room;
this.sendMessage('slack.json', ctx, channel);
}
};

/**
* Creates the context object to send the
* template engine and then renders the template
*/
SlackNotifier.prototype.sendMessage = function(templateName, ctx, channel) {
var _this = this;
winston.debug('Posting slack message to ' + channel + ' channel');

// Context object with any addition notification details
var obj = {
ctx: ctx,
notification: {
channel: channel,
date: Date.now() / 1000
}
};

// Render the template and send the message
nunjucks.render(templateName, obj, function(err, resp) {
if(!err) {
_this.slack.send(JSON.parse(resp));
} else {
winston.error('Failed to render template. Reason: ' + err.message);
}
});
};

0 comments on commit efd3d9d

Please sign in to comment.