Last active
December 29, 2015 10:46
-
-
Save jbgutierrez/31095ee327420fcf8565 to your computer and use it in GitHub Desktop.
Migrate Things 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
#!/usr/bin/env ruby | |
# coding: UTF-8 | |
require "logger" | |
require "pp" | |
require "yaml" | |
require "active_support/inflector" | |
logger = Logger.new $stderr | |
logger.level = Logger.const_get ENV.fetch "LOG_LEVEL", "INFO" | |
require 'asana' | |
ACCESS_TOKEN="SECRET" | |
client = Asana::Client.new { |c| c.authentication :access_token, ACCESS_TOKEN } | |
workspace = client.workspaces.find_all.first | |
create_project = -> (name) { | |
logger.info "Creating project #{name}" | |
client.projects.create(workspace: workspace.id, name: name) | |
} | |
tags = client.tags.find_all(workspace: workspace.id).to_a | |
create_tag = -> (name) { | |
tag = tags.detect { |t| t.name == name } | |
return tag if tag | |
logger.info "Creating tag #{name}" | |
tags << tag = client.tags.create(workspace: workspace.id, name: name) | |
tag | |
} | |
create_task = -> (name, options) { | |
logger.info "Creating task #{name}" | |
options[:workspace] = workspace.id | |
options[:name] = name | |
client.tasks.create options | |
} | |
# project = create_project.("Lorem ipsum") | |
# project_id = "57013582069211" | |
# create_task.(project_id, "New Task", "My notes", ["New tag"]) | |
# project = client.projects.find_all(workspace: workspace.id).first | |
# pp client.tasks.find_by_project(projectId: project.id).first.tags.first | |
# client.tasks.create_in_workspace(workspace: workspace.id, name: 'foo') | |
require 'sequel' | |
DB = Sequel.connect('sqlite://things.db') | |
Things = DB.from(:ZTHING) | |
Tags = DB.from(:Z_12TAGS) | |
PROJECT_TYPE = 1 | |
TASK_TYPE = 0 | |
OFFICE_ITEMS = 393 | |
ACTIVE = 1 | |
SOMEDAY = 2 | |
ARCHIVED_STATUS = 3 | |
NEXT_STATUS = 0 | |
fix_date = -> (date) { | |
require 'time' | |
return date if date.nil? | |
milliseconds = date.to_i | |
january = Date.parse('2001-01-01').to_time | |
january + (milliseconds / (24 * 3600)) | |
} | |
START_NOTE = "<note xml:space=\"preserve\">" | |
END_NOTE = "</note>" | |
fix_note = -> (note) { | |
return nil if note[:ZNOTES].nil? | |
note[:ZNOTES].sub(START_NOTE, '').sub(END_NOTE, '') | |
} | |
file = File.open("exported.txt", "w") do |f| | |
projects = Things.where(ZAREA: OFFICE_ITEMS, ZTYPE: PROJECT_TYPE, ZSTART: ACTIVE, ZSTOPPEDDATE: nil) | |
projects.reverse_order(:ZSTATUS, :ZINDEX).each_with_index do |project| | |
project_name = project[:ZTITLE] | |
p = create_project.(project_name) | |
f.puts project_name | |
f.puts project_name.gsub(/./, '_') | |
Things.where(ZPROJECT: project[:Z_PK]).reverse_order(:ZSTATUS, :ZINDEX).each do |task| | |
id = task[:Z_PK] | |
status = task[:ZSTATUS] | |
start = task[:ZSTART] | |
ent = task[:Z_ENT] | |
opt = task[:ZINDEX] | |
tag_name = task[:ZTITLE] | |
completed_at = fix_date.(task[:ZSTOPPEDDATE]) | |
completed = !completed_at.nil? | |
next if completed | |
tags_ids = Tags.where( Z_12NOTES: id ).map{ |r| r[:Z_14TAGS] } | |
task_tags = Things.where(Z_PK: tags_ids).map{ |t| t[:ZTITLE] } | |
f.puts "#{id} - #{status} #{start} #{ent} #{opt} #{completed_at} #{tag_name} #{tags}" | |
task_tags = task_tags.map { |name| create_tag.(name).id } | |
options = { | |
tags: task_tags, | |
notes: fix_note.(task), | |
projects: [p.id] | |
} | |
create_task.(tag_name, options) | |
end | |
f.puts "" | |
f.puts "" | |
end | |
end | |
puts "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment