Search in sources :

Example 11 with ServerApi

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

the class ConnectedModeTest method canFetchHotspot.

@Test
public void canFetchHotspot() throws InvalidProtocolBufferException {
    assumeTrue("SonarQube should support opening security hotspots", ORCHESTRATOR.getServer().version().isGreaterThanOrEquals(8, 6));
    analyzeMavenProject(PROJECT_KEY_JAVA_HOTSPOT);
    var securityHotspotsService = new ServerApi(endpointParams(ORCHESTRATOR), sqHttpClient()).hotspot();
    var remoteHotspot = securityHotspotsService.fetch(new GetSecurityHotspotRequestParams(getFirstHotspotKey(PROJECT_KEY_JAVA_HOTSPOT), PROJECT_KEY_JAVA_HOTSPOT));
    assertThat(remoteHotspot).isNotEmpty();
    var actualHotspot = remoteHotspot.get();
    assertThat(actualHotspot.message).isEqualTo("Make sure using this hardcoded IP address is safe here.");
    assertThat(actualHotspot.filePath).isEqualTo("src/main/java/foo/Foo.java");
    assertThat(actualHotspot.textRange).isEqualToComparingFieldByField(new TextRange(5, 14, 5, 29));
    assertThat(actualHotspot.author).isEmpty();
    assertThat(actualHotspot.status).isEqualTo(ServerHotspot.Status.TO_REVIEW);
    assertThat(actualHotspot.resolution).isNull();
    assertThat(actualHotspot.rule.key).isEqualTo("java:S1313");
}
Also used : GetSecurityHotspotRequestParams(org.sonarsource.sonarlint.core.serverapi.hotspot.GetSecurityHotspotRequestParams) ServerApi(org.sonarsource.sonarlint.core.serverapi.ServerApi) TextRange(org.sonarsource.sonarlint.core.analysis.api.TextRange) Test(org.junit.Test)

Example 12 with ServerApi

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

the class IssueDownloader method download.

/**
 * Fetch all issues of the component with specified key.
 * If the component doesn't exist or it exists but has no issues, an empty iterator is returned.
 *
 * @param key project key, or file key.
 * @param branchName name of the branch. If null - issues will be downloaded only for the main branch.
 * @return Iterator of issues. It can be empty but never null.
 */
public List<Sonarlint.ServerIssue> download(ServerApiHelper serverApiHelper, String key, ProjectConfiguration projectConfiguration, boolean fetchTaintVulnerabilities, @Nullable String branchName, ProgressMonitor progress) {
    var issueApi = new ServerApi(serverApiHelper).issue();
    var issueBuilder = Sonarlint.ServerIssue.newBuilder();
    var locationBuilder = Location.newBuilder();
    var textRangeBuilder = Sonarlint.ServerIssue.TextRange.newBuilder();
    var flowBuilder = Sonarlint.ServerIssue.Flow.newBuilder();
    List<Sonarlint.ServerIssue> result = new ArrayList<>();
    var batchIssues = issueApi.downloadAllFromBatchIssues(key, branchName);
    Set<String> taintRuleKeys = new HashSet<>();
    for (ScannerInput.ServerIssue batchIssue : batchIssues) {
        if (IssueApi.TAINT_REPOS.contains(batchIssue.getRuleRepository())) {
            if (NON_CLOSED_STATUSES.contains(batchIssue.getStatus())) {
                taintRuleKeys.add(new org.sonarsource.sonarlint.core.client.api.common.RuleKey(batchIssue.getRuleRepository(), batchIssue.getRuleKey()).toString());
            }
        } else {
            result.add(toStorageIssue(batchIssue, projectConfiguration, issueBuilder, locationBuilder, textRangeBuilder));
        }
    }
    if (fetchTaintVulnerabilities && !taintRuleKeys.isEmpty()) {
        Map<String, String> sourceCodeByKey = new HashMap<>();
        try {
            var downloadVulnerabilitiesForRules = issueApi.downloadVulnerabilitiesForRules(key, taintRuleKeys, branchName, progress);
            downloadVulnerabilitiesForRules.getIssues().forEach(i -> result.add(convertTaintIssue(new ServerApi(serverApiHelper).source(), projectConfiguration, issueBuilder, locationBuilder, textRangeBuilder, flowBuilder, i, downloadVulnerabilitiesForRules.getComponentPathsByKey(), sourceCodeByKey)));
        } catch (Exception e) {
            LOG.warn("Unable to fetch taint vulnerabilities", e);
        }
    }
    return result;
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ServerIssue(org.sonarsource.sonarlint.core.proto.Sonarlint.ServerIssue) ScannerInput(org.sonar.scanner.protocol.input.ScannerInput) ServerApi(org.sonarsource.sonarlint.core.serverapi.ServerApi) HashSet(java.util.HashSet)

Example 13 with ServerApi

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

the class ModuleHierarchyDownloader method fetchModuleHierarchy.

/**
 * Downloads the module hierarchy information starting from a given module key.
 * It returns the relative paths to the given root module for all its sub projects.
 *
 * @param projectKey project for which the hierarchy will be returned.
 * @return Mapping of moduleKey -&gt; relativePath from given module
 */
public Map<String, String> fetchModuleHierarchy(ServerApiHelper serverApiHelper, String projectKey, ProgressMonitor progress) {
    var componentApi = new ServerApi(serverApiHelper).component();
    var modules = componentApi.getSubProjects(projectKey, progress);
    // doesn't include root
    Map<String, ComponentPath> modulesByKey = modules.stream().collect(Collectors.toMap(ComponentPath::getKey, Function.identity()));
    // component -> ancestorComponent. Doesn't include root
    Map<ComponentPath, ComponentPath> ancestors = new HashMap<>();
    for (ComponentPath c : modules) {
        ancestors.put(c, modulesByKey.get(componentApi.fetchFirstAncestorKey(c.getKey()).orElse(null)));
    }
    // module key -> path from root project base directory
    Map<String, String> modulesWithPath = new HashMap<>();
    modulesWithPath.put(projectKey, "");
    modules.forEach(c -> modulesWithPath.put(c.getKey(), findPathFromRoot(c, ancestors)));
    return modulesWithPath;
}
Also used : ComponentPath(org.sonarsource.sonarlint.core.serverapi.component.ComponentPath) HashMap(java.util.HashMap) ServerApi(org.sonarsource.sonarlint.core.serverapi.ServerApi)

Example 14 with ServerApi

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

the class SonarCloudTest method downloadUserOrganizations.

@Test
public void downloadUserOrganizations() {
    var helper = new ServerApi(sonarcloudEndpointITOrg(), new SonarLintHttpClientOkHttpImpl(SC_CLIENT)).organization();
    assertThat(helper.listUserOrganizations(progress)).hasSize(1);
}
Also used : ServerApi(org.sonarsource.sonarlint.core.serverapi.ServerApi) Test(org.junit.Test)

Example 15 with ServerApi

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

the class ConnectedSonarLintEngineImpl method sync.

@Override
public void sync(EndpointParams endpoint, HttpClient client, Set<String> projectKeys, @Nullable ClientProgressMonitor monitor) {
    var serverApi = new ServerApi(new ServerApiHelper(endpoint, client));
    var result = storageSynchronizer.synchronize(serverApi, projectKeys, new ProgressMonitor(monitor));
    if (result.hasAnalyzerBeenUpdated()) {
        restartAnalysisEngine();
    }
}
Also used : ClientProgressMonitor(org.sonarsource.sonarlint.core.commons.progress.ClientProgressMonitor) ProgressMonitor(org.sonarsource.sonarlint.core.commons.progress.ProgressMonitor) ServerApi(org.sonarsource.sonarlint.core.serverapi.ServerApi) ServerApiHelper(org.sonarsource.sonarlint.core.serverapi.ServerApiHelper)

Aggregations

ServerApi (org.sonarsource.sonarlint.core.serverapi.ServerApi)23 Test (org.junit.jupiter.api.Test)13 ProgressMonitor (org.sonarsource.sonarlint.core.commons.progress.ProgressMonitor)13 Test (org.junit.Test)5 HashMap (java.util.HashMap)3 ServerApiHelper (org.sonarsource.sonarlint.core.serverapi.ServerApiHelper)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Optional (java.util.Optional)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Set (java.util.Set)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1