-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
321 lines (269 loc) · 8.35 KB
/
index.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
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
'use strict';
var path = require('path');
var fs = require('fs');
var pivotal = require('pivotal');
var log = require('npmlog');
var request = require('hyperquest');
var async = require('async');
var FormData = require('form-data');
var Trello = require('node-trello');
var PROXY = process.env.PROXY || '';
var VERBOSE = process.env.VERBOSE || false;
var trelloAPI = process.env.TRELLO_API || 'https://api.trello.com';
var requiredLists = ['accepted', 'delivered', 'rejected', 'finished', 'current', 'backlog', 'icebox'];
if(PROXY){
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
}
function organizeLists(trelloLists){
var list = trelloLists.reduce(function(a, list){
a[list.name.toLowerCase()] = list;
return a;
}, {});
return list;
}
function addChecklistsToCard(trello, cardId, items, name, cb){
// we have to create a checkilst itself before we can add items to it...
trello.post('/1/cards/'+cardId+'/checklists', function(err, checklist){
if(err){
return cb(err);
}
var tasks = [];
items.forEach(function(checkItem){
if(VERBOSE){
log.info('pivotal-to-trello', 'adding checkItem: %s for %s', checkItem.description, name);
}
var checkItemPayload = {
name: checkItem.description,
pos: checkItem.position,
idChecklist: checklist.id,
checked: checkItem.complete
};
var checkItemURI = '/1/checklists/'+checklist.id+'/checkItems';
tasks.push(function(cb){
trello.post(checkItemURI, checkItemPayload, cb);
});
});
async.parallel(tasks, function(err){
if(err){
return cb(err);
}
cb();
});
});
}
function addCommentsToCard(trello, cardId, comments, name, cb){
var tasks = [];
comments.forEach(function(comment){
if(VERBOSE){
log.info('pivotal-to-trello', 'adding comment: %s for %s', comment.text, name);
}
var commentPayload = {
text: comment.text
};
tasks.push(function(cb){
trello.post('/1/cards/'+cardId+'/comments', commentPayload, cb);
});
});
async.parallel(tasks, function(err){
if(err){
return cb(err);
}
cb();
});
}
function addAttachmentsToCard(key, token, pivotal, cardId, attachments, name, cb){
var attachmentsURI = trelloAPI+'/1/cards/'+cardId+'/attachments?key='+key+'&token='+token;
var tasks = [];
var makeAttachment = function(attachment, cb){
log.info('pivotal-to-trello', 'adding attachment: %s for %s', attachment.filename, name);
var fn = path.join('/tmp', path.basename(attachment.filename));
var s = fs.createWriteStream(fn);
s.on('error', function(err){
log.error('pivotal-to-trello', err);
});
s.on('close', function(){
fs.readFile(fn, function(err, data){
if(err){
return cb(err);
}
// the trello API is VERY pedantic about what it recieves and for
// some reason request wasn't doing it right, so we'll build
// the request using form-data and hyperquest ourselves
var form = new FormData();
form.append('name', attachment.filename);
form.append('file', data, {filename: fn});
var headers = form.getHeaders();
headers['content-length'] = form.getLengthSync();
var req = request(attachmentsURI, {
method: 'POST',
headers: headers
});
req.on('error', function(err){
log.error('pivotal-to-trello', 'Could not create attachment', err);
log.error('pivotal-to-trello', attachment);
return cb();
});
req.on('response', function(res){
if(res.statusCode !== 200){
log.error('pivotal-to-trello', 'Could not create attachment', res.statusCode);
res.pipe(process.stderr);
}
return cb();
});
form.pipe(req);
});
});
var pivotalRequest = request(attachment.url, {
headers: {
'X-TrackerToken': pivotal
}});
pivotalRequest.on('response', function(res){
res.pipe(s);
});
};
attachments.forEach(function(attachment){
tasks.push(makeAttachment.bind(null, attachment));
});
async.parallel(tasks, function(err){
if(err){
cb(err);
}
cb();
});
}
function attachCardData(trello, key, token, pivotal, story, card, cb){
var tasks, notes, attachments;
var asyncTasks = [];
if(story.tasks && story.tasks.task){
tasks = Array.isArray(story.tasks.task) ? story.tasks.task : [story.tasks.task];
asyncTasks.push(function(cb){
addChecklistsToCard(trello, card.id, tasks, story.name, cb);
});
}
if(story.notes && story.notes.note){
notes = Array.isArray(story.notes.note) ? story.notes.note : [story.notes.note];
asyncTasks.push(function(cb){
addCommentsToCard(trello, card.id, notes, story.name, cb);
});
}
if(story.attachments && story.attachments.attachment){
attachments = Array.isArray(story.attachments.attachment) ? story.attachments.attachment : [story.attachments.attachment];
asyncTasks.push(function(cb){
addAttachmentsToCard(key, token, pivotal, card.id, attachments, story.name, cb);
});
}
async.parallel(asyncTasks, function(err){
if(err){
return cb(err);
}
cb();
});
}
function createTrelloCard(trello, key, token, pivotal, lists, story, storyIndex, cb){
var destList = story.current_state.toLowerCase();
if(destList === 'unscheduled'){
destList = 'icebox';
} else if(destList === 'unstarted'){
destList = 'backlog';
} else if(destList === 'started'){
destList = 'current';
}
var trelloList = lists[destList].id;
var labels = [story.story_type];
if(story.labels){
labels = labels.concat(story.labels);
}
var trelloPayload = {
name: story.name,
desc: story.description || '',
labels: labels,
pos: storyIndex,
idList: trelloList
};
trello.post('/1/cards', trelloPayload, function(err, card){
log.info('pivotal-to-trello', 'migrating', story.id, story.name);
if(err){
return cb(err);
}
attachCardData(trello, key, token, pivotal, story, card, cb);
});
}
function createTrelloCards(trello, opts, lists, stories, cb){
var tasks = [];
stories.story.forEach(function(story, storyIndex){
tasks.push(createTrelloCard.bind(null, trello, opts.trello_key, opts.trello_token, opts.pivotal, lists, story, storyIndex));
});
async.parallel(tasks, function(err){
if(err){
return cb(err);
}
cb();
});
}
function readPivotalStories(pivotal, trello, opts, cb){
var lists = organizeLists(opts.lists);
pivotal.getStories(opts.from, {}, function(err, stories){
if(err){
return cb(err);
}
createTrelloCards(trello, opts, lists, stories, cb);
});
}
function makeLists(needed, opts, trello, pivotal, cb){
var tasks = [];
var boardUrl = '/1/boards/'+opts.to+'/lists';
needed.forEach(function(list){
var listPayload = {
name: list
};
tasks.push(function(cb){
trello.post(boardUrl, listPayload, cb);
});
});
async.parallel(tasks, function(err, res){
opts.lists = opts.lists.concat(res);
readPivotalStories(pivotal, trello, opts, cb);
});
}
function verifyLists(lists, opts, trello, pivotal, cb){
var needed = requiredLists.slice(0);
if(Array.isArray(lists)){
lists.forEach(function(list){
var idx = needed.indexOf(list.name.toLowerCase());
if(idx > -1){
needed.splice(idx, 1);
opts.lists.push(list);
}
});
if(needed.length > 0){
log.info('pivotal-to-trello', 'The following lists will be created:', needed.join(', '));
makeLists(needed, opts, trello, pivotal, cb);
} else {
readPivotalStories(pivotal, trello, opts, cb);
}
} else {
log.error('pivotal-to-trello', 'Sorry, there was an error with getting the lists on your board from Trello');
process.exit(1);
}
}
function getListsFromBoard(trello, opts, cb){
trello.get('/1/boards/'+opts.to+'/lists', function(err, res){
if(err){
return cb(err);
}
return verifyLists(res, opts, trello, pivotal, cb);
});
}
function importer(opts){
opts.lists = [];
var trello = new Trello(opts.trello_key, opts.trello_token);
pivotal.useToken(opts.pivotal);
getListsFromBoard(trello, opts, function(err){
if(err){
log.error('pivotal-to-trello', err);
process.exit(1);
}
log.info('pivotal-to-trello', 'Finished');
});
}
module.exports = importer;