Search in sources :

Example 1 with InvalidBindingException

use of org.sonarlint.intellij.exception.InvalidBindingException in project sonarlint-intellij by SonarSource.

the class SonarLintAnalyzer method analyzeModule.

public AnalysisResults analyzeModule(Module module, Collection<VirtualFile> filesToAnalyze, IssueListener listener, ProgressMonitor progressMonitor) {
    // Configure plugin properties. Nothing might be done if there is no configurator available for the extensions loaded in runtime.
    Map<String, String> pluginProps = new HashMap<>();
    AnalysisConfigurator[] analysisConfigurators = AnalysisConfigurator.EP_NAME.getExtensions();
    if (analysisConfigurators.length > 0) {
        for (AnalysisConfigurator config : analysisConfigurators) {
            console.debug("Configuring analysis with " + config.getClass().getName());
            pluginProps.putAll(config.configure(module));
        }
    } else {
        console.info("No analysis configurator found");
    }
    // configure files
    VirtualFileTestPredicate testPredicate = SonarLintUtils.get(module, VirtualFileTestPredicate.class);
    List<ClientInputFile> inputFiles = getInputFiles(module, testPredicate, filesToAnalyze);
    // Analyze
    long start = System.currentTimeMillis();
    try {
        SonarLintFacade facade = projectBindingManager.getFacade(true);
        String what;
        if (filesToAnalyze.size() == 1) {
            what = "'" + filesToAnalyze.iterator().next().getName() + "'";
        } else {
            what = Integer.toString(filesToAnalyze.size()) + " files";
        }
        console.info("Analysing " + what + "...");
        if (facade.requiresSavingFiles()) {
            console.debug("Saving files");
            LOG.assertTrue(!ApplicationManager.getApplication().isReadAccessAllowed(), "Should not be in a read action (risk of dead lock)");
            ApplicationManager.getApplication().invokeAndWait(() -> SonarLintUtils.saveFiles(filesToAnalyze), ModalityState.defaultModalityState());
        }
        AnalysisResults result = facade.startAnalysis(inputFiles, listener, pluginProps, progressMonitor);
        console.debug("Done in " + (System.currentTimeMillis() - start) + "ms\n");
        if (filesToAnalyze.size() == 1) {
            telemetry.analysisDoneOnSingleFile(filesToAnalyze.iterator().next().getExtension(), (int) (System.currentTimeMillis() - start));
        } else {
            telemetry.analysisDoneOnMultipleFiles();
        }
        return result;
    } catch (InvalidBindingException e) {
        // should not happen, as analysis should not have been submitted in this case.
        throw new IllegalStateException(e);
    }
}
Also used : InvalidBindingException(org.sonarlint.intellij.exception.InvalidBindingException) HashMap(java.util.HashMap) SonarLintFacade(org.sonarlint.intellij.core.SonarLintFacade) AnalysisResults(org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile)

Example 2 with InvalidBindingException

use of org.sonarlint.intellij.exception.InvalidBindingException in project sonarlint-intellij by SonarSource.

the class ServerIssueUpdater method fetchAndMatchServerIssues.

public void fetchAndMatchServerIssues(Collection<VirtualFile> virtualFiles, ProgressIndicator indicator, boolean waitForCompletion) {
    if (!projectSettings.isBindingEnabled()) {
        // not in connected mode
        return;
    }
    try {
        SonarQubeServer server = projectBindingManager.getSonarQubeServer();
        ConnectedSonarLintEngine engine = projectBindingManager.getConnectedEngine();
        String moduleKey = projectSettings.getProjectKey();
        boolean downloadAll = virtualFiles.size() >= FETCH_ALL_ISSUES_THRESHOLD;
        String msg;
        if (downloadAll) {
            msg = "Fetching all server issues";
        } else {
            msg = "Fetching server issues";
        }
        if (waitForCompletion) {
            msg += " (waiting for results)";
        }
        console.debug(msg);
        indicator.setText(msg);
        // submit tasks
        List<Future<Void>> updateTasks = fetchAndMatchServerIssues(virtualFiles, server, engine, moduleKey, downloadAll);
        if (waitForCompletion) {
            waitForTasks(updateTasks);
        }
    } catch (InvalidBindingException e) {
    // ignore, do nothing
    }
}
Also used : InvalidBindingException(org.sonarlint.intellij.exception.InvalidBindingException) SonarQubeServer(org.sonarlint.intellij.config.global.SonarQubeServer) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) ConnectedSonarLintEngine(org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine)

Example 3 with InvalidBindingException

use of org.sonarlint.intellij.exception.InvalidBindingException in project sonarlint-intellij by SonarSource.

the class SonarLintEngineManager method checkConnectedEngineStatus.

private static void checkConnectedEngineStatus(ConnectedSonarLintEngine engine, SonarLintProjectNotifications notifications, String serverId, String projectKey) throws InvalidBindingException {
    // Check if engine's global storage is OK
    ConnectedSonarLintEngine.State state = engine.getState();
    if (state != ConnectedSonarLintEngine.State.UPDATED) {
        if (state != ConnectedSonarLintEngine.State.NEED_UPDATE) {
            notifications.notifyServerNotUpdated();
        } else if (state != ConnectedSonarLintEngine.State.NEVER_UPDATED) {
            notifications.notifyServerStorageNeedsUpdate(serverId);
        }
        throw new InvalidBindingException("Server is not updated: " + serverId);
    }
    // Check if module's storage is OK. Global storage was updated and all project's binding that were open too,
    // but we might have now opened a new project with a different binding.
    ModuleStorageStatus moduleStorageStatus = engine.getModuleStorageStatus(projectKey);
    if (moduleStorageStatus == null) {
        notifications.notifyModuleInvalid();
        throw new InvalidBindingException("Project is bound to a module that doesn't exist: " + projectKey);
    } else if (moduleStorageStatus.isStale()) {
        notifications.notifyModuleStale();
        throw new InvalidBindingException("Stale module's storage: " + projectKey);
    }
}
Also used : InvalidBindingException(org.sonarlint.intellij.exception.InvalidBindingException) ConnectedSonarLintEngine(org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine) ModuleStorageStatus(org.sonarsource.sonarlint.core.client.api.connected.ModuleStorageStatus)

Example 4 with InvalidBindingException

use of org.sonarlint.intellij.exception.InvalidBindingException in project sonarlint-intellij by SonarSource.

the class SonarQubeEventNotifications method register.

private void register(SonarLintProjectSettings settings) {
    unregister();
    if (settings.isBindingEnabled()) {
        SonarQubeServer server;
        try {
            server = bindingManager.getSonarQubeServer();
        } catch (InvalidBindingException e) {
            // do nothing
            return;
        }
        if (server.enableNotifications()) {
            NotificationConfiguration config = createConfiguration(settings, server);
            SonarQubeNotifications.get().register(config);
        }
    }
}
Also used : InvalidBindingException(org.sonarlint.intellij.exception.InvalidBindingException) SonarQubeServer(org.sonarlint.intellij.config.global.SonarQubeServer) NotificationConfiguration(org.sonarsource.sonarlint.core.client.api.common.NotificationConfiguration)

Example 5 with InvalidBindingException

use of org.sonarlint.intellij.exception.InvalidBindingException in project sonarlint-intellij by SonarSource.

the class SonarLinkHandler method getDescription.

@Nullable
@Override
public String getDescription(@NotNull String refSuffix, @NotNull Editor editor) {
    Project project = editor.getProject();
    if (project == null || project.isDisposed()) {
        return null;
    }
    ProjectBindingManager projectBindingManager = SonarLintUtils.get(project, ProjectBindingManager.class);
    try {
        SonarLintFacade sonarlint = projectBindingManager.getFacade();
        String description = sonarlint.getDescription(refSuffix);
        String name = sonarlint.getRuleName(refSuffix);
        return transform(refSuffix, name, description);
    } catch (InvalidBindingException e) {
        return "";
    }
}
Also used : Project(com.intellij.openapi.project.Project) InvalidBindingException(org.sonarlint.intellij.exception.InvalidBindingException) ProjectBindingManager(org.sonarlint.intellij.core.ProjectBindingManager) SonarLintFacade(org.sonarlint.intellij.core.SonarLintFacade) Nullable(javax.annotation.Nullable)

Aggregations

InvalidBindingException (org.sonarlint.intellij.exception.InvalidBindingException)10 ConnectedSonarLintEngine (org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine)7 SonarQubeServer (org.sonarlint.intellij.config.global.SonarQubeServer)6 Project (com.intellij.openapi.project.Project)4 List (java.util.List)3 SonarLintProjectSettings (org.sonarlint.intellij.config.project.SonarLintProjectSettings)3 SonarLintConsole (org.sonarlint.intellij.ui.SonarLintConsole)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 FileUtil (com.intellij.openapi.util.io.FileUtil)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Collections (java.util.Collections)2 LinkedList (java.util.LinkedList)2 Nullable (javax.annotation.Nullable)2 Before (org.junit.Before)2 Rule (org.junit.Rule)2 Test (org.junit.Test)2 TemporaryFolder (org.junit.rules.TemporaryFolder)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2