Search in sources :

Example 1 with ListAzureProjectsWsResponse

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

the class ListAzureProjectsAction method doHandle.

private ListAzureProjectsWsResponse 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");
        GsonAzureProjectList projectList = azureDevOpsHttpClient.getProjects(url, pat);
        List<AzureProject> values = projectList.getValues().stream().map(ListAzureProjectsAction::toAzureProject).sorted(comparing(AzureProject::getName, String::compareToIgnoreCase)).collect(Collectors.toList());
        ListAzureProjectsWsResponse.Builder builder = ListAzureProjectsWsResponse.newBuilder().addAllProjects(values);
        return builder.build();
    }
}
Also used : GsonAzureProjectList(org.sonar.alm.client.azure.GsonAzureProjectList) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) ListAzureProjectsWsResponse(org.sonarqube.ws.AlmIntegrations.ListAzureProjectsWsResponse) GsonAzureProject(org.sonar.alm.client.azure.GsonAzureProject) AzureProject(org.sonarqube.ws.AlmIntegrations.AzureProject) DbSession(org.sonar.db.DbSession) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto)

Example 2 with ListAzureProjectsWsResponse

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

the class ListAzureProjectsAction method handle.

@Override
public void handle(Request request, Response response) {
    ListAzureProjectsWsResponse wsResponse = doHandle(request);
    writeProtobuf(wsResponse, request, response);
}
Also used : ListAzureProjectsWsResponse(org.sonarqube.ws.AlmIntegrations.ListAzureProjectsWsResponse)

Example 3 with ListAzureProjectsWsResponse

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

the class ListAzureProjectsActionTest method list_projects.

@Test
public void list_projects() {
    AlmSettingDto almSetting = insertAlmSetting();
    ListAzureProjectsWsResponse response = ws.newRequest().setParam("almSetting", almSetting.getKey()).executeProtobuf(ListAzureProjectsWsResponse.class);
    assertThat(response.getProjectsCount()).isEqualTo(2);
    assertThat(response.getProjectsList()).extracting(AzureProject::getName, AzureProject::getDescription).containsExactly(tuple("name", "description"), tuple("name", ""));
}
Also used : ListAzureProjectsWsResponse(org.sonarqube.ws.AlmIntegrations.ListAzureProjectsWsResponse) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Test(org.junit.Test)

Example 4 with ListAzureProjectsWsResponse

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

the class ListAzureProjectsActionTest method list_projects_alphabetically_sorted.

@Test
public void list_projects_alphabetically_sorted() {
    mockClient(ImmutableList.of(new GsonAzureProject("BBB project", "BBB project description"), new GsonAzureProject("AAA project 1", "AAA project description"), new GsonAzureProject("zzz project", "zzz project description"), new GsonAzureProject("aaa project", "aaa project description")));
    AlmSettingDto almSetting = insertAlmSetting();
    ListAzureProjectsWsResponse response = ws.newRequest().setParam("almSetting", almSetting.getKey()).executeProtobuf(ListAzureProjectsWsResponse.class);
    assertThat(response.getProjectsCount()).isEqualTo(4);
    assertThat(response.getProjectsList()).extracting(AzureProject::getName, AzureProject::getDescription).containsExactly(tuple("aaa project", "aaa project description"), tuple("AAA project 1", "AAA project description"), tuple("BBB project", "BBB project description"), tuple("zzz project", "zzz project description"));
}
Also used : ListAzureProjectsWsResponse(org.sonarqube.ws.AlmIntegrations.ListAzureProjectsWsResponse) GsonAzureProject(org.sonar.alm.client.azure.GsonAzureProject) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Test(org.junit.Test)

Aggregations

ListAzureProjectsWsResponse (org.sonarqube.ws.AlmIntegrations.ListAzureProjectsWsResponse)4 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)3 Test (org.junit.Test)2 GsonAzureProject (org.sonar.alm.client.azure.GsonAzureProject)2 GsonAzureProjectList (org.sonar.alm.client.azure.GsonAzureProjectList)1 DbSession (org.sonar.db.DbSession)1 AlmPatDto (org.sonar.db.alm.pat.AlmPatDto)1 NotFoundException (org.sonar.server.exceptions.NotFoundException)1 AzureProject (org.sonarqube.ws.AlmIntegrations.AzureProject)1