From fde8f9690a55a8f585ce160a3f5a7dfabb59e97b Mon Sep 17 00:00:00 2001 From: "an.nechaev" Date: Thu, 18 Jun 2026 19:18:02 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=BE=D0=BD?= =?UTF-8?q?=D0=B0=D0=BB=20=D1=8D=D0=BA=D1=81=D0=BF=D0=BE=D1=80=D1=82=D0=B0?= =?UTF-8?q?=20=D1=81=D0=BF=D1=80=D0=B8=D0=BD=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/postgres/init/001_schema.sql | 3 +- docker/postgres/init/002_dump.sql | 10 +++--- src/Features/Jira/JiraApiClient.php | 16 ++++++++-- src/console/jira_export_sprints.php | 49 ++++++++++++++++++++--------- 4 files changed, 55 insertions(+), 23 deletions(-) diff --git a/docker/postgres/init/001_schema.sql b/docker/postgres/init/001_schema.sql index 473ef21..94f4ce0 100644 --- a/docker/postgres/init/001_schema.sql +++ b/docker/postgres/init/001_schema.sql @@ -2,7 +2,8 @@ CREATE TABLE IF NOT EXISTS teams ( id SERIAL PRIMARY KEY, team_name character(16) NOT NULL, team_board_id INT NOT NULL, - has_own_project BOOLEAN NOT NULL DEFAULT FALSE + has_own_project BOOLEAN NOT NULL DEFAULT FALSE, + sprint_alias TEXT ); CREATE TYPE role AS ENUM ('analytic', 'backend', 'designer', 'frontend', 'qa', 'project'); diff --git a/docker/postgres/init/002_dump.sql b/docker/postgres/init/002_dump.sql index 5263cd8..8fcdc2a 100644 --- a/docker/postgres/init/002_dump.sql +++ b/docker/postgres/init/002_dump.sql @@ -1,10 +1,10 @@ -- Teams -INSERT INTO teams (id, team_name, team_board_id, has_own_project) +INSERT INTO teams (id, team_name, team_board_id, has_own_project, sprint_alias) VALUES - (1, 'ts', 613, TRUE), - (2, 'ts-uh', 327, FALSE), - (3, 'ts-prime', 327, FALSE), - (4, 'bryansk', 166, TRUE); + (1, 'ts', 613, TRUE, 'TS'), + (2, 'ts-uh', 327, FALSE, 'Center'), + (3, 'ts-prime', 327, FALSE, 'Center'), + (4, 'bryansk', 166, TRUE, 'Брянск'); -- Employees diff --git a/src/Features/Jira/JiraApiClient.php b/src/Features/Jira/JiraApiClient.php index f1e175e..710d0df 100644 --- a/src/Features/Jira/JiraApiClient.php +++ b/src/Features/Jira/JiraApiClient.php @@ -19,7 +19,7 @@ class JiraApiClient { ]); } - public function getClosedSprintsListByBoardId(int $boardId, int $lastCount = 100): array + public function getClosedSprintsListByBoardId(int $boardId, int $lastCount = 100, string $alias = ''): array { $sprintList = []; try { @@ -38,7 +38,19 @@ class JiraApiClient { $isLast = $sprints['isLast']; $startAt += 50; - $sprintList = array_merge($sprintList, $sprints['values']); + + $newSprints = []; + if ($alias !== '') { + foreach ($sprints['values'] as $sprint) { + if (str_starts_with($sprint['name'], $alias)) { + $newSprints[] = $sprint; + } + } + } else { + $newSprints = $sprints['values']; + } + + $sprintList = array_merge($sprintList, $newSprints); } while (!$isLast); return array_slice($sprintList, $lastCount*(-1)); diff --git a/src/console/jira_export_sprints.php b/src/console/jira_export_sprints.php index 40b19fe..ec2b47d 100644 --- a/src/console/jira_export_sprints.php +++ b/src/console/jira_export_sprints.php @@ -6,7 +6,7 @@ require_once __DIR__ . '/../functions.php'; require_once __DIR__ . '/../../init.php'; try { - $teamList = App::getDb()->getRowset("SELECT id, TRIM(team_name) as name, team_board_id FROM teams"); + $teamList = App::getDb()->getRowset("SELECT id, TRIM(team_name) as name, team_board_id, sprint_alias FROM teams"); $availableTeams = array_column($teamList, 'name'); } catch (PDOException $e) { die("Ошибка: " . $e->getMessage()); @@ -14,7 +14,7 @@ try { $numberOfSprints = 100; -$options = getopt('h::', ['team::', 'number-of-sprints::', 'non-interactive::', 'help::']); +$options = getopt('h::', ['team::', 'number-of-sprints::', 'non-interactive::', 'dry-run::', 'help::']); if (isset($options['h']) || isset($options['help'])) { $helpText = "Использование: php " . basename($argv[0]) . " [ОПЦИИ]\n" . @@ -23,7 +23,8 @@ if (isset($options['h']) || isset($options['help'])) { " -h, --help Вывести эту справку\n" . " --team Команда, для которой сделать экспорт. Доступные варианты: ".implode(', ', $availableTeams)."\n" . " --number-of-sprints Количество спринтов для экпорта, начиная с самых свежих, по умолчанию: {$numberOfSprints}\n" . - " --non-interactive Не задавать вопросы в процессе работы\n"; + " --non-interactive Не задавать вопросы в процессе работы\n" . + " --dry-run Не записывать данные в БД, только вывести, что будет сделано\n"; fwrite(STDOUT, $helpText); exit(0); @@ -33,6 +34,8 @@ $team = 'all'; $interactiveMode = !isset($options['non-interactive']); +$dryRun = isset($options['dry-run']); + if (!empty($options['team'])) { if (!in_array(strtolower($options['team']), $availableTeams)) { echo "Не корректное название команды: '".$options['team']."'\n"; @@ -47,6 +50,10 @@ if (!empty($options['number-of-sprints'])) { $numberOfSprints = ((int)$options['number-of-sprints']); } +if ($dryRun) { + echo "Режим dry-run: данные не будут записаны в БД.\n"; +} + if ($interactiveMode) { $answer = readline("Получаем данные по спринтам команды '{$team}' в количестве {$numberOfSprints} шт. Продолжить? (Y/n): "); // Приводим ответ к нижнему регистру для надежности @@ -70,25 +77,37 @@ foreach ($teamList as $team) { } $boardId = $team['team_board_id']; + $sprintAlias = isset($team['sprint_alias']) ? trim((string)$team['sprint_alias']) : ''; - $sprints = App::getJiraApiClient()->getClosedSprintsListByBoardId($boardId, $numberOfSprints); + $sprints = App::getJiraApiClient()->getClosedSprintsListByBoardId($boardId, $numberOfSprints, $sprintAlias); foreach ($sprints as $sprintRawData) { $data = []; - $data['jiraSprintId'] = (int)$sprintRawData['jiraSprintId']; - $data['teamId'] = (int)$sprintRawData['teamId']; + $data['jiraSprintId'] = (int)$sprintRawData['id']; + $data['teamId'] = (int)$team['id']; $data['sprintName'] = $sprintRawData['name']; - $data['sprintActivateDatetime'] = $sprintRawData['activeDate']; - $data['sprintCompleteDatetime'] = $sprintRawData['completionDate']; + $data['sprintActivateDatetime'] = $sprintRawData['activatedDate']; + $data['sprintCompleteDatetime'] = $sprintRawData['completeDate']; $query = " - INSERT INTO sprints - SET - jira_sprint_id = :jiraSprintId, - team_id = :teamId, - sprint_name = :sprintName, - sprint_activate_datetime = :sprintActivateDatetime, - sprint_complete_datetime = :sprintCompleteDatetime + INSERT INTO sprints ( + jira_sprint_id, + team_id, + sprint_name, + sprint_activate_datetime, + sprint_complete_datetime + ) VALUES ( + :jiraSprintId, + :teamId, + :sprintName, + :sprintActivateDatetime, + :sprintCompleteDatetime + ) "; + if ($dryRun) { + echo "[dry-run] Спринт '{$data['sprintName']}' (jiraSprintId={$data['jiraSprintId']}, teamId={$data['teamId']}) будет добавлен.\n"; + continue; + } + App::getDb()->execute($query, $data); } }