-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathasana.js
223 lines (194 loc) · 7.42 KB
/
asana.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
const fs = require('fs-extra');
const util = require('util');
const Promise = require("bluebird");
const _ = require('underscore');
const Asana = require('asana');
const request = require('request');
const path = require('path');
const opts = require('nomnom')
.help('Copy Asana tasks in the project to another project')
.options({
config: {
help: 'Specify Config File',
string: '-c PATH, --config PATH',
'default': 'config.json'
},
from: {
help: 'Project name'
},
to: {
help: 'Project name'
},
section: {
help: 'Section name'
},
start: {
help: 'Start task id'
}
})
.parse();
const fetch = function (result) {
return _.isFunction(result.fetch) ? result.fetch() : [ result ];
};
const 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(config => {
const client = Asana.Client.create().useAccessToken(config.asana.personal_access_token);
const 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);
});
});
};
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;
});
}
if (!opts.from || !opts.to) {
console.log('You should select origin and target projects. (from,to)');
return client.projects.findByTeam(config.asana.team).then(fetch).then(result => {
console.log(result);
});
}
const taskOptions = {
opt_fields: ['name', 'attachments', 'assignee', 'followers', 'created_at', 'completed', 'notes', 'parent', 'subtasks', 'tags'].join(',')
};
// test 579942885784874
// return client.tasks.findById(579942885784874, taskOptions).then(fetch).then(result => {
return client.tasks.findByProject(opts.from, taskOptions).then(fetch).then(result => {
const createTask = function createTask(data, parentTask) {
const insertData = {
assignee: !_.isEmpty(data.assignee) ? data.assignee.id : null,
followers: !_.isEmpty(data.followers) ? _.pluck(data.followers, 'id') : [],
name: data.name,
notes: data.notes,
tags: !_.isEmpty(data.tags) ? _.pluck(data.tags, 'id') : []
};
let promise = Promise.resolve();
if (parentTask) {
insertData.workspace = opts.workspace;
promise = client.tasks.addSubtask(parentTask.id, insertData);
} else {
_.extend(insertData, {
memberships: [{
project: opts.to
}],
projects: [ opts.to ] // projects 혹은 workspace를 명시해줘야 한다
});
promise = client.tasks.create(insertData);
}
return promise.then(result => {
const task = result;
const promises = [];
if (!_.isEmpty(data.subtasks)) {
promises.push(client.tasks.subtasks(data.id, taskOptions).then(fetch).then(result => {
return Promise.mapSeries(result, data => {
return createTask(data, task);
});
}));
}
if (!_.isEmpty(data.attachments)) {
promises.push(client.attachments.findByTask(data.id, {
opt_fields: ['name', 'view_url'].join(',')
}).then(fetch).then(result => {
return Promise.mapSeries(result, attachment => {
return fetchImage(attachment.view_url).then(image => {
return uploadImageToAsana(task.id, image, attachment.name);
}).catch(reason => {
console.log('Failed to upload attachment', reason);
})
});
}));
}
if (!_.isEmpty(data.stories)) {
promises.push(client.stories.findByTask(data.id).then(fetch).then(result => {
return Promise.mapSeries(result, story => {
return client.tasks.addComment(task.id, {
text: `${story.created_by.name}: ${story.text}`
});
});
}));
}
if (!_.isEmpty(promises)) {
return Promise.all(promises);
} else {
return;
}
});
};
if (opts.start) {
_.some(result, (v, i) => {
// type 비교를 안함
if (v.id == opts.start) {
result = result.slice(i);
console.log(`${v.id} 이후부터 시작합니다.`);
return true;
}
});
}
return Promise.mapSeries(result, item => {
return createTask(item).then(() => {
console.log(`${item.id} task copied.`);
});
});
});
}).catch(reason => {
console.error(util.inspect(reason, null, 4));
}).catch(Promise.CancellationError, function (reason) {
// nothing to do
});