Created
October 24, 2012 16:43
-
-
Save mhdhejazi/3947237 to your computer and use it in GitHub Desktop.
Copy/Move project to a new workspace in 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
function asanaRequest($methodPath, $httpMethod = 'GET', $body = null) | |
{ | |
$apiKey = 'ASANA_API_KEY_HERE'; /// Get it from http://app.asana.com/-/account_api | |
$url = "https://app.asana.com/api/1.0/$methodPath"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ; | |
curl_setopt($ch, CURLOPT_USERPWD, $apiKey); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); | |
if ($body) | |
{ | |
if (!is_string($body)) | |
{ | |
$body = json_encode($body); | |
} | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); | |
} | |
$data = curl_exec($ch); | |
curl_close($ch); | |
$result = json_decode($data, true); | |
return $result; | |
} | |
function createTask($workspaceId, $projectId, $task) | |
{ | |
$data = array('data' => $task); | |
$result = asanaRequest("workspaces/$workspaceId/tasks", 'POST', $data); | |
if ($result['data']) | |
{ | |
$newTask = $result['data']; | |
$newTaskId = $newTask['id']; | |
$data = array('data' => array('project' => $projectId)); | |
$result = asanaRequest("tasks/$newTaskId/addProject", 'POST', $data); | |
return $newTask; | |
} | |
return $result; | |
} | |
function copyTasks($fromProjectId, $toProjectId) | |
{ | |
$result = asanaRequest("projects/$toProjectId"); | |
if (!$result['data']) | |
{ | |
return; | |
} | |
$workspaceId = $result['data']['workspace']['id']; | |
$result = asanaRequest("projects/$fromProjectId/tasks?opt_pretty&opt_fields=name,due_on,assignee_status,notes,assignee"); | |
$tasks = $result['data']; | |
for ($i = count($tasks) - 1; $i >= 0; $i--) | |
{ | |
$task = $tasks[$i]; | |
$newTask = $task; | |
unset($newTask['id']); | |
$newTask['assignee'] = $newTask['assignee']['id']; | |
foreach ($newTask as $key => $value) | |
{ | |
if (empty($value)) | |
{ | |
unset($newTask[$key]); | |
} | |
} | |
$newTask = createTask($workspaceId, $toProjectId, $newTask); | |
if ($newTask['id']) | |
{ | |
$taskId = $task['id']; | |
$result = asanaRequest("tasks/$taskId/stories"); | |
$comments = array(); | |
foreach ($result['data'] as $story) | |
{ | |
$date = date('l M d, Y h:i A', strtotime($story['created_at'])); | |
$comment = " \n" . $story['created_by']['name'] . ' on ' . $date . ":\n" . $story['text']; | |
$comments[] = $comment; | |
} | |
$comment = implode("\n----------------------", $comments); | |
$data = array('data' => array('text' => $comment)); | |
$newTaskId = $newTask['id']; | |
$result = asanaRequest("tasks/$newTaskId/stories", 'POST', $data); | |
} | |
} | |
} | |
/// Sample usage | |
copyTasks(12345678910, 98765432110); /// Get those numbers from the URL of the project |
So good - thank you! Would love to see it handle attachments.
Totally needed this! Thanks! Support for labels would also be nice.
How to move to a different workspace? Can you post a sample use? Thanks!
Can you update this to use a personal token? ... as the API key will be deprecated
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, worked great!