diff --git a/src/time_report.php b/src/time_report.php index 56b4b77..5e1fe1d 100755 --- a/src/time_report.php +++ b/src/time_report.php @@ -9,6 +9,11 @@ // we will then have $client and $repo available require 'setup.php'; +// set the repo username and month to get the report for +echo "Enter username for report: "; +$name = trim(fgets(STDIN)); // read from stdin +$date = date('Y-m', strtotime('now -1 month')); //last month +echo "Generateing report for {$repo->user}/{$repo->repo} of time spent in month $date by $name...\n"; // Build a crazy id to number lookup table // https://github.com/go-gitea/gitea/issues/8513 foreach ($repo->getIssues() as $issue) { @@ -25,35 +30,45 @@ foreach ($repo->getPulls(['state' => 'closed']) as $pull) { $issue = $repo->getIssue($pull->number); $issueNumbers[$issue->id] = $issue->number; } -$name = 'jhodges'; -$date = '2019-10'; -$sums = []; +// process and sum up all the times by issue number +// (not ID as retrned by API) +$totals = []; foreach ($repo->getTimesByUsername($name) as $time) { + // check the month if ($date == date('Y-m', strtotime($time->created))) { + // get the issue number $number = $issueNumbers[$time->issue_id]; - if (!isset($sums[$number])) { + // if we've not already got a total for this issue + // then create the entry, looking up issue title + if (!isset($totals[$number])) { $issue = $repo->getIssue($number); - $sums[$number] = [ + $totals[$number] = [ 'id' => $number, 'name' => $issue->title, 'time' => 0, ]; } - $sums[$number]['time'] += $time->time; + // add the time to the total + $totals[$number]['time'] += $time->time; } } +// helper to format seconds into H:M:S format function format_time($t) { return sprintf('%02d:%02d:%02d', floor($t / 3600), ($t / 60) % 60, $t % 60); } +// save the report $f = fopen('report.csv', 'w'); +// ouput headers fputcsv($f, ['ID', 'Description', 'Hours', 'H:M:S']); -foreach ($sums as $id => $data) { - $data['hours'] = $data['time']/60/60; - $data['hms'] = format_time($data['time']); +// output data +foreach ($totals as $id => $data) { + $data['hours'] = $data['time']/60/60; //decimal hours + $data['hms'] = format_time($data['time']); //H:M:S + unset($data['time']); //dont want seconds in the report fputcsv($f, $data); } fclose($f);