diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2022-06-22 16:17:26 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-07-23 20:02:53 +0000 |
commit | 0fb5e45d935ad212aa3fe32202c33dd8395078c5 (patch) | |
tree | 4c2afc044dc034f9c70a11b58f02a762b3ba3448 /plugins/sonar-xoo-plugin | |
parent | 1220331cf405fb916a005284120b0ed02ea67ac2 (diff) | |
download | sonarqube-0fb5e45d935ad212aa3fe32202c33dd8395078c5.tar.gz sonarqube-0fb5e45d935ad212aa3fe32202c33dd8395078c5.zip |
SONAR-17044 Optimize Compute Engine issue tracking and persisting of measures when file is marked as unchanged
Diffstat (limited to 'plugins/sonar-xoo-plugin')
4 files changed, 128 insertions, 0 deletions
diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java index 9f000f49013..ef3d0cce1e5 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java @@ -46,6 +46,8 @@ import org.sonar.xoo.rule.HasTagSensor; import org.sonar.xoo.rule.hotspot.HotspotWithSingleContextSensor; import org.sonar.xoo.rule.hotspot.HotspotWithoutContextSensor; import org.sonar.xoo.rule.hotspot.HotspotWithContextsSensor; +import org.sonar.xoo.rule.HotspotSensor; +import org.sonar.xoo.rule.MarkAsUnchangedSensor; import org.sonar.xoo.rule.MultilineIssuesSensor; import org.sonar.xoo.rule.NoSonarSensor; import org.sonar.xoo.rule.OneBlockerIssuePerFileSensor; @@ -183,6 +185,7 @@ public class XooPlugin implements Plugin { AnalysisErrorSensor.class, // Other + MarkAsUnchangedSensor.class, XooProjectBuilder.class, XooPostJob.class, XooIssueFilter.class, diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/MarkAsUnchangedSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/MarkAsUnchangedSensor.java new file mode 100644 index 00000000000..68ece9802a8 --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/MarkAsUnchangedSensor.java @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 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.xoo.rule; + +import org.sonar.api.batch.fs.FilePredicates; +import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.sensor.Sensor; +import org.sonar.api.batch.sensor.SensorContext; +import org.sonar.api.batch.sensor.SensorDescriptor; +import org.sonar.xoo.Xoo; +import org.sonar.xoo.Xoo2; + +public class MarkAsUnchangedSensor implements Sensor { + public static final String ACTIVATE_MARK_AS_UNCHANGED = "sonar.markAsUnchanged"; + + @Override + public void describe(SensorDescriptor descriptor) { + descriptor.name("Mark As Unchanged Sensor") + .onlyOnLanguages(Xoo.KEY, Xoo2.KEY) + .onlyWhenConfiguration(c -> c.getBoolean(ACTIVATE_MARK_AS_UNCHANGED).orElse(false)) + .processesFilesIndependently(); + } + + @Override + public void execute(SensorContext context) { + FileSystem fs = context.fileSystem(); + FilePredicates p = fs.predicates(); + for (InputFile f : fs.inputFiles(p.all())) { + context.markAsUnchanged(f); + } + } +} diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneIssuePerLineSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneIssuePerLineSensor.java index 74c713014a4..34d402e3672 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneIssuePerLineSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneIssuePerLineSensor.java @@ -45,6 +45,7 @@ public class OneIssuePerLineSensor implements Sensor { descriptor .name("One Issue Per Line") .onlyOnLanguages(Xoo.KEY, Xoo2.KEY) + .onlyWhenConfiguration(c -> !c.getBoolean("sonar.markAsUnchanged").orElse(false)) .createIssuesForRuleRepositories(XooRulesDefinition.XOO_REPOSITORY, XooRulesDefinition.XOO2_REPOSITORY) .processesFilesIndependently(); } diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/MarkAsUnchangedSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/MarkAsUnchangedSensorTest.java new file mode 100644 index 00000000000..2b9af3daee3 --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/MarkAsUnchangedSensorTest.java @@ -0,0 +1,74 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 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.xoo.rule; + +import java.io.IOException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.api.batch.fs.internal.TestInputFileBuilder; +import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; +import org.sonar.api.batch.sensor.internal.SensorContextTester; +import org.sonar.api.config.Configuration; +import org.sonar.api.config.internal.MapSettings; +import org.sonar.xoo.Xoo; + +import static org.assertj.core.api.Assertions.assertThat; + +public class MarkAsUnchangedSensorTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + private final MarkAsUnchangedSensor sensor = new MarkAsUnchangedSensor(); + + @Test + public void mark_as_unchanged_for_all_files() throws IOException { + SensorContextTester context = SensorContextTester.create(temp.newFolder()); + DefaultInputFile inputFile1 = createFile("file1"); + DefaultInputFile inputFile2 = createFile("file2"); + + context.fileSystem() + .add(inputFile1) + .add(inputFile2); + + sensor.execute(context); + assertThat(inputFile1.isMarkedAsUnchanged()).isTrue(); + assertThat(inputFile2.isMarkedAsUnchanged()).isTrue(); + } + + @Test + public void only_runs_if_property_is_set() { + DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor(); + sensor.describe(descriptor); + Configuration configWithProperty = new MapSettings().setProperty("sonar.markAsUnchanged", "true").asConfig(); + Configuration configWithoutProperty = new MapSettings().asConfig(); + + assertThat(descriptor.configurationPredicate().test(configWithoutProperty)).isFalse(); + assertThat(descriptor.configurationPredicate().test(configWithProperty)).isTrue(); + } + + private DefaultInputFile createFile(String name) { + return new TestInputFileBuilder("foo", "src/" + name) + .setLanguage(Xoo.KEY) + .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n") + .build(); + } + +} |