diff options
author | Zipeng WU <zipeng.wu@sonarsource.com> | 2022-02-24 17:40:25 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-02-26 20:02:50 +0000 |
commit | 30b3456935e069ae51179ea33ea6b57f42bf1d46 (patch) | |
tree | 96ab00efcb033f8c31261a761cac356981d4b641 | |
parent | 765ac490d9bb24d575f331b4fbb2b5d2f6654319 (diff) | |
download | sonarqube-30b3456935e069ae51179ea33ea6b57f42bf1d46.tar.gz sonarqube-30b3456935e069ae51179ea33ea6b57f42bf1d46.zip |
SONAR-16064 new API in SensorContext to indicate possibility to skip unchanged files
7 files changed, 53 insertions, 12 deletions
diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java index 458860be4f8..d5d0c27f8da 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java @@ -110,6 +110,7 @@ public class SensorContextTester implements SensorContext { private DefaultInputProject project; private DefaultInputModule module; private SonarRuntime runtime; + private boolean canSkipUnchangedFiles; private boolean cancelled; private SensorContextTester(Path moduleBaseDir) { @@ -198,6 +199,16 @@ public class SensorContextTester implements SensorContext { } @Override + public boolean canSkipUnchangedFiles() { + return canSkipUnchangedFiles; + } + + public SensorContextTester setCanSkipUnchangedFiles(boolean canSkipUnchangedFiles) { + this.canSkipUnchangedFiles = canSkipUnchangedFiles; + return this; + } + + @Override public InputModule module() { return module; } diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java index 6d30694829b..eb61b3a55d4 100644 --- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java +++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java @@ -75,6 +75,13 @@ public class SensorContextTesterTest { } @Test + public void test_canSkipUnchangedFiles() { + assertThat(tester.canSkipUnchangedFiles()).isFalse(); + tester.setCanSkipUnchangedFiles(true); + assertThat(tester.canSkipUnchangedFiles()).isTrue(); + } + + @Test public void testActiveRules() { NewActiveRule activeRule = new NewActiveRule.Builder() .setRuleKey(RuleKey.of("foo", "bar")) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java index 943731c438c..1dec70d9422 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java @@ -48,9 +48,7 @@ public interface InputFile extends IndexedFile { /** * Status regarding previous analysis - * @deprecated since 7.7 preview mode was dropped */ - @Deprecated enum Status { SAME, CHANGED, ADDED } @@ -134,10 +132,6 @@ public interface InputFile extends IndexedFile { */ String contents() throws IOException; - /** - * @deprecated since 7.7 preview/issue mode was removed - */ - @Deprecated Status status(); /** diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java index f1d4b06c413..d1a43ae930d 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java @@ -57,6 +57,7 @@ public interface SensorContext { */ @Deprecated Settings settings(); + /** * Get settings of the project. * @since 6.5 @@ -64,6 +65,14 @@ public interface SensorContext { Configuration config(); /** + * In some situations, there is no need to analyze unchanged files, as reported by {@link InputFile#status()} as {@link InputFile.Status#SAME}, + * and analyzers can skip reporting any data for those files. + * Plugins should be prepared for the flag to be always false in the future. + * @since 9.4 + */ + boolean canSkipUnchangedFiles(); + + /** * Get filesystem of the project. */ FileSystem fileSystem(); @@ -182,9 +191,9 @@ public interface SensorContext { /** * Builder to declare which parts of the code is significant code. * Ranges that are not reported as significant code will be ignored and won't be considered when calculating which lines were modified. - * + * * If the significant code is not reported for a file, it is assumed that the entire file is significant code. - * + * * @since 7.2 */ NewSignificantCode newSignificantCode(); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ModuleSensorContext.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ModuleSensorContext.java index a33a00961f4..461fe146a76 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ModuleSensorContext.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ModuleSensorContext.java @@ -28,6 +28,7 @@ import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.batch.sensor.internal.SensorStorage; import org.sonar.api.config.Configuration; import org.sonar.api.config.Settings; +import org.sonar.scanner.scan.branch.BranchConfiguration; @ThreadSafe public class ModuleSensorContext extends ProjectSensorContext { @@ -35,8 +36,8 @@ public class ModuleSensorContext extends ProjectSensorContext { private final InputModule module; public ModuleSensorContext(DefaultInputProject project, InputModule module, Configuration config, Settings mutableModuleSettings, FileSystem fs, ActiveRules activeRules, - SensorStorage sensorStorage, SonarRuntime sonarRuntime) { - super(project, config, mutableModuleSettings, fs, activeRules, sensorStorage, sonarRuntime); + SensorStorage sensorStorage, SonarRuntime sonarRuntime, BranchConfiguration branchConfiguration) { + super(project, config, mutableModuleSettings, fs, activeRules, sensorStorage, sonarRuntime, branchConfiguration); this.module = module; } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ProjectSensorContext.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ProjectSensorContext.java index 653baa3bf50..fc78598b80a 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ProjectSensorContext.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ProjectSensorContext.java @@ -53,6 +53,7 @@ import org.sonar.api.config.Configuration; import org.sonar.api.config.Settings; import org.sonar.api.scanner.fs.InputProject; import org.sonar.api.utils.Version; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.sensor.noop.NoOpNewAnalysisError; @ThreadSafe @@ -67,9 +68,10 @@ public class ProjectSensorContext implements SensorContext { private final DefaultInputProject project; private final SonarRuntime sonarRuntime; private final Configuration config; + private final boolean skipUnchangedFiles; public ProjectSensorContext(DefaultInputProject project, Configuration config, Settings mutableSettings, FileSystem fs, ActiveRules activeRules, - SensorStorage sensorStorage, SonarRuntime sonarRuntime) { + SensorStorage sensorStorage, SonarRuntime sonarRuntime, BranchConfiguration branchConfiguration) { this.project = project; this.config = config; this.mutableSettings = mutableSettings; @@ -77,6 +79,7 @@ public class ProjectSensorContext implements SensorContext { this.activeRules = activeRules; this.sensorStorage = sensorStorage; this.sonarRuntime = sonarRuntime; + this.skipUnchangedFiles = branchConfiguration.isPullRequest(); } @Override @@ -184,4 +187,9 @@ public class ProjectSensorContext implements SensorContext { public NewSignificantCode newSignificantCode() { return new DefaultSignificantCode(sensorStorage); } + + @Override + public boolean canSkipUnchangedFiles() { + return this.skipUnchangedFiles; + } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java index 7caadeb8aec..5b886662ecf 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java @@ -37,6 +37,7 @@ import org.sonar.api.config.internal.MapSettings; import org.sonar.api.internal.SonarRuntimeImpl; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.utils.Version; +import org.sonar.scanner.scan.branch.BranchConfiguration; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -53,6 +54,7 @@ public class ModuleSensorContextTest { private MapSettings settings; private SensorStorage sensorStorage; private SonarRuntime runtime; + private BranchConfiguration branchConfiguration; @Before public void prepare() throws Exception { @@ -63,16 +65,18 @@ public class ModuleSensorContextTest { when(metricFinder.<String>findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION); settings = new MapSettings(); sensorStorage = mock(SensorStorage.class); + branchConfiguration = mock(BranchConfiguration.class); runtime = SonarRuntimeImpl.forSonarQube(Version.parse("5.5"), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY); - adaptor = new ModuleSensorContext(mock(DefaultInputProject.class), mock(InputModule.class), settings.asConfig(), settings, fs, activeRules, sensorStorage, runtime); } @Test public void shouldProvideComponents() { + adaptor = new ModuleSensorContext(mock(DefaultInputProject.class), mock(InputModule.class), settings.asConfig(), settings, fs, activeRules, sensorStorage, runtime, branchConfiguration); assertThat(adaptor.activeRules()).isEqualTo(activeRules); assertThat(adaptor.fileSystem()).isEqualTo(fs); assertThat(adaptor.getSonarQubeVersion()).isEqualTo(Version.parse("5.5")); assertThat(adaptor.runtime()).isEqualTo(runtime); + assertThat(adaptor.canSkipUnchangedFiles()).isFalse(); assertThat(adaptor.newIssue()).isNotNull(); assertThat(adaptor.newExternalIssue()).isNotNull(); @@ -83,4 +87,11 @@ public class ModuleSensorContextTest { assertThat(adaptor.newSignificantCode()).isNotNull(); } + @Test + public void pull_request_can_skip_unchanged_files() { + when(branchConfiguration.isPullRequest()).thenReturn(true); + adaptor = new ModuleSensorContext(mock(DefaultInputProject.class), mock(InputModule.class), settings.asConfig(), settings, fs, activeRules, sensorStorage, runtime, branchConfiguration); + assertThat(adaptor.canSkipUnchangedFiles()).isTrue(); + } + } |