tidy and better comment

This commit is contained in:
James 2019-12-02 17:51:11 +00:00
parent 9ab50f6bc4
commit e869bc2d8e

View File

@ -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);