Repository navigation
Expand file tree
/
Copy pathTtSyncSubticketsCommand.php
More file actions
103 lines (89 loc) · 3.39 KB
/
Copy pathTtSyncSubticketsCommand.php
File metadata and controls
103 lines (89 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/*
* Copyright (c) 2025-2026 Netresearch DTT GmbH
* SPDX-License-Identifier: AGPL-3.0-only
*/
declare(strict_types=1);
namespace App\Command;
use App\Entity\Project;
use App\Exception\Integration\Jira\JiraApiUnauthorizedException;
use App\Service\SubticketSyncService;
use Doctrine\ORM\EntityManagerInterface;
use InvalidArgumentException;
use LogicException;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function count;
use function is_scalar;
#[AsCommand(name: 'tt:sync-subtickets', description: 'Update project subtickets from Jira')]
class TtSyncSubticketsCommand extends Command
{
/**
* @throws LogicException
*/
public function __construct(private readonly SubticketSyncService $subticketSyncService, private readonly EntityManagerInterface $entityManager)
{
parent::__construct();
}
/**
* @throws InvalidArgumentException
*
* @psalm-return 0|1
*/
public function __invoke(#[Argument(description: 'Single project ID to update', name: 'project')]
?string $project, InputInterface $input, OutputInterface $output): int
{
$projectArg = $project;
$symfonyStyle = new SymfonyStyle($input, $output);
$projectId = is_scalar($projectArg) ? $projectArg : null;
$entityRepository = $this->entityManager
->getRepository(Project::class);
if (null !== $projectId && '' !== $projectId) {
$project = $entityRepository->find($projectId);
if (!$project instanceof Project) {
$symfonyStyle->error('Project does not exist');
return 1;
}
$projects = [$project];
} else {
$projects = $entityRepository->createQueryBuilder('p')
->where('p.ticketSystem IS NOT NULL')
->getQuery()
->getResult();
}
/** @var array<int, Project> $projects */
$count = count($projects);
$output->writeln(
'Found ' . $count . ' projects with ticket system',
OutputInterface::VERBOSITY_VERBOSE,
);
foreach ($projects as $projectEntity) {
$projectId = $projectEntity->getId() ?? 0;
$projectName = $projectEntity->getName();
$output->writeln(
'Syncing ' . $projectId . ' ' . $projectName,
OutputInterface::VERBOSITY_VERBOSE,
);
try {
$subtickets = $this->subticketSyncService->syncProjectSubtickets($projectEntity);
} catch (JiraApiUnauthorizedException $e) {
throw new JiraApiUnauthorizedException($e->getMessage() . ' - project ' . $projectEntity->getName(), $e->getCode(), $e->getRedirectUrl(), $e);
}
$output->writeln(
' ' . count($subtickets) . ' subtickets found',
OutputInterface::VERBOSITY_VERBOSE,
);
if ([] !== $subtickets) {
$output->writeln(
' ' . implode(',', $subtickets),
OutputInterface::VERBOSITY_VERY_VERBOSE,
);
}
}
return Command::SUCCESS;
}
}