aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Young <mike.young@sonarsource.com>2025-03-10 16:52:36 -0400
committersonartech <sonartech@sonarsource.com>2025-03-11 20:03:05 +0000
commit1287057dea02380dc5e6d6dc0f73a62daf825266 (patch)
tree9351cce0e60779d7ea389149cba86fc04b2629c4
parent40d3cc75670683ddb0b9932d904244febc94bc8e (diff)
downloadsonarqube-1287057dea02380dc5e6d6dc0f73a62daf825266.tar.gz
sonarqube-1287057dea02380dc5e6d6dc0f73a62daf825266.zip
SCA-112 Send SQ Server Type and Version to CLI
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/sca/CliService.java7
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sca/CliServiceTest.java93
-rw-r--r--sonar-scanner-engine/src/test/resources/org/sonar/scanner/sca/echo_args.bat2
-rwxr-xr-xsonar-scanner-engine/src/test/resources/org/sonar/scanner/sca/echo_args.sh2
4 files changed, 60 insertions, 44 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sca/CliService.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sca/CliService.java
index 39f46bb00b7..c2a474c4e52 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sca/CliService.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sca/CliService.java
@@ -30,6 +30,7 @@ import java.util.function.Consumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.platform.Server;
import org.sonar.api.utils.System2;
import org.sonar.core.util.ProcessWrapperFactory;
import org.sonar.scanner.config.DefaultConfiguration;
@@ -46,11 +47,13 @@ public class CliService {
private final ProcessWrapperFactory processWrapperFactory;
private final TelemetryCache telemetryCache;
private final System2 system2;
+ private final Server server;
- public CliService(ProcessWrapperFactory processWrapperFactory, TelemetryCache telemetryCache, System2 system2) {
+ public CliService(ProcessWrapperFactory processWrapperFactory, TelemetryCache telemetryCache, System2 system2, Server server) {
this.processWrapperFactory = processWrapperFactory;
this.telemetryCache = telemetryCache;
this.system2 = system2;
+ this.server = server;
}
public File generateManifestsZip(DefaultInputModule module, File cliExecutable, DefaultConfiguration configuration) throws IOException, IllegalStateException {
@@ -89,6 +92,8 @@ public class CliService {
// sending this will tell the CLI to skip checking for the latest available version on startup
envProperties.put("TIDELIFT_SKIP_UPDATE_CHECK", "1");
envProperties.put("TIDELIFT_ALLOW_MANIFEST_FAILURES", "1");
+ envProperties.put("TIDELIFT_CLI_INSIDE_SCANNER_ENGINE", "1");
+ envProperties.put("TIDELIFT_CLI_SQ_SERVER_VERSION", server.getVersion());
envProperties.putAll(ScaProperties.buildFromScannerProperties(configuration));
LOG.debug("Environment properties: {}", envProperties);
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sca/CliServiceTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sca/CliServiceTest.java
index 6b6dde4efcf..b8bb1a26961 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sca/CliServiceTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sca/CliServiceTest.java
@@ -34,6 +34,7 @@ import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.platform.Server;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.api.utils.System2;
import org.sonar.core.util.ProcessWrapperFactory;
@@ -48,29 +49,25 @@ import static org.slf4j.event.Level.INFO;
class CliServiceTest {
private TelemetryCache telemetryCache;
+ private DefaultInputModule rootInputModule;
+ private final Server server = mock(Server.class);
@RegisterExtension
private final LogTesterJUnit5 logTester = new LogTesterJUnit5();
+ @TempDir
+ Path rootModuleDir;
private CliService underTest;
@BeforeEach
void setup() {
telemetryCache = new TelemetryCache();
- underTest = new CliService(new ProcessWrapperFactory(), telemetryCache, System2.INSTANCE);
+ rootInputModule = new DefaultInputModule(
+ ProjectDefinition.create().setBaseDir(rootModuleDir.toFile()).setWorkDir(rootModuleDir.toFile()));
+ underTest = new CliService(new ProcessWrapperFactory(), telemetryCache, System2.INSTANCE, server);
}
@Test
- void generateZip_shouldCallProcessCorrectly_andRegisterTelemetry(@TempDir Path rootModuleDir) throws IOException, URISyntaxException {
- DefaultInputModule root = new DefaultInputModule(
- ProjectDefinition.create().setBaseDir(rootModuleDir.toFile()).setWorkDir(rootModuleDir.toFile()));
-
- // There is a custom test Bash script available in src/test/resources/org/sonar/scanner/sca that
- // will serve as our "CLI". This script will output some messages about what arguments were passed
- // to it and will try to generate a zip file in the location the process specifies. This allows us
- // to simulate a real CLI call without needing an OS specific CLI executable to run on a real project.
- URL scriptUrl = CliServiceTest.class.getResource(SystemUtils.IS_OS_WINDOWS ? "echo_args.bat" : "echo_args.sh");
- assertThat(scriptUrl).isNotNull();
- File scriptDir = new File(scriptUrl.toURI());
+ void generateZip_shouldCallProcessCorrectly_andRegisterTelemetry() throws IOException, URISyntaxException {
assertThat(rootModuleDir.resolve("test_file").toFile().createNewFile()).isTrue();
// We need to set the logging level to debug in order to be able to view the shell script's output
@@ -81,9 +78,9 @@ class CliServiceTest {
"save-lockfiles",
"--zip",
"--zip-filename",
- root.getWorkDir().resolve("dependency-files.zip").toString(),
+ rootInputModule.getWorkDir().resolve("dependency-files.zip").toString(),
"--directory",
- root.getBaseDir().toString(),
+ rootInputModule.getBaseDir().toString(),
"--debug");
String argumentOutput = "Arguments Passed In: " + String.join(" ", args);
@@ -91,7 +88,7 @@ class CliServiceTest {
when(configuration.getProperties()).thenReturn(Map.of("sonar.sca.recursiveManifestSearch", "true"));
when(configuration.get("sonar.sca.recursiveManifestSearch")).thenReturn(Optional.of("true"));
- File producedZip = underTest.generateManifestsZip(root, scriptDir, configuration);
+ File producedZip = underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
assertThat(producedZip).exists();
assertThat(logTester.logs(DEBUG))
@@ -106,17 +103,7 @@ class CliServiceTest {
}
@Test
- void generateZip_whenDebugLogLevel_shouldCallProcessCorrectly(@TempDir Path rootModuleDir) throws IOException, URISyntaxException {
- DefaultInputModule root = new DefaultInputModule(
- ProjectDefinition.create().setBaseDir(rootModuleDir.toFile()).setWorkDir(rootModuleDir.toFile()));
-
- // There is a custom test Bash script available in src/test/resources/org/sonar/scanner/sca that
- // will serve as our "CLI". This script will output some messages about what arguments were passed
- // to it and will try to generate a zip file in the location the process specifies. This allows us
- // to simulate a real CLI call without needing an OS specific CLI executable to run on a real project.
- URL scriptUrl = CliServiceTest.class.getResource(SystemUtils.IS_OS_WINDOWS ? "echo_args.bat" : "echo_args.sh");
- assertThat(scriptUrl).isNotNull();
- File scriptDir = new File(scriptUrl.toURI());
+ void generateZip_whenDebugLogLevel_shouldCallProcessCorrectly() throws IOException, URISyntaxException {
assertThat(rootModuleDir.resolve("test_file").toFile().createNewFile()).isTrue();
// We need to set the logging level to debug in order to be able to view the shell script's output
@@ -127,9 +114,9 @@ class CliServiceTest {
"save-lockfiles",
"--zip",
"--zip-filename",
- root.getWorkDir().resolve("dependency-files.zip").toString(),
+ rootInputModule.getWorkDir().resolve("dependency-files.zip").toString(),
"--directory",
- root.getBaseDir().toString(),
+ rootInputModule.getBaseDir().toString(),
"--debug");
String argumentOutput = "Arguments Passed In: " + String.join(" ", args);
@@ -137,24 +124,14 @@ class CliServiceTest {
when(configuration.getProperties()).thenReturn(Map.of("sonar.sca.recursiveManifestSearch", "true"));
when(configuration.get("sonar.sca.recursiveManifestSearch")).thenReturn(Optional.of("true"));
- underTest.generateManifestsZip(root, scriptDir, configuration);
+ underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
assertThat(logTester.logs(DEBUG))
.contains(argumentOutput);
}
@Test
- void generateZip_whenScaDebugEnabled_shouldCallProcessCorrectly(@TempDir Path rootModuleDir) throws IOException, URISyntaxException {
- DefaultInputModule root = new DefaultInputModule(
- ProjectDefinition.create().setBaseDir(rootModuleDir.toFile()).setWorkDir(rootModuleDir.toFile()));
-
- // There is a custom test Bash script available in src/test/resources/org/sonar/scanner/sca that
- // will serve as our "CLI". This script will output some messages about what arguments were passed
- // to it and will try to generate a zip file in the location the process specifies. This allows us
- // to simulate a real CLI call without needing an OS specific CLI executable to run on a real project.
- URL scriptUrl = CliServiceTest.class.getResource(SystemUtils.IS_OS_WINDOWS ? "echo_args.bat" : "echo_args.sh");
- assertThat(scriptUrl).isNotNull();
- File scriptDir = new File(scriptUrl.toURI());
+ void generateZip_whenScaDebugEnabled_shouldCallProcessCorrectly() throws IOException, URISyntaxException {
assertThat(rootModuleDir.resolve("test_file").toFile().createNewFile()).isTrue();
// Set the logging level to info so that we don't automatically set --debug flag
@@ -165,9 +142,9 @@ class CliServiceTest {
"save-lockfiles",
"--zip",
"--zip-filename",
- root.getWorkDir().resolve("dependency-files.zip").toString(),
+ rootInputModule.getWorkDir().resolve("dependency-files.zip").toString(),
"--directory",
- root.getBaseDir().toString(),
+ rootInputModule.getBaseDir().toString(),
"--debug");
String argumentOutput = "Arguments Passed In: " + String.join(" ", args);
@@ -176,9 +153,39 @@ class CliServiceTest {
when(configuration.get("sonar.sca.recursiveManifestSearch")).thenReturn(Optional.of("true"));
when(configuration.getBoolean("sonar.sca.debug")).thenReturn(Optional.of(true));
- underTest.generateManifestsZip(root, scriptDir, configuration);
+ underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
assertThat(logTester.logs(INFO))
.contains(argumentOutput);
}
+
+ @Test
+ void generateZip_shouldSendSQEnvVars() throws IOException, URISyntaxException {
+ // We need to set the logging level to debug in order to be able to view the shell script's output
+ logTester.setLevel(DEBUG);
+
+ var version = "1.0.0";
+ when(server.getVersion()).thenReturn(version);
+
+ DefaultConfiguration configuration = mock(DefaultConfiguration.class);
+ underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
+
+ assertThat(logTester.logs(DEBUG))
+ .contains("TIDELIFT_CLI_INSIDE_SCANNER_ENGINE=1")
+ .contains("TIDELIFT_CLI_SQ_SERVER_VERSION=" + version);
+ }
+
+ private URL scriptUrl() {
+ // There is a custom test Bash script available in src/test/resources/org/sonar/scanner/sca that
+ // will serve as our "CLI". This script will output some messages about what arguments were passed
+ // to it and will try to generate a zip file in the location the process specifies. This allows us
+ // to simulate a real CLI call without needing an OS specific CLI executable to run on a real project.
+ URL scriptUrl = CliServiceTest.class.getResource(SystemUtils.IS_OS_WINDOWS ? "echo_args.bat" : "echo_args.sh");
+ assertThat(scriptUrl).isNotNull();
+ return scriptUrl;
+ }
+
+ private File scriptDir() throws URISyntaxException {
+ return new File(scriptUrl().toURI());
+ }
}
diff --git a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/sca/echo_args.bat b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/sca/echo_args.bat
index e2a4fd8687b..5677cf5c437 100644
--- a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/sca/echo_args.bat
+++ b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/sca/echo_args.bat
@@ -20,5 +20,7 @@ goto loop
echo TIDELIFT_SKIP_UPDATE_CHECK=%TIDELIFT_SKIP_UPDATE_CHECK%
echo TIDELIFT_ALLOW_MANIFEST_FAILURES=%TIDELIFT_ALLOW_MANIFEST_FAILURES%
echo TIDELIFT_RECURSIVE_MANIFEST_SEARCH=%TIDELIFT_RECURSIVE_MANIFEST_SEARCH%
+echo TIDELIFT_CLI_INSIDE_SCANNER_ENGINE=%TIDELIFT_CLI_INSIDE_SCANNER_ENGINE%
+echo TIDELIFT_CLI_SQ_SERVER_VERSION=%TIDELIFT_CLI_SQ_SERVER_VERSION%
echo ZIP FILE LOCATION = %FILENAME%
echo. > %FILENAME%
diff --git a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/sca/echo_args.sh b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/sca/echo_args.sh
index 3875827056f..881be2eaac5 100755
--- a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/sca/echo_args.sh
+++ b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/sca/echo_args.sh
@@ -24,6 +24,8 @@ set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
echo "TIDELIFT_SKIP_UPDATE_CHECK=${TIDELIFT_SKIP_UPDATE_CHECK}"
echo "TIDELIFT_ALLOW_MANIFEST_FAILURES=${TIDELIFT_ALLOW_MANIFEST_FAILURES}"
echo "TIDELIFT_RECURSIVE_MANIFEST_SEARCH=${TIDELIFT_RECURSIVE_MANIFEST_SEARCH}"
+echo "TIDELIFT_CLI_INSIDE_SCANNER_ENGINE=${TIDELIFT_CLI_INSIDE_SCANNER_ENGINE}"
+echo "TIDELIFT_CLI_SQ_SERVER_VERSION=${TIDELIFT_CLI_SQ_SERVER_VERSION}"
# print filename location for debug purposes
echo "ZIP FILE LOCATION = ${FILENAME}"