Search in sources :

Example 1 with RepositoryList

use of org.sonar.alm.client.bitbucket.bitbucketcloud.RepositoryList in project sonarqube by SonarSource.

the class SearchBitbucketCloudReposAction method doHandle.

private SearchBitbucketcloudReposWsResponse doHandle(Request request) {
    userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
    String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
    String repoName = request.param(PARAM_REPO_NAME);
    int page = request.mandatoryParamAsInt(PAGE);
    int pageSize = request.mandatoryParamAsInt(PAGE_SIZE);
    try (DbSession dbSession = dbClient.openSession(false)) {
        AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey).orElseThrow(() -> new NotFoundException(String.format("ALM Setting '%s' not found", almSettingKey)));
        String workspace = ofNullable(almSettingDto.getAppId()).orElseThrow(() -> new IllegalArgumentException(String.format("workspace for alm setting %s is missing", almSettingDto.getKey())));
        String userUuid = requireNonNull(userSession.getUuid(), "User UUID cannot be null");
        Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
        String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
        RepositoryList repositoryList = bitbucketCloudRestClient.searchRepos(pat, workspace, repoName, page, pageSize);
        Map<String, String> sqProjectKeyByRepoSlug = getSqProjectKeyByRepoSlug(dbSession, almSettingDto, repositoryList.getValues());
        List<BBCRepo> bbcRepos = repositoryList.getValues().stream().map(repository -> toBBCRepo(repository, workspace, sqProjectKeyByRepoSlug)).collect(toList());
        SearchBitbucketcloudReposWsResponse.Builder builder = SearchBitbucketcloudReposWsResponse.newBuilder().setIsLastPage(repositoryList.getNext() == null).setPaging(Paging.newBuilder().setPageIndex(page).setPageSize(pageSize).build()).addAllRepositories(bbcRepos);
        return builder.build();
    }
}
Also used : AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) BBCRepo(org.sonarqube.ws.AlmIntegrations.BBCRepo) PROVISION_PROJECTS(org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS) Repository(org.sonar.alm.client.bitbucket.bitbucketcloud.Repository) DbSession(org.sonar.db.DbSession) SearchBitbucketcloudReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchBitbucketcloudReposWsResponse) PAGE(org.sonar.api.server.ws.WebService.Param.PAGE) 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) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) Collectors.toSet(java.util.stream.Collectors.toSet) Optional.ofNullable(java.util.Optional.ofNullable) BitbucketCloudRestClient(org.sonar.alm.client.bitbucket.bitbucketcloud.BitbucketCloudRestClient) Set(java.util.Set) Paging(org.sonarqube.ws.Common.Paging) RepositoryList(org.sonar.alm.client.bitbucket.bitbucketcloud.RepositoryList) 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) ProjectDto(org.sonar.db.project.ProjectDto) Optional(java.util.Optional) PAGE_SIZE(org.sonar.api.server.ws.WebService.Param.PAGE_SIZE) AlmIntegrationsWsAction(org.sonar.server.almintegration.ws.AlmIntegrationsWsAction) UserSession(org.sonar.server.user.UserSession) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) BBCRepo(org.sonarqube.ws.AlmIntegrations.BBCRepo) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) DbSession(org.sonar.db.DbSession) SearchBitbucketcloudReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchBitbucketcloudReposWsResponse) RepositoryList(org.sonar.alm.client.bitbucket.bitbucketcloud.RepositoryList) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto)

Example 2 with RepositoryList

use of org.sonar.alm.client.bitbucket.bitbucketcloud.RepositoryList in project sonarqube by SonarSource.

the class SearchBitbucketCloudReposActionTest method return_empty_list_when_no_bbs_repo.

@Test
public void return_empty_list_when_no_bbs_repo() {
    RepositoryList repositoryList = new RepositoryList(null, emptyList(), 1, 100);
    when(bitbucketCloudRestClient.searchRepos(any(), any(), any(), any(), any())).thenReturn(repositoryList);
    UserDto user = db.users().insertUser();
    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
    AlmSettingDto almSetting = db.almSettings().insertBitbucketCloudAlmSetting();
    db.almPats().insert(dto -> {
        dto.setAlmSettingUuid(almSetting.getUuid());
        dto.setPersonalAccessToken("abc:xyz");
        dto.setUserUuid(user.getUuid());
    });
    ProjectDto projectDto = db.components().insertPrivateProjectDto();
    db.almSettings().insertBitbucketCloudProjectAlmSetting(almSetting, projectDto, s -> s.setAlmRepo("repo-slug-2"));
    SearchBitbucketcloudReposWsResponse response = ws.newRequest().setParam("almSetting", almSetting.getKey()).executeProtobuf(SearchBitbucketcloudReposWsResponse.class);
    assertThat(response.getIsLastPage()).isTrue();
    assertThat(response.getRepositoriesList()).isEmpty();
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) SearchBitbucketcloudReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchBitbucketcloudReposWsResponse) RepositoryList(org.sonar.alm.client.bitbucket.bitbucketcloud.RepositoryList) UserDto(org.sonar.db.user.UserDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Test(org.junit.Test)

Aggregations

RepositoryList (org.sonar.alm.client.bitbucket.bitbucketcloud.RepositoryList)2 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)2 ProjectDto (org.sonar.db.project.ProjectDto)2 SearchBitbucketcloudReposWsResponse (org.sonarqube.ws.AlmIntegrations.SearchBitbucketcloudReposWsResponse)2 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Set (java.util.Set)1 BinaryOperator (java.util.function.BinaryOperator)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 Test (org.junit.Test)1 BitbucketCloudRestClient (org.sonar.alm.client.bitbucket.bitbucketcloud.BitbucketCloudRestClient)1 Repository (org.sonar.alm.client.bitbucket.bitbucketcloud.Repository)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