feat : add Gitea repositories list endpoint
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
32
src/ApiResource/GiteaRepository.php
Normal file
32
src/ApiResource/GiteaRepository.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\ApiResource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use App\State\GiteaRepositoryProvider;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new GetCollection(
|
||||
uriTemplate: '/gitea/repositories',
|
||||
normalizationContext: ['groups' => ['gitea_repo:read']],
|
||||
provider: GiteaRepositoryProvider::class,
|
||||
security: "is_granted('ROLE_ADMIN')",
|
||||
),
|
||||
],
|
||||
)]
|
||||
final class GiteaRepository
|
||||
{
|
||||
#[Groups(['gitea_repo:read'])]
|
||||
public string $fullName = '';
|
||||
|
||||
#[Groups(['gitea_repo:read'])]
|
||||
public string $name = '';
|
||||
|
||||
#[Groups(['gitea_repo:read'])]
|
||||
public string $owner = '';
|
||||
}
|
||||
36
src/State/GiteaRepositoryProvider.php
Normal file
36
src/State/GiteaRepositoryProvider.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\GiteaRepository;
|
||||
use App\Exception\GiteaApiException;
|
||||
use App\Service\GiteaApiService;
|
||||
|
||||
final readonly class GiteaRepositoryProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private GiteaApiService $giteaApiService,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
|
||||
{
|
||||
try {
|
||||
$repos = $this->giteaApiService->listRepositories();
|
||||
} catch (GiteaApiException) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_map(static function (array $repo): GiteaRepository {
|
||||
$dto = new GiteaRepository();
|
||||
$dto->fullName = $repo['full_name'] ?? '';
|
||||
$dto->name = $repo['name'] ?? '';
|
||||
$dto->owner = $repo['owner']['login'] ?? '';
|
||||
|
||||
return $dto;
|
||||
}, $repos);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user