Add option to output reports.
parent
9f8c1616f5
commit
1e2e6bade4
|
@ -50,6 +50,11 @@
|
||||||
*** ValueObjects
|
*** ValueObjects
|
||||||
|
|
||||||
|
|
||||||
|
*** List my reports
|
||||||
|
|
||||||
|
#+begin_example bash
|
||||||
|
curl 'https://drunomics.myjetbrains.com/youtrack/api/reports?$top=-1&fields=id,name,own,owner(login,name)&query=owner:+%7Bme%7D' -H "Authorization: Bearer $YTOKEN" -H 'Cache-Control: no-cache' | jq '.[] | select( .own == true )'
|
||||||
|
#+end_example
|
||||||
|
|
||||||
** Motivation
|
** Motivation
|
||||||
- practice php
|
- practice php
|
||||||
|
|
|
@ -75,7 +75,7 @@ class RprtCommand extends Command
|
||||||
'youtrack',
|
'youtrack',
|
||||||
'y',
|
'y',
|
||||||
InputOption::VALUE_NONE,
|
InputOption::VALUE_NONE,
|
||||||
'Use youtrack api to get a report. If this option is used --file does not have any effect.'
|
'Use youtrack api to get a report. If this option is used --file does not have any effect..'
|
||||||
);
|
);
|
||||||
$this->addOption(
|
$this->addOption(
|
||||||
'pdf',
|
'pdf',
|
||||||
|
@ -102,7 +102,7 @@ class RprtCommand extends Command
|
||||||
'Send pdf export via email to recipient.'
|
'Send pdf export via email to recipient.'
|
||||||
);
|
);
|
||||||
$this->addOption(
|
$this->addOption(
|
||||||
'send-to',
|
'recipients',
|
||||||
'r',
|
'r',
|
||||||
InputOption::VALUE_REQUIRED,
|
InputOption::VALUE_REQUIRED,
|
||||||
'Comma separated list of recipients that should get the exported pdf.'
|
'Comma separated list of recipients that should get the exported pdf.'
|
||||||
|
@ -121,6 +121,19 @@ class RprtCommand extends Command
|
||||||
'Additional custom work untracked in format: name1=time1;name2=time2... Project to assign work items to has to be configured in app config. Leave empty for interactive output.',
|
'Additional custom work untracked in format: name1=time1;name2=time2... Project to assign work items to has to be configured in app config. Leave empty for interactive output.',
|
||||||
FALSE
|
FALSE
|
||||||
);
|
);
|
||||||
|
$this->addOption(
|
||||||
|
'list-reports',
|
||||||
|
'l',
|
||||||
|
InputOption::VALUE_NONE,
|
||||||
|
'List my reports'
|
||||||
|
);
|
||||||
|
$this->addOption(
|
||||||
|
'report',
|
||||||
|
'g',
|
||||||
|
InputOption::VALUE_OPTIONAL,
|
||||||
|
'Show time tracked for report.',
|
||||||
|
FALSE
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output) : int
|
protected function execute(InputInterface $input, OutputInterface $output) : int
|
||||||
|
@ -129,15 +142,35 @@ class RprtCommand extends Command
|
||||||
$test = $this->youtrack->testYoutrackapi();
|
$test = $this->youtrack->testYoutrackapi();
|
||||||
$output->writeln($test);
|
$output->writeln($test);
|
||||||
}
|
}
|
||||||
|
if ($input->getOption('list-reports')) {
|
||||||
|
$list = $this->youtrack->listReports();
|
||||||
|
$output->writeln(var_export($list, TRUE));
|
||||||
|
}
|
||||||
|
if ($input->hasParameterOption('--report') || $input->hasParameterOption('-g')) {
|
||||||
|
if ($report = $input->getOption('report')) {
|
||||||
|
$this->youtrack->setReportId($report);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$reports = $this->youtrack->listReports();
|
||||||
|
$count = 1;
|
||||||
|
foreach ($reports as $id => $name) {
|
||||||
|
$output->writeln("[{$count}] {$name} ({$id})");
|
||||||
|
$count++;
|
||||||
|
}
|
||||||
|
$report = readline('Select id of the report: ');
|
||||||
|
// Asume people are literate.
|
||||||
|
$this->youtrack->setReportId($report);
|
||||||
|
}
|
||||||
|
}
|
||||||
if ($youtrack = $input->getOption('youtrack')) {
|
if ($youtrack = $input->getOption('youtrack')) {
|
||||||
$report_id = $this->youtrack->getReportId();
|
$report_id = $this->youtrack->getReportId();
|
||||||
$file = $this->youtrack->downloadReport($report_id);
|
$file = $this->youtrack->downloadReport($report_id);
|
||||||
}
|
}
|
||||||
if ($input->hasParameterOption('expenses') || $input->hasParameterOption('-e')) {
|
if ($input->hasParameterOption('--expenses') || $input->hasParameterOption('-e')) {
|
||||||
$expenses = $this->getExpenses($input->getOption('expenses'));
|
$expenses = $this->getCustomWorkOrExpenses($input->getOption('expenses'), self::TYPE_EXPENSE);
|
||||||
}
|
}
|
||||||
if ($input->hasParameterOption('custom') || $input->hasParameterOption('-c')) {
|
if ($input->hasParameterOption('--custom') || $input->hasParameterOption('-c')) {
|
||||||
$custom = $this->getCustomWork($input->getOption('custom'));
|
$custom = $this->getCustomWorkOrExpenses($input->getOption('custom'), self::TYPE_WORK);
|
||||||
}
|
}
|
||||||
if ($youtrack || $file = $input->getOption('file')) {
|
if ($youtrack || $file = $input->getOption('file')) {
|
||||||
// Youtrack can also provide a file name.
|
// Youtrack can also provide a file name.
|
||||||
|
|
|
@ -105,7 +105,7 @@ class ReportCsv implements ReportCsvInterface
|
||||||
$hours = $config['time_format'] == 'm' ? $time/60 : $time;
|
$hours = $config['time_format'] == 'm' ? $time/60 : $time;
|
||||||
$price = $hours * (float) $config['price'];
|
$price = $hours * (float) $config['price'];
|
||||||
$row = [
|
$row = [
|
||||||
$config['label'] ?? $project,
|
$config['name'] ?? $project,
|
||||||
number_format($hours, 2, ',', '.'),
|
number_format($hours, 2, ',', '.'),
|
||||||
number_format($config['price'], 2, ',', '.'),
|
number_format($config['price'], 2, ',', '.'),
|
||||||
number_format($price, 2, ',', '.'),
|
number_format($price, 2, ',', '.'),
|
||||||
|
|
|
@ -75,7 +75,7 @@ class PdfExportService implements PdfExportInterface {
|
||||||
}
|
}
|
||||||
if ($colspan) {
|
if ($colspan) {
|
||||||
$colspan += 1;
|
$colspan += 1;
|
||||||
$cells[] = "<td class=\"td-{$index}\" colspan=\"{$colspan}\">{$cell}</td>";
|
$cells[] = "<td class=\"td-{$index} colspan\" colspan=\"{$colspan}\">{$cell}</td>";
|
||||||
$colspan = 0;
|
$colspan = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -19,6 +19,8 @@ class YoutrackService implements YoutrackInterface
|
||||||
|
|
||||||
protected $httpClient;
|
protected $httpClient;
|
||||||
|
|
||||||
|
protected $report_id;
|
||||||
|
|
||||||
public function __construct(ConfigurationInterface $config, ClientInterface $http_client)
|
public function __construct(ConfigurationInterface $config, ClientInterface $http_client)
|
||||||
{
|
{
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
|
@ -50,10 +52,26 @@ class YoutrackService implements YoutrackInterface
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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',
|
||||||
|
];
|
||||||
|
return $this->httpClient->request('GET', $yt_url, [
|
||||||
|
'query' => $query,
|
||||||
|
'headers' => $headers
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
public function getReportId(): ?string
|
public function getReportId(): ?string
|
||||||
{
|
{
|
||||||
// --report option value should take precedence.
|
// --report option value should take precedence.
|
||||||
// @TODO error handling.
|
// @TODO error handling.
|
||||||
|
if (isset($this->report_id)) {
|
||||||
|
return $this->report_id;
|
||||||
|
}
|
||||||
$yt_report_id = $this->config->get('tracking_service.youtrack.report_id');
|
$yt_report_id = $this->config->get('tracking_service.youtrack.report_id');
|
||||||
if (!$yt_report_id) {
|
if (!$yt_report_id) {
|
||||||
$yt_report_id = readline('Enter the report id: ');
|
$yt_report_id = readline('Enter the report id: ');
|
||||||
|
@ -61,10 +79,14 @@ class YoutrackService implements YoutrackInterface
|
||||||
return $yt_report_id;
|
return $yt_report_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setReportId(string $report_id) :void {
|
||||||
|
$this->report_id = $report_id;
|
||||||
|
}
|
||||||
|
|
||||||
public function downloadReport(string $report_id): ?string
|
public function downloadReport(string $report_id): ?string
|
||||||
{
|
{
|
||||||
$path = "youtrack/api/reports/$report_id/export/csv";
|
$path = "youtrack/api/reports/$report_id/export/csv";
|
||||||
$query = ['\$top' => -1];
|
$query = ['$top' => -1];
|
||||||
$yt_token = $this->getYtToken();
|
$yt_token = $this->getYtToken();
|
||||||
$headers = [
|
$headers = [
|
||||||
'Accept' => 'text/plain, */*',
|
'Accept' => 'text/plain, */*',
|
||||||
|
@ -117,4 +139,22 @@ class YoutrackService implements YoutrackInterface
|
||||||
public function setYtUrl(string $base_url) {
|
public function setYtUrl(string $base_url) {
|
||||||
$this->ytBaseUrl = $base_url;
|
$this->ytBaseUrl = $base_url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function listReports() {
|
||||||
|
// Now filter results by own = true;
|
||||||
|
$url = '/youtrack/api/reports';
|
||||||
|
$query = [
|
||||||
|
'$top' => -1,
|
||||||
|
'fields' => 'id,name,own,owner(login,name)',
|
||||||
|
];
|
||||||
|
$response = $this->requestYoutrackPath($url, $query);
|
||||||
|
$body = (string) $response->getBody()->getContents();
|
||||||
|
$body_json = (array) json_decode($body);
|
||||||
|
$my_reports = array_filter($body_json, fn($report) => $report->own);
|
||||||
|
$reports = array_combine(
|
||||||
|
array_map(fn($r) => $r->id, $my_reports),
|
||||||
|
array_map(fn ($r) => $r->name, $my_reports)
|
||||||
|
);
|
||||||
|
return $reports;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue