feat(tickets): suivi bidirectionnel des tickets utilisateur via Gitea
CI - Intégration continue / quality-and-tests (pull_request) Successful in 3m27s
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>
This commit is contained in:
@@ -2,52 +2,132 @@
|
||||
|
||||
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
|
||||
{
|
||||
/**
|
||||
* Crée un ticket (issue) dans le dépôt Gitea configuré.
|
||||
*
|
||||
* @return bool true si le ticket a bien été créé, false sinon.
|
||||
* Prépare un client HTTP authentifié, ou null si Gitea n'est pas configuré.
|
||||
*/
|
||||
public function createIssue(string $title, string $body): bool
|
||||
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) : ticket non créé.');
|
||||
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 {
|
||||
// Gitea attend l'en-tête "Authorization: token <TOKEN>".
|
||||
$response = Http::withToken($token, 'token')
|
||||
->acceptJson()
|
||||
->timeout(10)
|
||||
->post("{$url}/api/v1/repos/{$repo}/issues", [
|
||||
'title' => $title,
|
||||
'body' => $body,
|
||||
]);
|
||||
$response = $client->post("/issues/{$number}/comments", ['body' => $body]);
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Appel API Gitea impossible : '.$e->getMessage());
|
||||
Log::error('Ajout de commentaire Gitea impossible : '.$e->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($response->failed()) {
|
||||
Log::error('Création du ticket Gitea échouée', [
|
||||
'status' => $response->status(),
|
||||
'body' => $response->body(),
|
||||
]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return $response->successful();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user