Search in sources :

Example 1 with BBSRepo

use of org.sonarqube.ws.AlmIntegrations.BBSRepo in project sonarqube by SonarSource.

the class SearchBitbucketServerReposAction method doHandle.

private SearchBitbucketserverReposWsResponse doHandle(Request request) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
        String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
        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 projectKey = request.param(PARAM_PROJECT_NAME);
        String repoName = request.param(PARAM_REPO_NAME);
        String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
        String url = requireNonNull(almSettingDto.getUrl(), "ALM url cannot be null");
        RepositoryList gsonBBSRepoList = bitbucketServerRestClient.getRepos(url, pat, projectKey, repoName);
        Map<String, String> sqProjectsKeyByBBSKey = getSqProjectsKeyByBBSKey(dbSession, almSettingDto, gsonBBSRepoList);
        List<BBSRepo> bbsRepos = gsonBBSRepoList.getValues().stream().map(gsonBBSRepo -> toBBSRepo(gsonBBSRepo, sqProjectsKeyByBBSKey)).collect(toList());
        SearchBitbucketserverReposWsResponse.Builder builder = SearchBitbucketserverReposWsResponse.newBuilder().setIsLastPage(gsonBBSRepoList.isLastPage()).addAllRepositories(bbsRepos);
        return builder.build();
    }
}
Also used : AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Arrays(java.util.Arrays) SearchBitbucketserverReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchBitbucketserverReposWsResponse) PROVISION_PROJECTS(org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) TreeSet(java.util.TreeSet) Request(org.sonar.api.server.ws.Request) WebService(org.sonar.api.server.ws.WebService) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Response(org.sonar.api.server.ws.Response) ProjectAlmSettingDao(org.sonar.db.alm.setting.ProjectAlmSettingDao) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) Collectors.toSet(java.util.stream.Collectors.toSet) ProjectDao(org.sonar.db.project.ProjectDao) Set(java.util.Set) BinaryOperator(java.util.function.BinaryOperator) Repository(org.sonar.alm.client.bitbucketserver.Repository) NotFoundException(org.sonar.server.exceptions.NotFoundException) RepositoryList(org.sonar.alm.client.bitbucketserver.RepositoryList) DbClient(org.sonar.db.DbClient) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) BBSRepo(org.sonarqube.ws.AlmIntegrations.BBSRepo) ProjectDto(org.sonar.db.project.ProjectDto) Optional(java.util.Optional) AlmIntegrationsWsAction(org.sonar.server.almintegration.ws.AlmIntegrationsWsAction) UserSession(org.sonar.server.user.UserSession) BitbucketServerRestClient(org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) SearchBitbucketserverReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchBitbucketserverReposWsResponse) DbSession(org.sonar.db.DbSession) BBSRepo(org.sonarqube.ws.AlmIntegrations.BBSRepo) RepositoryList(org.sonar.alm.client.bitbucketserver.RepositoryList) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto)

Aggregations

Arrays (java.util.Arrays)1 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 TreeSet (java.util.TreeSet)1 BinaryOperator (java.util.function.BinaryOperator)1 Function (java.util.function.Function)1 Collectors.toList (java.util.stream.Collectors.toList)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 BitbucketServerRestClient (org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient)1 Repository (org.sonar.alm.client.bitbucketserver.Repository)1 RepositoryList (org.sonar.alm.client.bitbucketserver.RepositoryList)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