diff --git a/src/ApiResource/GiteaBranch.php b/src/ApiResource/GiteaBranch.php new file mode 100644 index 0000000..8a5e5ed --- /dev/null +++ b/src/ApiResource/GiteaBranch.php @@ -0,0 +1,46 @@ + ['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 + */ + #[Groups(['gitea_branch:read'])] + public array $commits = []; +} diff --git a/src/State/GiteaBranchProcessor.php b/src/State/GiteaBranchProcessor.php new file mode 100644 index 0000000..6814630 --- /dev/null +++ b/src/State/GiteaBranchProcessor.php @@ -0,0 +1,55 @@ +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; + } +} diff --git a/src/State/GiteaBranchProvider.php b/src/State/GiteaBranchProvider.php new file mode 100644 index 0000000..62ebde1 --- /dev/null +++ b/src/State/GiteaBranchProvider.php @@ -0,0 +1,69 @@ +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; + } +}