client(); if ($client === null) { return null; } try { $response = $client->post($this->repoPath().'/issues', [ 'title' => $title, 'body' => $body, ]); } catch (\Throwable $e) { Log::error('Appel API Gitea impossible : '.$e->getMessage()); return null; } if ($response->failed()) { Log::error('Création du ticket Gitea échouée', [ 'status' => $response->status(), 'body' => $response->body(), ]); return null; } return (int) $response->json('number'); } /** * Récupère l'état d'un ticket (état ouvert/fermé, nombre de commentaires). * * @return array{state: string, comments: int, title: string}|null */ public function getIssue(int $number): ?array { $client = $this->client(); if ($client === null) { return null; } try { $response = $client->get($this->repoPath().'/issues/'.$number); } catch (\Throwable $e) { Log::error('Lecture du ticket Gitea impossible : '.$e->getMessage()); return null; } if ($response->failed()) { return null; } return [ 'state' => (string) $response->json('state'), 'comments' => (int) $response->json('comments'), 'title' => (string) $response->json('title'), ]; } /** * Récupère les commentaires (réponses) d'un ticket. * * @return list */ public function getComments(int $number): array { $client = $this->client(); if ($client === null) { return []; } try { $response = $client->get($this->repoPath().'/issues/'.$number.'/comments'); } catch (\Throwable $e) { Log::error('Lecture des commentaires Gitea impossible : '.$e->getMessage()); return []; } if ($response->failed()) { return []; } $commentaires = []; foreach ($response->json() ?? [] as $commentaire) { $commentaires[] = [ 'auteur' => (string) ($commentaire['user']['username'] ?? $commentaire['user']['login'] ?? 'inconnu'), 'corps' => (string) ($commentaire['body'] ?? ''), 'date' => (string) ($commentaire['created_at'] ?? ''), ]; } return $commentaires; } /** * Ajoute un commentaire (réponse de l'utilisateur) sur un ticket. */ public function addComment(int $number, string $body): bool { $client = $this->client(); if ($client === null) { return false; } try { $response = $client->post($this->repoPath().'/issues/'.$number.'/comments', [ 'body' => $body, ]); } catch (\Throwable $e) { Log::error('Ajout du commentaire Gitea impossible : '.$e->getMessage()); return false; } if ($response->failed()) { Log::error('Ajout du commentaire Gitea échoué', [ 'status' => $response->status(), 'body' => $response->body(), ]); return false; } return true; } /** * Client HTTP configuré, ou null si la configuration Gitea est incomplète. */ private function client(): ?PendingRequest { $url = rtrim((string) config('services.gitea.url'), '/'); $token = (string) config('services.gitea.token'); if ($url === '' || $this->repo() === '' || $token === '') { Log::warning('Gitea non configuré (URL / token / dépôt manquant).'); return null; } // Gitea attend l'en-tête "Authorization: token ". return Http::baseUrl($url) ->withToken($token, 'token') ->acceptJson() ->timeout(10); } private function repo(): string { return trim((string) config('services.gitea.repo'), '/'); } private function repoPath(): string { return '/api/v1/repos/'.$this->repo(); } }