Skip to content

Instantly share code, notes, and snippets.

@gcpantazis
Created June 1, 2014 17:55
Show Gist options
  • Save gcpantazis/e35a1ac0116ffa045790 to your computer and use it in GitHub Desktop.
Save gcpantazis/e35a1ac0116ffa045790 to your computer and use it in GitHub Desktop.
Hubot watch for JIRA worklogs.
// Description:
// Watch methods for JIRA worklogs
//
// Commands:
// Hubot Set JIRA Worklogs Watch <query> - Have Hubot watch this JQL search for worklog changes.
// Hubot List JIRA Worklog Watches - Displays all the watches Hubot is managing for this channel.
// Hubot Clear JIRA Worklog Watches - Destroy all watches Hubot is managing for this channel.
/*jshint camelcase: false */
'use strict';
var JiraApi = require('jira').JiraApi;
var _ = require('underscore');
var jira = new JiraApi('https', process.env.JIRA_HOST, 443, process.env.JIRA_USERNAME, process.env.JIRA_PASSWORD, '2');
module.exports = function(robot) {
var lastCheckTime = new Date();
function getRoomsObject() {
return JSON.parse(robot.brain.get('JIRAWorklogWatchRooms')) || {};
}
function setRoomsObject(object) {
robot.brain.set('JIRAWorklogWatchRooms', JSON.stringify(object));
return;
}
// `searchStr` is in the FQL format, such as:
// project = SMTH AND resolution = Unresolved AND fixVersion = v0.2.0 AND updated >= -1d ORDER BY priority DESC
function checkForNewWorklogs(searchStr, channel) {
jira.searchJira(searchStr, {
maxResults: 1000,
fields: ['timetracking', 'worklog']
}, function(error, results) {
if (error) {
console.log(error);
return;
}
_.each(results.issues, function(task) {
if (task.fields.worklog) {
_.each(task.fields.worklog.worklogs, function(worklog) {
if (new Date(worklog.updated) > lastCheckTime) {
// Let's compile Hubot output.
var taskTime = task.fields.timetracking;
var output = '';
output += '/me : (' + worklog.author.name.replace(/\W/g, '') + ') ';
output += 'logged ' + worklog.timeSpent + ' on ' + task.key + '. ';
output += taskTime.remainingEstimate + ' ';
output += 'of ' + taskTime.originalEstimate + ' remaining.';
robot.adapter.connector.message(channel, output);
}
});
}
});
lastCheckTime = new Date();
});
}
// Nubot Bindings
// --------------
robot.respond(/Set JIRA Worklogs Watch (.*)/i, function(msg) {
var roomName = msg.message.user.reply_to;
var newSearch = msg.match[1];
var rooms = getRoomsObject();
rooms[roomName] = rooms[roomName] || [];
rooms[roomName].push(newSearch);
rooms[roomName] = _.uniq(rooms[roomName]);
setRoomsObject(rooms);
return msg.emote('added a JIRA worklog watch for this channel (thumbsup)');
});
robot.respond(/List JIRA Worklog Watches/i, function(msg) {
var rooms = getRoomsObject();
return msg.send(JSON.stringify(rooms[msg.message.user.reply_to]));
});
robot.respond(/Clear JIRA Worklog Watches/i, function(msg) {
var rooms = getRoomsObject();
delete rooms[msg.message.user.reply_to];
setRoomsObject(rooms);
return msg.emote('has cleared JIRA worklog watches for this channel (boom)');
});
// Interval Polling
// ----------------
setInterval(function() {
_.each(getRoomsObject(), function(queries, room) {
_.each(queries, function(query) {
checkForNewWorklogs(query, room);
});
});
}, 60 * 1000);
};
@gcpantazis
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment