-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
438 lines (384 loc) · 16.1 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
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
var fs = require('fs-extra');
var util = require('util');
var Promise = require("bluebird");
var _ = require('underscore');
var package = fs.readJsonSync('package.json');
var Asana = require('asana');
var Trello = require("node-trello");
var request = require('request');
var path = require('path');
var LABEL_COLOR = {
green: 'light-green',
yellow: 'light-yellow',
orange: 'dark-orange',
red: 'light-red',
purple: 'dark-purple',
blue: 'dark-blue',
sky: 'light-blue',
lime: 'light-orange',
pink: 'light-pink',
black: 'dark-warm-gray'
};
var opts = require('nomnom')
.help('Import Asana Project from Trello Board with exported JSON file')
.options({
files: {
help: 'Path JSON file exported from Trello',
position: 0,
list: true,
required: true
},
config: {
help: 'Specify Config File',
string: '-c PATH, --config PATH',
'default': 'config.json'
},
onlyMembers: {
help: 'See Members of Trello without execute scripts.',
abbr: 'm',
full: 'only-members',
flag: true
},
append: {
help: 'Append tasks when exists the same project',
abbr: 'a',
flag: true
}
})
.parse();
var parseJson = function (path) {
return fs.readJson(path).then(function (data) {
var output = _.pick(data, [
'name', 'desc', 'labels', 'cards', 'lists', 'members', 'checklists'
]);
var closedListIds = [];
output.lists = _.filter(output.lists, function (list) {
if (list.closed) {
closedListIds.push(list.id);
return false;
} else {
return true;
}
});
// Cards in archived the List is not closed
output.cards = _.filter(output.cards, function (card) {
return !card.closed && _.indexOf(closedListIds, card.idList) === -1;
});
return output;
});
};
var fetch = function (result) {
return result.fetch();
};
var getUniqueName = function getUniqueName(name, haystack) {
const rxPostfix = / \(([0-9]+)\)$/;
if (_.contains(haystack, name)) {
const mat = rxPostfix.exec(name);
let pureName = name.replace(rxPostfix, '');
const number = mat ? parseInt(mat[1], 10) + 1 : 1;
return getUniqueName(pureName + ` (${number})`, haystack);
} else {
return name;
}
};
var convertMap = function convertMap(data, map) {
if (_.isArray(data)) {
return _.compact(_.map(data, id => {
return convertMap(id, map);
}));
}
if (typeof map[data] !== 'undefined') {
return map[data];
} else {
return null;
}
};
var fetchImage = function (url) {
return new Promise(function (resolve, reject) {
request.get({
url: url,
encoding: null
}, function (err, res, body) {
if (err || res.statusCode !== 200) {
reject(err || res.statusCode);
return;
}
if (body) {
resolve(body);
} else {
resolve(null);
}
});
});
};
fs.readJson(opts.config).then(function (config) {
var client = Asana.Client.create().useAccessToken(config.asana.personal_access_token);
var trello = new Trello(config.trello.key, config.trello.token);
var asanaData = {
projects: [],
tags: [],
users: [],
tasks: [],
sections: []
};
var uploadImageToAsana = function (taskId, file, filename) {
return new Promise(function (resolve, reject) {
request.post({
url: `https://app.asana.com/api/1.0/tasks/${taskId}/attachments`,
headers: {
Authorization: `Bearer ${config.asana.personal_access_token}`
},
formData: {
file: {
value: file,
options: {
filename: filename
}
}
}
}, function (err, res, body) {
if (err || res.statusCode !== 200) {
reject(err || res.statusCode);
return;
}
if (body) {
try {
body = JSON.parse(body);
} catch (e) {}
}
resolve(body);
});
});
};
Promise.promisifyAll(trello);
if (!config.asana.workspace) {
console.log('You should select your workspace in asana.');
console.log('<id>: <name>');
return client.workspaces.findAll().then(fetch).then(workspaces => {
_.each(workspaces, workspace => {
console.log(`${workspace.id}: ${workspace.name}`);
});
throw Promise.CancellationError;
});
}
if (!config.asana.team) {
return client.teams.findByOrganization(config.asana.workspace).then(fetch).then(teams => {
console.log('You should select a team in asana.');
console.log('<id>: <name>');
_.each(teams, team => {
console.log(`${team.id}: ${team.name}`);
});
throw Promise.CancellationError;
});
}
// Prepare asana data to avoid duplicated
return Promise.join(
client.projects.findByTeam(config.asana.team).then(fetch),
client.tags.findByWorkspace(config.asana.workspace).then(fetch),
client.users.findByWorkspace(config.asana.workspace).then(fetch),
(projects, tags, users) => {
asanaData.projects = projects;
asanaData.tags = tags;
asanaData.users = users;
}
).then(function () {
return Promise.map(opts.files, parseJson);
}).then(function (files) {
var trellMembers = _.flatten(_.pluck(files, 'members'));
// Check only member list
if (opts.onlyMembers) {
console.log('Trello Users');
console.log('<id>: <FullName>(<username>)');
console.log(_.map(trellMembers, function (member) {
return `${member.id}: ${member.fullName}(${member.username})`;
}).join('\n'));
console.log('\nAsana Users');
console.log('<id>: <Name>');
_.each(asanaData.users, user => {
console.log(`${user.id}: ${user.name}`);
});
throw Promise.CancellationError;
}
// Executes in order
return Promise.mapSeries(files, function (file) {
let projectData;
let listToSectionMap = {};
let cardToTaskMap = {};
let labelToTagMap = {};
let checklistMap = {};
let userMap = {};
let promise;
_.each(file.checklists, checklist => {
checklistMap[checklist.id] = checklist;
});
_.each(asanaData.users, user => {
userMap[user.id] = user.name;
});
// Append tasks
if (opts.append && _.contains(_.pluck(asanaData.projects, 'name'), file.name)) {
projectData = _.find(asanaData.projects, project => {
return project.name === file.name;
});
promise = client.tasks.findByProject(projectData.id).then(fetch).then(tasks => {
console.log(`Loaded exists ${tasks.length} tasks.`);
asanaData.tasks = tasks;
return client.sections.findByProject(projectData.id);
}).then(sections => {
console.log(`Loaded exists ${sections.length} sections.`);
asanaData.sections = sections;
});
} else {
promise = client.projects.createInTeam(config.asana.team, {
name: getUniqueName(file.name, _.pluck(asanaData.projects, 'name')),
notes: file.desc,
layout: 'board'
}).then(result => {
console.log(`Created ${result.name} project in your team.`);
projectData = result;
asanaData.projects.push(result);
});
}
return promise.then(function () {
var filteredList = _.filter(file.lists, list => {
var matchedSection = _.find(asanaData.sections, section => {
return section.name === list.name;
});
if (matchedSection) {
listToSectionMap[list.id] = matchedSection.id;
return false;
} else {
return true;
}
});
// Creates sections in order
return Promise.mapSeries(filteredList, list => {
return client.sections.createInProject(projectData.id, {
name: list.name
}).then(result => {
listToSectionMap[list.id] = result.id;
console.log(`Created ${list.name} section.`);
});
});
}).then(function () {
// Filter exists tags same with label
var labels = _.filter(file.labels, label => {
var matchedTag = _.find(asanaData.tags, tag => {
return tag.name === label.name;
});
if (matchedTag) {
labelToTagMap[label.id] = matchedTag.id;
return false;
} else {
return true;
}
});
// Creates tags
console.log(`Creating ${labels.length} tags...`);
return Promise.map(labels, label => {
return client.tags.createInWorkspace(config.asana.workspace, {
name: label.name,
color: LABEL_COLOR[label.color],
notes: 'Created by Trello'
}).then(result => {
labelToTagMap[label.id] = result.id;
asanaData.tags.push(result);
console.log(`Created ${result.name}(${result.id}) tag.`);
});
}, {
concurrency: 3
}).then(function () {
let countTask = 0;
var filteredCards = _.filter(file.cards, card => {
var matchedTask = _.find(asanaData.tasks, task => {
return task.name === card.name;
});
if (matchedTask) {
cardToTaskMap[card.id] = matchedTask.id
return false;
} else {
return true;
}
});
console.log(`Creating ${filteredCards.length} of ${file.cards.length} tasks...`);
// Creates tasks
return Promise.mapSeries(filteredCards, card => {
return client.tasks.create({
assignee: card.idMembers.length ? convertMap(_.first(card.idMembers), config.member) : null,
due_at: card.due,
followers: card.idMembers.length > 1 ? convertMap(card.idMembers, config.member) : [],
name: card.name,
notes: card.desc,
memberships: [{
project: projectData.id,
section: convertMap(card.idList, listToSectionMap)
}],
tags: card.idLabels.length ? convertMap(card.idLabels, labelToTagMap) : [],
projects: [ projectData.id ]
}).then(result => {
var promises = [];
var taskData = result;
cardToTaskMap[card.id] = result.id;
countTask++;
if (countTask % 10 === 0) {
console.log(`${countTask}...`);
}
if (card.idChecklists.length) {
promises.push(
Promise.mapSeries(convertMap(card.idChecklists.reverse(), checklistMap), checklist => {
return Promise.mapSeries(checklist.checkItems.reverse(), item => {
return client.tasks.addSubtask(taskData.id, {
name: item.name,
completed: item.state !== 'incomplete'
});
}).then(function () {
return client.tasks.addSubtask(taskData.id, {
name: `${checklist.name}:`
});
});
})
);
}
if (parseInt(card.badges.comments, 10) > 0) {
promises.push(
// Trello export has limitation for count of actions as 1000. so we need to request directly trello API.
trello.getAsync(`/1/cards/${card.id}/actions?limit=1000`).then(result => {
var comments = _.filter(result, action => {
return action.type === 'commentCard';
});
return Promise.mapSeries(comments.reverse(), comment => {
var member = convertMap(comment.idMemberCreator, config.member);
var text = comment.data.text;
var memberName = member ? convertMap(member, userMap) : comment.memberCreator.fullName;
text = `${memberName}: ${text} from Trello`;
return client.tasks.addComment(taskData.id, {
text: text
});
});
})
);
}
if (card.attachments.length) {
promises.push(
Promise.mapSeries(card.attachments, attachment => {
return fetchImage(attachment.url).then(image => {
return uploadImageToAsana(taskData.id, image, path.basename(attachment.url));
}).catch(reason => {
console.log('Failed to upload attachment', reason);
});
})
);
}
return Promise.all(promises);
});
});
});
}).then(function () {
console.log('complete!');
});
});
});
}).catch(reason => {
console.error(reason);
}).catch(Promise.CancellationError, function (reason) {
// nothing to do
});