Search in sources :

Example 1 with RepositoryList

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

the class SearchBitbucketServerReposActionTest method before.

@Before
public void before() {
    RepositoryList gsonBBSRepoList = new RepositoryList();
    gsonBBSRepoList.setLastPage(true);
    List<Repository> values = new ArrayList<>();
    values.add(getGsonBBSRepo1());
    values.add(getGsonBBSRepo2());
    gsonBBSRepoList.setValues(values);
    when(bitbucketServerRestClient.getRepos(any(), any(), any(), any())).thenReturn(gsonBBSRepoList);
}
Also used : Repository(org.sonar.alm.client.bitbucketserver.Repository) RepositoryList(org.sonar.alm.client.bitbucketserver.RepositoryList) ArrayList(java.util.ArrayList) Before(org.junit.Before)

Example 2 with RepositoryList

use of org.sonar.alm.client.bitbucketserver.RepositoryList 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)

Example 3 with RepositoryList

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

the class SearchBitbucketServerReposActionTest method return_empty_list_when_no_bbs_repo.

@Test
public void return_empty_list_when_no_bbs_repo() {
    RepositoryList gsonBBSRepoList = new RepositoryList();
    gsonBBSRepoList.setLastPage(true);
    gsonBBSRepoList.setValues(new ArrayList<>());
    when(bitbucketServerRestClient.getRepos(any(), any(), any(), any())).thenReturn(gsonBBSRepoList);
    UserDto user = db.users().insertUser();
    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
    AlmSettingDto almSetting = db.almSettings().insertBitbucketAlmSetting();
    db.almPats().insert(dto -> {
        dto.setAlmSettingUuid(almSetting.getUuid());
        dto.setUserUuid(user.getUuid());
    });
    ProjectDto projectDto = db.components().insertPrivateProjectDto();
    db.almSettings().insertBitbucketProjectAlmSetting(almSetting, projectDto, s -> s.setAlmRepo("projectKey2"), s -> s.setAlmSlug("repo-slug-2"));
    AlmIntegrations.SearchBitbucketserverReposWsResponse response = ws.newRequest().setParam("almSetting", almSetting.getKey()).executeProtobuf(SearchBitbucketserverReposWsResponse.class);
    assertThat(response.getIsLastPage()).isTrue();
    assertThat(response.getRepositoriesList()).isEmpty();
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) RepositoryList(org.sonar.alm.client.bitbucketserver.RepositoryList) UserDto(org.sonar.db.user.UserDto) SearchBitbucketserverReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchBitbucketserverReposWsResponse) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) AlmIntegrations(org.sonarqube.ws.AlmIntegrations) Test(org.junit.Test)

Aggregations

RepositoryList (org.sonar.alm.client.bitbucketserver.RepositoryList)3 Repository (org.sonar.alm.client.bitbucketserver.Repository)2 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)2 ProjectDto (org.sonar.db.project.ProjectDto)2 SearchBitbucketserverReposWsResponse (org.sonarqube.ws.AlmIntegrations.SearchBitbucketserverReposWsResponse)2 ArrayList (java.util.ArrayList)1 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 Before (org.junit.Before)1 Test (org.junit.Test)1