Skip to content

Instantly share code, notes, and snippets.

@joh6nn
Created January 4, 2015 15:19
Show Gist options
  • Save joh6nn/b951894535d7bc97bbff to your computer and use it in GitHub Desktop.
Save joh6nn/b951894535d7bc97bbff to your computer and use it in GitHub Desktop.
asana csv import
#!/usr/bin/env ruby
require "rubygems"
require "json"
require "net/https"
require "csv"
require 'io/console'
def rest_req(uri, req_type, data)
# content-type header is common to all requests
header = { "Content-Type" => "application/json" }
uri = URI.parse(uri)
# set up HTTPS connection
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
#get all the existing tags in the workspace
req = req_type.new(uri.path, header)
req.basic_auth($api_key, '')
req.body = {
"data" => data
}.to_json()
# issue the request
response = http.start { |http| http.request(req) }
# output
return JSON.parse(response.body)
end
def condense(unabridged)
result = Hash.new
unabridged.each do |obj|
result.store(obj['name'], obj['id'])
end
return result
end
asana_url = "https://app.asana.com/api/1.0"
$api_key = "api123"
workspace_id = "workspace123"
csv_file = "/path/to/csv"
tasks = Array.new
ws_tags = condense(rest_req("#{asana_url}/workspaces/#{workspace_id}/tags", Net::HTTP::Get, {})['data'])
ws_projects = condense(rest_req("#{asana_url}/workspaces/#{workspace_id}/projects", Net::HTTP::Get, {})['data'])
puts "projects: #{ws_projects}"
puts "tags: #{ws_tags}"
puts ""
CSV.foreach(csv_file) do |row|
#make sure the project for this task exists
if not row[9].nil? then
row_projects = row[9].split(',')
row_projects.each do |project|
print "\nproject name: '#{project}', "
if not ws_projects.has_key?(project) then
req_data = {
"name" => project
}
response = rest_req("#{asana_url}/workspaces/#{workspace_id}/projects", Net::HTTP::Post, req_data)
if response['errors'] then
puts "\nServer returned an error: project create: #{response['errors'][0]['message']}"
print "Continue anyway? (y/n)"
if STDIN.getc == "y"
next
else
exit
end
else
ws_projects.store(project, response['data']['id'])
print "Created project with id: #{response['data']['id']} \n"
end
end
end
end
if row[10].nil? then
print "task: #{row[4]}, "
uri = "#{asana_url}/workspaces/#{workspace_id}/tasks"
else
parent_id = tasks[row[10].to_i]
print "subtask: #{row[4]}. parent is #{parent_id}, "
uri = "#{asana_url}/tasks/#{parent_id}/subtasks"
end
req_data = {
"name" => row[4],
"assignee" => row[5],
"due_on" => row[6],
"notes" => (row[8] || ""),
"projects" => Array.new(1, ws_projects[row[9]])
}
response = rest_req(uri, Net::HTTP::Post, req_data)
if response['errors'] then
puts "\nServer returned an error: task create: #{response['errors'][0]['message']}"
next
else
tasks.push(response['data']['id'])
print "Created task with id: #{response['data']['id']}"
end
#tags can't be added to tasks during task creation, so have to do it separately after task creation
#start by making sure that all the tags have been created
if not row[7].nil? then
row_tags = row[7].split(',')
row_tags.each do |tag|
print ", tag name: '#{tag}', "
if not ws_tags.has_key?(tag) then
print "creating tag, "
req_data = {
"name" => tag
}
response = rest_req("#{asana_url}/workspaces/#{workspace_id}/tags", Net::HTTP::Post, req_data)
if response['errors'] then
puts "\nServer returned an error: tag create: #{response['errors'][0]['message']}"
print "Continue anyway? (y/n)"
if STDIN.getc == "y"
next
else
exit
end
else
#add tag we created to list of known tags
ws_tags.store(tag, response['data']['id'])
print "Created tag with id: #{response['data']['id']} \n"
end
end
req_data = {
"tag" => ws_tags[tag]
}
response = rest_req("#{asana_url}/tasks/#{tasks.last}/addTag", Net::HTTP::Post, req_data)
if response['errors'] then
puts "\nServer returned an error: add tag: #{response['errors'][0]['message']}"
print "Continue anyway? (y/n)"
if STDIN.getc == "y"
next
else
exit
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment