/
hipchat-trello-retrospectives.js
155 lines (146 loc) · 4.01 KB
/
hipchat-trello-retrospectives.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/***
#
# Description
# A hubot script that allows users to add cards to a trello board used to run
# project retrospectives
#
# Configuration:
# TRELLO_KEY
# TRELLO_TOKEN
# TRELLO_ORGANIZATION
#
# Commands:
# hubot retro: (text of issue) - Adds a card to the Trello board with the
# name of the current HipChat room within a specified Trello
# organization. If the board doesn't exist, the board is
# created and assigned lists representing commonly used
# retrospective categories.
# Notes:
#
# Author:
# Aaron Fritsch[@CASE Inc]
#
***/
var moment = require('moment');
var _ = require('lodash');
var async = require('async');
var request = require('request');
var Trello = require("node-trello");
var t = new Trello(process.env.TRELLO_KEY, process.env.TRELLO_TOKEN);
var retroColumns = [
"Uncategorized",
"Went Well",
"Needs to Change",
"Question & Discussion",
"Action Items"
];
//***** HUBOT RESPONSE *****
module.exports = function(robot) {
robot.respond(/retro: (.*)/i, function(msg) {
var cardText = msg.match[1],
room = msg.message.room;
checkIfBoardExists(room, function(boardId) {
if (boardId !== false) {
createCard(cardText, boardId, function(card) {
msg.send("Successfully added retrospective card: " +
card.shortUrl);
});
} else {
createBoard(room, function(boardId) {
createCard(cardText, boardId, function(card) {
msg.send(
"Successfully added retrospective card: " +
card.shortUrl);
});
});
}
});
});
}
//***** HELPER FUNCTIONS *****
function createBoard(room, callback) {
t.post("/1/boards", {
idOrganization: process.env.TRELLO_ORGANIZATION,
name: room,
prefs_permissionLevel: "org"
}, function(err, data) {
if (err) throw err;
var boardId = data.id;
async.series(
[
function(subcallback) {
removeDefaultLists(boardId, subcallback);
},
function(subcallback) {
addRetroLists(boardId, retroColumns, subcallback);
}
], function(err, results) {
callback(boardId);
});
});
return false;
}
function removeDefaultLists(boardId, callback) {
t.get("/1/boards/" + boardId + "/lists", function(listsErr, lists) {
if (listsErr) throw lists_err;
async.eachSeries(lists, function(list, subcallback) {
t.put("/1/lists/" + list.id + "/closed", {
value: true
}, function(listClosedErr) {
if (listClosedErr) throw listClosedErr;
subcallback();
});
}, function(asyncError) {
if (asyncError) throw asyncError;
callback();
});
});
}
function addRetroLists(boardId, columns, callback) {
async.eachSeries(columns, function(column, subcallback) {
t.post("/1/lists/", {
name: column,
idBoard: boardId,
pos: 'bottom'
}, function(newListErr, newList) {
if (newListErr) throw newListErr;
subcallback();
});
}, function(asyncError) {
if (asyncError) throw asyncError;
callback();
});
}
function checkIfBoardExists(roomName, callback) {
t.get("/1/organizations/" + process.env.TRELLO_ORGANIZATION + "/boards", {
filter: "open"
}, function(err, data) {
if (err) throw err;
var boardId = false;
async.eachSeries(data, function(board, subcallback) {
if (board.name == roomName) boardId = board.id;
subcallback();
}, function(boardErr) {
if (boardErr) throw boardErr;
callback(boardId);
});
});
}
function createCard(cardText, boardId, callback) {
t.get("1/boards/" + boardId + "/lists", {
filter: "open"
}, function(err, lists) {
if (err) throw err;
if (lists.length) {
t.post("1/cards/", {
due: null,
idList: lists[0].id,
urlSource: null,
name: cardText
}, function(cardErr, card) {
if (cardErr) throw cardErr;
callback(card);
})
}
})
}