diff --git a/app/config/rprt.example.config.yaml b/app/config/rprt.example.config.yaml index afc3585..19f703d 100644 --- a/app/config/rprt.example.config.yaml +++ b/app/config/rprt.example.config.yaml @@ -6,6 +6,9 @@ tracking_service: auth_token: '' base_url: 'https://test.youtrack.com' report_id: '<89-123>' + report: + # default report id for report command + default: '<83-541>' export: template_path: '~/.config/rprt-cli/invoice-template.html' output: '/tmp/[[YEAR]]-[[month]]-invoice.pdf' diff --git a/app/dependencies.php b/app/dependencies.php index ff8167a..b3a52a6 100644 --- a/app/dependencies.php +++ b/app/dependencies.php @@ -22,6 +22,11 @@ use RprtCli\Utils\TimeTrackingServices\YoutrackService; # use Symfony\Component\Translation\Translator; #use Symfony\Component\Translation\Loader\PoFileLoader; +// @TODO I could still use this d-i to have a plugin system. +// New plugins should have a dependencies.php in their folders +// and have dependencies defined there. Construct method +// should inject the right service. + return [ 'config.file' => 'rprt.config.yml', 'config.path' => '~/.config/rprt-cli/', diff --git a/app/rprt.php b/app/rprt.php index 27282c6..94ebec0 100755 --- a/app/rprt.php +++ b/app/rprt.php @@ -9,7 +9,7 @@ use DI\ContainerBuilder; require __DIR__ . '/vendor/autoload.php'; $builder = new ContainerBuilder(); -$builder->addDefinitions('dependencies.php'); +$builder->addDefinitions(__DIR__ . '/dependencies.php'); $container = $builder->build(); $application = new Application(); diff --git a/app/src/Commands/ReportCommand.php b/app/src/Commands/ReportCommand.php index 12a4cfb..67d54ea 100644 --- a/app/src/Commands/ReportCommand.php +++ b/app/src/Commands/ReportCommand.php @@ -70,10 +70,13 @@ class ReportCommand extends Command { $this->trackingService->setReportId($report); } } - + elseif ($report = $this->config->get('tracking_service.youtrack.report.default')) { + $this->trackingService->setReportId($report); + } // Currently we only support csv download. $report_id = $this->trackingService->getReportId(); $report_name = $reports[$report_id]; + $this->trackingService->clearReportCache($report_id); $file = $this->trackingService->downloadReport($report_id); // var_dump($file); $output->writeln("report: {$report_name}"); diff --git a/app/src/Utils/CsvReport/ReportCsv.php b/app/src/Utils/CsvReport/ReportCsv.php index c366585..e331ac2 100644 --- a/app/src/Utils/CsvReport/ReportCsv.php +++ b/app/src/Utils/CsvReport/ReportCsv.php @@ -175,6 +175,9 @@ class ReportCsv implements ReportCsvInterface public function generateReportTable(string $filePath) { // ticket-id, ticket-name, time-spent $data = $this->parseReportData($filePath); + if (empty($data)) { + return []; + } [$previous, $time_sum, $project_time, $table, $all_projects] = [$data[0]['id'], 0, 0, [], []]; foreach ($data as $line) { $project = explode('-', $line['id'])[0]; diff --git a/app/src/Utils/TimeTrackingServices/YoutrackService.php b/app/src/Utils/TimeTrackingServices/YoutrackService.php index 393c06f..e3bc97f 100644 --- a/app/src/Utils/TimeTrackingServices/YoutrackService.php +++ b/app/src/Utils/TimeTrackingServices/YoutrackService.php @@ -54,17 +54,21 @@ class YoutrackService implements YoutrackInterface protected function requestYoutrackPath(string $path, array $query) { $yt_url = $this->getYtUrl($path); - $yt_token = $this->getYtToken(); - $headers = [ - "Authorization" => "Bearer $yt_token", - 'Cache-Control' => 'no-cache', - ]; + $headers = $this->getHeaders(); return $this->httpClient->request('GET', $yt_url, [ 'query' => $query, 'headers' => $headers ]); } + protected function getHeaders() { + $yt_token = $this->getYtToken(); + return [ + "Authorization" => "Bearer $yt_token", + 'Cache-Control' => 'no-cache', + ]; + } + public function getReportId(): ?string { // --report option value should take precedence. @@ -157,4 +161,31 @@ class YoutrackService implements YoutrackInterface ); return $reports; } + + public function clearReportCache(string $report_id) :void { + $path = "/youtrack/api/reports/${report_id}/status"; + $query = [ + '$top' => -1, + 'fields' => 'calculationInProgress,error(id),errorMessage,isOutdated,lastCalculated,progress,wikifiedErrorMessage' + ]; + $post = [ + 'lastCalculated' => floor(microtime(true) * 1000), + 'calculationInProgress' => true, + 'wikifiedErrorMessage' => '', + 'isOutdated' => false, + 'progress' => -1, + 'error' => null, + 'errorMessage' => null, + '$type' => 'ReportStatus' + ]; + $yt_url = $this->getYtUrl($path); + $response = $this->httpClient->request('POST', $yt_url, [ + 'query' => $query, + 'headers' => $this->getHeaders(), + 'json' => $post, + ]); + $body = (string) $response->getBody()->getContents(); + // var_dump($body); + } + }