Last active
April 26, 2017 05:10
-
-
Save costlockerbot/2e9f86340dd4f4967bb0ebc69dc623ea to your computer and use it in GitHub Desktop.
Import Toggl weekly report to Costlocker https://costlocker.github.io/blog/2017-03-08-import-toggl-weekly-report-to-costlocker.html
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
<?php | |
$config = [ | |
'costlocker' => [ | |
'url' => 'https://new.costlocker.com/api-public/v2/timeentries/', | |
'token' => 'COSTLOCKER_TOKEN', | |
], | |
'toggl' => [ | |
'url' => 'https://toggl.com/reports/api/v2/weekly', | |
'token' => 'TOGGL_TOKEN', | |
'workspace' => 'TOGGL_WORKSPACE', | |
'selectedWeek' => date('Y-m-d', strtotime('last monday')), | |
], | |
'mapping' => [ | |
'description' => function (array $trelloUser, array $trelloProject) { | |
return "Toggl import - user '{$trelloUser['title']['user']}', project '{$trelloProject['title']['project']}', client '{$trelloProject['title']['client']}'"; | |
}, | |
'projects' => [ | |
'TOGGL_PID' => [ | |
'project_id' => COSTLOCKER_PROJECT_ID, | |
'activity_id' => COSTLOCKER_ACTIVITY_ID, | |
], | |
], | |
'users' => [ | |
'TOGGL_UID' => [ | |
'person_id' => COSTLOCKER_PERSON_ID, | |
], | |
], | |
], | |
]; | |
list($togglWeeklyReport, $togglDuration) = getTogglWeeklyReport($config); | |
$costlockerEntries = convertTogglWeeklyReport($togglWeeklyReport, $config); | |
list($createdEntries, $costlockerDuration) = createCostlockerEntries($costlockerEntries, $config); | |
debugJson([ | |
'toggl' => $togglWeeklyReport, 'costlocker' => $costlockerEntries, 'created' => $createdEntries, | |
'durations' => ['toggl' => $togglDuration, 'costlocker' => $costlockerDuration], | |
]); | |
function getTogglWeeklyReport(array $config) | |
{ | |
return httpRequest([ | |
'url' => $config['toggl']['url'], | |
'query' => [ | |
'workspace_id' => $config['toggl']['workspace'], | |
'since' => $config['toggl']['selectedWeek'], | |
'project_ids' => implode(',', array_keys($config['mapping']['projects'])), | |
'user_ids' => implode(',', array_keys($config['mapping']['users'])), | |
'user_agent' => 'api_test', | |
], | |
'auth' => [ | |
$config['toggl']['token'], | |
'api_token', | |
], | |
]); | |
} | |
function convertTogglWeeklyReport(array $togglWeeklyReport, array $config) | |
{ | |
$costlockerEntries = []; | |
foreach ($togglWeeklyReport['data'] as $project) { | |
foreach ($project['details'] as $user) { | |
foreach ($user['totals'] as $dayIndex => $miliseconds) { | |
if ($miliseconds && $dayIndex < 7) { | |
$costlockerEntries[] = [ | |
"date" => date('Y-m-d H:i:s', strtotime("{$config['toggl']['selectedWeek']} + {$dayIndex} day 8:00")), | |
"duration" => $miliseconds / 1000, | |
"description" => $config['mapping']['description']($user, $project), | |
'assignment' => $config['mapping']['users'][$user['uid']] + $config['mapping']['projects'][$project['pid']], | |
]; | |
} | |
} | |
} | |
} | |
return $costlockerEntries; | |
} | |
function createCostlockerEntries(array $costlockerEntries, array $config) | |
{ | |
return httpRequest([ | |
'url' => $config['costlocker']['url'], | |
'json' => $costlockerEntries, | |
'auth' => [ | |
'costlocker/import-toggl-entries', | |
$config['costlocker']['token'], | |
], | |
]); | |
} | |
function httpRequest(array $rawRequest) | |
{ | |
$start = microtime(true); | |
$request = $rawRequest + ['json' => [], 'query' => []]; | |
$ch = curl_init(); | |
curl_setopt_array($ch, [ | |
CURLOPT_CUSTOMREQUEST => $request['json'] ? 'POST' : 'GET', | |
CURLOPT_URL => $request['url'] . '?' . http_build_query($request['query']), | |
CURLOPT_POSTFIELDS => json_encode($request['json'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), | |
CURLOPT_HTTPHEADER => ['Content-Type: application/json'], | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_HTTPAUTH => CURLAUTH_BASIC, | |
CURLOPT_USERPWD => implode(':', $request['auth']), | |
]); | |
$response = curl_exec($ch); | |
curl_close($ch); | |
$duration = microtime(true) - $start; | |
return [json_decode($response, true), $duration]; | |
} | |
function debugJson($data) | |
{ | |
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n"; | |
} |
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
{ | |
"toggl": { | |
"total_grand": 14452000, | |
"total_billable": null, | |
"total_currencies": [ | |
{ | |
"currency": null, | |
"amount": null | |
} | |
], | |
"data": [ | |
{ | |
"title": { | |
"client": "Atoto", | |
"project": "Trello (legacy)", | |
"color": "0", | |
"hex_color": "#3750b5" | |
}, | |
"pid": 32699163, | |
"totals": [ | |
null, | |
null, | |
3896000, | |
null, | |
10556000, | |
null, | |
null, | |
14452000 | |
], | |
"details": [ | |
{ | |
"uid": 1117082, | |
"title": { | |
"user": "Zdeněk Drahoš" | |
}, | |
"totals": [ | |
null, | |
null, | |
3896000, | |
null, | |
10556000, | |
null, | |
null, | |
14452000 | |
] | |
} | |
] | |
} | |
], | |
"week_totals": [ | |
null, | |
null, | |
3896000, | |
null, | |
10556000, | |
null, | |
null, | |
14452000 | |
] | |
}, | |
"costlocker": [ | |
{ | |
"date": "2017-03-29 08:00:00", | |
"duration": 3896, | |
"description": "Toggl import - user 'Zdeněk Drahoš', project 'Trello (legacy)', client 'Atoto'", | |
"assignment": { | |
"person_id": 1, | |
"project_id": 30, | |
"activity_id": 2 | |
} | |
}, | |
{ | |
"date": "2017-03-31 08:00:00", | |
"duration": 10556, | |
"description": "Toggl import - user 'Zdeněk Drahoš', project 'Trello (legacy)', client 'Atoto'", | |
"assignment": { | |
"person_id": 1, | |
"project_id": 30, | |
"activity_id": 2 | |
} | |
} | |
], | |
"created": { | |
"meta": { | |
"created": 2, | |
"updated": 0 | |
}, | |
"data": [ | |
{ | |
"uuid": "090f5be8-935f-4064-88e9-77604b73e2a4" | |
}, | |
{ | |
"uuid": "d65a6d42-b628-432e-bc2e-6f9d85aa2676" | |
} | |
] | |
}, | |
"durations": { | |
"toggl": 0.87678790092468, | |
"costlocker": 0.18552803993225 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment