Last active
December 2, 2015 14:15
-
-
Save addadi/025ec7b93fe18ddc5003 to your computer and use it in GitHub Desktop.
utility script to migrate remember the milk tasks to asana
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//migrate rtm tasks to asana | |
//1. export tasks using https://github.com/aliva/rememberthemilk-export-json and use var rtmTasks to store the json | |
//2. clean null elements in rtmTasks | |
//3. use this in browser enviroment with artoo.js (can be used also in nodejs with require instead of injectScript) | |
//4. get personal access token in profile settings in asana. use it in const part | |
//5. check workspace id and use it for WS (find it on the console by running client.workspaces.findAll().then(function(x){console.log(x)})). use it in const part | |
//6. run it "manually" : use init() after underscore, asana and async are loaded. use runMig() after all tags have been added | |
//7. check error on the console by evaluating errors var | |
var addTag, addTask; | |
artoo.injectScript('https://rawgit.com/jashkenas/underscore/master/underscore-min.js', function() { | |
console.log('underscore load finished, we have %s', _.VERSION); | |
}); | |
//async is better than bluebird | |
artoo.injectScript('https://rawgit.com/caolan/async/master/lib/async.js', function() { | |
addTask = async.queue(addAsTask, 1); | |
addTask.drain = function() { | |
artoo.log.debug('all tasks have been added'); | |
}; | |
addTag = async.queue(addAsTag, 1); | |
addTag.drain = function() { | |
artoo.log.debug('all tags have been added'); | |
}; | |
artoo.log('async load finished'); | |
}); | |
artoo.injectScript('https://github.com/Asana/node-asana/releases/download/v0.13.0/asana.js', function() { | |
console.log('asana load finished, we have %s', Asana.VERSION); | |
}); | |
var rtmTags = []; | |
var errors = []; | |
//first collect all rtm tasks' tags | |
rtmTasks.map(function(t) { | |
rtmTags = rtmTags.concat(t.tags.tag) | |
rtmTags = _.unique(rtmTags) | |
}); | |
function filterTags(e) { | |
if (e) return e; | |
}; | |
rtmTags = rtmTags.filter(filterTags); | |
//const declarations | |
var TOKEN = 'XXXXXXXX'; //Personal Access Token | |
var WS = 0000000; //the workspace to import | |
var asanaTagsDb = {}; | |
var client = Asana.Client.create().useAccessToken(TOKEN); | |
//first, for all rtm tags, either get existing tag id in asana or create them (and get the new tags id) store it in asanaTagsDb | |
function init() { | |
client.tags.findAll({ | |
workspace: WS | |
}) | |
.then( | |
function(res) { | |
res.data.map(function(tgs) { | |
var name = tgs.name; | |
var id = tgs.id; | |
asanaTagsDb[name] = id; | |
}); | |
}) | |
.then(function(e) { | |
rtmTags.forEach(function(t) { | |
addTag.push(t), | |
function(err) { | |
if (err) artoo.log.error(err); | |
artoo.log('addTag callback called'); | |
} | |
}) | |
}) | |
.catch(function(error) { | |
console.log(error); | |
}); | |
} | |
//run actual import to asana tasks | |
function runMig() { | |
rtmTasks.forEach(function(t) { | |
addTask.push(asanize(parseRtmTaskOb(t)), function(err) { | |
if (err) artoo.log.error(err); | |
artoo.log('addTask callback called'); | |
}); | |
}) | |
} | |
//check if tag exists in asana or create it. add tag id to asanaTagsDb | |
var addAsTag = function(tagName, cb) { | |
if (!(asanaTagsDb[tagName])) { | |
client.tags.create({ | |
workspace: WS, | |
name: tagName | |
}) | |
.then(function(t) { | |
console.log('added tag %s', t); | |
asanaTagsDb[t.name] = t.id; | |
cb(); | |
}) | |
.catch(function(error) { | |
console.log(error); | |
}) | |
} else { | |
cb(); | |
} | |
} | |
function addAsTask(obj, cb) { | |
client.tasks.create(obj).then(function(t) { | |
cb(); | |
}).catch(function(error) { | |
errors.push({ | |
errmsg: error, | |
task: obj | |
}); | |
}) | |
} | |
//parse rtm task from json to be simple obj | |
function parseRtmTaskOb(task) { | |
function parseNotes(notes) { | |
var parsedNote = ''; | |
function simpleStingNote(obj) { | |
return obj.title + obj.$t; | |
} | |
if (_.isArray(notes)) { | |
return undefined; | |
} else if (_.isObject(notes)) { | |
if (_.isArray(notes.note)) { | |
notes.note.forEach(function(n) { | |
parsedNote += simpleStingNote(n); | |
}) | |
} else { | |
parsedNote += simpleStingNote(notes.note); | |
} | |
} | |
return parsedNote; | |
} | |
function parseTags(tags) { | |
if (_.isArray(tags)) { | |
return tags; | |
} else if (_.isString(tags.tag)) { | |
return [asanaTagsDb[tags.tag]]; | |
} else { | |
var tagsID = tags.tag.map(function(t) { | |
return asanaTagsDb[t]; | |
}) | |
return tagsID; | |
} | |
} | |
return { | |
completed: task.task.completed ? true : false, | |
due_at: task.task.due ? task.task.due : undefined, | |
name: task.name, | |
notes: parseNotes(task.notes), | |
tags: parseTags(task.tags) | |
} | |
} | |
//make the simple task obj asana compatible with assignee and ws | |
function asanize(task) { | |
task.assignee = 'me'; | |
task.workspace = WS; | |
return task; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment