Search in sources :

Example 1 with ListWsResponse

use of org.sonarqube.ws.AlmSettings.ListWsResponse in project sonarqube by SonarSource.

the class ListActionTest method list_is_ordered_by_alm_key.

@Test
public void list_is_ordered_by_alm_key() {
    UserDto user = db.users().insertUser();
    db.components().insertPrivateProject();
    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
    db.almSettings().insertGitHubAlmSetting(almSetting -> almSetting.setKey("GitHub1"));
    db.almSettings().insertGitHubAlmSetting(almSetting -> almSetting.setKey("GitHub2"));
    db.almSettings().insertAzureAlmSetting(almSetting -> almSetting.setKey("Azure"));
    db.almSettings().insertGitlabAlmSetting(almSetting -> almSetting.setKey("Gitlab"));
    db.almSettings().insertBitbucketAlmSetting(almSetting -> almSetting.setKey("Bitbucket"));
    ListWsResponse response = ws.newRequest().executeProtobuf(ListWsResponse.class);
    assertThat(response.getAlmSettingsList()).extracting(AlmSetting::getKey).containsExactly("Azure", "Bitbucket", "GitHub1", "GitHub2", "Gitlab");
}
Also used : ListWsResponse(org.sonarqube.ws.AlmSettings.ListWsResponse) UserDto(org.sonar.db.user.UserDto) Test(org.junit.Test)

Example 2 with ListWsResponse

use of org.sonarqube.ws.AlmSettings.ListWsResponse in project sonarqube by SonarSource.

the class ListActionTest method list.

@Test
public void list() {
    UserDto user = db.users().insertUser();
    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
    AlmSettingDto githubAlmSetting1 = db.almSettings().insertGitHubAlmSetting();
    AlmSettingDto githubAlmSetting2 = db.almSettings().insertGitHubAlmSetting();
    AlmSettingDto azureAlmSetting = db.almSettings().insertAzureAlmSetting();
    AlmSettingDto azureAlmSettingWithoutUrl = db.almSettings().insertAzureAlmSetting(s -> s.setUrl(null));
    AlmSettingDto gitlabAlmSetting = db.almSettings().insertGitlabAlmSetting();
    AlmSettingDto gitlabAlmSettingWithoutUrl = db.almSettings().insertGitlabAlmSetting(s -> s.setUrl(null));
    AlmSettingDto bitbucketAlmSetting = db.almSettings().insertBitbucketAlmSetting();
    AlmSettingDto bitbucketCloudAlmSetting = db.almSettings().insertBitbucketCloudAlmSetting();
    ListWsResponse response = ws.newRequest().executeProtobuf(ListWsResponse.class);
    assertThat(response.getAlmSettingsList()).extracting(AlmSetting::getAlm, AlmSetting::getKey, AlmSetting::hasUrl, AlmSetting::getUrl).containsExactlyInAnyOrder(tuple(AlmSettings.Alm.github, githubAlmSetting1.getKey(), true, githubAlmSetting1.getUrl()), tuple(AlmSettings.Alm.github, githubAlmSetting2.getKey(), true, githubAlmSetting2.getUrl()), tuple(AlmSettings.Alm.azure, azureAlmSetting.getKey(), true, azureAlmSetting.getUrl()), tuple(AlmSettings.Alm.azure, azureAlmSettingWithoutUrl.getKey(), false, ""), tuple(AlmSettings.Alm.gitlab, gitlabAlmSetting.getKey(), true, gitlabAlmSetting.getUrl()), tuple(AlmSettings.Alm.gitlab, gitlabAlmSettingWithoutUrl.getKey(), false, ""), tuple(AlmSettings.Alm.bitbucket, bitbucketAlmSetting.getKey(), true, bitbucketAlmSetting.getUrl()), tuple(AlmSettings.Alm.bitbucketcloud, bitbucketCloudAlmSetting.getKey(), true, "https://bitbucket.org/" + bitbucketCloudAlmSetting.getAppId() + "/"));
}
Also used : ListWsResponse(org.sonarqube.ws.AlmSettings.ListWsResponse) UserDto(org.sonar.db.user.UserDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Test(org.junit.Test)

Example 3 with ListWsResponse

use of org.sonarqube.ws.AlmSettings.ListWsResponse in project sonarqube by SonarSource.

the class ListAction method handle.

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

Example 4 with ListWsResponse

use of org.sonarqube.ws.AlmSettings.ListWsResponse in project sonarqube by SonarSource.

the class ListAction method doHandle.

private ListWsResponse doHandle(Request request) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        Request.StringParam projectKey = request.getParam(PARAM_PROJECT);
        if (projectKey.isPresent()) {
            ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey.getValue());
            userSession.checkProjectPermission(ADMIN, project);
        } else {
            userSession.checkPermission(PROVISION_PROJECTS);
        }
        List<AlmSettingDto> settings = dbClient.almSettingDao().selectAll(dbSession);
        List<AlmSetting> wsAlmSettings = settings.stream().sorted(Comparator.comparing(AlmSettingDto::getKey)).map(almSetting -> {
            AlmSetting.Builder almSettingBuilder = AlmSetting.newBuilder().setKey(almSetting.getKey()).setAlm(AlmSettingsSupport.toAlmWs(almSetting.getAlm()));
            if (almSetting.getAlm() == ALM.BITBUCKET_CLOUD) {
                almSettingBuilder.setUrl(BITBUCKETCLOUD_ROOT_URL + almSetting.getAppId() + "/");
            } else {
                ofNullable(almSetting.getUrl()).ifPresent(almSettingBuilder::setUrl);
            }
            return almSettingBuilder.build();
        }).collect(Collectors.toList());
        return ListWsResponse.newBuilder().addAllAlmSettings(wsAlmSettings).build();
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ComponentFinder(org.sonar.server.component.ComponentFinder) AlmSetting(org.sonarqube.ws.AlmSettings.AlmSetting) Optional.ofNullable(java.util.Optional.ofNullable) PROVISION_PROJECTS(org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS) ALM(org.sonar.db.alm.setting.ALM) ListWsResponse(org.sonarqube.ws.AlmSettings.ListWsResponse) Collectors(java.util.stream.Collectors) DbSession(org.sonar.db.DbSession) ADMIN(org.sonar.api.web.UserRole.ADMIN) Request(org.sonar.api.server.ws.Request) DbClient(org.sonar.db.DbClient) List(java.util.List) WebService(org.sonar.api.server.ws.WebService) Response(org.sonar.api.server.ws.Response) ProjectDto(org.sonar.db.project.ProjectDto) Change(org.sonar.api.server.ws.Change) UserSession(org.sonar.server.user.UserSession) Comparator(java.util.Comparator) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) DbSession(org.sonar.db.DbSession) AlmSetting(org.sonarqube.ws.AlmSettings.AlmSetting) Request(org.sonar.api.server.ws.Request) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto)

Aggregations

ListWsResponse (org.sonarqube.ws.AlmSettings.ListWsResponse)4 Test (org.junit.Test)2 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)2 UserDto (org.sonar.db.user.UserDto)2 Comparator (java.util.Comparator)1 List (java.util.List)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Collectors (java.util.stream.Collectors)1 Change (org.sonar.api.server.ws.Change)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 ADMIN (org.sonar.api.web.UserRole.ADMIN)1 DbClient (org.sonar.db.DbClient)1 DbSession (org.sonar.db.DbSession)1 ALM (org.sonar.db.alm.setting.ALM)1 PROVISION_PROJECTS (org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS)1 ProjectDto (org.sonar.db.project.ProjectDto)1 ComponentFinder (org.sonar.server.component.ComponentFinder)1 UserSession (org.sonar.server.user.UserSession)1