Search in sources :

Example 1 with Settings

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

the class SettingsDownloaderTest method testFetchProjectSettings.

@Test
public void testFetchProjectSettings() throws Exception {
    SonarLintWsClient wsClient = WsClientTestUtils.createMock();
    Settings.FieldValues.Value.Builder valuesBuilder = Value.newBuilder();
    valuesBuilder.getMutableValue().put("filepattern", "**/*.xml");
    valuesBuilder.getMutableValue().put("rulepattern", "*:S12345");
    Value value1 = valuesBuilder.build();
    valuesBuilder.clear();
    valuesBuilder.getMutableValue().put("filepattern", "**/*.java");
    valuesBuilder.getMutableValue().put("rulepattern", "*:S456");
    Value value2 = valuesBuilder.build();
    ValuesWsResponse response = ValuesWsResponse.newBuilder().addSettings(Setting.newBuilder().setKey("sonar.inclusions").setValues(Values.newBuilder().addValues("**/*.java"))).addSettings(Setting.newBuilder().setKey("sonar.java.fileSuffixes").setValue("*.java")).addSettings(Setting.newBuilder().setKey("sonar.issue.exclusions.multicriteria").setFieldValues(FieldValues.newBuilder().addFieldValues(value1).addFieldValues(value2)).build()).build();
    PipedInputStream in = new PipedInputStream();
    final PipedOutputStream out = new PipedOutputStream(in);
    response.writeTo(out);
    out.close();
    WsClientTestUtils.addResponse(wsClient, "/api/settings/values.protobuf?component=foo", in);
    Builder builder = ModuleConfiguration.newBuilder();
    new SettingsDownloader(wsClient).fetchProjectSettings("6.3", "foo", null, builder);
    assertThat(builder.getPropertiesMap()).containsOnly(entry("sonar.inclusions", "**/*.java"), entry("sonar.java.fileSuffixes", "*.java"), entry("sonar.issue.exclusions.multicriteria", "1,2"), entry("sonar.issue.exclusions.multicriteria.1.filepattern", "**/*.xml"), entry("sonar.issue.exclusions.multicriteria.1.rulepattern", "*:S12345"), entry("sonar.issue.exclusions.multicriteria.2.filepattern", "**/*.java"), entry("sonar.issue.exclusions.multicriteria.2.rulepattern", "*:S456"));
}
Also used : ValuesWsResponse(org.sonarqube.ws.Settings.ValuesWsResponse) Builder(org.sonarsource.sonarlint.core.proto.Sonarlint.ModuleConfiguration.Builder) Value(org.sonarqube.ws.Settings.FieldValues.Value) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Settings(org.sonarqube.ws.Settings) SonarLintWsClient(org.sonarsource.sonarlint.core.container.connected.SonarLintWsClient) Test(org.junit.Test)

Example 2 with Settings

use of org.sonarqube.ws.Settings in project sonarqube by SonarSource.

the class AbstractSettingsLoader method load.

Map<String, String> load(@Nullable String componentKey) {
    String url = "api/settings/values.protobuf";
    Profiler profiler = Profiler.create(LOG);
    if (componentKey != null) {
        url += "?component=" + ScannerUtils.encodeForUrl(componentKey);
        profiler.startInfo(String.format("Load project settings for component key: '%s'", componentKey));
    } else {
        profiler.startInfo("Load global settings");
    }
    try (InputStream is = wsClient.call(new GetRequest(url)).contentStream()) {
        Settings.ValuesWsResponse values = Settings.ValuesWsResponse.parseFrom(is);
        profiler.stopInfo();
        return toMap(values.getSettingsList());
    } catch (HttpException e) {
        if (e.code() == HttpURLConnection.HTTP_NOT_FOUND) {
            return Collections.emptyMap();
        }
        throw e;
    } catch (IOException e) {
        throw new IllegalStateException("Unable to load settings", e);
    }
}
Also used : Profiler(org.sonar.api.utils.log.Profiler) InputStream(java.io.InputStream) GetRequest(org.sonarqube.ws.client.GetRequest) HttpException(org.sonarqube.ws.client.HttpException) IOException(java.io.IOException) Settings(org.sonarqube.ws.Settings)

Example 3 with Settings

use of org.sonarqube.ws.Settings in project sonarqube by SonarSource.

the class ValuesAction method loadSettings.

private List<Setting> loadSettings(DbSession dbSession, Optional<ComponentDto> component, Set<String> keys) {
    // List of settings must be kept in the following orders : default -> global -> component -> branch
    List<Setting> settings = new ArrayList<>();
    settings.addAll(loadDefaultValues(keys));
    settings.addAll(loadGlobalSettings(dbSession, keys));
    if (component.isPresent() && component.get().getBranch() != null && component.get().getMainBranchProjectUuid() != null) {
        ComponentDto project = dbClient.componentDao().selectOrFailByUuid(dbSession, component.get().getMainBranchProjectUuid());
        settings.addAll(loadComponentSettings(dbSession, keys, project).values());
    }
    component.ifPresent(componentDto -> settings.addAll(loadComponentSettings(dbSession, keys, componentDto).values()));
    return settings.stream().filter(s -> settingsWsSupport.isVisible(s.getKey(), component)).collect(Collectors.toList());
}
Also used : Settings(org.sonarqube.ws.Settings) AbstractUserSession.insufficientPrivilegesException(org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException) ComponentFinder(org.sonar.server.component.ComponentFinder) SettingsWsSupport.isSecured(org.sonar.server.setting.ws.SettingsWsSupport.isSecured) GlobalPermission(org.sonar.db.permission.GlobalPermission) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) PropertySetExtractor.extractPropertySetKeys(org.sonar.server.setting.ws.PropertySetExtractor.extractPropertySetKeys) ValuesWsResponse(org.sonarqube.ws.Settings.ValuesWsResponse) ArrayList(java.util.ArrayList) PropertyDefinition(org.sonar.api.config.PropertyDefinition) Request(org.sonar.api.server.ws.Request) WebService(org.sonar.api.server.ws.WebService) TreeMultimap(com.google.common.collect.TreeMultimap) SERVER_STARTTIME(org.sonar.api.CoreProperties.SERVER_STARTTIME) Map(java.util.Map) Response(org.sonar.api.server.ws.Response) PARAM_COMPONENT(org.sonar.server.setting.ws.SettingsWsParameters.PARAM_COMPONENT) Stream.concat(java.util.stream.Stream.concat) Splitter(com.google.common.base.Splitter) Nullable(javax.annotation.Nullable) PropertyDefinitions(org.sonar.api.config.PropertyDefinitions) PROPERTY_SET(org.sonar.api.PropertyType.PROPERTY_SET) PARAM_KEYS(org.sonar.server.setting.ws.SettingsWsParameters.PARAM_KEYS) ImmutableSet(com.google.common.collect.ImmutableSet) StringUtils.isEmpty(org.apache.commons.lang.StringUtils.isEmpty) USER(org.sonar.api.web.UserRole.USER) Set(java.util.Set) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) SERVER_ID(org.sonar.api.CoreProperties.SERVER_ID) UserRole(org.sonar.api.web.UserRole) Ordering(com.google.common.collect.Ordering) Optional(java.util.Optional) Change(org.sonar.api.server.ws.Change) KEY_PROJECT_EXAMPLE_001(org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001) UserSession(org.sonar.server.user.UserSession) PropertyDto(org.sonar.db.property.PropertyDto) CheckForNull(javax.annotation.CheckForNull) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) ArrayList(java.util.ArrayList) ComponentDto(org.sonar.db.component.ComponentDto)

Aggregations

Settings (org.sonarqube.ws.Settings)2 Splitter (com.google.common.base.Splitter)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Multimap (com.google.common.collect.Multimap)1 Ordering (com.google.common.collect.Ordering)1 TreeMultimap (com.google.common.collect.TreeMultimap)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 String.format (java.lang.String.format)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1