forked from GezimSejdiu/Rocket.Chat-Trello-Integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trello.js
212 lines (181 loc) · 9.31 KB
/
Trello.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* exported Script */
/* globals console, _, s */
/** Global Helpers
*
* console - A normal console instance
* _ - An underscore instance
* s - An underscore string instance
*/
class Script {
/**
* @params {object} request
*/
process_incoming_request({ request }) {
// request.url.hash
// request.url.search
// request.url.query
// request.url.pathname
// request.url.path
// request.url_raw
// request.url_params
// request.headers
// request.user._id
// request.user.name
// request.user.username
// request.content_raw
// request.content
// console is a global helper to improve debug
console.log(request.content);
var attachment_text = "";
var title_text = "";
var url = request.content.model.shortUrl;
var author_name = request.content.action.memberCreator.fullName;
var card_name = request.content.action.data.card.name;
var card_link = request.content.action.data.card.shortLink;
var board_link = request.content.action.data.board.shortLink;
var board_name = request.content.action.data.board.name;
var user_avatar = "https://trello-avatars.s3.amazonaws.com/"+request.content.action.memberCreator.avatarHash +"/170.png";
var message_text = author_name;
/** This is used to prevent multiple submissions from Trello */
if (request.content.model.name == "Exergo") return "";
/** If a Trello card is updated, created or deleted */
if (request.content.action.type == "updateCheckItemStateOnCard" ||
request.content.action.type == "deleteCheckItem" || request.content.action.type == "createCheckItem") {
if (request.content.action.type == "updateCheckItemStateOnCard") {
if (request.content.action.data.checkItem.state == "incomplete") message_text += " unchecked ";
else message_text += " checked ";
message_text += " a checklist item\n";
}
else if (request.content.action.type == "deleteCheckItem") {
message_text += " deleted a checklist item";
}
else if (request.content.action.type == "createCheckItem") {
message_text += " created a checklist item";
}
title_text = card_name;
attachment_text = "\nItem: " + request.content.action.data.checkItem.name;
attachment_text += "\nChecklist: " + request.content.action.data.checklist.name;
attachment_text += "\nCard: " + card_name;
}
/** If a check list is added to a card */
else if (request.content.action.type == "addChecklistToCard") {
message_text += " added a checklist to card";
title_text = card_name;
attachment_text += "\nChecklist: " + request.content.action.data.checklist.name;
attachment_text += "\nCard: " + card_name;
}
/** If a checklist is removed from a card */
else if (request.content.action.type == "removeChecklistFromCard") {
message_text += " removed a checklist from card";
title_text = card_name;
attachment_text += "\nChecklist: " + request.content.action.data.checklist.name;
attachment_text += "\nCard: " + card_name;
}
/** If a card is created */
else if (request.content.action.type == "createCard") {
var list_name = request.content.action.data.list.name;
//message_text += " added a new card to the list: " + request.content.model.name;
//message_text += ' added a new card "['+card_name+'](https://trello.com/c/'+card_link+')" to the list ['+list_name+'](https://trello.com/b/'+board_link+')';
message_text = " New card ["+card_name+"](https://trello.com/c/"+card_link+") added to list ["+list_name+"](https://trello.com/b/"+board_link+")"
url = "https://trello.com/c/"+request.content.action.data.card.shortLink;
title_text = card_name;
}
/** If a card is deleted */
else if (request.content.action.type == "deleteCard") {
var list_name = request.content.action.data.list.name;
message_text = "Card [#"+request.content.action.data.card.idShort+"](https://trello.com/c/"+card_link+") deleted from list ["+list_name+"](https://trello.com/b/"+board_link+")"
//message_text += " deleted a card";
url = "https://trello.com/c/"+request.content.action.data.card.shortLink;
title_text = "";
}
/** If a card is updated */
else if (request.content.action.type == "updateCard") {
var data = request.content.action.data;
//if data.get('listAfter') and data.get('listBefore'):
if(data.listBefore){//moveCardToList
//var list_name = request.content.action.data.list.name;
message_text = 'Card moved: "['+card_name+'](https://trello.com/c/'+card_link+')" from list "' + data.listBefore.name + "' to '" + data.listAfter.name + "'";
//request.content.action.data.listBefore.name + "' to '" + request.content.action.data.listAfter.name + "'";
}
else if(data.old.name){//renameCard
var card_old_name = data.old.name;
message_text = 'Card renamed from "'+card_old_name+'" to "['+card_name+'](https://trello.com/c/'+card_link+')"';
}
else if(data.old.desc != data.card.desc){//renameCardDesc
var card_desc = data.card.desc;
var old_desc = data.old.desc;
if(old_desc!=""){
card_desc ='from "' + old_desc + '" to "' + card_desc +'"';
}
message_text = 'Card updated: "['+card_name+'](https://trello.com/c/'+card_link+')"\n**Description**: '+ card_desc +'';
//message_text = JSON.stringify(request.content.action.data) +'Card renamed from "'+card_old_name+'" to "['+card_name+'](https://trello.com/c/'+card_link+')"'
}
else if(data.card.due!=data.old.due){//updateCardDueDate
var card_due = data.card.due;
var due_action = card_due;
if(due_action==null)
due_action = 'Removed';//removeCardDueDate
message_text ='Card updated: "['+card_name+'](https://trello.com/c/'+card_link+')"\n**Due Date**: '+due_action+''
}
else if(data.card.closed ==true){//archiveCard
message_text ='Card archived: "['+card_name+'](https://trello.com/c/'+card_link+')"'
}
else if (data.card.closed==false){//unarchiveCard
message_text ='Card unarchived: "['+card_name+'](https://trello.com/c/'+card_link+')"'
}
}
else if (request.content.action.type == "commentCard"){
var data = request.content.action.data;
message_text = '**New comment on card "['+card_name+'](https://trello.com/c/'+card_link+')" by '+author_name+'**'
attachment_text += "-----------------------------------------------------------\n" +data.text
}
else if (request.content.action.type == "moveCardFromBoard"){
var data = request.content.action.data;
var board_target_name = data.boardTarget.name;
message_text = 'Card moved: "['+card_name+'](https://trello.com/c/'+card_link+')" moved from "['+board_name+'](https://trello.com/b/'+board_link+') to board "'+board_target_name+'"'
}
else if (request.content.action.type == "moveCardToBoard"){
var data = request.content.action.data;
var board_source_name = data.boardSource.name;
message_text = 'Card moved: "['+card_name+'](https://trello.com/c/'+card_link+')" moved to "['+board_name+'](https://trello.com/b/'+board_link+') from board "'+board_source_name+'"'
}
else if (request.content.action.type == "addMemberToCard"){
var data = request.content.action.data;
var member = request.content.action.member.fullName;
var member_url =request.content.action.member.username
message_text = 'New member ['+member+'](https://trello.com/'+member_url+') added to card "['+card_name+'](https://trello.com/c/'+card_link+')"'
}
else if (request.content.action.type == "removeMemberFromCard"){
var data = request.content.action.data;
var member = request.content.action.member.fullName;
var member_url =request.content.action.member.username
message_text = 'Member ['+member+'](https://trello.com/'+member_url+') is removed from card "['+card_name+'](https://trello.com/c/'+card_link+')"'
}
else if (request.content.action.type == "addAttachmentToCard"){
var data = request.content.action.data;
var attachment_name = data.attachment.name;
var attachment_url = data.attachment.url
var attachment_preview_url = data.attachment.previewUrl
message_text ='New attachment added to card "['+card_name+'](https://trello.com/c/'+card_link+')"\n **['+attachment_name+']('+attachment_url+')**'
}
if (url == undefined || message_text == author_name) return;
/** For debugging purpose. It displays the entire message contents sent by Trello */
//message_text = JSON.stringify(request.content);
return {
content:{
alias: author_name,
icon_url:user_avatar,
//text: message_text,
"attachments": [{
"color": "#FF0000",
//"author_name": request.content.action.memberCreator.avatarHash,
//"author_link": url,
// "author_icon": request.content.action.memberCreator.avatarHash,
"title": request.content.model.name,
"title_link": request.content.model.shortUrl,
"text": message_text +"\n" +attachment_text
}]
}
};
}
}