Search in sources :

Example 6 with ServerApiHelper

use of org.sonarsource.sonarlint.core.serverapi.ServerApiHelper in project sonarlint-core by SonarSource.

the class ConnectedSonarLintEngineImpl method getActiveRuleDetails.

@Override
public CompletableFuture<ConnectedRuleDetails> getActiveRuleDetails(EndpointParams endpoint, HttpClient client, String ruleKey, @Nullable String projectKey) {
    var allRulesDefinitionsByKey = analysisContext.get().allRulesDefinitionsByKey;
    var ruleDefFromPlugin = allRulesDefinitionsByKey.get(ruleKey);
    if (ruleDefFromPlugin != null && (globalConfig.getExtraPluginsPathsByKey().containsKey(ruleDefFromPlugin.getLanguage().getPluginKey()) || projectKey == null)) {
        // if no project key, or for rules from extra plugins there will be no rules metadata in the storage
        return CompletableFuture.completedFuture(new DefaultRuleDetails(ruleKey, ruleDefFromPlugin.getName(), ruleDefFromPlugin.getHtmlDescription(), ruleDefFromPlugin.getSeverity(), ruleDefFromPlugin.getType(), ruleDefFromPlugin.getLanguage(), ""));
    }
    if (projectKey != null) {
        var analyzerConfiguration = projectStorage.getAnalyzerConfiguration(projectKey);
        var storageActiveRule = analyzerConfiguration.getRuleSetByLanguageKey().values().stream().flatMap(s -> s.getRules().stream()).filter(r -> r.getRuleKey().equals(ruleKey)).findFirst();
        if (storageActiveRule.isPresent()) {
            var activeRuleFromStorage = storageActiveRule.get();
            var serverSeverity = activeRuleFromStorage.getSeverity();
            if (StringUtils.isNotBlank(activeRuleFromStorage.getTemplateKey())) {
                var templateRuleDefFromPlugin = Optional.ofNullable(allRulesDefinitionsByKey.get(activeRuleFromStorage.getTemplateKey())).orElseThrow(() -> new IllegalStateException("Unable to find rule definition for rule template " + activeRuleFromStorage.getTemplateKey()));
                return new ServerApi(new ServerApiHelper(endpoint, client)).rules().getRule(ruleKey).thenApply(serverRule -> new DefaultRuleDetails(ruleKey, serverRule.getName(), serverRule.getHtmlDesc(), serverSeverity, templateRuleDefFromPlugin.getType(), templateRuleDefFromPlugin.getLanguage(), serverRule.getHtmlNote()));
            } else {
                if (ruleDefFromPlugin == null) {
                    throw new IllegalStateException("Unable to find rule definition for rule " + ruleKey);
                }
                return new ServerApi(new ServerApiHelper(endpoint, client)).rules().getRule(ruleKey).thenApply(serverRule -> new DefaultRuleDetails(ruleKey, ruleDefFromPlugin.getName(), ruleDefFromPlugin.getHtmlDescription(), serverSeverity != null ? serverSeverity : ruleDefFromPlugin.getSeverity(), ruleDefFromPlugin.getType(), ruleDefFromPlugin.getLanguage(), serverRule.getHtmlNote()));
            }
        }
    }
    throw new IllegalStateException("Unable to find rule details for '" + ruleKey + "'");
}
Also used : ClientLogOutput(org.sonarsource.sonarlint.core.commons.log.ClientLogOutput) FileMatcher(org.sonarsource.sonarlint.core.container.storage.FileMatcher) Language(org.sonarsource.sonarlint.core.commons.Language) GlobalUpdateStatusReader(org.sonarsource.sonarlint.core.container.storage.GlobalUpdateStatusReader) DefaultClientIssue(org.sonarsource.sonarlint.core.client.api.common.analysis.DefaultClientIssue) IssueStoreReader(org.sonarsource.sonarlint.core.container.storage.IssueStoreReader) SonarLintRuleDefinition(org.sonarsource.sonarlint.core.rule.extractor.SonarLintRuleDefinition) Version(org.sonarsource.sonarlint.core.commons.Version) StringUtils(org.apache.commons.lang3.StringUtils) ProjectStorageStatus(org.sonarsource.sonarlint.core.client.api.connected.ProjectStorageStatus) ServerApiHelper(org.sonarsource.sonarlint.core.serverapi.ServerApiHelper) AnalyzeCommand(org.sonarsource.sonarlint.core.analysis.command.AnalyzeCommand) Map(java.util.Map) ConnectedRuleDetails(org.sonarsource.sonarlint.core.client.api.connected.ConnectedRuleDetails) GlobalStores(org.sonarsource.sonarlint.core.container.storage.GlobalStores) Path(java.nio.file.Path) ActiveRule(org.sonarsource.sonarlint.core.analysis.api.ActiveRule) ConnectedSonarLintEngine(org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine) EndpointParams(org.sonarsource.sonarlint.core.serverapi.EndpointParams) StringUtils.trimToNull(org.apache.commons.lang3.StringUtils.trimToNull) StorageException(org.sonarsource.sonarlint.core.client.api.exceptions.StorageException) StorageFileExclusions(org.sonarsource.sonarlint.core.container.storage.StorageFileExclusions) Predicate(java.util.function.Predicate) DefaultRuleDetails(org.sonarsource.sonarlint.core.container.storage.DefaultRuleDetails) PluginInstancesRepository(org.sonarsource.sonarlint.core.plugin.commons.PluginInstancesRepository) Collection(java.util.Collection) ServerActiveRule(org.sonarsource.sonarlint.core.serverapi.rules.ServerActiveRule) Set(java.util.Set) ServerProject(org.sonarsource.sonarlint.core.serverapi.component.ServerProject) ProjectListDownloader(org.sonarsource.sonarlint.core.container.connected.update.ProjectListDownloader) Collectors(java.util.stream.Collectors) List(java.util.List) SonarLintWrappedException(org.sonarsource.sonarlint.core.client.api.exceptions.SonarLintWrappedException) AnalysisEngineConfiguration(org.sonarsource.sonarlint.core.analysis.api.AnalysisEngineConfiguration) HttpClient(org.sonarsource.sonarlint.core.commons.http.HttpClient) Optional(java.util.Optional) FilenameUtils(org.apache.commons.io.FilenameUtils) IssueStoreFactory(org.sonarsource.sonarlint.core.container.connected.IssueStoreFactory) AnalysisResults(org.sonarsource.sonarlint.core.analysis.api.AnalysisResults) GlobalStorageUpdateExecutor(org.sonarsource.sonarlint.core.container.connected.update.perform.GlobalStorageUpdateExecutor) Configuration(org.sonarsource.sonarlint.core.plugin.commons.PluginInstancesRepository.Configuration) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ConnectedAnalysisConfiguration(org.sonarsource.sonarlint.core.client.api.connected.ConnectedAnalysisConfiguration) ServerIssue(org.sonarsource.sonarlint.core.client.api.connected.ServerIssue) AnalysisEngine(org.sonarsource.sonarlint.core.analysis.AnalysisEngine) ProjectStorageStatusReader(org.sonarsource.sonarlint.core.container.storage.ProjectStorageStatusReader) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ConnectedGlobalConfiguration(org.sonarsource.sonarlint.core.client.api.connected.ConnectedGlobalConfiguration) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) IssueListener(org.sonarsource.sonarlint.core.client.api.common.analysis.IssueListener) ProjectStoragePaths.encodeForFs(org.sonarsource.sonarlint.core.container.storage.ProjectStoragePaths.encodeForFs) UpdateResult(org.sonarsource.sonarlint.core.client.api.connected.UpdateResult) LocalStorageSynchronizer(org.sonarsource.sonarlint.core.storage.LocalStorageSynchronizer) ProjectStorageUpdateExecutor(org.sonarsource.sonarlint.core.container.connected.update.perform.ProjectStorageUpdateExecutor) Issue(org.sonarsource.sonarlint.core.analysis.api.Issue) Objects.requireNonNull(java.util.Objects.requireNonNull) ProjectStorage(org.sonarsource.sonarlint.core.storage.ProjectStorage) IssueStorePaths(org.sonarsource.sonarlint.core.container.connected.update.IssueStorePaths) ProjectStoragePaths(org.sonarsource.sonarlint.core.container.storage.ProjectStoragePaths) DownloadException(org.sonarsource.sonarlint.core.client.api.exceptions.DownloadException) Nullable(javax.annotation.Nullable) ProjectBranches(org.sonarsource.sonarlint.core.client.api.connected.ProjectBranches) AnalysisConfiguration(org.sonarsource.sonarlint.core.analysis.api.AnalysisConfiguration) ProjectBinding(org.sonarsource.sonarlint.core.client.api.connected.ProjectBinding) StorageReader(org.sonarsource.sonarlint.core.container.storage.StorageReader) PluginDetails(org.sonarsource.sonarlint.core.client.api.common.PluginDetails) ClientProgressMonitor(org.sonarsource.sonarlint.core.commons.progress.ClientProgressMonitor) PluginsStorage(org.sonarsource.sonarlint.core.storage.PluginsStorage) ServerApi(org.sonarsource.sonarlint.core.serverapi.ServerApi) PartialUpdaterFactory(org.sonarsource.sonarlint.core.container.storage.partialupdate.PartialUpdaterFactory) Paths(java.nio.file.Paths) GlobalStorageStatus(org.sonarsource.sonarlint.core.client.api.connected.GlobalStorageStatus) ProgressMonitor(org.sonarsource.sonarlint.core.commons.progress.ProgressMonitor) DefaultRuleDetails(org.sonarsource.sonarlint.core.container.storage.DefaultRuleDetails) ServerApi(org.sonarsource.sonarlint.core.serverapi.ServerApi) ServerApiHelper(org.sonarsource.sonarlint.core.serverapi.ServerApiHelper)

Example 7 with ServerApiHelper

use of org.sonarsource.sonarlint.core.serverapi.ServerApiHelper in project sonarlint-core by SonarSource.

the class OrganizationApiTests method testListUserOrganizationWithMoreThan20Pages.

@Test
void testListUserOrganizationWithMoreThan20Pages() {
    var underTest = new OrganizationApi(new ServerApiHelper(mockServer.endpointParams("myOrg"), MockWebServerExtension.httpClient()));
    mockServer.addStringResponse("/api/system/status", "{\"id\": \"20160308094653\",\"version\": \"7.9\",\"status\": \"UP\"}");
    for (var i = 0; i < 21; i++) {
        mockOrganizationsPage(i + 1, 10500);
    }
    var orgs = underTest.listUserOrganizations(progressMonitor);
    assertThat(orgs).hasSize(10500);
}
Also used : ServerApiHelper(org.sonarsource.sonarlint.core.serverapi.ServerApiHelper) Test(org.junit.jupiter.api.Test)

Example 8 with ServerApiHelper

use of org.sonarsource.sonarlint.core.serverapi.ServerApiHelper in project sonarlint-core by SonarSource.

the class OrganizationApiTests method should_not_get_organization_if_server_is_too_old.

@Test
void should_not_get_organization_if_server_is_too_old() {
    mockServer.addStringResponse("/api/system/status", "{" + "\"status\": \"UP\"," + "\"version\": \"7.8.0\"" + "}");
    var underTest = new OrganizationApi(new ServerApiHelper(mockServer.endpointParams(), MockWebServerExtension.httpClient()));
    var throwable = catchThrowable(() -> underTest.getOrganization("org:key", progressMonitor));
    assertThat(throwable).isInstanceOf(UnsupportedServerException.class).hasMessageContaining("SonarQube server has version 7.8.0. Version should be greater or equal to");
}
Also used : ServerApiHelper(org.sonarsource.sonarlint.core.serverapi.ServerApiHelper) UnsupportedServerException(org.sonarsource.sonarlint.core.serverapi.exception.UnsupportedServerException) Test(org.junit.jupiter.api.Test)

Example 9 with ServerApiHelper

use of org.sonarsource.sonarlint.core.serverapi.ServerApiHelper in project sonarlint-core by SonarSource.

the class ConnectionValidatorTests method testConnection_ok.

@Test
void testConnection_ok() throws ExecutionException, InterruptedException {
    var underTest = new ConnectionValidator(new ServerApiHelper(mockServer.endpointParams(), MockWebServerExtension.httpClient()));
    mockServer.addStringResponse("/api/system/status", "{\"id\": \"20160308094653\",\"version\": \"7.9\",\"status\": \"UP\"}");
    mockServer.addStringResponse("/api/authentication/validate?format=json", "{\"valid\": true}");
    var futureValidation = underTest.validateConnection();
    var validation = futureValidation.get();
    assertThat(validation.success()).isTrue();
    assertThat(mockServer.takeRequest().getPath()).isEqualTo("/api/system/status");
    assertThat(mockServer.takeRequest().getPath()).isEqualTo("/api/authentication/validate?format=json");
}
Also used : ServerApiHelper(org.sonarsource.sonarlint.core.serverapi.ServerApiHelper) Test(org.junit.jupiter.api.Test)

Example 10 with ServerApiHelper

use of org.sonarsource.sonarlint.core.serverapi.ServerApiHelper in project sonarlint-core by SonarSource.

the class ConnectionValidatorTests method testClientError.

@Test
void testClientError() throws ExecutionException, InterruptedException {
    var underTest = new ConnectionValidator(new ServerApiHelper(mockServer.endpointParams(), MockWebServerExtension.httpClient()));
    var mockResponse = new MockResponse();
    mockResponse.setResponseCode(400);
    mockServer.addResponse("/api/system/status", mockResponse);
    var futureValidation = underTest.validateConnection();
    var validation = futureValidation.get();
    assertThat(validation.success()).isFalse();
    assertThat(validation.message()).isEqualTo("Error 400 on " + mockServer.endpointParams().getBaseUrl() + "api/system/status");
}
Also used : MockResponse(mockwebserver3.MockResponse) ServerApiHelper(org.sonarsource.sonarlint.core.serverapi.ServerApiHelper) Test(org.junit.jupiter.api.Test)

Aggregations

ServerApiHelper (org.sonarsource.sonarlint.core.serverapi.ServerApiHelper)18 Test (org.junit.jupiter.api.Test)10 ClientProgressMonitor (org.sonarsource.sonarlint.core.commons.progress.ClientProgressMonitor)4 ProgressMonitor (org.sonarsource.sonarlint.core.commons.progress.ProgressMonitor)4 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Test (org.junit.Test)2 PluginDetails (org.sonarsource.sonarlint.core.client.api.common.PluginDetails)2 ConnectedGlobalConfiguration (org.sonarsource.sonarlint.core.client.api.connected.ConnectedGlobalConfiguration)2 ConnectedSonarLintEngine (org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine)2 ConnectionValidator (org.sonarsource.sonarlint.core.client.api.connected.ConnectionValidator)2 UpdateResult (org.sonarsource.sonarlint.core.client.api.connected.UpdateResult)2 StorageException (org.sonarsource.sonarlint.core.client.api.exceptions.StorageException)2 Orchestrator (com.sonar.orchestrator.Orchestrator)1 MavenLocation (com.sonar.orchestrator.locator.MavenLocation)1 SONAR_VERSION (its.tools.ItUtils.SONAR_VERSION)1 Paths (java.nio.file.Paths)1 Collection (java.util.Collection)1 Collections.emptySet (java.util.Collections.emptySet)1