diff --git a/src/Controller/IssueController.php b/src/Controller/IssueController.php index 4ebc912..0230eee 100644 --- a/src/Controller/IssueController.php +++ b/src/Controller/IssueController.php @@ -69,22 +69,30 @@ class IssueController extends AbstractController return new JsonResponse(['message' => 'Erreur Redmine : Déplacement Interdit'], 400); } - $payload = [ - 'fixed_version_id' => $targetVersion, - 'agile_data_attributes' => ['agile_sprint_id' => $targetSprint], - 'status_id' => $targetStatus, - ]; - $this->redmineService->updateIssue($id, $payload, $this->getParameter('redmineApikey')); + try { + $payload = [ + 'fixed_version_id' => $targetVersion, + 'agile_data_attributes' => ['agile_sprint_id' => $targetSprint], + 'status_id' => $targetStatus, + ]; + $this->redmineService->updateIssue($id, $payload, $this->getUser()->getApikey()); + } catch (\RuntimeException $e) { + // Récupère le message de l'exception + $errorMessage = $e->getMessage(); + dump($e->getMessage()); + // Par exemple, retour JSON d'erreur : + return new JsonResponse(['message' => $errorMessage], 400); + } /* $payload = [ - 'id' => $id, - 'issue' => [ - 'status_id' => $targetStatus, - 'fixed_version_id' => $targetVersion, - ], - 'positions' => [], + 'id' => $id, + 'issue' => [ + 'status_id' => $targetStatus, + 'fixed_version_id' => $targetVersion, + ], + 'positions' => [], ]; foreach ($targetIssues as $key => $issue) { diff --git a/src/Service/RedmineService.php b/src/Service/RedmineService.php index ca8464b..0a78302 100644 --- a/src/Service/RedmineService.php +++ b/src/Service/RedmineService.php @@ -299,7 +299,7 @@ class RedmineService public function getIssue(int $issueId, string $apiKey): array { try { - $url = "{$this->baseUrl}/issues/{$issueId}.json?include=allowed_statuses"; + $url = "{$this->baseUrl}/issues/{$issueId}.json?include=allowed_statuses,journals"; $response = $this->client->request('GET', $url, [ 'headers' => [ @@ -345,7 +345,7 @@ class RedmineService } } - public function updateIssue(int $id, array $data, string $apiKey): array + public function updateIssue(int $id, array $data, ?string $apiKey): array { $url = $this->baseUrl.'/issues/'.$id.'.json'; @@ -358,25 +358,15 @@ class RedmineService 'json' => ['issue' => $data], ]); - $statusCode = $response->getStatusCode(); - $content = trim($response->getContent(false)); + if (200 !== $response->getStatusCode()) { + if (401 === $response->getStatusCode()) { + throw new \RuntimeException('Erreur Redmine ('.$response->getStatusCode().') : Opération non autorisée, avez-vous placé votre apikey redmine sur votre profil'); + } - // Si vide et code 200, c’est peut-être une réussite silencieuse - if ('' === $content) { - return ['success' => true, 'message' => 'OK, mais pas de contenu']; + throw new \RuntimeException('Erreur de communication avec Redmine : '.$response->getStatusCode()); } - $decoded = json_decode($content, true); - - if (isset($decoded['errors']) && is_array($decoded['errors']) && count($decoded['errors']) > 0) { - throw new \RuntimeException('Erreur Redmine : '.implode(', ', $decoded['errors'])); - } - - return $decoded; - } catch (ClientExceptionInterface|ServerExceptionInterface $e) { - // Erreur HTTP (4xx ou 5xx) - $errorBody = $e->getResponse()->getContent(false); - throw new \RuntimeException('Erreur Redmine: '.$errorBody, $e->getCode(), $e); + return $response->toArray(); } catch (TransportExceptionInterface $e) { throw new \RuntimeException('Erreur de communication avec Redmine : '.$e->getMessage()); } diff --git a/templates/issue/view.html.twig b/templates/issue/view.html.twig index a3e8f57..2d77af2 100644 --- a/templates/issue/view.html.twig +++ b/templates/issue/view.html.twig @@ -6,7 +6,15 @@
{{ issue.redmine.description|textile_to_html|raw }}
+