use of org.sonarqube.ws.AlmIntegrations.GitlabRepository in project sonarqube by SonarSource.
the class SearchGitlabReposAction method doHandle.
private AlmIntegrations.SearchGitlabReposWsResponse doHandle(Request request) {
String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
String projectName = request.param(PARAM_PROJECT_NAME);
int pageNumber = request.mandatoryParamAsInt("p");
int pageSize = request.mandatoryParamAsInt("ps");
try (DbSession dbSession = dbClient.openSession(false)) {
userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
String userUuid = requireNonNull(userSession.getUuid(), "User UUID cannot be null");
AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey).orElseThrow(() -> new NotFoundException(String.format("ALM Setting '%s' not found", almSettingKey)));
Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
String personalAccessToken = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
String gitlabUrl = requireNonNull(almSettingDto.getUrl(), "ALM url cannot be null");
ProjectList gitlabProjectList = gitlabHttpClient.searchProjects(gitlabUrl, personalAccessToken, projectName, pageNumber, pageSize);
Map<String, ProjectKeyName> sqProjectsKeyByGitlabProjectId = getSqProjectsKeyByGitlabProjectId(dbSession, almSettingDto, gitlabProjectList);
List<GitlabRepository> gitlabRepositories = gitlabProjectList.getProjects().stream().map(project -> toGitlabRepository(project, sqProjectsKeyByGitlabProjectId)).collect(toList());
Paging.Builder pagingBuilder = Paging.newBuilder().setPageIndex(gitlabProjectList.getPageNumber()).setPageSize(gitlabProjectList.getPageSize());
Integer gitlabProjectListTotal = gitlabProjectList.getTotal();
if (gitlabProjectListTotal != null) {
pagingBuilder.setTotal(gitlabProjectListTotal);
}
return AlmIntegrations.SearchGitlabReposWsResponse.newBuilder().addAllRepositories(gitlabRepositories).setPaging(pagingBuilder.build()).build();
}
}
Aggregations