From a5d66fb9a31d362a00c754ebd8d8c86d09b3533e Mon Sep 17 00:00:00 2001 From: "an.nechaev" Date: Tue, 16 Jun 2026 16:50:22 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20Jira=20Api=20Client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Core/App.php | 11 +++++++ src/Features/Jira/JiraApiClient.php | 49 +++++++++++++++++++++++++++++ src/console/jira_export_sprints.php | 20 +----------- src/functions.php | 37 ---------------------- 4 files changed, 61 insertions(+), 56 deletions(-) create mode 100644 src/Features/Jira/JiraApiClient.php diff --git a/src/Core/App.php b/src/Core/App.php index 7d69e3c..1eed74a 100644 --- a/src/Core/App.php +++ b/src/Core/App.php @@ -2,6 +2,8 @@ namespace Nechaev\Reports\Core; +use Nechaev\Reports\Features\Jira\JiraApiClient; + /** * Class App * @package Nechaev\Reports\Core @@ -14,4 +16,13 @@ final class App { } return self::$config; } + + private static JiraApiClient|null $jiraApiClient = null; + + public static function getJiraApiClient(): JiraApiClient { + if (self::$jiraApiClient === null) { + self::$jiraApiClient = new JiraApiClient(self::getConfig()->jira_url, self::getConfig()->jira_token); + } + return self::$jiraApiClient; + } } diff --git a/src/Features/Jira/JiraApiClient.php b/src/Features/Jira/JiraApiClient.php new file mode 100644 index 0000000..9f0de4c --- /dev/null +++ b/src/Features/Jira/JiraApiClient.php @@ -0,0 +1,49 @@ +client = new Client([ + 'base_uri' => $url, + 'headers' => [ + 'Authorization' => 'Bearer ' . $token, + 'Accept' => 'application/json', + ], + ]); + } + + public function getClosedSprintsListByBoardId(int $boardId, int $lastCount = 100): array + { + $sprintList = []; + try { + $startAt = 0; + do { + // Запрос к API для поиска спринта + $sprintResponse = $this->client->request('GET', "/rest/agile/1.0/board/{$boardId}/sprint", [ + 'query' => [ + 'state' => 'closed', // Ищем среди завершенных + 'maxResults' => 50, + 'startAt' => $startAt, + ] + ]); + + $sprints = json_decode($sprintResponse->getBody(), true); + + $isLast = $sprints['isLast']; + $startAt += 50; + $sprintList = array_merge($sprintList, $sprints['values']); + } while (!$isLast); + return array_slice($sprintList, $lastCount*(-1)); + + } catch (GuzzleException $e) { + throw new Exception("Ошибка при запросе к Jira API: " . $e->getMessage()); + } + } +} diff --git a/src/console/jira_export_sprints.php b/src/console/jira_export_sprints.php index 85c6117..2542379 100644 --- a/src/console/jira_export_sprints.php +++ b/src/console/jira_export_sprints.php @@ -1,28 +1,10 @@ jira_url; -$apiToken = App::getConfig()->jira_token; -$maxResults = App::getConfig()->jira_max_results; - -$client = new Client([ - 'base_uri' => $jiraUrl, - 'headers' => [ - 'Authorization' => 'Bearer ' . $apiToken, - 'Accept' => 'application/json', - ], -]); - // Подключаемся к БД $host = App::getConfig()->postgres_host; $port = App::getConfig()->postgres_port; @@ -103,7 +85,7 @@ foreach ($teamList as $team) { $boardId = $team['team_board_id']; - $sprints = getClosedSprintsListByBoardId($client, $boardId, $numberOfSprints); + $sprints = App::getJiraApiClient()->getClosedSprintsListByBoardId($boardId, $numberOfSprints); foreach ($sprints as $sprintRawData) { $sql = "INSERT INTO sprints (jira_sprint_id, team_id, sprint_name, sprint_activate_datetime, sprint_complete_datetime) VALUES (:jiraSprintId, :teamId, :sprintName, :sprintActivateDatetime, :sprintCompleteDatetime)"; diff --git a/src/functions.php b/src/functions.php index 4b21081..5bb1502 100644 --- a/src/functions.php +++ b/src/functions.php @@ -89,40 +89,3 @@ function filterByExecutors(array $data, array $executors): array { } return $result; } - -/** - * Получает отчет по задачам в указанном спринте на доске Jira. - * - * @param Client $client - * @param int $boardId Идентификатор доски Agile (Scrum/Kanban). - * @param int $lastCount Количество спринтов - * @return array Возвращает массив со спринтами. - * @throws Exception - */ -function getClosedSprintsListByBoardId(Client $client, int $boardId, int $lastCount = 100): array -{ - $sprintList = []; - try { - $startAt = 0; - do { - // Запрос к API для поиска спринта - $sprintResponse = $client->request('GET', "/rest/agile/1.0/board/{$boardId}/sprint", [ - 'query' => [ - 'state' => 'closed', // Ищем среди завершенных - 'maxResults' => 50, - 'startAt' => $startAt, - ] - ]); - - $sprints = json_decode($sprintResponse->getBody(), true); - - $isLast = $sprints['isLast']; - $startAt += 50; - $sprintList = array_merge($sprintList, $sprints['values']); - } while (!$isLast); - return array_slice($sprintList, $lastCount*(-1)); - - } catch (GuzzleException $e) { - throw new Exception("Ошибка при запросе к Jira API: " . $e->getMessage()); - } -}