From 100b941b08c934a27c90da08793bd76a04e84a07 Mon Sep 17 00:00:00 2001 From: Bob E Moe Date: Sat, 18 Jan 2020 09:15:10 +0000 Subject: [PATCH] update to use new api, much cleaner code now :) --- src/time_report.php | 54 ++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/src/time_report.php b/src/time_report.php index 5e1fe1d..3b8c757 100755 --- a/src/time_report.php +++ b/src/time_report.php @@ -12,46 +12,30 @@ 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) { - $issueNumbers[$issue->id] = $issue->number; -} -foreach ($repo->getPulls() as $pull) { - $issue = $repo->getIssue($pull->number); - $issueNumbers[$issue->id] = $issue->number; -} -foreach ($repo->getIssues(['state' => 'closed']) as $issue) { - $issueNumbers[$issue->id] = $issue->number; -} -foreach ($repo->getPulls(['state' => 'closed']) as $pull) { - $issue = $repo->getIssue($pull->number); - $issueNumbers[$issue->id] = $issue->number; -} + +echo "Enter month for report (0 = this, 1 = last, ...): "; +$ago = (int)(trim(fgets(STDIN))); // read from stdin +$date_start = date('Y-m-01', strtotime("now -$ago month")); +$date_end = date('Y-m-t', strtotime("now -$ago month")); + +$date = date('Y-m', strtotime("now -$ago month")); +echo "Generateing report for {$repo->user}/{$repo->repo} of time spent between $date_start - $date_end by $name...\n"; // 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 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); - $totals[$number] = [ - 'id' => $number, - 'name' => $issue->title, - 'time' => 0, - ]; - } - // add the time to the total - $totals[$number]['time'] += $time->time; +foreach ($repo->getTimesByUsername($name,new \DateTime("{$date_start} 00:00:00") , new \DateTime("{$date_end} 23:59:59")) as $time) { + // if we've not already got a total for this issue + // then create the entry, looking up issue title + if (!isset($totals[$time->issue->number])) { + $totals[$time->issue->number] = [ + 'id' => $time->issue->number, + 'name' => $time->issue->title, + 'time' => 0, + ]; } + // add the time to the total + $totals[$time->issue->number]['time'] += $time->time; } // helper to format seconds into H:M:S format