Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/Reference/GitlabReferenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions lib/Service/GitlabAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,23 @@ 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];
}

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];
}
Expand Down