diff options
author | Mike Young <mike.young@sonarsource.com> | 2025-03-21 14:07:48 -0400 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2025-03-21 20:03:04 +0000 |
commit | 2aef8713df05a9bf9b2e5816bcec50c920f89d5c (patch) | |
tree | e00a2b6b9fd1bfc9162ca7ecdec8066e7d6c7444 /sonar-scanner-engine | |
parent | f7af7b9a9d3226c8ca617bfeda522de96cea4906 (diff) | |
download | sonarqube-2aef8713df05a9bf9b2e5816bcec50c920f89d5c.tar.gz sonarqube-2aef8713df05a9bf9b2e5816bcec50c920f89d5c.zip |
SONAR-24679 Remove Cycle Dependency on CliService
Diffstat (limited to 'sonar-scanner-engine')
3 files changed, 18 insertions, 25 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 589f8a319c8..8a8e90cce25 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 @@ -29,6 +29,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.function.Consumer; import javax.annotation.Nullable; import org.apache.commons.csv.CSVFormat; @@ -103,7 +104,9 @@ public class CliService { 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)); + // EXCLUDED_MANIFESTS_PROP_KEY is a special case which we handle via --args, not environment variables + Set<String> ignoredProperties = Set.of(EXCLUDED_MANIFESTS_PROP_KEY); + envProperties.putAll(ScaProperties.buildFromScannerProperties(configuration, ignoredProperties)); LOG.info("Running command: {}", args); LOG.info("Environment properties: {}", envProperties); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sca/ScaProperties.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sca/ScaProperties.java index 7aed17f8b09..5c848b4ddbc 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sca/ScaProperties.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sca/ScaProperties.java @@ -22,6 +22,7 @@ package org.sonar.scanner.sca; import com.fasterxml.jackson.databind.PropertyNamingStrategies; import java.util.HashMap; import java.util.Map; +import java.util.Set; import java.util.regex.Pattern; import java.util.stream.Collectors; import org.sonar.scanner.config.DefaultConfiguration; @@ -45,9 +46,10 @@ public class ScaProperties { * { "sonar.someOtherProperty" : "value" } returns an empty map * * @param configuration the scanner configuration possibly containing sonar.sca.* properties + * @param ignoredPropertyNames property names that should not be processed as a property * @return a map of Tidelift CLI compatible environment variable names to their configuration values */ - public static Map<String, String> buildFromScannerProperties(DefaultConfiguration configuration) { + public static Map<String, String> buildFromScannerProperties(DefaultConfiguration configuration, Set<String> ignoredPropertyNames) { HashMap<String, String> props = new HashMap<>(configuration.getProperties()); // recursive mode defaults to true @@ -59,8 +61,7 @@ public class ScaProperties { .entrySet() .stream() .filter(entry -> entry.getKey().startsWith(SONAR_SCA_PREFIX)) - // EXCLUDED_MANIFESTS_PROP_KEY is a special case which we handle via --args, not environment variables - .filter(entry -> !entry.getKey().equals(CliService.EXCLUDED_MANIFESTS_PROP_KEY)) + .filter(entry -> !ignoredPropertyNames.contains(entry.getKey())) .collect(Collectors.toMap(entry -> convertPropToEnvVariable(entry.getKey()), Map.Entry::getValue)); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sca/ScaPropertiesTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sca/ScaPropertiesTest.java index 34c25ac31e7..e598a225b9c 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sca/ScaPropertiesTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sca/ScaPropertiesTest.java @@ -19,9 +19,11 @@ */ package org.sonar.scanner.sca; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.Set; import org.junit.jupiter.api.Test; import org.sonar.scanner.config.DefaultConfiguration; @@ -37,22 +39,23 @@ class ScaPropertiesTest { void buildFromScannerProperties_withNoProperties_returnsDefaultMap() { when(configuration.get(anyString())).thenReturn(Optional.empty()); - var result = ScaProperties.buildFromScannerProperties(configuration); + var result = ScaProperties.buildFromScannerProperties(configuration, Collections.emptySet()); assertThat(result).containsExactly( Map.entry("TIDELIFT_RECURSIVE_MANIFEST_SEARCH", "true")); } @Test - void buildFromScannerProperties_withUnmappedProperties_ignoresUnmappedProperties() { + void buildFromScannerProperties_withUnmappedProperties_ignoresProperties() { var inputProperties = new HashMap<String, String>(); inputProperties.put("sonar.sca.pythonBinary", "/usr/bin/python3"); inputProperties.put("sonar.sca.unknownProperty", "value"); - inputProperties.put("sonar.somethingElse", "ignoreMe"); + inputProperties.put("sonar.somethingElse", "dont-include-non-sca"); + inputProperties.put("sonar.sca.ignoredProperty", "ignore-me"); when(configuration.getProperties()).thenReturn(inputProperties); when(configuration.get(anyString())).thenAnswer(i -> Optional.ofNullable(inputProperties.get(i.getArgument(0, String.class)))); - var result = ScaProperties.buildFromScannerProperties(configuration); + var result = ScaProperties.buildFromScannerProperties(configuration, Set.of("sonar.sca.ignoredProperty")); assertThat(result).containsExactly( Map.entry("TIDELIFT_RECURSIVE_MANIFEST_SEARCH", "true"), @@ -96,25 +99,11 @@ class ScaPropertiesTest { expectedProperties.put("TIDELIFT_PYTHON_RESOLVE_LOCAL", "false"); expectedProperties.put("TIDELIFT_RECURSIVE_MANIFEST_SEARCH", "true"); - var result = ScaProperties.buildFromScannerProperties(configuration); + var result = ScaProperties.buildFromScannerProperties(configuration, Collections.emptySet()); assertThat(result).containsExactlyInAnyOrderEntriesOf(expectedProperties); } - @Test - void buildFromScannerProperties_withExcludedManifestProp_ignoresExcludedManifests() { - var inputProperties = new HashMap<String, String>(); - inputProperties.put("sonar.sca.unknownProperty", "value"); - inputProperties.put("sonar.sca.excludedManifests", "ignore-me"); - when(configuration.getProperties()).thenReturn(inputProperties); - when(configuration.get(anyString())).thenAnswer(i -> Optional.ofNullable(inputProperties.get(i.getArgument(0, String.class)))); - - var result = ScaProperties.buildFromScannerProperties(configuration); - - assertThat(result).containsExactly( - Map.entry("TIDELIFT_RECURSIVE_MANIFEST_SEARCH", "true"), - Map.entry("TIDELIFT_UNKNOWN_PROPERTY", "value")); - } @Test void buildFromScannerProperties_withoutRecursiveModeProp_defaultsRecursiveModeTrue() { @@ -122,7 +111,7 @@ class ScaPropertiesTest { when(configuration.getProperties()).thenReturn(inputProperties); when(configuration.get(anyString())).thenAnswer(i -> Optional.ofNullable(inputProperties.get(i.getArgument(0, String.class)))); - var result = ScaProperties.buildFromScannerProperties(configuration); + var result = ScaProperties.buildFromScannerProperties(configuration, Collections.emptySet()); assertThat(result).containsExactly( Map.entry("TIDELIFT_RECURSIVE_MANIFEST_SEARCH", "true")); @@ -135,7 +124,7 @@ class ScaPropertiesTest { when(configuration.getProperties()).thenReturn(inputProperties); when(configuration.get(anyString())).thenAnswer(i -> Optional.ofNullable(inputProperties.get(i.getArgument(0, String.class)))); - var result = ScaProperties.buildFromScannerProperties(configuration); + var result = ScaProperties.buildFromScannerProperties(configuration, Collections.emptySet()); assertThat(result).containsExactly( Map.entry("TIDELIFT_RECURSIVE_MANIFEST_SEARCH", "false")); |