70 lines
2.1 KiB
PHP
70 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\State;
|
|
|
|
use ApiPlatform\Metadata\Operation;
|
|
use ApiPlatform\Metadata\Post;
|
|
use ApiPlatform\State\ProviderInterface;
|
|
use App\ApiResource\GiteaBranch;
|
|
use App\Entity\Task;
|
|
use App\Exception\GiteaApiException;
|
|
use App\Service\GiteaApiService;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
final readonly class GiteaBranchProvider implements ProviderInterface
|
|
{
|
|
public function __construct(
|
|
private GiteaApiService $giteaApiService,
|
|
private EntityManagerInterface $em,
|
|
) {}
|
|
|
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array|GiteaBranch
|
|
{
|
|
if ($operation instanceof Post) {
|
|
return new GiteaBranch();
|
|
}
|
|
|
|
$task = $this->em->getRepository(Task::class)->find($uriVariables['taskId'] ?? 0);
|
|
if (null === $task || null === $task->getProject()) {
|
|
return [];
|
|
}
|
|
|
|
$project = $task->getProject();
|
|
if (!$project->hasGiteaRepo()) {
|
|
return [];
|
|
}
|
|
|
|
$taskCode = $project->getCode().'-'.$task->getNumber();
|
|
|
|
try {
|
|
$branches = $this->giteaApiService->listBranches($project, $taskCode);
|
|
} catch (GiteaApiException) {
|
|
return [];
|
|
}
|
|
|
|
$result = [];
|
|
foreach ($branches as $branch) {
|
|
$dto = new GiteaBranch();
|
|
$dto->name = $branch['name'];
|
|
|
|
try {
|
|
$commits = $this->giteaApiService->listBranchCommits($project, $branch['name']);
|
|
$dto->commits = array_map(static fn (array $c): array => [
|
|
'sha' => substr($c['sha'] ?? '', 0, 7),
|
|
'message' => $c['commit']['message'] ?? '',
|
|
'author' => $c['commit']['author']['name'] ?? '',
|
|
'date' => $c['commit']['author']['date'] ?? $c['created'] ?? '',
|
|
], $commits);
|
|
} catch (GiteaApiException) {
|
|
$dto->commits = [];
|
|
}
|
|
|
|
$result[] = $dto;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|