7d52c1e440
CI - Intégration continue / quality-and-tests (pull_request) Successful in 3m27s
Le formulaire 'fire-and-forget' devient un vrai systeme de suivi : - table locale tickets (user <-> numero d'issue Gitea) - liste 'Mes tickets' avec etat (En cours / Resolu) et jalon - page de suivi : etat, labels, jalon, fil de discussion (commentaires) - reponse depuis le web (ajoute un commentaire sur l'issue Gitea) - controle d'acces : un utilisateur ne voit que ses propres tickets - GiteaIssueService etendu (getIssue, getComments, addComment) - remplace l'ancien SuggestionController ; 5 tests de bout en bout Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
134 lines
3.5 KiB
PHP
134 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Http\Client\PendingRequest;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
/**
|
|
* Petit client de l'API Gitea (issues) utilisé par le système de tickets :
|
|
* création, consultation, commentaires.
|
|
*/
|
|
class GiteaIssueService
|
|
{
|
|
/**
|
|
* Prépare un client HTTP authentifié, ou null si Gitea n'est pas configuré.
|
|
*/
|
|
private function client(): ?PendingRequest
|
|
{
|
|
$url = rtrim((string) config('services.gitea.url'), '/');
|
|
$repo = trim((string) config('services.gitea.repo'), '/');
|
|
$token = (string) config('services.gitea.token');
|
|
|
|
if ($url === '' || $repo === '' || $token === '') {
|
|
Log::warning('Gitea non configuré (URL / token / dépôt manquant).');
|
|
|
|
return null;
|
|
}
|
|
|
|
// Gitea attend l'en-tête "Authorization: token <TOKEN>".
|
|
return Http::withToken($token, 'token')
|
|
->acceptJson()
|
|
->timeout(10)
|
|
->baseUrl("{$url}/api/v1/repos/{$repo}");
|
|
}
|
|
|
|
/**
|
|
* Crée un ticket (issue). Retourne les données de l'issue créée
|
|
* (dont "number") ou null en cas d'échec.
|
|
*
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
public function createIssue(string $title, string $body): ?array
|
|
{
|
|
$client = $this->client();
|
|
if (! $client) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
$response = $client->post('/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()]);
|
|
|
|
return null;
|
|
}
|
|
|
|
return (array) $response->json();
|
|
}
|
|
|
|
/**
|
|
* Récupère une issue (état, labels, jalon…).
|
|
*
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
public function getIssue(int $number): ?array
|
|
{
|
|
$client = $this->client();
|
|
if (! $client) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
$response = $client->get("/issues/{$number}");
|
|
} catch (\Throwable $e) {
|
|
Log::error('Lecture du ticket Gitea impossible : '.$e->getMessage());
|
|
|
|
return null;
|
|
}
|
|
|
|
return $response->successful() ? (array) $response->json() : null;
|
|
}
|
|
|
|
/**
|
|
* Récupère les commentaires d'une issue (fil de discussion).
|
|
*
|
|
* @return array<int, mixed>
|
|
*/
|
|
public function getComments(int $number): array
|
|
{
|
|
$client = $this->client();
|
|
if (! $client) {
|
|
return [];
|
|
}
|
|
|
|
try {
|
|
$response = $client->get("/issues/{$number}/comments");
|
|
} catch (\Throwable $e) {
|
|
Log::error('Lecture des commentaires Gitea impossible : '.$e->getMessage());
|
|
|
|
return [];
|
|
}
|
|
|
|
return $response->successful() ? (array) $response->json() : [];
|
|
}
|
|
|
|
/**
|
|
* Ajoute un commentaire (réponse) à une issue.
|
|
*/
|
|
public function addComment(int $number, string $body): bool
|
|
{
|
|
$client = $this->client();
|
|
if (! $client) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
$response = $client->post("/issues/{$number}/comments", ['body' => $body]);
|
|
} catch (\Throwable $e) {
|
|
Log::error('Ajout de commentaire Gitea impossible : '.$e->getMessage());
|
|
|
|
return false;
|
|
}
|
|
|
|
return $response->successful();
|
|
}
|
|
}
|