54 lines
1.5 KiB
PHP
54 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
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.
|
|
*/
|
|
public function createIssue(string $title, string $body): bool
|
|
{
|
|
$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éé.');
|
|
|
|
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,
|
|
]);
|
|
} catch (\Throwable $e) {
|
|
Log::error('Appel API 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;
|
|
}
|
|
}
|