diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2021-11-23 13:06:28 -0600 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-11-29 20:03:39 +0000 |
commit | d91ea92fe22cf1b6b52a8efba122daf8e7e58282 (patch) | |
tree | cd9123d893d5f67f66cf30be326f5d61320a9d55 /sonar-plugin-api-impl | |
parent | 331d44eca2bc7e646d91bf8d7d8a18f2f93c872d (diff) | |
download | sonarqube-d91ea92fe22cf1b6b52a8efba122daf8e7e58282.tar.gz sonarqube-d91ea92fe22cf1b6b52a8efba122daf8e7e58282.zip |
SONAR-15686 Files provided are restricted when PR for selected sensors
Diffstat (limited to 'sonar-plugin-api-impl')
4 files changed, 181 insertions, 0 deletions
diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/ChangedFilePredicate.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/ChangedFilePredicate.java new file mode 100644 index 00000000000..08736a7e61d --- /dev/null +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/ChangedFilePredicate.java @@ -0,0 +1,38 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.api.batch.fs.internal.predicates; + +import org.sonar.api.batch.fs.FilePredicate; +import org.sonar.api.batch.fs.InputFile; + +public class ChangedFilePredicate implements FilePredicate { + + private final FilePredicate originalPredicate; + + public ChangedFilePredicate(FilePredicate originalPredicate) { + this.originalPredicate = originalPredicate; + } + + @Override + public boolean apply(InputFile inputFile) { + return originalPredicate.apply(inputFile) && InputFile.Status.SAME != inputFile.status(); + } + +} diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java index ee5ced6886b..e0819050190 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java @@ -21,13 +21,23 @@ package org.sonar.api.batch.sensor.internal; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; +import java.util.Set; import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.annotation.Nullable; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.sensor.SensorDescriptor; import org.sonar.api.config.Configuration; public class DefaultSensorDescriptor implements SensorDescriptor { + public static final Set<String> SENSORS_ONLY_CHANGED_IN_PR = Collections.unmodifiableSet(Stream.of( + "CSS Metrics", + "CSS Rules", + "HTML", + "XML Sensor" + ).collect(Collectors.toSet())); private String name; private String[] languages = new String[0]; @@ -35,6 +45,7 @@ public class DefaultSensorDescriptor implements SensorDescriptor { private String[] ruleRepositories = new String[0]; private boolean global = false; private Predicate<Configuration> configurationPredicate; + private boolean onlyChangedFilesInPullRequests = false; public String name() { return name; @@ -61,8 +72,16 @@ public class DefaultSensorDescriptor implements SensorDescriptor { return global; } + public boolean onlyChangedFilesInPullRequest() { + return onlyChangedFilesInPullRequests; + } + @Override public DefaultSensorDescriptor name(String name) { + // TODO: Add onlyChangedFilesInPullRequest into API and implement it at sensors + if (SENSORS_ONLY_CHANGED_IN_PR.contains(name)) { + onlyChangedFilesInPullRequests = true; + } this.name = name; return this; } diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/predicates/ChangedFilePredicateTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/predicates/ChangedFilePredicateTest.java new file mode 100644 index 00000000000..f3fcd9309a2 --- /dev/null +++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/predicates/ChangedFilePredicateTest.java @@ -0,0 +1,94 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.api.batch.fs.internal.predicates; + +import org.assertj.core.api.Assertions; +import org.junit.Test; +import org.sonar.api.batch.fs.FilePredicate; +import org.sonar.api.batch.fs.InputFile; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class ChangedFilePredicateTest { + + private final FilePredicate predicate = mock(FilePredicate.class); + private final InputFile inputFile = mock(InputFile.class); + + private final ChangedFilePredicate underTest = new ChangedFilePredicate(predicate); + + @Test + public void apply_when_file_is_changed_and_predicate_is_true() { + when(inputFile.status()).thenReturn(InputFile.Status.CHANGED); + when(predicate.apply(inputFile)).thenReturn(true); + + Assertions.assertThat(underTest.apply(inputFile)).isTrue(); + + verify(predicate, times(1)).apply(any()); + verify(inputFile, times(1)).status(); + } + + @Test + public void apply_when_file_is_added_and_predicate_is_true() { + when(inputFile.status()).thenReturn(InputFile.Status.ADDED); + when(predicate.apply(inputFile)).thenReturn(true); + + Assertions.assertThat(underTest.apply(inputFile)).isTrue(); + + verify(predicate, times(1)).apply(any()); + verify(inputFile, times(1)).status(); + } + + @Test + public void do_not_apply_when_file_is_same_and_predicate_is_true() { + when(inputFile.status()).thenReturn(InputFile.Status.SAME); + when(predicate.apply(inputFile)).thenReturn(true); + + Assertions.assertThat(underTest.apply(inputFile)).isFalse(); + + verify(predicate, times(1)).apply(any()); + verify(inputFile, times(1)).status(); + } + + @Test + public void predicate_is_evaluated_before_file_status() { + when(predicate.apply(inputFile)).thenReturn(false); + + Assertions.assertThat(underTest.apply(inputFile)).isFalse(); + + verify(inputFile, never()).status(); + } + + @Test + public void do_not_apply_when_file_is_same_and_predicate_is_false() { + when(inputFile.status()).thenReturn(InputFile.Status.SAME); + when(predicate.apply(inputFile)).thenReturn(true); + + Assertions.assertThat(underTest.apply(inputFile)).isFalse(); + + verify(predicate, times(1)).apply(any()); + verify(inputFile, times(1)).status(); + } + +} diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptorTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptorTest.java index ac6c72c24dd..e34c5385cad 100644 --- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptorTest.java +++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptorTest.java @@ -19,12 +19,17 @@ */ package org.sonar.api.batch.sensor.internal; +import com.tngtech.java.junit.dataprovider.DataProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.java.junit.dataprovider.UseDataProvider; import org.junit.Test; +import org.junit.runner.RunWith; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.config.internal.MapSettings; import static org.assertj.core.api.Assertions.assertThat; +@RunWith(DataProviderRunner.class) public class DefaultSensorDescriptorTest { @Test @@ -48,4 +53,29 @@ public class DefaultSensorDescriptorTest { assertThat(descriptor.ruleRepositories()).containsOnly("squid-java"); } + @Test + @UseDataProvider("sensorsOnlyChangedInPR") + public void describe_with_restricted_sensor(String sensorName) { + DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor(); + descriptor + .name(sensorName); + + assertThat(descriptor.onlyChangedFilesInPullRequest()).isTrue(); + } + + @Test + @UseDataProvider("sensorsOnlyChangedInPR") + public void describe_with_non_restricted_sensor(String sensorName) { + DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor(); + descriptor + .name(sensorName + "other"); + + assertThat(descriptor.onlyChangedFilesInPullRequest()).isFalse(); + } + + @DataProvider + public static Object[][] sensorsOnlyChangedInPR() { + return new Object[][] {DefaultSensorDescriptor.SENSORS_ONLY_CHANGED_IN_PR.toArray()}; + } + } |