{"record":{"id":"283fb38e56a686ff","repo":"SonarSource/sonarqube","slug":"failed-to-fetch-gitlab-project-with-id-s-from","errorCode":null,"errorMessage":"Failed to fetch GitLab project with ID '%s' from '%s'","messagePattern":"Failed to fetch GitLab project with ID '(.+?)' from '(.+?)'","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"server/sonar-webserver-common/src/main/java/org/sonar/server/common/almsettings/gitlab/GitlabDevOpsProjectCreationContextService.java","lineNumber":99,"sourceCode":"  }\n\n  private String findPersonalAccessTokenOrThrow(AlmSettingDto almSettingDto) {\n    try (DbSession dbSession = dbClient.openSession(false)) {\n      String userUuid = requireNonNull(userSession.getUuid(), \"User UUID cannot be null.\");\n      Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);\n      return almPatDto.map(AlmPatDto::getPersonalAccessToken)\n        .orElseThrow(() -> new IllegalArgumentException(format(\"Personal access token for '%s' is missing\", almSettingDto.getKey())));\n    }\n  }\n\n  private Project fetchGitlabProject(String gitlabUrl, String pat, Long gitlabProjectId) {\n    try {\n      return gitlabApplicationClient.getProject(\n        gitlabUrl,\n        pat,\n        gitlabProjectId);\n    } catch (GitlabServerException e) {\n      throw new IllegalStateException(format(\"Failed to fetch GitLab project with ID '%s' from '%s'\", gitlabProjectId, gitlabUrl), e);\n    }\n  }\n\n  private Optional<String> getDefaultBranchOnGitlab(String gitlabUrl, String pat, long gitlabProjectId) {\n    Optional<GitLabBranch> almMainBranch = gitlabApplicationClient.getBranches(gitlabUrl, pat, gitlabProjectId).stream().filter(GitLabBranch::isDefault).findFirst();\n    return almMainBranch.map(GitLabBranch::getName);\n  }\n\n}\n","sourceCodeStart":81,"sourceCodeEnd":109,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-webserver-common/src/main/java/org/sonar/server/common/almsettings/gitlab/GitlabDevOpsProjectCreationContextService.java#L81-L109","documentation":"This IllegalStateException wraps a GitlabServerException raised by the GitLab application client while fetching a project by numeric ID via GET /projects/:id. It is thrown when GitLab rejects or fails the API call during DevOps platform binding of a project. The original exception is preserved as the cause.","triggerScenarios":"Calling createGitlabProject / DevOps project creation with a gitlabProjectId that does not exist in the GitLab instance, an expired/insufficient Personal Access Token, an incorrect gitlabUrl, or a network error reaching GitLab while gitlabApplicationClient.getProject executes.","commonSituations":"Admin entered a wrong project ID when binding a SonarQube project to GitLab; PAT lacks 'api' scope or was revoked; GitLab URL points to a different instance than the project ID belongs to; GitLab is temporarily down or behind a misconfigured reverse proxy returning 5xx.","solutions":["Verify the GitLab project ID exists: GET <gitlabUrl>/api/v4/projects/<id> with the same PAT","Regenerate the PAT with api (read_api) scope and non-expired expiry, and update the ALM setting in SonarQube","Confirm the GitLab URL in the DevOps platform configuration is correct and reachable from SonarQube","Inspect the cause (GitlabServerException) message/status code to distinguish 401/404 vs connectivity failure"],"exampleFix":"// before\nlong gitlabProjectId = 12345L; // guessed ID\n// after\nlong gitlabProjectId = 42L; // from GitLab UI: Settings > General > Project ID, verified via GET /projects/42","handlingStrategy":"try-catch","validationCode":"// pre-check\nHttpResponse r = client.send(GET gitlabUrl + \"/api/v4/projects/\" + id, \"PRIVATE-TOKEN\", pat);\nif (r.status == 404) throw new IllegalArgumentException(\"project id \" + id + \" not found on \" + gitlabUrl);\nif (r.status == 401) throw new IllegalArgumentException(\"PAT invalid or missing api scope\");","typeGuard":"boolean isReachableGitlabProject(long id, String url, String pat) {\n  int s = probeStatus(url + \"/api/v4/projects/\" + id, pat);\n  return s == 200;\n}","tryCatchPattern":"try {\n  project = gitlabApplicationClient.getProject(url, pat, id);\n} catch (GitlabServerException e) {\n  log.error(\"GitLab project fetch failed: status={}, cause={}\", e.status(), e.getCause(), e);\n  throw new IllegalStateException(\"Failed to fetch GitLab project \" + id, e);\n}","preventionTips":["Verify project ID via GitLab UI (Settings > General) before binding","Keep PATs scoped (api) and track expiry dates","Confirm GitLab URL matches the instance the project belongs to","Monitor cause exception status codes (401 vs 404 vs 5xx) to separate config vs outage"],"tags":["gitlab","api-client","integration","configuration"],"backgroundTag":"api-error-response","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}