feat : add branch name generation endpoint
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
25
src/ApiResource/GiteaBranchName.php
Normal file
25
src/ApiResource/GiteaBranchName.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\ApiResource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use App\State\GiteaBranchNameProvider;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new Get(
|
||||
uriTemplate: '/tasks/{taskId}/gitea/branch-name/{type}',
|
||||
normalizationContext: ['groups' => ['gitea_branch_name:read']],
|
||||
provider: GiteaBranchNameProvider::class,
|
||||
),
|
||||
],
|
||||
)]
|
||||
final class GiteaBranchName
|
||||
{
|
||||
#[Groups(['gitea_branch_name:read'])]
|
||||
public string $name = '';
|
||||
}
|
||||
42
src/State/GiteaBranchNameProvider.php
Normal file
42
src/State/GiteaBranchNameProvider.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\GiteaBranchName;
|
||||
use App\Entity\Task;
|
||||
use App\Service\GiteaApiService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
final readonly class GiteaBranchNameProvider implements ProviderInterface
|
||||
{
|
||||
private const array ALLOWED_TYPES = ['feature', 'fix', 'refactor', 'hotfix', 'chore'];
|
||||
|
||||
public function __construct(
|
||||
private GiteaApiService $giteaApiService,
|
||||
private EntityManagerInterface $em,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): GiteaBranchName
|
||||
{
|
||||
$task = $this->em->getRepository(Task::class)->find($uriVariables['taskId'] ?? 0);
|
||||
if (null === $task) {
|
||||
throw new NotFoundHttpException('Task not found.');
|
||||
}
|
||||
|
||||
$type = $uriVariables['type'] ?? 'feature';
|
||||
if (!in_array($type, self::ALLOWED_TYPES, true)) {
|
||||
throw new BadRequestHttpException('Invalid branch type.');
|
||||
}
|
||||
|
||||
$dto = new GiteaBranchName();
|
||||
$dto->name = $this->giteaApiService->generateBranchName($task, $type);
|
||||
|
||||
return $dto;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user