Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Copy/Move project to a new workspace in Asana
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
@gtscl

This comment has been minimized.

Copy link

@gtscl gtscl commented Apr 18, 2013

Thank you, worked great!

@kirkouimet

This comment has been minimized.

Copy link

@kirkouimet kirkouimet commented Jun 17, 2013

So good - thank you! Would love to see it handle attachments.

@hannahd

This comment has been minimized.

Copy link

@hannahd hannahd commented Jun 19, 2013

Totally needed this! Thanks! Support for labels would also be nice.

@amarred

This comment has been minimized.

Copy link

@amarred amarred commented Jun 24, 2013

How to move to a different workspace? Can you post a sample use? Thanks!

@aminur

This comment has been minimized.

Copy link

@aminur aminur commented Oct 24, 2016

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