Добавлен Jira Api Client

master
an.nechaev 2026-06-16 16:50:22 +04:00
parent b325cb227f
commit a5d66fb9a3
4 changed files with 61 additions and 56 deletions

View File

@ -2,6 +2,8 @@
namespace Nechaev\Reports\Core; namespace Nechaev\Reports\Core;
use Nechaev\Reports\Features\Jira\JiraApiClient;
/** /**
* Class App * Class App
* @package Nechaev\Reports\Core * @package Nechaev\Reports\Core
@ -14,4 +16,13 @@ final class App {
} }
return self::$config; 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;
}
} }

View File

@ -0,0 +1,49 @@
<?php declare(strict_types=1);
namespace Nechaev\Reports\Features\Jira;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
class JiraApiClient {
private Client $client;
public function __construct(string $url, string $token) {
$this->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());
}
}
}

View File

@ -1,28 +1,10 @@
<?php <?php
/**
* @var array $config
* @var array $teams
*/
use GuzzleHttp\Client;
use Nechaev\Reports\Core\App; use Nechaev\Reports\Core\App;
require_once __DIR__ . '/../functions.php'; require_once __DIR__ . '/../functions.php';
require_once __DIR__ . '/../../init.php'; require_once __DIR__ . '/../../init.php';
// Извлекаем настройки из конфига
$jiraUrl = App::getConfig()->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; $host = App::getConfig()->postgres_host;
$port = App::getConfig()->postgres_port; $port = App::getConfig()->postgres_port;
@ -103,7 +85,7 @@ foreach ($teamList as $team) {
$boardId = $team['team_board_id']; $boardId = $team['team_board_id'];
$sprints = getClosedSprintsListByBoardId($client, $boardId, $numberOfSprints); $sprints = App::getJiraApiClient()->getClosedSprintsListByBoardId($boardId, $numberOfSprints);
foreach ($sprints as $sprintRawData) { 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)"; $sql = "INSERT INTO sprints (jira_sprint_id, team_id, sprint_name, sprint_activate_datetime, sprint_complete_datetime) VALUES (:jiraSprintId, :teamId, :sprintName, :sprintActivateDatetime, :sprintCompleteDatetime)";

View File

@ -89,40 +89,3 @@ function filterByExecutors(array $data, array $executors): array {
} }
return $result; 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());
}
}