Search in sources :

Example 1 with SearchWsResponse

use of org.sonarqube.ws.Qualityprofiles.SearchWsResponse in project sonarqube by SonarSource.

the class DefaultQualityProfileLoader method loadResource.

private List<QualityProfile> loadResource(String url) {
    GetRequest getRequest = new GetRequest(url);
    InputStream is = wsClient.call(getRequest).contentStream();
    SearchWsResponse profiles = null;
    try {
        profiles = SearchWsResponse.parseFrom(is);
    } catch (IOException e) {
        throw new IllegalStateException("Failed to load quality profiles", e);
    } finally {
        IOUtils.closeQuietly(is);
    }
    List<QualityProfile> profilesList = profiles.getProfilesList();
    if (profilesList == null || profilesList.isEmpty()) {
        throw MessageException.of("No quality profiles have been found, you probably don't have any language plugin installed.");
    }
    return profilesList;
}
Also used : InputStream(java.io.InputStream) GetRequest(org.sonarqube.ws.client.GetRequest) QualityProfile(org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile) SearchWsResponse(org.sonarqube.ws.QualityProfiles.SearchWsResponse) IOException(java.io.IOException)

Example 2 with SearchWsResponse

use of org.sonarqube.ws.Qualityprofiles.SearchWsResponse in project sonarlint-core by SonarSource.

the class ConnectedModeTest method checkForUpdate.

@Test
public void checkForUpdate() throws Exception {
    updateGlobal();
    updateModule(PROJECT_KEY_JAVA);
    ServerConfiguration serverConfig = getServerConfig(true);
    StorageUpdateCheckResult result = engine.checkIfGlobalStorageNeedUpdate(serverConfig, null);
    assertThat(result.needUpdate()).isFalse();
    // restarting server should not lead to notify an update
    ORCHESTRATOR.restartServer();
    result = engine.checkIfGlobalStorageNeedUpdate(serverConfig, null);
    assertThat(result.needUpdate()).isFalse();
    // Change a global setting that is not in the whitelist
    setSettings(null, "sonar.foo", "bar");
    result = engine.checkIfGlobalStorageNeedUpdate(serverConfig, null);
    assertThat(result.needUpdate()).isFalse();
    // Change a global setting that *is* in the whitelist
    setSettingsMultiValue(null, "sonar.inclusions", "**/*");
    // Activate a new rule
    SearchWsResponse response = newAdminWsClient(ORCHESTRATOR).qualityProfiles().search(new SearchWsRequest().setLanguage("java"));
    String profileKey = response.getProfilesList().stream().filter(p -> p.getName().equals("SonarLint IT Java")).findFirst().get().getKey();
    ORCHESTRATOR.getServer().adminWsClient().post("api/qualityprofiles/activate_rule", "profile_key", profileKey, "rule_key", "squid:S1228");
    result = engine.checkIfGlobalStorageNeedUpdate(serverConfig, null);
    assertThat(result.needUpdate()).isTrue();
    assertThat(result.changelog()).containsOnly("Global settings updated", "Quality profile 'SonarLint IT Java' for language 'Java' updated");
    result = engine.checkIfModuleStorageNeedUpdate(serverConfig, PROJECT_KEY_JAVA, null);
    assertThat(result.needUpdate()).isFalse();
    // Change a project setting that is not in the whitelist
    setSettings(PROJECT_KEY_JAVA, "sonar.foo", "biz");
    result = engine.checkIfModuleStorageNeedUpdate(serverConfig, PROJECT_KEY_JAVA, null);
    assertThat(result.needUpdate()).isFalse();
    // Change a project setting that *is* in the whitelist
    setSettingsMultiValue(PROJECT_KEY_JAVA, "sonar.exclusions", "**/*.foo");
    result = engine.checkIfModuleStorageNeedUpdate(serverConfig, PROJECT_KEY_JAVA, null);
    assertThat(result.needUpdate()).isTrue();
    assertThat(result.changelog()).containsOnly("Project settings updated");
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) PermissionParameters(org.sonar.wsclient.permissions.PermissionParameters) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) OrchestratorBuilder(com.sonar.orchestrator.OrchestratorBuilder) UnsupportedServerException(org.sonarsource.sonarlint.core.client.api.exceptions.UnsupportedServerException) InetAddress(java.net.InetAddress) WsClient(org.sonarqube.ws.client.WsClient) MovedContextHandler(org.eclipse.jetty.server.handler.MovedContextHandler) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Map(java.util.Map) After(org.junit.After) ClassRule(org.junit.ClassRule) Path(java.nio.file.Path) Server(org.eclipse.jetty.server.Server) ConnectedSonarLintEngine(org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine) StorageUpdateCheckResult(org.sonarsource.sonarlint.core.client.api.connected.StorageUpdateCheckResult) AfterClass(org.junit.AfterClass) RemoveGroupWsRequest(org.sonarqube.ws.client.permission.RemoveGroupWsRequest) NetworkUtils(com.sonar.orchestrator.util.NetworkUtils) FileLocation(com.sonar.orchestrator.locator.FileLocation) WsClientFactories(org.sonarqube.ws.client.WsClientFactories) SearchWsResponse(org.sonarqube.ws.QualityProfiles.SearchWsResponse) StandardCharsets(java.nio.charset.StandardCharsets) Configuration(com.sonar.orchestrator.config.Configuration) List(java.util.List) ExternalResource(org.junit.rules.ExternalResource) Assertions.fail(org.assertj.core.api.Assertions.fail) Assume.assumeTrue(org.junit.Assume.assumeTrue) SearchWsRequest(org.sonarqube.ws.client.qualityprofile.SearchWsRequest) Orchestrator(com.sonar.orchestrator.Orchestrator) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) QualityProfile(org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile) BeforeClass(org.junit.BeforeClass) ConnectedSonarLintEngineImpl(org.sonarsource.sonarlint.core.ConnectedSonarLintEngineImpl) HashMap(java.util.HashMap) PropertyDeleteQuery(org.sonar.wsclient.services.PropertyDeleteQuery) ConnectedGlobalConfiguration(org.sonarsource.sonarlint.core.client.api.connected.ConnectedGlobalConfiguration) ArrayList(java.util.ArrayList) HttpConnector(org.sonarqube.ws.client.HttpConnector) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) WsHelper(org.sonarsource.sonarlint.core.client.api.connected.WsHelper) ExpectedException(org.junit.rules.ExpectedException) State(org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine.State) Nullable(javax.annotation.Nullable) Before(org.junit.Before) PropertyCreateQuery(org.sonar.wsclient.services.PropertyCreateQuery) MavenBuild(com.sonar.orchestrator.build.MavenBuild) Files(java.nio.file.Files) UserParameters(org.sonar.wsclient.user.UserParameters) PostRequest(org.sonarqube.ws.client.PostRequest) Assertions.tuple(org.assertj.core.api.Assertions.tuple) WsResponse(org.sonarqube.ws.client.WsResponse) SetRequest(org.sonarqube.ws.client.setting.SetRequest) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) ServerConfiguration(org.sonarsource.sonarlint.core.client.api.connected.ServerConfiguration) WsHelperImpl(org.sonarsource.sonarlint.core.WsHelperImpl) File(java.io.File) ServerConnector(org.eclipse.jetty.server.ServerConnector) Rule(org.junit.Rule) Paths(java.nio.file.Paths) AnalysisResults(org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults) WsRequest(org.sonarqube.ws.client.WsRequest) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) StorageUpdateCheckResult(org.sonarsource.sonarlint.core.client.api.connected.StorageUpdateCheckResult) ServerConfiguration(org.sonarsource.sonarlint.core.client.api.connected.ServerConfiguration) SearchWsRequest(org.sonarqube.ws.client.qualityprofile.SearchWsRequest) SearchWsResponse(org.sonarqube.ws.QualityProfiles.SearchWsResponse) Test(org.junit.Test)

Example 3 with SearchWsResponse

use of org.sonarqube.ws.Qualityprofiles.SearchWsResponse in project sonarqube by SonarSource.

the class SearchAction method buildResponse.

private SearchWsResponse buildResponse(SearchData data) {
    List<QProfileDto> profiles = data.getProfiles();
    Map<String, QProfileDto> profilesByKey = profiles.stream().collect(Collectors.toMap(QProfileDto::getKee, identity()));
    boolean isGlobalQProfileAdmin = userSession.hasPermission(ADMINISTER_QUALITY_PROFILES);
    SearchWsResponse.Builder response = SearchWsResponse.newBuilder();
    response.setActions(SearchWsResponse.Actions.newBuilder().setCreate(isGlobalQProfileAdmin));
    for (QProfileDto profile : profiles) {
        QualityProfile.Builder profileBuilder = response.addProfilesBuilder();
        String profileKey = profile.getKee();
        profileBuilder.setKey(profileKey);
        ofNullable(profile.getName()).ifPresent(profileBuilder::setName);
        ofNullable(profile.getRulesUpdatedAt()).ifPresent(profileBuilder::setRulesUpdatedAt);
        ofNullable(profile.getLastUsed()).ifPresent(last -> profileBuilder.setLastUsed(formatDateTime(last)));
        ofNullable(profile.getUserUpdatedAt()).ifPresent(userUpdatedAt -> profileBuilder.setUserUpdatedAt(formatDateTime(userUpdatedAt)));
        profileBuilder.setActiveRuleCount(data.getActiveRuleCount(profileKey));
        profileBuilder.setActiveDeprecatedRuleCount(data.getActiveDeprecatedRuleCount(profileKey));
        boolean isDefault = data.isDefault(profile);
        profileBuilder.setIsDefault(isDefault);
        if (!isDefault) {
            profileBuilder.setProjectCount(data.getProjectCount(profileKey));
        }
        writeLanguageFields(profileBuilder, profile);
        writeParentFields(profileBuilder, profile, profilesByKey);
        profileBuilder.setIsInherited(profile.getParentKee() != null);
        profileBuilder.setIsBuiltIn(profile.isBuiltIn());
        profileBuilder.setActions(SearchWsResponse.QualityProfile.Actions.newBuilder().setEdit(!profile.isBuiltIn() && (isGlobalQProfileAdmin || data.isEditable(profile))).setSetAsDefault(!isDefault && isGlobalQProfileAdmin).setCopy(isGlobalQProfileAdmin).setDelete(!isDefault && !profile.isBuiltIn() && (isGlobalQProfileAdmin || data.isEditable(profile))).setAssociateProjects(!isDefault && (isGlobalQProfileAdmin || data.isEditable(profile))));
    }
    return response.build();
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) QualityProfile(org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile) SearchWsResponse(org.sonarqube.ws.Qualityprofiles.SearchWsResponse)

Example 4 with SearchWsResponse

use of org.sonarqube.ws.Qualityprofiles.SearchWsResponse in project sonarqube by SonarSource.

the class SearchActionTest method ignore_profiles_on_unknown_language.

@Test
public void ignore_profiles_on_unknown_language() {
    QProfileDto profile1OnXoo1 = db.qualityProfiles().insert(p -> p.setLanguage(XOO1.getKey()));
    QProfileDto profile2OnXoo1 = db.qualityProfiles().insert(p -> p.setLanguage(XOO2.getKey()));
    QProfileDto profileOnUnknownLanguage = db.qualityProfiles().insert(p -> p.setLanguage("unknown"));
    SearchWsResponse result = call(ws.newRequest());
    assertThat(result.getProfilesList()).extracting(QualityProfile::getKey).containsExactlyInAnyOrder(profile1OnXoo1.getKee(), profile2OnXoo1.getKee()).doesNotContain(profileOnUnknownLanguage.getKee());
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) QualityProfile(org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile) SearchWsResponse(org.sonarqube.ws.Qualityprofiles.SearchWsResponse) Test(org.junit.Test)

Example 5 with SearchWsResponse

use of org.sonarqube.ws.Qualityprofiles.SearchWsResponse in project sonarqube by SonarSource.

the class SearchActionTest method actions_when_not_logged_in.

@Test
public void actions_when_not_logged_in() {
    QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO1.getKey()));
    userSession.anonymous();
    SearchWsResponse result = call(ws.newRequest());
    assertThat(result.getProfilesList()).extracting(QualityProfile::getKey, qp -> qp.getActions().getEdit(), qp -> qp.getActions().getCopy(), qp -> qp.getActions().getSetAsDefault(), qp -> qp.getActions().getDelete(), qp -> qp.getActions().getAssociateProjects()).containsExactlyInAnyOrder(tuple(profile.getKee(), false, false, false, false, false));
    assertThat(result.getActions().getCreate()).isFalse();
}
Also used : ComponentFinder(org.sonar.server.component.ComponentFinder) SearchWsResponse(org.sonarqube.ws.Qualityprofiles.SearchWsResponse) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) IntStream.range(java.util.stream.IntStream.range) GlobalPermission(org.sonar.db.permission.GlobalPermission) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Languages(org.sonar.api.resources.Languages) QualityProfileDbTester(org.sonar.db.qualityprofile.QualityProfileDbTester) PARAM_PROJECT(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT) WebService(org.sonar.api.server.ws.WebService) DateUtils(org.sonar.api.utils.DateUtils) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) PARAM_LANGUAGE(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE) PARAM_DEFAULTS(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_DEFAULTS) MediaTypes(org.sonarqube.ws.MediaTypes) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Language(org.sonar.api.resources.Language) PARAM_QUALITY_PROFILE(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE) PARAM_PROJECT_KEY(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT_KEY) GroupDto(org.sonar.db.user.GroupDto) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) QualityProfile(org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile) System2(org.sonar.api.utils.System2) Assertions.tuple(org.assertj.core.api.Assertions.tuple) QualityProfileTesting.newQualityProfileDto(org.sonar.db.qualityprofile.QualityProfileTesting.newQualityProfileDto) LanguageTesting.newLanguage(org.sonar.server.language.LanguageTesting.newLanguage) TestRequest(org.sonar.server.ws.TestRequest) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Test(org.junit.Test) WsActionTester(org.sonar.server.ws.WsActionTester) DEPRECATED(org.sonar.api.rule.RuleStatus.DEPRECATED) NotFoundException(org.sonar.server.exceptions.NotFoundException) DbClient(org.sonar.db.DbClient) List(java.util.List) Rule(org.junit.Rule) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) ProjectDto(org.sonar.db.project.ProjectDto) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) Change(org.sonar.api.server.ws.Change) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) SearchWsResponse(org.sonarqube.ws.Qualityprofiles.SearchWsResponse) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)21 SearchWsResponse (org.sonarqube.ws.Qualityprofiles.SearchWsResponse)21 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)16 QualityProfile (org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile)14 ProjectDto (org.sonar.db.project.ProjectDto)9 Languages (org.sonar.api.resources.Languages)6 ComponentFinder (org.sonar.server.component.ComponentFinder)6 WsActionTester (org.sonar.server.ws.WsActionTester)6 SearchWsResponse (org.sonarqube.ws.QualityProfiles.SearchWsResponse)6 List (java.util.List)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Assertions.tuple (org.assertj.core.api.Assertions.tuple)5 Rule (org.junit.Rule)5 QualityProfileTesting.newQualityProfileDto (org.sonar.db.qualityprofile.QualityProfileTesting.newQualityProfileDto)5 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 IntStream.range (java.util.stream.IntStream.range)4 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)4 Language (org.sonar.api.resources.Language)4 DEPRECATED (org.sonar.api.rule.RuleStatus.DEPRECATED)4