2021-09-20 01:08:42 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
// src/Utils/TimeTrackingServices/YoutrackService.php
|
|
|
|
|
|
|
|
namespace RprtCli\Utils\TimeTrackingServices;
|
|
|
|
|
|
|
|
use GuzzleHttp\ClientInterface;
|
|
|
|
use RprtCli\Utils\Configuration\ConfigurationInterface;
|
|
|
|
|
|
|
|
class YoutrackService implements YoutrackInterface
|
|
|
|
{
|
|
|
|
|
2021-09-21 01:13:15 +02:00
|
|
|
protected $ytToken;
|
|
|
|
protected $ytBaseUrl;
|
|
|
|
|
2021-09-20 01:08:42 +02:00
|
|
|
protected $config;
|
|
|
|
|
|
|
|
protected $httpClient;
|
|
|
|
|
2022-05-02 13:42:41 +02:00
|
|
|
protected $report_id;
|
|
|
|
|
2021-09-20 01:08:42 +02:00
|
|
|
public function __construct(ConfigurationInterface $config, ClientInterface $http_client)
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
$this->httpClient = $http_client;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testYoutrackapi(): ?string
|
|
|
|
{
|
|
|
|
// Get base url from config or add input.
|
|
|
|
// Get token or add input.
|
|
|
|
$path = 'youtrack/api/admin/users/me';
|
|
|
|
$yt_url = $this->getYtUrl($path);
|
|
|
|
$yt_token = $this->getYtToken();
|
|
|
|
$query = ['fields' => 'id,email,fullName'];
|
|
|
|
$headers = [
|
|
|
|
"Authorization" => "Bearer $yt_token",
|
|
|
|
'Cache-Control' =>'no-cache',
|
|
|
|
];
|
|
|
|
$me_response = $this->httpClient->request('GET', $yt_url, [
|
|
|
|
'query' => $query,
|
|
|
|
'headers' => $headers
|
|
|
|
]);
|
|
|
|
$test = (string) $me_response->getBody()->getContents();
|
|
|
|
$me_json = (array) json_decode($test);
|
|
|
|
if ($me_json && isset($me_json['fullName'])) {
|
|
|
|
return $me_json['fullName'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-05-02 13:42:41 +02:00
|
|
|
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
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-09-20 01:08:42 +02:00
|
|
|
public function getReportId(): ?string
|
|
|
|
{
|
|
|
|
// --report option value should take precedence.
|
|
|
|
// @TODO error handling.
|
2022-05-02 13:42:41 +02:00
|
|
|
if (isset($this->report_id)) {
|
|
|
|
return $this->report_id;
|
|
|
|
}
|
2021-09-21 01:13:15 +02:00
|
|
|
$yt_report_id = $this->config->get('tracking_service.youtrack.report_id');
|
2021-09-20 01:08:42 +02:00
|
|
|
if (!$yt_report_id) {
|
|
|
|
$yt_report_id = readline('Enter the report id: ');
|
|
|
|
}
|
|
|
|
return $yt_report_id;
|
|
|
|
}
|
|
|
|
|
2022-05-02 13:42:41 +02:00
|
|
|
public function setReportId(string $report_id) :void {
|
|
|
|
$this->report_id = $report_id;
|
|
|
|
}
|
|
|
|
|
2021-09-20 01:08:42 +02:00
|
|
|
public function downloadReport(string $report_id): ?string
|
|
|
|
{
|
|
|
|
$path = "youtrack/api/reports/$report_id/export/csv";
|
2022-05-02 13:42:41 +02:00
|
|
|
$query = ['$top' => -1];
|
2021-09-20 01:08:42 +02:00
|
|
|
$yt_token = $this->getYtToken();
|
|
|
|
$headers = [
|
|
|
|
'Accept' => 'text/plain, */*',
|
|
|
|
'Accept-Language' => 'en-US,en;q=0.5',
|
|
|
|
"Authorization" => "Bearer $yt_token",
|
|
|
|
];
|
|
|
|
|
|
|
|
$csv_response = $this->httpClient->request('GET', $this->getYtUrl($path), [
|
|
|
|
'headers' => $headers,
|
|
|
|
'query' => $query,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Write csv response test into temporary file.
|
|
|
|
$csv_file = tempnam(sys_get_temp_dir(), 'yt_csv_');
|
|
|
|
file_put_contents($csv_file, $csv_response->getBody());
|
|
|
|
return $csv_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getYtToken(): string {
|
2021-09-21 01:13:15 +02:00
|
|
|
if (isset($this->ytToken)) {
|
|
|
|
return $this->ytToken;
|
|
|
|
}
|
2021-09-20 01:08:42 +02:00
|
|
|
$yt_token = $this->config->get('tracking_service.youtrack.auth_token', FALSE); //
|
|
|
|
if (!$yt_token) {
|
|
|
|
$yt_token = readline('Enter your youtrack authentication token: ');
|
|
|
|
}
|
|
|
|
return $yt_token;
|
|
|
|
}
|
|
|
|
|
2021-09-21 01:13:15 +02:00
|
|
|
public function setYtToken(string $token): void {
|
|
|
|
$this->ytToken = $token;
|
|
|
|
}
|
|
|
|
|
2021-09-20 01:08:42 +02:00
|
|
|
protected function getYtUrl(string $path = ''): ?string {
|
2021-09-21 01:13:15 +02:00
|
|
|
if (isset($this->ytBaseUrl)) {
|
|
|
|
$yt_base_url = $this->ytBaseUrl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$yt_base_url = $this->config->get('tracking_service.youtrack.base_url', FALSE);
|
|
|
|
}
|
|
|
|
if (empty($yt_base_url)) {
|
2021-09-20 01:08:42 +02:00
|
|
|
$yt_base_url = readline('Enter base url for of the youtrack service: ');
|
|
|
|
}
|
|
|
|
if (!empty($path)) {
|
|
|
|
$yt_base_url = $yt_base_url . '/' . trim($path, '/');
|
|
|
|
}
|
|
|
|
return $yt_base_url;
|
|
|
|
}
|
2021-09-21 01:13:15 +02:00
|
|
|
|
|
|
|
public function setYtUrl(string $base_url) {
|
|
|
|
$this->ytBaseUrl = $base_url;
|
|
|
|
}
|
2022-05-02 13:42:41 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2021-09-20 01:08:42 +02:00
|
|
|
}
|