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; } public function getReportId(): ?string { // --report option value should take precedence. // @TODO error handling. $yt_report_id = $this->config->get('youtrack.report_id'); if (!$yt_report_id) { $yt_report_id = readline('Enter the report id: '); } return $yt_report_id; } public function downloadReport(string $report_id): ?string { $path = "youtrack/api/reports/$report_id/export/csv"; $query = ['$top' => -1]; $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 { $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; } protected function getYtUrl(string $path = ''): ?string { $yt_base_url = $this->config->get('tracking_service.youtrack.base_url', FALSE); if (!$yt_base_url) { $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; } }