Добавлен Jira Api Client
parent
b325cb227f
commit
a5d66fb9a3
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,28 +1,10 @@
|
|||
<?php
|
||||
/**
|
||||
* @var array $config
|
||||
* @var array $teams
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Nechaev\Reports\Core\App;
|
||||
|
||||
require_once __DIR__ . '/../functions.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;
|
||||
$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)";
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue