This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
forked from jontcarroll/trellomirror
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.coffee
571 lines (531 loc) · 22.1 KB
/
index.coffee
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
settings = require './settings'
Promise = require 'bluebird'
Neo4j = require 'rainbird-neo4j'
NodeTrello = require 'node-trello'
raygun = require 'raygun'
moment = require 'moment'
express = require 'express'
bodyParser = require 'body-parser'
Redis = require 'then-redis'
url = require 'url'
zipObject = require 'lodash.zipobject'
values = require 'lodash.values'
refbot = require './refbot'
Trello = Promise.promisifyAll new NodeTrello settings.TRELLO_API_KEY, settings.TRELLO_BOT_TOKEN
Neo = new Neo4j settings.NEO4J_URL
Neo.queryAsync = Promise.promisify Neo.query
Neo.execute = -> Neo.queryAsync.apply(Neo, arguments).then((res) -> res[0][0])
raygunClient = new raygun.Client().init(apiKey: settings.RAYGUN_APIKEY)
app = express()
app.use '/static', express.static('static')
app.use bodyParser.json()
app.get '/', (req, res) ->
res.status(200).json({status:"ok"})
matchCheckItem = (sourceCheckItem, targetCheckItems) ->
for checkItem in targetCheckItems
if checkItem.name == sourceCheckItem.name
return checkItem
return null
sendOk = (request, response) ->
console.log 'trello checks this endpoint when creating a webhook'
response.send 'ok'
app.get '/webhooks/trello-bot', sendOk # global
app.get '/webhooks/mirrored-card', sendOk # cardsync
app.get '/webhooks/tracked-board', sendOk # refbot
app.post '/webhooks/trello-bot', (request, response) ->
payload = request.body
console.log '- bot: ' + payload.action.type
response.send 'ok'
switch payload.action.type
#cardsync hook
when 'addMemberToCard'
# add webhook to this card
Trello.putAsync('/1/webhooks',
callbackURL: settings.SERVICE_URL + '/webhooks/mirrored-card'
idModel: payload.action.data.card.id
description: 'cardsync webhook for card https://trello.com/c/' + payload.action.data.card.id
).then((data) ->
console.log 'webhook created'
Neo.execute '''
MERGE (card:Card {shortLink: {SL}})
SET card.webhook = {WH}
MERGE (user:User {id: {USERID}})
MERGE (user)-[:OWNS]->(card)
WITH user, card
MERGE (user)-[:OWNS]->(i)-[:MIRRORS]->(source:Source {name: {NAME}})
MERGE (card)-[:MIRRORS]->(source)
WITH i WHERE i.shortLink IS NULL
MATCH (i)-[r]-()
DELETE i, r
''',
SL: payload.action.data.card.shortLink
NAME: payload.action.data.card.name
WH: data.id
USERID: payload.action.memberCreator.id
).then( ->
sourceShortLink = payload.action.data.card.shortLink;
sourceCardName = payload.action.data.card.name;
console.log "card '#{sourceShortLink}' added to db"
Trello.getAsync("/1/cards/#{sourceShortLink}/checklists").then((sourceChecklists) ->
console.log JSON.stringify("sourceChecklists = #{JSON.stringify(sourceChecklists)}")
Promise.resolve().then(->
Neo.execute '''
MATCH (original:Card {shortLink: {SL}})-[:MIRRORS]->(source:Source)
SET source.name = {NAME}
WITH source, original
MATCH (source)<-[:MIRRORS]-(target:Card)
WHERE target <> original
RETURN target, source
''',
SL: sourceShortLink
NAME: sourceCardName
).then((res) ->
targets = (row['target'].shortLink for row in res)
console.log '> will sync', targets
return targets
).map((target) ->
console.log "Syncing #{JSON.stringify(target)}"
Trello.getAsync("/1/cards/#{target}/checklists").then((targetChecklists) ->
console.log JSON.stringify("targetChecklists = #{JSON.stringify(targetChecklists)}")
updateChecklists = Promise.resolve().then( ->
[0...sourceChecklists.length ]
).map((i) ->
sourceChecklist = sourceChecklists[i]
targetChecklist = targetChecklists[i]
if !targetChecklist?
console.log "Skyped checklist! the list does not exist in target card."
updateChecklists.cancel
else
console.log "SYNCING CHECKLISTS #{sourceChecklist.id} to #{targetChecklist.id}"
Promise.resolve().then(->
Neo.execute '''
MATCH (original:Card {shortLink: {ORIG}})
MATCH (target:Card {shortLink: {TGT}})
MATCH (original)-[:MIRRORS]->(source:Source)<-[:MIRRORS]-(target)
MERGE (source)-[:HAS]->(chl:Checklist)-[:CORRESPONDS_TO {id: {OCLID}}]->(original)
MERGE (chl)-[:CORRESPONDS_TO {id: {TCLID}}]->(target)
''',
ORIG: sourceShortLink
TGT: target
OCLID: sourceChecklist.id
TCLID: targetChecklist.id
).then(->
for j in [0...sourceChecklist.checkItems.length]
sourceCheckItem = sourceChecklist.checkItems[j]
targetCheckItem = matchCheckItem(sourceCheckItem, targetChecklist.checkItems);
if targetCheckItem?
console.log "Syncing checkitems #{sourceCheckItem.id} to #{targetCheckItem.id}"
Neo.execute '''
MATCH (target:Card {shortLink: {TGT}})
MATCH (chl:Checklist)-[:CORRESPONDS_TO {id: {OCLID}}]-(original:Card)
MERGE (chl)-[:CONTAINS]->(chi:CheckItem)-[:CORRESPONDS_TO {id: {OCIID}}]->(original)
MERGE (chi)-[:CORRESPONDS_TO {id: {TCIID}}]->(target)
''',
TGT: target
OCLID: sourceChecklist.id
OCIID: sourceCheckItem.id
TCIID: targetCheckItem.id
else
console.log "Skyped checkitem! the item does not exist in target checklist."
)
)
)
)
)
).catch(console.log.bind console)
#cardsync hook
when 'removeMemberFromCard'
Promise.resolve().then(->
Neo.execute '''
MATCH (card:Card {shortLink: {SL}})
RETURN card
'''
, SL: payload.action.data.card.shortLink
).then((res) ->
card = res[0]['card']
Trello.delAsync '/1/webhooks/' + card.webhook
).then(->
console.log 'webhook deleted'
Neo.execute '''
MATCH (card:Card {shortLink: {SL}})<-[owsh:OWNS]-()
OPTIONAL MATCH (card)-[drel:HAS]->(direct)
OPTIONAL MATCH (direct)-[idrel:CONTAINS]-(indirect)
OPTIONAL MATCH (card)-[cr:MIRRORS]->(source:Source)
DELETE card, owsh, cr, idrel, drel, direct, indirect
WITH source
OPTIONAL MATCH (source)<-[or:MIRRORS]-(others:Card)
WITH source, others WHERE others IS NULL
DELETE source
''',
SL: payload.action.data.card.shortLink
).then(->
console.log 'card deleted from db'
).catch(console.log.bind console)
#refbot hook
when 'addMemberToBoard'
# add webhook to this board
Trello.putAsync('/1/webhooks',
callbackURL: settings.SERVICE_URL + '/webhooks/tracked-board'
idModel: payload.action.data.board.id
description: 'refbot webhook for this board'
).then((data) ->
console.log 'added to board', payload.action.data.board.name, 'webhook created'
).catch(console.log.bind console)
#refbot hook
when 'removeMemberFromBoard'
Promise.resolve().then(->
Trello.getAsync '/1/token/' + settings.TRELLO_BOT_TOKEN + '/webhooks'
).then((webhooks) ->
for webhook in webhooks
if webhook.idModel == payload.action.data.board.id
Trello.delAsync '/1/webhooks/' + webhook.id
).spread(->
console.log 'webhook deleted'
).catch(console.log.bind console)
app.post '/webhooks/mirrored-card', (request, response) ->
payload = request.body
action = payload.action
data = action.data
console.log 'card ' + payload.model.shortUrl + ': ' + payload.action.type
console.log JSON.stringify payload.action, null, 2
response.send 'ok'
if action.memberCreator.id == settings.TRELLO_BOT_ID
return
if action.type not in [
"addAttachmentToCard"
"addChecklistToCard"
"commentCard"
"createCheckItem"
"deleteAttachmentFromCard"
"deleteCard"
"deleteCheckItem"
"deleteComment"
"removeChecklistFromCard"
"updateCard"
"updateCheckItem"
"updateCheckItemStateOnCard"
"updateComment"
]
return
if action.type == "deleteCard"
Neo.execute '''
MATCH (card:Card {shortLink: {SL}})<-[owsh:OWNS]-()
OPTIONAL MATCH (card)-[drel:HAS]->(direct)
OPTIONAL MATCH (direct)-[idrel:CONTAINS]-(indirect)
OPTIONAL MATCH (card)-[cr:MIRRORS]->(source:Source)
DELETE card, owsh, cr, idrel, drel, direct, indirect
WITH source
OPTIONAL MATCH (source)<-[or:MIRRORS]-(others:Card)
WITH source, others WHERE others IS NULL
DELETE source
''',
SL: payload.model.shortUrl.split('/')[4]
return
Promise.resolve().then(->
Neo.execute '''
MATCH (original:Card {shortLink: {SL}})-[:MIRRORS]->(source:Source)
SET source.name = {NAME}
WITH source, original
MATCH (source)<-[:MIRRORS]-(target:Card)
WHERE target <> original
RETURN target, source
''',
SL: data.card.shortLink
NAME: data.card.name
).then((res) ->
targets = (row['target'].shortLink for row in res)
console.log '> will update', targets
return targets
).map((target) ->
switch action.type
when "updateCard"
changed = Object.keys(data.old)[0]
if changed in ['name', 'due', 'desc']
Trello.putAsync "/1/cards/#{target}/#{changed}"
, value: data.card[changed]
else if changed =='idAttachmentCover'
Promise.resolve().then(->
if not data.card.idAttachmentCover
return ''
return Neo.execute('''
MATCH (target:Card {shortLink: {TGT}})-->(source:Source)
MATCH (source)-[:HAS]->(att:Attachment)-[:CORRESPONDS_TO {id: {OAID}}]-()
MATCH (att)-[c:CORRESPONDS_TO]-(target)
RETURN c.id AS taid
''',
TGT: target
OAID: data.card.idAttachmentCover
).then((res) ->
return res[0]['taid']
)
).then((targetAttachmentId) ->
console.log 'taid', targetAttachmentId
Trello.putAsync "1/cards/#{target}/idAttachmentCover"
, value: targetAttachmentId
)
else if changed == "idList"
console.log "update list!"
newListName = data.listAfter.name
console.log "newListName = '#{newListName}'"
Trello.getAsync("1/cards/#{target}").then((card) ->
idBoard = card.idBoard
console.log "idBoard = #{idBoard}"
currentListId = card.idList
console.log "currentListId = #{currentListId}"
Trello.getAsync("1/boards/#{idBoard}/lists").then((lists) ->
newListId = null
console.log "lists = #{JSON.stringify(lists)}"
for index, list of lists
if list.name == newListName
newListId = list.id
console.log "newListId = #{newListId}"
if (newListId != null) && (newListId != currentListId)
console.log "Moving card from #{currentListId} to #{newListId}"
Trello.putAsync "/1/cards/#{target}/#{changed}", value: newListId
)
)
when "commentCard"
Promise.resolve().then(->
date = moment(action.date).format('MMMM Do YYYY, h:mm:ssa UTC')
text = '>' + data.text.replace /\n/g, '\n>'
Trello.postAsync "/1/cards/#{target}/actions/comments"
, text: """
[#{action.memberCreator.username}](https://trello.com/#{action.memberCreator.username}) on [#{date}](https://trello.com/c/#{data.card.shortLink})
#{text}
"""
).then((newComment) ->
Neo.execute '''
MATCH (original:Card {shortLink: {ORIG}})
MATCH (target:Card {shortLink: {TGT}})
MATCH (original)-[:MIRRORS]->(source:Source)<-[:MIRRORS]-(target)
MERGE (source)-[:HAS]->(comm:Comment)-[:CORRESPONDS_TO {id: {OCID}}]->(original)
MERGE (comm)-[:CORRESPONDS_TO {id: {TCID}}]->(target)
''',
ORIG: data.card.shortLink
TGT: target
OCID: action.id
TCID: newComment.id
)
when "updateComment"
Promise.resolve().then(->
Neo.execute '''
MATCH (target:Card {shortLink: {TGT}})-->(source:Source)
MATCH (source)-[:HAS]->(comm:Comment)-[:CORRESPONDS_TO {id: {OCID}}]-()
MATCH (comm)-[c:CORRESPONDS_TO]-(target)
RETURN c.id AS tcid
''',
TGT: target
OCID: data.action.id
).then((res) ->
return res[0]['tcid']
).then((targetCommentId) ->
date = moment(action.date).format('MMMM Do YYYY, h:mm:ssa UTC')
text = '>' + data.action.text.replace /\n/g, '\n>'
Trello.putAsync "/1/cards/#{target}/actions/#{targetCommentId}/comments"
, text: """
[#{action.memberCreator.username}](https://trello.com/#{action.memberCreator.username}) on [#{date}](https://trello.com/c/#{data.card.shortLink})
#{text}
"""
)
when "deleteComment"
Promise.resolve().then(->
Neo.execute '''
MATCH (target:Card {shortLink: {TGT}})-->(source:Source)
MATCH (source)-[:HAS]->(comm:Comment)-[:CORRESPONDS_TO {id: {OCID}}]-()
MATCH (comm)-[c:CORRESPONDS_TO]-(target)
RETURN c.id AS tcid
''',
TGT: target
OCID: data.action.id
).then((res) ->
return res[0]['tcid']
).then((targetCommentId) ->
Trello.delAsync "/1/cards/#{target}/actions/#{targetCommentId}/comments"
Neo.execute '''
MATCH (comm:Comment)-[c:CORRESPONDS_TO {id: {TCID}}]-(target)
DELETE c
WITH comm, target
OPTIONAL MATCH (comm)-[cr:CORRESPONDS_TO]-(cards:Card)
WHERE cr.id <> {OCID}
WITH cards, comm WHERE cards IS NULL
MATCH (comm)-[r]-()
DELETE comm, r
''',
OCID: data.action.id
TCID: targetCommentId
)
when "addAttachmentToCard"
Promise.resolve().then(->
Trello.postAsync "/1/cards/#{target}/attachments"
, {url: data.attachment.url, name: data.attachment.name}
).then((newAttachment) ->
Neo.execute '''
MATCH (original:Card {shortLink: {ORIG}})
MATCH (target:Card {shortLink: {TGT}})
MATCH (original)-[:MIRRORS]->(source:Source)<-[:MIRRORS]-(target)
MERGE (source)-[:HAS]->(att:Attachment)-[:CORRESPONDS_TO {id: {OAID}}]->(original)
MERGE (att)-[:CORRESPONDS_TO {id: {TAID}}]->(target)
''',
ORIG: data.card.shortLink
TGT: target
OAID: data.attachment.id
TAID: newAttachment.id
)
when "deleteAttachmentFromCard"
Promise.resolve().then(->
Neo.execute '''
MATCH (target:Card {shortLink: {TGT}})-->(source:Source)
MATCH (source)-[:HAS]->(att:Attachment)-[:CORRESPONDS_TO {id: {OAID}}]-()
MATCH (att)-[c:CORRESPONDS_TO]-(target)
RETURN c.id AS taid
''',
TGT: target
OAID: data.attachment.id
).then((res) ->
return res[0]['taid']
).then((targetAttachmentId) ->
Trello.delAsync "/1/cards/#{target}/attachments/#{targetAttachmentId}"
Neo.execute '''
MATCH (att:Attachment)-[c:CORRESPONDS_TO {id: {TAID}}]-(target)
DELETE c
WITH att, target
OPTIONAL MATCH (att)-[cr:CORRESPONDS_TO]-(cards:Card)
WHERE cr.id <> {OAID}
WITH cards, att WHERE cards IS NULL
MATCH (att)-[r]-()
DELETE att, r
''',
OAID: data.attachment.id
TAID: targetAttachmentId
)
when "addChecklistToCard"
Promise.resolve().then(->
Trello.postAsync "/1/cards/#{target}/checklists"
, name: data.checklist.name
).then((newChecklist) ->
Neo.execute '''
MATCH (original:Card {shortLink: {ORIG}})
MATCH (target:Card {shortLink: {TGT}})
MATCH (original)-[:MIRRORS]->(source:Source)<-[:MIRRORS]-(target)
MERGE (source)-[:HAS]->(chl:Checklist)-[:CORRESPONDS_TO {id: {OCLID}}]->(original)
MERGE (chl)-[:CORRESPONDS_TO {id: {TCLID}}]->(target)
''',
ORIG: data.card.shortLink
TGT: target
OCLID: data.checklist.id
TCLID: newChecklist.id
)
when "removeChecklistFromCard"
Promise.resolve().then(->
Neo.execute '''
MATCH (target:Card {shortLink: {TGT}})-->(source:Source)
MATCH (source)-[:HAS]->(chl:Checklist)-[:CORRESPONDS_TO {id: {OCLID}}]-()
MATCH (chl)-[c:CORRESPONDS_TO]-(target)
RETURN c.id AS tclid
''',
TGT: target
OCLID: data.checklist.id
).then((res) ->
return res[0]['tclid']
).then((targetChecklistId) ->
Trello.delAsync "/1/cards/#{target}/checklists/#{targetChecklistId}"
Neo.execute '''
MATCH (chl:Checklist)-[c:CORRESPONDS_TO {id: {TCLID}}]-(target)
DELETE c
WITH chl, target
OPTIONAL MATCH (chl)-[cr:CORRESPONDS_TO]-(cards:Card)
WHERE cr.id <> {OCLID}
WITH cards, chl WHERE cards IS NULL
MATCH (chl)-[r]-()
MATCH (chl)-[cir]-(chi:CheckItem)-[cisr]-()
DELETE chl, r, cir, chi, cisr
''',
OCLID: data.checklist.id
TCLID: targetChecklistId
)
when "createCheckItem"
checkids = Promise.resolve().then(->
Neo.execute '''
MATCH (target:Card {shortLink: {TGT}})-->(source:Source)
MATCH (source)-[:HAS]->(chl:Checklist)-[:CORRESPONDS_TO {id: {OCLID}}]-()
MATCH (chl)-[c:CORRESPONDS_TO]-(target)
RETURN c.id AS tclid
''',
TGT: target
OCLID: data.checklist.id
).then((res) ->
if res.length == 0
console.log "Checklist was not updated because it is not under sync"
return checkids.cancel();
else
return res[0]['tclid']
).then((targetChecklistId) ->
Trello.postAsync "/1/cards/#{target}/checklist/#{targetChecklistId}/checkItem"
, name: data.checkItem.name
).then((newCheckItem) ->
Neo.execute '''
MATCH (target:Card {shortLink: {TGT}})
MATCH (chl:Checklist)-[:CORRESPONDS_TO {id: {OCLID}}]-(original:Card)
MERGE (chl)-[:CONTAINS]->(chi:CheckItem)-[:CORRESPONDS_TO {id: {OCIID}}]->(original)
MERGE (chi)-[:CORRESPONDS_TO {id: {TCIID}}]->(target)
''',
TGT: target
OCLID: data.checklist.id
OCIID: data.checkItem.id
TCIID: newCheckItem.id
)
when "updateCheckItem", "updateCheckItemStateOnCard", "deleteCheckItem"
checkids = Promise.resolve().then(->
Neo.execute '''
MATCH (target:Card {shortLink: {TGT}})
MATCH (chi:CheckItem)-[:CORRESPONDS_TO {id: {OCIID}}]-(original:Card)
MATCH (chl:Checklist)-[:CONTAINS]->(chi)-[ctci:CORRESPONDS_TO]-(target)
MATCH (chl)-[ctcl:CORRESPONDS_TO]-(target)
RETURN ctcl.id AS tclid, ctci.id AS tciid
''',
OCIID: data.checkItem.id
TGT: target
).then((res) ->
if res.length == 0
console.log "Checklist was not updated because it is not under sync"
return checkids.cancel();
else
return [res[0]['tclid'], res[0]['tciid']]
)
switch payload.action.type
when "updateCheckItem"
checkids = checkids.spread((targetChecklistId, targetCheckItemId) ->
Trello.putAsync "/1/cards/#{target}/checklist/#{targetChecklistId}/checkItem/#{targetCheckItemId}/name"
, value: data.checkItem.name
)
when "updateCheckItemStateOnCard"
checkids = checkids.spread((targetChecklistId, targetCheckItemId) ->
Trello.putAsync "/1/cards/#{target}/checklist/#{targetChecklistId}/checkItem/#{targetCheckItemId}/state"
, value: data.checkItem.state
)
when "deleteCheckItem"
checkids = checkids.spread((targetChecklistId, targetCheckItemId) ->
Trello.delAsync "/1/cards/#{target}/checklist/#{targetChecklistId}/checkItem/#{targetCheckItemId}"
Neo.execute '''
MATCH (chi:CheckItem)-[c:CORRESPONDS_TO {id: {TCIID}}]-(target:Card)
DELETE c
WITH chi, target
OPTIONAL MATCH (chi)-[cr:CORRESPONDS_TO]-(cards:Card)
WHERE cr.id <> {OCIID}
WITH cards, chi WHERE cards IS NULL
MATCH (chi)-[r]-()
DELETE chi, r
''',
OCIID: data.checkItem.id
TCIID: targetCheckItemId
)
).then(->
console.log '> everything done.'
).catch((e) ->
raygunClient.send e, {'message': 'error catched in the .catch handler'}, (->), request
)
app.use raygunClient.expressHandler
port = process.env.PORT or 5000
refbot.addRefbotHooks app
app.listen port, '0.0.0.0', ->
console.log 'running at 0.0.0.0:' + port