-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
350 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.sonarsource.javascript</groupId> | ||
<artifactId>javascript-it-plugin-plugins</artifactId> | ||
<version>10.15.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>consumer-plugin</artifactId> | ||
<packaging>sonar-plugin</packaging> | ||
|
||
<name>JavaScript :: IT :: Plugin :: Plugins :: Consumer Plugin</name> | ||
<description>Consumer Plugin</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.sonarsource.api.plugin</groupId> | ||
<artifactId>sonar-plugin-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.sonarsource.javascript</groupId> | ||
<artifactId>api</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId> | ||
<artifactId>sonar-packaging-maven-plugin</artifactId> | ||
<extensions>true</extensions> | ||
<configuration> | ||
<pluginApiMinVersion>${pluginApiMinVersion}</pluginApiMinVersion> | ||
<pluginClass>org.sonar.samples.javascript.consumer.ConsumerPlugin</pluginClass> | ||
<requiredForLanguages>js</requiredForLanguages> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
55 changes: 55 additions & 0 deletions
55
...plugins/consumer-plugin/src/main/java/org/sonar/samples/javascript/consumer/Consumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2012-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.samples.javascript.consumer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.sonar.plugins.javascript.api.JsAnalysisConsumer; | ||
import org.sonar.plugins.javascript.api.JsFile; | ||
|
||
public class Consumer implements JsAnalysisConsumer { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(Consumer.class); | ||
|
||
private final List<JsFile> jsFiles = new ArrayList<>(); | ||
private boolean done; | ||
|
||
@Override | ||
public void accept(JsFile jsFile) { | ||
LOG.info("Accepted file: {}", jsFile.inputFile()); | ||
jsFiles.add(jsFile); | ||
} | ||
|
||
@Override | ||
public void doneAnalysis() { | ||
LOG.info("Done analysis"); | ||
done = true; | ||
} | ||
|
||
public List<JsFile> getJsFiles() { | ||
return jsFiles; | ||
} | ||
|
||
public boolean isDone() { | ||
return done; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...s/consumer-plugin/src/main/java/org/sonar/samples/javascript/consumer/ConsumerPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2012-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.samples.javascript.consumer; | ||
|
||
import org.sonar.api.Plugin; | ||
|
||
public class ConsumerPlugin implements Plugin { | ||
|
||
@Override | ||
public void define(Context context) { | ||
context.addExtensions( | ||
Consumer.class, | ||
ConsumerSensor.class | ||
); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...s/consumer-plugin/src/main/java/org/sonar/samples/javascript/consumer/ConsumerSensor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2012-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.samples.javascript.consumer; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.sonar.api.batch.DependsUpon; | ||
import org.sonar.api.batch.sensor.SensorContext; | ||
import org.sonar.api.batch.sensor.SensorDescriptor; | ||
import org.sonar.api.scanner.ScannerSide; | ||
import org.sonar.api.scanner.sensor.ProjectSensor; | ||
import org.sonar.plugins.javascript.api.JsFile; | ||
|
||
@ScannerSide | ||
// We depend on the "js-analysis" extension to make sure that the analysis is done before we consume it | ||
@DependsUpon("js-analysis") | ||
public class ConsumerSensor implements ProjectSensor { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ConsumerSensor.class); | ||
|
||
private final Consumer consumer; | ||
|
||
/** | ||
* We use Dependency Injection to get Consumer instance | ||
* | ||
* @param consumer Consumer instance | ||
*/ | ||
public ConsumerSensor(Consumer consumer) { | ||
this.consumer = consumer; | ||
} | ||
|
||
@Override | ||
public void describe(SensorDescriptor descriptor) { | ||
descriptor.name("Consumer Sensor"); | ||
} | ||
|
||
@Override | ||
public void execute(SensorContext context) { | ||
if (!consumer.isDone()) { | ||
throw new IllegalStateException("Consumer is not done"); | ||
} | ||
for (JsFile jsFile : consumer.getJsFiles()) { | ||
LOG.info("Processing file {}", jsFile.inputFile()); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ins/consumer-plugin/src/main/java/org/sonar/samples/javascript/consumer/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
@ParametersAreNonnullByDefault | ||
package org.sonar.samples.javascript.consumer; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; |
23 changes: 23 additions & 0 deletions
23
...s/eslint-custom-rules-plugin/src/main/java/org/sonar/samples/javascript/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
@ParametersAreNonnullByDefault | ||
package org.sonar.samples.javascript; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
|
||
<modules> | ||
<module>eslint-custom-rules-plugin</module> | ||
<module>consumer-plugin</module> | ||
</modules> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
s2259(); |
102 changes: 102 additions & 0 deletions
102
its/plugin/tests/src/test/java/com/sonar/javascript/it/plugin/ConsumerPluginTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2012-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package com.sonar.javascript.it.plugin; | ||
|
||
import static com.sonar.javascript.it.plugin.OrchestratorStarter.JAVASCRIPT_PLUGIN_LOCATION; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.sonar.orchestrator.Orchestrator; | ||
import com.sonar.orchestrator.build.BuildResult; | ||
import com.sonar.orchestrator.build.SonarScanner; | ||
import com.sonar.orchestrator.junit5.OrchestratorExtension; | ||
import com.sonar.orchestrator.locator.FileLocation; | ||
import java.io.File; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ConsumerPluginTest { | ||
|
||
private static final String PLUGIN_ARTIFACT_ID = "consumer-plugin"; | ||
|
||
private static OrchestratorExtension orchestrator; | ||
|
||
@BeforeAll | ||
public static void before() { | ||
orchestrator = initOrchestrator(PLUGIN_ARTIFACT_ID); | ||
} | ||
|
||
@AfterAll | ||
public static void after() { | ||
orchestrator.stop(); | ||
} | ||
|
||
static OrchestratorExtension initOrchestrator(String customRulesArtifactId) { | ||
var orchestrator = OrchestratorExtension | ||
.builderEnv() | ||
.useDefaultAdminCredentialsForBuilds(true) | ||
.setSonarVersion(System.getProperty("sonar.runtimeVersion", "LATEST_RELEASE")) | ||
.addPlugin(JAVASCRIPT_PLUGIN_LOCATION) | ||
.restoreProfileAtStartup(FileLocation.ofClasspath("/empty-js-profile.xml")) | ||
.addPlugin( | ||
FileLocation.byWildcardMavenFilename( | ||
new File("../plugins/" + customRulesArtifactId + "/target"), | ||
customRulesArtifactId + "-*.jar" | ||
) | ||
) | ||
.restoreProfileAtStartup(FileLocation.ofClasspath("/profile-javascript-custom-rules.xml")) | ||
.restoreProfileAtStartup(FileLocation.ofClasspath("/profile-typescript-custom-rules.xml")) | ||
.restoreProfileAtStartup(FileLocation.ofClasspath("/nosonar.xml")) | ||
.build(); | ||
// Installation of SQ server in orchestrator is not thread-safe, so we need to synchronize | ||
synchronized (OrchestratorStarter.class) { | ||
orchestrator.start(); | ||
} | ||
return orchestrator; | ||
} | ||
|
||
static BuildResult runBuild(Orchestrator orchestrator) { | ||
SonarScanner build = OrchestratorStarter | ||
.createScanner() | ||
.setProjectDir(TestUtils.projectDirNoCopy("custom_rules")) | ||
.setProjectKey("custom-rules") | ||
.setProjectName("Custom Rules") | ||
.setProjectVersion("1.0") | ||
.setDebugLogs(true) | ||
.setSourceDirs("src"); | ||
orchestrator.getServer().provisionProject("custom-rules", "Custom Rules"); | ||
orchestrator | ||
.getServer() | ||
.associateProjectToQualityProfile("custom-rules", "js", "javascript-custom-rules-profile"); | ||
orchestrator | ||
.getServer() | ||
.associateProjectToQualityProfile("custom-rules", "ts", "ts-custom-rules-profile"); | ||
return orchestrator.executeBuild(build); | ||
} | ||
|
||
@Test | ||
void test() { | ||
var buildResult = runBuild(orchestrator); | ||
var logMatch = ".*DEBUG: Registered JsAnalysisConsumers \\[org.sonar.samples.javascript.consumer.Consumer.*]"; | ||
assertThat(buildResult.getLogsLines(l -> l.matches(logMatch))).hasSize(1); | ||
|
||
assertThat(buildResult.getLogsLines(l -> l.matches(".*Processing file src/dir.*"))).hasSize(3); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.