feat : add task Gitea pull requests endpoint
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
49
src/ApiResource/GiteaPullRequest.php
Normal file
49
src/ApiResource/GiteaPullRequest.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\ApiResource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use App\State\GiteaPullRequestProvider;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new GetCollection(
|
||||
uriTemplate: '/tasks/{taskId}/gitea/pull-requests',
|
||||
normalizationContext: ['groups' => ['gitea_pr:read']],
|
||||
provider: GiteaPullRequestProvider::class,
|
||||
),
|
||||
],
|
||||
)]
|
||||
final class GiteaPullRequest
|
||||
{
|
||||
#[Groups(['gitea_pr:read'])]
|
||||
public int $number = 0;
|
||||
|
||||
#[Groups(['gitea_pr:read'])]
|
||||
public string $title = '';
|
||||
|
||||
#[Groups(['gitea_pr:read'])]
|
||||
public string $state = '';
|
||||
|
||||
#[Groups(['gitea_pr:read'])]
|
||||
public bool $merged = false;
|
||||
|
||||
#[Groups(['gitea_pr:read'])]
|
||||
public string $headBranch = '';
|
||||
|
||||
#[Groups(['gitea_pr:read'])]
|
||||
public string $author = '';
|
||||
|
||||
#[Groups(['gitea_pr:read'])]
|
||||
public string $url = '';
|
||||
|
||||
/**
|
||||
* @var array<array{context: string, status: string, target_url: string}>
|
||||
*/
|
||||
#[Groups(['gitea_pr:read'])]
|
||||
public array $ciStatuses = [];
|
||||
}
|
||||
60
src/State/GiteaPullRequestProvider.php
Normal file
60
src/State/GiteaPullRequestProvider.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\GiteaPullRequest;
|
||||
use App\Entity\Task;
|
||||
use App\Exception\GiteaApiException;
|
||||
use App\Service\GiteaApiService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
final readonly class GiteaPullRequestProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private GiteaApiService $giteaApiService,
|
||||
private EntityManagerInterface $em,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
|
||||
{
|
||||
$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 {
|
||||
$prs = $this->giteaApiService->listPullRequests($project, $taskCode);
|
||||
} catch (GiteaApiException) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_map(static function (array $pr): GiteaPullRequest {
|
||||
$dto = new GiteaPullRequest();
|
||||
$dto->number = $pr['number'] ?? 0;
|
||||
$dto->title = $pr['title'] ?? '';
|
||||
$dto->state = $pr['state'] ?? '';
|
||||
$dto->merged = $pr['merged'] ?? false;
|
||||
$dto->headBranch = $pr['head']['ref'] ?? '';
|
||||
$dto->author = $pr['user']['login'] ?? '';
|
||||
$dto->url = $pr['html_url'] ?? '';
|
||||
$dto->ciStatuses = array_map(static fn (array $s): array => [
|
||||
'context' => $s['context'] ?? '',
|
||||
'status' => $s['status'] ?? '',
|
||||
'target_url' => $s['target_url'] ?? '',
|
||||
], $pr['ci_statuses'] ?? []);
|
||||
|
||||
return $dto;
|
||||
}, $prs);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user