Search in sources :

Example 1 with GitlabRepository

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();
    }
}
Also used : AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectList(org.sonar.alm.client.gitlab.ProjectList) PROVISION_PROJECTS(org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS) AlmIntegrations(org.sonarqube.ws.AlmIntegrations) GitlabRepository(org.sonarqube.ws.AlmIntegrations.GitlabRepository) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) Request(org.sonar.api.server.ws.Request) WebService(org.sonar.api.server.ws.WebService) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) Map(java.util.Map) GitlabHttpClient(org.sonar.alm.client.gitlab.GitlabHttpClient) Objects.requireNonNull(java.util.Objects.requireNonNull) Response(org.sonar.api.server.ws.Response) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) Collectors.toSet(java.util.stream.Collectors.toSet) Project(org.sonar.alm.client.gitlab.Project) Set(java.util.Set) Paging(org.sonarqube.ws.Common.Paging) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) NotFoundException(org.sonar.server.exceptions.NotFoundException) DbClient(org.sonar.db.DbClient) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Optional(java.util.Optional) AlmIntegrationsWsAction(org.sonar.server.almintegration.ws.AlmIntegrationsWsAction) UserSession(org.sonar.server.user.UserSession) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) Paging(org.sonarqube.ws.Common.Paging) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) GitlabRepository(org.sonarqube.ws.AlmIntegrations.GitlabRepository) DbSession(org.sonar.db.DbSession) ProjectList(org.sonar.alm.client.gitlab.ProjectList) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto)

Aggregations

List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 Set (java.util.Set)1 BinaryOperator (java.util.function.BinaryOperator)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 GitlabHttpClient (org.sonar.alm.client.gitlab.GitlabHttpClient)1 Project (org.sonar.alm.client.gitlab.Project)1 ProjectList (org.sonar.alm.client.gitlab.ProjectList)1 Request (org.sonar.api.server.ws.Request)1 Response (org.sonar.api.server.ws.Response)1 WebService (org.sonar.api.server.ws.WebService)1 DbClient (org.sonar.db.DbClient)1 DbSession (org.sonar.db.DbSession)1 AlmPatDto (org.sonar.db.alm.pat.AlmPatDto)1 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)1