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,46 @@
<?php
declare(strict_types=1);
namespace App\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use App\State\GiteaBranchProcessor;
use App\State\GiteaBranchProvider;
use Symfony\Component\Serializer\Attribute\Groups;
#[ApiResource(
operations: [
new GetCollection(
uriTemplate: '/tasks/{taskId}/gitea/branches',
normalizationContext: ['groups' => ['gitea_branch:read']],
provider: GiteaBranchProvider::class,
),
new Post(
uriTemplate: '/tasks/{taskId}/gitea/branches',
denormalizationContext: ['groups' => ['gitea_branch:write']],
normalizationContext: ['groups' => ['gitea_branch:read']],
provider: GiteaBranchProvider::class,
processor: GiteaBranchProcessor::class,
),
],
)]
final class GiteaBranch
{
#[Groups(['gitea_branch:read'])]
public string $name = '';
#[Groups(['gitea_branch:write'])]
public string $type = 'feature';
#[Groups(['gitea_branch:write'])]
public string $baseBranch = 'main';
/**
* @var array<array{sha: string, message: string, author: string, date: string}>
*/
#[Groups(['gitea_branch:read'])]
public array $commits = [];
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\ApiResource\GiteaBranch;
use App\Entity\Task;
use App\Exception\GiteaApiException;
use App\Service\GiteaApiService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
final readonly class GiteaBranchProcessor implements ProcessorInterface
{
private const array ALLOWED_TYPES = ['feature', 'fix', 'refactor', 'hotfix', 'chore'];
public function __construct(
private GiteaApiService $giteaApiService,
private EntityManagerInterface $em,
) {}
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): GiteaBranch
{
assert($data instanceof GiteaBranch);
$task = $this->em->getRepository(Task::class)->find($uriVariables['taskId'] ?? 0);
if (null === $task || null === $task->getProject()) {
throw new NotFoundHttpException('Task not found.');
}
$project = $task->getProject();
if (!$project->hasGiteaRepo()) {
throw new BadRequestHttpException('Project has no Gitea repository.');
}
if (!in_array($data->type, self::ALLOWED_TYPES, true)) {
throw new BadRequestHttpException('Invalid branch type.');
}
try {
$branchName = $this->giteaApiService->createBranch($project, $task, $data->type, $data->baseBranch);
} catch (GiteaApiException $e) {
throw new BadRequestHttpException($e->getMessage());
}
$result = new GiteaBranch();
$result->name = $branchName;
return $result;
}
}

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;
}
}