Search in sources :

Example 1 with ProjectList

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

the class ListBitbucketServerProjectsActionTest method before.

@Before
public void before() {
    ProjectList projectList = new ProjectList();
    List<Project> values = new ArrayList<>();
    Project project = new Project();
    project.setId(1);
    project.setKey("key");
    project.setName("name");
    values.add(project);
    projectList.setValues(values);
    when(bitbucketServerRestClient.getProjects(anyString(), anyString())).thenReturn(projectList);
}
Also used : Project(org.sonar.alm.client.bitbucketserver.Project) ProjectList(org.sonar.alm.client.bitbucketserver.ProjectList) ArrayList(java.util.ArrayList) Before(org.junit.Before)

Example 2 with ProjectList

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

the class ListBitbucketServerProjectsAction method doHandle.

private ListBitbucketserverProjectsWsResponse 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 is not 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 pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
        String url = requireNonNull(almSettingDto.getUrl(), "URL cannot be null");
        ProjectList projectList = bitbucketServerRestClient.getProjects(url, pat);
        List<AlmProject> values = projectList.getValues().stream().map(ListBitbucketServerProjectsAction::toAlmProject).collect(Collectors.toList());
        ListBitbucketserverProjectsWsResponse.Builder builder = ListBitbucketserverProjectsWsResponse.newBuilder().addAllProjects(values);
        return builder.build();
    }
}
Also used : DbSession(org.sonar.db.DbSession) ProjectList(org.sonar.alm.client.bitbucketserver.ProjectList) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) AlmProject(org.sonarqube.ws.AlmIntegrations.AlmProject) ListBitbucketserverProjectsWsResponse(org.sonarqube.ws.AlmIntegrations.ListBitbucketserverProjectsWsResponse) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto)

Aggregations

ProjectList (org.sonar.alm.client.bitbucketserver.ProjectList)2 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1 Project (org.sonar.alm.client.bitbucketserver.Project)1 DbSession (org.sonar.db.DbSession)1 AlmPatDto (org.sonar.db.alm.pat.AlmPatDto)1 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)1 NotFoundException (org.sonar.server.exceptions.NotFoundException)1 AlmProject (org.sonarqube.ws.AlmIntegrations.AlmProject)1 ListBitbucketserverProjectsWsResponse (org.sonarqube.ws.AlmIntegrations.ListBitbucketserverProjectsWsResponse)1