feat : add task Gitea branches endpoints (list + create)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Matthieu
2026-03-13 13:58:41 +01:00
parent 7b1aa22c15
commit dcbf5db308
3 changed files with 170 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
<?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->listCommits($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;
}
}