Search in sources :

Example 1 with ISonarLintFile

use of org.sonarlint.eclipse.core.resource.ISonarLintFile in project sonarlint-eclipse by SonarSource.

the class AsyncServerMarkerUpdaterJob method updateMarkers.

private void updateMarkers(IProgressMonitor monitor) {
    for (var entry : issuesPerResource.entrySet()) {
        var issuable = entry.getKey();
        if (issuable instanceof ISonarLintFile) {
            var documentOrNull = docPerFile.get(issuable);
            final IDocument documentNotNull;
            if (documentOrNull == null) {
                documentNotNull = ((ISonarLintFile) issuable).getDocument();
            } else {
                documentNotNull = documentOrNull;
            }
            var markerRule = ResourcesPlugin.getWorkspace().getRuleFactory().markerRule(issuable.getResource());
            try {
                getJobManager().beginRule(markerRule, monitor);
                SonarLintMarkerUpdater.updateMarkersWithServerSideData(issuable, documentNotNull, entry.getValue(), triggerType);
            } finally {
                getJobManager().endRule(markerRule);
            }
        }
    }
}
Also used : ISonarLintFile(org.sonarlint.eclipse.core.resource.ISonarLintFile) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with ISonarLintFile

use of org.sonarlint.eclipse.core.resource.ISonarLintFile in project sonarlint-eclipse by SonarSource.

the class EclipseInputFile method initFromFS.

private synchronized void initFromFS(ISonarLintFile file, Path temporaryDirectory) {
    IFileStore fileStore;
    try {
        fileStore = EFS.getStore(file.getResource().getLocationURI());
        var localFile = fileStore.toLocalFile(EFS.NONE, null);
        if (localFile == null) {
            // For analyzers to properly work we should ensure the temporary file has a "correct" name, and not a generated one
            localFile = new File(temporaryDirectory.toFile(), file.getProjectRelativePath());
            Files.createDirectories(localFile.getParentFile().toPath());
            fileStore.copy(EFS.getStore(localFile.toURI()), EFS.OVERWRITE, null);
        }
        filePath = localFile.toPath().toAbsolutePath();
    } catch (Exception e) {
        throw new IllegalStateException("Unable to find path for file " + file, e);
    }
}
Also used : IFileStore(org.eclipse.core.filesystem.IFileStore) ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) File(java.io.File) ISonarLintFile(org.sonarlint.eclipse.core.resource.ISonarLintFile) IOException(java.io.IOException)

Example 3 with ISonarLintFile

use of org.sonarlint.eclipse.core.resource.ISonarLintFile in project sonarlint-eclipse by SonarSource.

the class AbstractAnalyzeProjectJob method doRun.

@Override
protected IStatus doRun(final IProgressMonitor monitor) {
    if (monitor.isCanceled()) {
        return Status.CANCEL_STATUS;
    }
    var startTime = System.currentTimeMillis();
    SonarLintLogger.get().debug("Trigger: " + triggerType.name());
    Path analysisWorkDir = null;
    try {
        var excludedFiles = new ArrayList<ISonarLintFile>();
        var filesToAnalyze = new ArrayList<FileWithDocument>();
        var exclusionsChecker = new FileExclusionsChecker(getProject());
        files.forEach(fWithDoc -> {
            var file = fWithDoc.getFile();
            if (exclusionsChecker.isExcluded(file, true) || isScmIgnored(file)) {
                excludedFiles.add(file);
            } else {
                filesToAnalyze.add(fWithDoc);
            }
        });
        Map<ISonarLintFile, IDocument> filesToAnalyzeMap = filesToAnalyze.stream().collect(HashMap::new, (m, fWithDoc) -> m.put(fWithDoc.getFile(), fWithDoc.getDocument()), HashMap::putAll);
        SonarLintLogger.get().debug("Clear markers on " + excludedFiles.size() + " excluded files");
        ResourcesPlugin.getWorkspace().run(m -> {
            excludedFiles.forEach(SonarLintMarkerUpdater::clearMarkers);
            if (shouldClearReport) {
                SonarLintMarkerUpdater.deleteAllMarkersFromReport();
            }
        }, monitor);
        if (filesToAnalyze.isEmpty()) {
            return Status.OK_STATUS;
        }
        // Analyze
        SonarLintLogger.get().info(this.getName() + "...");
        // Configure
        var mergedExtraProps = new LinkedHashMap<String, String>();
        var usedDeprecatedConfigurators = configureDeprecated(getProject(), filesToAnalyzeMap.keySet(), mergedExtraProps, monitor);
        analysisWorkDir = Files.createTempDirectory(getProject().getWorkingDir(), "sonarlint");
        var inputFiles = buildInputFiles(analysisWorkDir, filesToAnalyzeMap);
        var usedConfigurators = configure(getProject(), inputFiles, mergedExtraProps, analysisWorkDir, monitor);
        extraProps.forEach(sonarProperty -> mergedExtraProps.put(sonarProperty.getName(), sonarProperty.getValue()));
        if (!inputFiles.isEmpty()) {
            runAnalysisAndUpdateMarkers(filesToAnalyzeMap, monitor, mergedExtraProps, inputFiles, analysisWorkDir);
        }
        analysisCompleted(usedDeprecatedConfigurators, usedConfigurators, mergedExtraProps, monitor);
        SonarLintCorePlugin.getAnalysisListenerManager().notifyListeners();
        SonarLintLogger.get().debug(String.format("Done in %d ms", System.currentTimeMillis() - startTime));
    } catch (CanceledException e) {
        return Status.CANCEL_STATUS;
    } catch (Exception e) {
        SonarLintLogger.get().error("Error during execution of SonarLint analysis", e);
        return new Status(IStatus.WARNING, SonarLintCorePlugin.PLUGIN_ID, "Error when executing SonarLint analysis", e);
    } finally {
        if (analysisWorkDir != null) {
            try {
                FileUtils.deleteRecursively(analysisWorkDir);
            } catch (Exception e) {
                SonarLintLogger.get().debug("Unable to delete temp directory: " + analysisWorkDir, e);
            }
        }
    }
    return monitor.isCanceled() ? Status.CANCEL_STATUS : Status.OK_STATUS;
}
Also used : Path(java.nio.file.Path) CanceledException(org.sonarsource.sonarlint.core.commons.progress.CanceledException) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FileExclusionsChecker(org.sonarlint.eclipse.core.internal.utils.FileExclusionsChecker) ArrayList(java.util.ArrayList) ISonarLintFile(org.sonarlint.eclipse.core.resource.ISonarLintFile) CanceledException(org.sonarsource.sonarlint.core.commons.progress.CanceledException) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) LinkedHashMap(java.util.LinkedHashMap) IDocument(org.eclipse.jface.text.IDocument)

Example 4 with ISonarLintFile

use of org.sonarlint.eclipse.core.resource.ISonarLintFile in project sonarlint-eclipse by SonarSource.

the class AnalyzeChangedFilesJob method runInWorkspace.

@Override
public IStatus runInWorkspace(IProgressMonitor monitor) {
    var global = SubMonitor.convert(monitor, 100);
    try {
        global.setTaskName("Collect changed file(s) list");
        SonarLintMarkerUpdater.deleteAllMarkersFromReport();
        var collectChangedFiles = collectChangedFiles(projects, global.newChild(20));
        if (collectChangedFiles.isEmpty()) {
            SonarLintLogger.get().info("No changed files found");
            return Status.OK_STATUS;
        }
        var changedFilesPerProject = collectChangedFiles.stream().collect(Collectors.groupingBy(ISonarLintFile::getProject));
        long fileCount = changedFilesPerProject.values().stream().flatMap(Collection::stream).count();
        SonarLintLogger.get().info("Analyzing " + fileCount + " changed file(s) in " + changedFilesPerProject.size() + " project(s)");
        global.setTaskName("Analysis");
        var analysisMonitor = SubMonitor.convert(global.newChild(80), changedFilesPerProject.size());
        for (var entry : changedFilesPerProject.entrySet()) {
            if (monitor.isCanceled()) {
                return Status.CANCEL_STATUS;
            }
            var project = entry.getKey();
            if (!project.isOpen()) {
                analysisMonitor.worked(1);
                continue;
            }
            global.setTaskName("Analyzing project " + project.getName());
            var filesToAnalyze = entry.getValue().stream().map(f -> new FileWithDocument(f, null)).collect(Collectors.toList());
            var req = new AnalyzeProjectRequest(project, filesToAnalyze, TriggerType.MANUAL_CHANGESET);
            var job = AbstractAnalyzeProjectJob.create(req);
            var subMonitor = analysisMonitor.newChild(1);
            job.run(subMonitor);
            subMonitor.done();
        }
    } catch (Exception e) {
        SonarLintLogger.get().error(UNABLE_TO_ANALYZE_CHANGED_FILES, e);
        return new Status(Status.ERROR, SonarLintCorePlugin.PLUGIN_ID, UNABLE_TO_ANALYZE_CHANGED_FILES, e);
    }
    return monitor.isCanceled() ? Status.CANCEL_STATUS : Status.OK_STATUS;
}
Also used : ISonarLintProject(org.sonarlint.eclipse.core.resource.ISonarLintProject) Job(org.eclipse.core.runtime.jobs.Job) SubMonitor(org.eclipse.core.runtime.SubMonitor) Collection(java.util.Collection) Status(org.eclipse.core.runtime.Status) SonarLintLogger(org.sonarlint.eclipse.core.SonarLintLogger) FileWithDocument(org.sonarlint.eclipse.core.internal.jobs.AnalyzeProjectRequest.FileWithDocument) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) IStatus(org.eclipse.core.runtime.IStatus) TriggerType(org.sonarlint.eclipse.core.internal.TriggerType) ISonarLintFile(org.sonarlint.eclipse.core.resource.ISonarLintFile) SonarLintCorePlugin(org.sonarlint.eclipse.core.internal.SonarLintCorePlugin) DefaultSonarLintProjectAdapter(org.sonarlint.eclipse.core.internal.resources.DefaultSonarLintProjectAdapter) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) FileWithDocument(org.sonarlint.eclipse.core.internal.jobs.AnalyzeProjectRequest.FileWithDocument)

Example 5 with ISonarLintFile

use of org.sonarlint.eclipse.core.resource.ISonarLintFile in project sonarlint-eclipse by SonarSource.

the class ConnectedEngineFacade method updateProjectStorage.

@Override
public void updateProjectStorage(String projectKey, @Nullable String branchName, IProgressMonitor monitor) {
    doWithEngine(engine -> {
        engine.updateProject(createEndpointParams(), buildClientWithProxyAndCredentials(), projectKey, true, branchName, new WrappedProgressMonitor(monitor, "Update configuration from server '" + getId() + "' for project '" + projectKey + "'"));
        getBoundProjects(projectKey).forEach(p -> {
            var projectBinding = engine.calculatePathPrefixes(projectKey, p.files().stream().map(ISonarLintFile::getProjectRelativePath).collect(toList()));
            var idePathPrefix = projectBinding.idePathPrefix();
            var sqPathPrefix = projectBinding.sqPathPrefix();
            SonarLintLogger.get().debug("Detected prefixes for " + p.getName() + ":\n  IDE prefix: " + idePathPrefix + "\n  Server side prefix: " + sqPathPrefix);
            SonarLintProjectConfiguration config = SonarLintCorePlugin.loadConfig(p);
            config.setProjectBinding(new EclipseProjectBinding(getId(), projectKey, sqPathPrefix, idePathPrefix));
            SonarLintCorePlugin.saveConfig(p, config);
        });
    });
    // Some prefix/suffix might have been changed
    notifyAllListenersStateChanged();
}
Also used : WrappedProgressMonitor(org.sonarlint.eclipse.core.internal.jobs.WrappedProgressMonitor) ISonarLintFile(org.sonarlint.eclipse.core.resource.ISonarLintFile) SonarLintProjectConfiguration(org.sonarlint.eclipse.core.internal.preferences.SonarLintProjectConfiguration) EclipseProjectBinding(org.sonarlint.eclipse.core.internal.preferences.SonarLintProjectConfiguration.EclipseProjectBinding)

Aggregations

ISonarLintFile (org.sonarlint.eclipse.core.resource.ISonarLintFile)8 ArrayList (java.util.ArrayList)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 IDocument (org.eclipse.jface.text.IDocument)2 ISonarLintProject (org.sonarlint.eclipse.core.resource.ISonarLintProject)2 File (java.io.File)1 IOException (java.io.IOException)1 FileSystems (java.nio.file.FileSystems)1 Path (java.nio.file.Path)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 Collectors (java.util.stream.Collectors)1 ValidationStatus (org.eclipse.core.databinding.validation.ValidationStatus)1 IFileStore (org.eclipse.core.filesystem.IFileStore)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1