Skip to content

Commit

Permalink
Display Node.js 10 deprecation warning also in SonarCloud UI (#2852)
Browse files Browse the repository at this point in the history
* Display Node.js 10 deprecation warning also in SonarCloud UI

We were showing the warning in the logs already.
  • Loading branch information
saberduck authored Oct 29, 2021
1 parent 0282d7a commit 7bd622a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.sonarsource.api.sonarlint.SonarLintSide;

@ScannerSide
@SonarLintSide
@SonarLintSide(lifespan = SonarLintSide.MULTIPLE_ANALYSES)
public class AnalysisWarningsWrapper {

private final AnalysisWarnings analysisWarnings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@

import java.util.Arrays;
import java.util.List;
import javax.annotation.Nullable;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarProduct;
import org.sonar.api.SonarRuntime;
import org.sonar.api.notifications.AnalysisWarnings;
import org.sonar.api.scanner.ScannerSide;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
Expand All @@ -39,15 +34,9 @@ public class NodeDeprecationWarning {
static final int MIN_NODE_VERSION = 10;
private static final int MIN_RECOMMENDED_NODE_VERSION = 12;
private static final List<Integer> SUPPORTED_NODE_VERSIONS = Arrays.asList(12, 14, 16);
private final SonarRuntime sonarRuntime;
private final AnalysisWarnings analysisWarnings;
private final AnalysisWarningsWrapper analysisWarnings;

public NodeDeprecationWarning(SonarRuntime sonarRuntime) {
this(sonarRuntime, null);
}

public NodeDeprecationWarning(SonarRuntime sonarRuntime, @Nullable AnalysisWarnings analysisWarnings) {
this.sonarRuntime = sonarRuntime;
public NodeDeprecationWarning(AnalysisWarningsWrapper analysisWarnings) {
this.analysisWarnings = analysisWarnings;
}

Expand All @@ -57,22 +46,12 @@ void logNodeDeprecation(int actualNodeVersion) {
"Support for this version will be dropped in future release, please upgrade Node.js to more recent version.",
actualNodeVersion);
LOG.warn(msg);
addWarning(msg);
analysisWarnings.addUnique(msg);
} else if (!SUPPORTED_NODE_VERSIONS.contains(actualNodeVersion)) {
String msg = String.format("Node.js version %d is not supported, you might experience issues. Please use " +
"a supported version of Node.js %s", actualNodeVersion, SUPPORTED_NODE_VERSIONS);
LOG.warn(msg, actualNodeVersion);
addWarning(msg);
}
}

private void addWarning(String msg) {
if (isSonarQube() && analysisWarnings != null) {
analysisWarnings.addUnique(msg);
}
}

private boolean isSonarQube() {
return sonarRuntime.getProduct() == SonarProduct.SONARQUBE && sonarRuntime.getEdition() != SonarEdition.SONARCLOUD;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public class EslintBridgeServerImplTest {
private final TestBundle testBundle = new TestBundle(START_SERVER_SCRIPT);

private final RulesBundles emptyRulesBundles = new RulesBundles();
private final SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(Version.create(8, 5), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);
private final NodeDeprecationWarning deprecationWarning = new NodeDeprecationWarning(sonarRuntime);
private final NodeDeprecationWarning deprecationWarning = new NodeDeprecationWarning(new AnalysisWarningsWrapper());

@BeforeEach
public void setUp() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,68 +42,56 @@ public class NodeDeprecationWarningTest {
@RegisterExtension
public final LogTesterJUnit5 logTester = new LogTesterJUnit5();

static class TestAnalysisWarnings extends AnalysisWarningsWrapper {
List<String> warnings = new ArrayList<>();

@Override
public void addUnique(String text) {
warnings.add(text);
}
}

TestAnalysisWarnings analysisWarnings = new TestAnalysisWarnings();
NodeDeprecationWarning deprecationWarning = new NodeDeprecationWarning(analysisWarnings);

@Test
public void test() {
SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(Version.create(8, 5), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);
List<String> warnings = new ArrayList<>();
NodeDeprecationWarning deprecationWarning = new NodeDeprecationWarning(sonarRuntime, warnings::add);
deprecationWarning.logNodeDeprecation(8);

assertThat(warnings).containsExactly(MSG);
assertThat(analysisWarnings.warnings).containsExactly(MSG);
assertThat(logTester.logs(LoggerLevel.WARN)).contains(MSG);
}

@Test
public void test_10() {
SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(Version.create(8, 5), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);
List<String> warnings = new ArrayList<>();
NodeDeprecationWarning deprecationWarning = new NodeDeprecationWarning(sonarRuntime, warnings::add);
deprecationWarning.logNodeDeprecation(10);

assertThat(warnings).containsExactly(MSG_10);
assertThat(analysisWarnings.warnings).containsExactly(MSG_10);
assertThat(logTester.logs(LoggerLevel.WARN)).contains(MSG_10);
}

@Test
public void test_good_version() {
SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(Version.create(8, 5), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);
List<String> warnings = new ArrayList<>();
NodeDeprecationWarning deprecationWarning = new NodeDeprecationWarning(sonarRuntime, warnings::add);
deprecationWarning.logNodeDeprecation(12);

assertThat(warnings).isEmpty();
assertThat(analysisWarnings.warnings).isEmpty();
assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty();
}

@Test
public void test_sonarcloud() {
SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(Version.create(8, 5), SonarQubeSide.SCANNER, SonarEdition.SONARCLOUD);
List<String> warnings = new ArrayList<>();
NodeDeprecationWarning deprecationWarning = new NodeDeprecationWarning(sonarRuntime, warnings::add);
deprecationWarning.logNodeDeprecation(8);

assertThat(warnings).isEmpty();
assertThat(logTester.logs(LoggerLevel.WARN)).contains(MSG);
}

@Test
public void test_no_warnings() {
// SonarLint doesn't provide AnalysisWarnings API
SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarLint(Version.create(8, 5));
NodeDeprecationWarning deprecationWarning = new NodeDeprecationWarning(sonarRuntime);
NodeDeprecationWarning deprecationWarning = new NodeDeprecationWarning(new AnalysisWarningsWrapper());
deprecationWarning.logNodeDeprecation(8);

assertThat(logTester.logs(LoggerLevel.WARN)).contains(MSG);
}

@Test
public void test_unsupported_version() {
SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(Version.create(8, 5), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);
List<String> warnings = new ArrayList<>();
NodeDeprecationWarning deprecationWarning = new NodeDeprecationWarning(sonarRuntime, warnings::add);
deprecationWarning.logNodeDeprecation(15);

assertThat(warnings).containsExactly(UNSUPPORTED_MSG);
assertThat(analysisWarnings.warnings).containsExactly(UNSUPPORTED_MSG);
assertThat(logTester.logs(LoggerLevel.WARN)).contains(UNSUPPORTED_MSG);
}

Expand Down

0 comments on commit 7bd622a

Please sign in to comment.