Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Support remote analyze (Sonarcloud/SonarQube) #61

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions .github/workflows/build-on-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,3 @@ jobs:
- name: Test with Maven
run: mvn -Ddependency-check.skip=true -DhasNodeJSRuntime clean test
timeout-minutes: 10
- name: Test Report
uses: dorny/test-reporter@v1
if: success() || failure() # run this step even if previous step failed
with:
# Name of the check run which will be created
name: JUnit Tests
# Path to test results
path: target/surefire-reports/*.xml
# Format of test results
reporter: java-junit
10 changes: 7 additions & 3 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ name: Java CI with Maven

on:
push:
branches: [ master ]
branches:
- master
- branch-*
pull_request:
branches: [ master ]
types: [opened, synchronize, reopened]

jobs:
build:
Expand All @@ -22,7 +24,9 @@ jobs:
java-version: 11
distribution: temurin
- name: Build with Maven
run: mvn clean package source:jar javadoc:jar verify spotless:check
env:
SONARLINT4NETBEANS_TEST_REMOTE_PROJECT_MAVEN1_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -D clean package source:jar javadoc:jar verify spotless:check
timeout-minutes: 10
- name: Test Report
uses: dorny/test-reporter@v1
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- master
- branch-*
pull_request:
types: [opened, synchronize, reopened]
jobs:
Expand Down
9 changes: 8 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
:toc:
:toc: left
:toclevels: 5
:sectnums: 8
:icons: font

= sonarlint4netbeans

Expand All @@ -25,9 +27,14 @@ It requests netbeans in version 13.0 or higher and Java in version 11 or higher.
- Enable or disabled test rules on test files
- Severity icons
- Partial Support on C/C++ project (show xref:README-CFamily.adoc[README-CFamily])
- SonarCloud/SonarQube support

== Release 2.x to 3.x

== SonarCloud

Branch analysis must be https://docs.sonarsource.com/sonarcloud/enriching/branch-analysis-setup/#sonarlint[correctly configured] to be fully functional, otherwise only analysis on the main branch will be performed.

=== Plugins

SonarLint no longer uses URLs to find plugins but file paths. +
Expand Down
66 changes: 38 additions & 28 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,15 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<systemProperties>
<property>
<name>java.util.logging.config.file</name>
<value>./src/test/resources/logging.properties</value>
</property>
<property>
<name>file.encoding</name>
<value>UTF-8</value>
</property>
<property>
<name>junit.jupiter.extensions.autodetection.enabled</name>
<value>true</value>
</property>
<property>
<name>hasNodeJSRuntime</name>
<value>${hasNodeJSRuntime}</value>
</property>
<property>
<name>sonarlint4netbeans.test.skip.javascript</name>
<value>${sonarlint4netbeans.test.skip.javascript}</value>
</property>
</systemProperties>
<systemPropertyVariables>
<java.util.logging.config.file>./src/test/resources/logging.properties</java.util.logging.config.file>
<file.encoding>UTF-8</file.encoding>
<junit.jupiter.extensions.autodetection.enabled>true</junit.jupiter.extensions.autodetection.enabled>
<hasNodeJSRuntime>${hasNodeJSRuntime}</hasNodeJSRuntime>
<sonarlint4netbeans.test.skip.javascript>${sonarlint4netbeans.test.skip.javascript}</sonarlint4netbeans.test.skip.javascript>
<!-- For test with real sonarcloud calls -->
<sonarlint4netbeans.test.remote.project-maven1.token>${sonarlint4netbeans.test.remote.project-maven1.token}</sonarlint4netbeans.test.remote.project-maven1.token>
</systemPropertyVariables>
<!-- https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html#surefire-extensions-and-reports-configuration-for-displayname -->
<!-- Surefire Extensions and Reports Configuration for @DisplayName -->
<statelessTestsetReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
Expand Down Expand Up @@ -270,6 +257,22 @@
<artifactId>sonarlint-core</artifactId>
<version>9.3.1.74774</version>
</dependency>
<!-- to connected engine -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>1.8.20</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.sonarsource.javascript</groupId>
<artifactId>nodejs-utils</artifactId>
Expand Down Expand Up @@ -330,6 +333,11 @@
<artifactId>org-netbeans-modules-projectapi</artifactId>
<version>${org.netbeans.version}</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-projectuiapi-base</artifactId>
<version>${org.netbeans.version}</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-projectuiapi</artifactId>
Expand Down Expand Up @@ -420,6 +428,11 @@
<artifactId>org-openide-modules</artifactId>
<version>${org.netbeans.version}</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-keyring</artifactId>
<version>${org.netbeans.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down Expand Up @@ -456,12 +469,6 @@
<version>1.21</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
Expand All @@ -483,5 +490,8 @@
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<!-- For most test result detail -->
<surefire.reportFormat>plain</surefire.reportFormat>
<sonarlint4netbeans.test.remote.project-maven1.token>
${env.SONARLINT4NETBEANS_TEST_REMOTE_PROJECT_MAVEN1_TOKEN}
</sonarlint4netbeans.test.remote.project-maven1.token>
</properties>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package com.github.philippefichet.sonarlint4netbeans;

import com.github.philippefichet.sonarlint4netbeans.remote.SourceLineHashesComputer;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -27,6 +28,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.openide.util.Exceptions;
import org.sonarsource.sonarlint.core.analysis.api.ClientInputFile;

/**
Expand All @@ -41,6 +43,7 @@ public class FSClientInputFile implements ClientInputFile {
private final Charset encoding;
private final String content;
private final List<ClientInputFileListener> clientInputFileURIEvents = new ArrayList<>();
private SourceLineHashesComputer sourceLineHashesComputer;

public FSClientInputFile(String content, Path path, String relativePath, boolean isTest, Charset encoding) {
this.content = content;
Expand Down Expand Up @@ -105,6 +108,21 @@ private void consumePathURI() {
}
}

public List<String> getLineHashes() {
if (sourceLineHashesComputer == null) {
try {
String[] split = contents().split("\n");
sourceLineHashesComputer = new SourceLineHashesComputer(split.length);
for (String line : split) {
sourceLineHashesComputer.addLine(line);
}
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
return sourceLineHashesComputer.getLineHashes();
}

@Override
public String toString() {
return "FSClientInputFile{" + "path=" + path + ", relativePath=" + relativePath + ", isTest=" + isTest + ", encoding=" + encoding + '}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@
* @author FICHET Philippe &lt;[email protected]&gt;
*/
public interface SonarLintDataManager {
/**
* Retrieve project preferences data for remote configuration (sonarcloud/sonarqube)
* @param project project to retrieve preference
* @return project preferences data for remote configuration (sonarcloud/sonarqube) or global settings preference if project is null
*/
public Preferences getRemoteConfigurationPreferences(Project project);

/**
* Retrieve project preferences data
* @param project project to retrieve preference
* @return project preferences data or global settings preference if project if null
* @return project preferences data or global settings preference if project is null
*/
public Preferences getPreferences(Project project);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.github.philippefichet.sonarlint4netbeans;

import com.github.philippefichet.sonarlint4netbeans.project.SonarLintProjectPreferenceScope;
import com.github.philippefichet.sonarlint4netbeans.remote.configuration.SonarLintRemoteProjectConfiguration;
import java.awt.Image;
import java.io.File;
import java.nio.charset.Charset;
Expand All @@ -43,6 +44,14 @@
*/
public class SonarLintDataManagerImpl implements SonarLintDataManager {
private static final String PREFERENCE_SCOPE_KEY = "sonarlint-preference-scope";
@Override
public Preferences getRemoteConfigurationPreferences(Project project) {
if (project == null) {
return getGlobalSettingsPreferences();
}
return ProjectUtils.getPreferences(project, SonarLintRemoteProjectConfiguration.class, true);
}

@Override
public Preferences getPreferences(Project project) {
if (project == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,17 @@ public final class SonarLintEngineImpl implements SonarLintEngine {
private final List<Consumer<SonarLintEngine>> configurationChanged = Collections.synchronizedList(new ArrayList<>());
private final Map<String, Path> pluginPaths = new HashMap<>();
private final Lookup lookup = Lookup.getDefault();
private final Language[] languages;

/**
* Default constructor for lookup
*/
public SonarLintEngineImpl() {
this(Language.values());
}

public SonarLintEngineImpl(Language[] languages) {
this.languages = languages;
SonarLintDataManager sonarLintDataManager = getSonarLintDataManager();
pluginPaths.put("java", sonarLintDataManager.getInstalledFile("sonar/plugins/sonar-java-plugin-" + SONAR_JAVA_PLUGIN_VERSION + ".jar").toPath());
pluginPaths.put("javascript", sonarLintDataManager.getInstalledFile("sonar/plugins/sonar-javascript-plugin-" + SONAR_JAVASCRIPT_PLUGIN_VERSION + ".jar").toPath());
Expand Down Expand Up @@ -160,7 +169,7 @@ private void createInternalEngine(StandaloneSonarLintEngineImpl oldStandaloneSon

List<Path> allPluginPaths = new ArrayList<>(allPlugins.values());
StandaloneGlobalConfiguration.Builder configBuilder = StandaloneGlobalConfiguration.builder()
.addEnabledLanguages(Language.values())
.addEnabledLanguages(languages)
.addPlugins(allPluginPaths.toArray(new Path[allPluginPaths.size()]));
Optional<String> nodeJSPathOptional = getNodeJSPath();
Optional<Version> nodeJSVersionOptional = getNodeJSVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class SonarLintOptions {
public FileObject getSonarLintDetailsStyle() throws IOException
{
if (stylesheet == null) {
// java:S2095 detected only in remote issue
OutputStream createAndOpen = getFileSystem().getRoot().createAndOpen("sonar-rule-details-style.css");
byte[] byteArray = getPreferences().getByteArray("options.stylesheet", null);
if (byteArray == null || byteArray.length == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package com.github.philippefichet.sonarlint4netbeans;

import com.github.philippefichet.sonarlint4netbeans.issue.IssueWrapperForServerIssue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
Expand Down Expand Up @@ -47,7 +48,6 @@ public class SonarLintTaskScanner extends FileTaskScanner implements PropertyCha

public SonarLintTaskScanner(String displayName, String description) {
super(displayName, description, "Miscellaneous/SonarLint");

}

public static SonarLintTaskScanner create() {
Expand All @@ -70,10 +70,15 @@ public List<? extends Task> scan(FileObject fileObject) {
return analyze.stream()
.map(issue -> {
Integer startLine = issue.getStartLine();
// TODO use rules store
Optional<StandaloneRuleDetails> ruleDetails = sonarLintEngine.getRuleDetails(issue.getRuleKey());
String prefix = issue instanceof IssueWrapperForServerIssue
? "nb-sonarcloud-"
: "nb-sonarlint-";
String groupName = issue.getSeverity().name().toLowerCase();
return Task.create(
fileObject,
"nb-sonarlint-" + issue.getSeverity().name().toLowerCase(),
prefix + groupName,
issue.getRuleKey() + " = " + ruleDetails.map(StandaloneRuleDetails::getName).orElse("unknown"),
startLine == null ? 1 : startLine
);
Expand Down
Loading
Loading