feat: ajout du suivi des suggestions avec intégration Gitea
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
@@ -10,37 +11,132 @@ 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.
|
||||
* @return int|null le numéro du ticket créé, ou null en cas d'échec.
|
||||
*/
|
||||
public function createIssue(string $title, string $body): bool
|
||||
public function createIssue(string $title, string $body): ?int
|
||||
{
|
||||
$url = rtrim((string) config('services.gitea.url'), '/');
|
||||
$repo = trim((string) config('services.gitea.repo'), '/');
|
||||
$token = (string) config('services.gitea.token');
|
||||
$client = $this->client();
|
||||
|
||||
if ($url === '' || $repo === '' || $token === '') {
|
||||
Log::warning('Gitea non configuré (URL / token / dépôt manquant) : ticket non créé.');
|
||||
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<array{auteur: string, corps: string, date: string}>
|
||||
*/
|
||||
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 {
|
||||
// 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($this->repoPath().'/issues/'.$number.'/comments', [
|
||||
'body' => $body,
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Appel API Gitea impossible : '.$e->getMessage());
|
||||
Log::error('Ajout du commentaire Gitea impossible : '.$e->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($response->failed()) {
|
||||
Log::error('Création du ticket Gitea échouée', [
|
||||
Log::error('Ajout du commentaire Gitea échoué', [
|
||||
'status' => $response->status(),
|
||||
'body' => $response->body(),
|
||||
]);
|
||||
@@ -50,4 +146,35 @@ class GiteaIssueService
|
||||
|
||||
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 <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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user