diff --git a/lib/Reference/GitlabReferenceProvider.php b/lib/Reference/GitlabReferenceProvider.php index eb6e3bc..ed425e7 100644 --- a/lib/Reference/GitlabReferenceProvider.php +++ b/lib/Reference/GitlabReferenceProvider.php @@ -153,6 +153,8 @@ public function resolveReference(string $referenceText): ?IReference { $projectLabels = $account !== null ? $this->gitlabAPIService->getProjectLabels($account, $baseUrl, $projectInfo['id']) : []; $commentInfo = $this->getIssueCommentInfo($account, $baseUrl, $projectInfo['id'], $issueId, $end); $issueInfo = $this->gitlabAPIService->getIssueInfo($account, $baseUrl, $projectInfo['id'], $issueId); + // Ensure all issue labels are in projectLabels (some may be group-level labels not returned by project labels API) + $projectLabels = $this->ensureLabelsExist($projectLabels, $issueInfo['labels'] ?? []); $reference = new Reference($referenceText); $reference->setRichObject( Application::APP_ID, @@ -183,6 +185,8 @@ public function resolveReference(string $referenceText): ?IReference { $projectLabels = $account !== null ? $this->gitlabAPIService->getProjectLabels($account, $baseUrl, $projectInfo['id']) : []; $commentInfo = $this->getPrCommentInfo($account, $baseUrl, $projectInfo['id'], $prId, $end); $prInfo = $this->gitlabAPIService->getPrInfo($account, $baseUrl, $projectInfo['id'], $prId); + // Ensure all PR labels are in projectLabels (some may be group-level labels not returned by project labels API) + $projectLabels = $this->ensureLabelsExist($projectLabels, $prInfo['labels'] ?? []); $reference = new Reference($referenceText); $reference->setRichObject( Application::APP_ID, @@ -319,6 +323,34 @@ private function getGenericPrInfo(array $prInfo, array $projectLabels): array { return $info; } + /** + * Ensure all labels from the issue/PR exist in the project labels array. + * This is needed because some labels may be group-level labels that are not returned + * by the project labels API, causing frontend errors when trying to find them. + * + * @param array $projectLabels + * @param array $itemLabels + * @return array + */ + private function ensureLabelsExist(array $projectLabels, array $itemLabels): array { + $existingLabelNames = array_map(static fn (array $label) => $label['name'], $projectLabels); + + foreach ($itemLabels as $labelName) { + if (!in_array($labelName, $existingLabelNames, true)) { + // Add a minimal label entry for labels not found in project labels + $projectLabels[] = [ + 'id' => null, + 'name' => $labelName, + 'color' => '#808080', + 'text_color' => '#FFFFFF', + 'description' => null, + ]; + } + } + + return $projectLabels; + } + /** * @param string $gitlabUrl * @param string $url diff --git a/lib/Service/GitlabAPIService.php b/lib/Service/GitlabAPIService.php index d72287f..af96d4a 100644 --- a/lib/Service/GitlabAPIService.php +++ b/lib/Service/GitlabAPIService.php @@ -214,7 +214,11 @@ public function getGroupsList(GitLabAccount $account): array { public function getUserAvatar(GitlabAccount $account, string $baseUrl, int $gitlabUserId): array { $userInfo = $this->request($account, $baseUrl, 'users/' . $gitlabUserId); if (!isset($userInfo['error']) && isset($userInfo['avatar_url'])) { - return ['avatarContent' => $this->client->get($userInfo['avatar_url'])->getBody()]; + try { + return ['avatarContent' => $this->client->get($userInfo['avatar_url'])->getBody()]; + } catch (Exception $e) { + $this->logger->debug('Failed to fetch GitLab user avatar: ' . $e->getMessage(), ['app' => Application::APP_ID]); + } } return ['userInfo' => $userInfo]; } @@ -222,7 +226,11 @@ public function getUserAvatar(GitlabAccount $account, string $baseUrl, int $gitl public function getProjectAvatar(GitlabAccount $account, string $baseUrl, int $projectId): array { $projectInfo = $this->request($account, $baseUrl, 'projects/' . $projectId); if (!isset($projectInfo['error']) && isset($projectInfo['avatar_url'])) { - return ['avatarContent' => $this->client->get($projectInfo['avatar_url'])->getBody()]; + try { + return ['avatarContent' => $this->client->get($projectInfo['avatar_url'])->getBody()]; + } catch (Exception $e) { + $this->logger->debug('Failed to fetch GitLab project avatar: ' . $e->getMessage(), ['app' => Application::APP_ID]); + } } return ['projectInfo' => $projectInfo]; }