From: Duarte Meneses Date: Tue, 12 Sep 2017 09:37:55 +0000 (+0200) Subject: SONAR-9670 Introduce sonar.branch.longLivedBranches.regex X-Git-Tag: 6.6-RC1~380^2~4 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f25d5fd106a0572344a250ec14930d2250e19efb;p=sonarqube.git SONAR-9670 Introduce sonar.branch.longLivedBranches.regex --- diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/DefaultAnalysisMode.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/DefaultAnalysisMode.java index 534dad293ad..1e669c1a5a2 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/DefaultAnalysisMode.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/DefaultAnalysisMode.java @@ -41,7 +41,7 @@ public class DefaultAnalysisMode implements AnalysisMode { private final GlobalAnalysisMode analysisMode; private final BranchConfiguration branchConfig; private final ProjectRepositories projectRepos; - private final ValidateIncremental validateIncremental; + private final IncrementalScannerHandler validateIncremental; private boolean scanAllFiles; private boolean incremental; @@ -51,7 +51,7 @@ public class DefaultAnalysisMode implements AnalysisMode { } public DefaultAnalysisMode(AnalysisProperties props, BranchConfiguration branchConfig, - GlobalAnalysisMode analysisMode, ProjectRepositories projectRepos, @Nullable ValidateIncremental validateIncremental) { + GlobalAnalysisMode analysisMode, ProjectRepositories projectRepos, @Nullable IncrementalScannerHandler validateIncremental) { this.branchConfig = branchConfig; this.analysisMode = analysisMode; this.projectRepos = projectRepos; @@ -84,12 +84,12 @@ public class DefaultAnalysisMode implements AnalysisMode { private boolean incremental() { String inc = analysisProps.get(KEY_INCREMENTAL); if ("true".equals(inc)) { - if (validateIncremental == null || !validateIncremental.validate()) { + if (validateIncremental == null || !validateIncremental.execute()) { throw MessageException.of("Incremental mode is not available. Please contact your administrator."); } if (!analysisMode.isPublish()) { - throw new IllegalStateException("Incremental analysis is only available in publish mode"); + throw MessageException.of("Incremental analysis is only available in publish mode"); } if (branchConfig.branchName() != null) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/IncrementalScannerHandler.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/IncrementalScannerHandler.java new file mode 100644 index 00000000000..f657598b26a --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/IncrementalScannerHandler.java @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.scanner.analysis; + +public interface IncrementalScannerHandler { + boolean execute(); +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/ValidateIncremental.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/ValidateIncremental.java deleted file mode 100644 index 0db769b34b3..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/ValidateIncremental.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.scanner.analysis; - -public interface ValidateIncremental { - boolean validate(); -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectSettingsProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectSettingsProvider.java index 4823c8ec006..9029c3ed486 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectSettingsProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectSettingsProvider.java @@ -23,8 +23,8 @@ import java.util.LinkedHashMap; import java.util.Map; import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.scanner.bootstrap.GlobalConfiguration; import org.sonar.scanner.bootstrap.GlobalAnalysisMode; +import org.sonar.scanner.bootstrap.GlobalConfiguration; import org.sonar.scanner.repository.settings.SettingsLoader; public class ProjectSettingsProvider extends ProviderAdapter { @@ -36,15 +36,11 @@ public class ProjectSettingsProvider extends ProviderAdapter { Map settings = new LinkedHashMap<>(); settings.putAll(globalSettings.getProperties()); - settings.putAll(loadProjectSettings(settingsLoader, reactor.getRoot().getKeyWithBranch())); + settings.putAll(settingsLoader.load(reactor.getRoot().getKeyWithBranch())); settings.putAll(reactor.getRoot().properties()); projectSettings = new ProjectSettings(globalSettings.getDefinitions(), globalSettings.getEncryption(), mode, settings); } return projectSettings; } - - private Map loadProjectSettings(SettingsLoader settingsLoader, String projectKey) { - return settingsLoader.load(projectKey); - } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/analysis/DefaultAnalysisModeTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/analysis/DefaultAnalysisModeTest.java index 5b68fd567a4..d0f27a19245 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/analysis/DefaultAnalysisModeTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/analysis/DefaultAnalysisModeTest.java @@ -20,10 +20,12 @@ package org.sonar.scanner.analysis; import java.util.Collections; +import java.util.Date; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.sonar.api.utils.MessageException; import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.repository.ProjectRepositories; import org.sonar.scanner.scan.branch.BranchConfiguration; @@ -36,14 +38,14 @@ public class DefaultAnalysisModeTest { private BranchConfiguration branchConfig; private ProjectRepositories projectRepos; private GlobalAnalysisMode globalMode; - private ValidateIncremental validateIncremental; + private IncrementalScannerHandler validateIncremental; @Before public void setUp() { branchConfig = mock(BranchConfiguration.class); projectRepos = mock(ProjectRepositories.class); globalMode = mock(GlobalAnalysisMode.class); - validateIncremental = mock(ValidateIncremental.class); + validateIncremental = mock(IncrementalScannerHandler.class); } @Rule @@ -70,6 +72,53 @@ public class DefaultAnalysisModeTest { assertThat(mode.isPreview()).isTrue(); } + @Test + public void incremental_not_found() { + AnalysisProperties analysisProps = new AnalysisProperties(Collections.singletonMap("sonar.incremental", "true")); + thrown.expect(MessageException.class); + thrown.expectMessage("Incremental mode is not available. Please contact your administrator."); + createmode(analysisProps); + } + + @Test + public void no_incremental_if_not_publish() { + when(validateIncremental.execute()).thenReturn(true); + AnalysisProperties analysisProps = new AnalysisProperties(Collections.singletonMap("sonar.incremental", "true")); + thrown.expect(MessageException.class); + thrown.expectMessage("Incremental analysis is only available in publish mode"); + createmode(analysisProps); + } + + @Test + public void no_incremental_mode_if_branches() { + when(globalMode.isPublish()).thenReturn(true); + when(validateIncremental.execute()).thenReturn(true); + when(branchConfig.branchName()).thenReturn("branch1"); + AnalysisProperties analysisProps = new AnalysisProperties(Collections.singletonMap("sonar.incremental", "true")); + DefaultAnalysisMode analysisMode = createmode(analysisProps); + assertThat(analysisMode.isIncremental()).isFalse(); + } + + @Test + public void no_incremental_mode_if_no_previous_analysis() { + when(validateIncremental.execute()).thenReturn(true); + when(globalMode.isPublish()).thenReturn(true); + AnalysisProperties analysisProps = new AnalysisProperties(Collections.singletonMap("sonar.incremental", "true")); + DefaultAnalysisMode analysisMode = createmode(analysisProps); + assertThat(analysisMode.isIncremental()).isFalse(); + } + + @Test + public void incremental_mode() { + when(validateIncremental.execute()).thenReturn(true); + when(globalMode.isPublish()).thenReturn(true); + when(projectRepos.lastAnalysisDate()).thenReturn(new Date()); + when(projectRepos.exists()).thenReturn(true); + AnalysisProperties analysisProps = new AnalysisProperties(Collections.singletonMap("sonar.incremental", "true")); + DefaultAnalysisMode analysisMode = createmode(analysisProps); + assertThat(analysisMode.isIncremental()).isTrue(); + } + @Test public void scan_all_if_publish() { when(globalMode.isIssues()).thenReturn(false); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/tasks/TasksMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/tasks/TasksMediumTest.java index d7cb80f787e..49f1c995652 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/tasks/TasksMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/tasks/TasksMediumTest.java @@ -19,17 +19,13 @@ */ package org.sonar.scanner.mediumtest.tasks; -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.File; +import com.google.common.collect.ImmutableMap; import java.util.Arrays; import java.util.List; - import org.assertj.core.api.Condition; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; import org.sonar.api.SonarPlugin; import org.sonar.api.task.Task; import org.sonar.api.task.TaskDefinition; @@ -37,7 +33,7 @@ import org.sonar.api.utils.MessageException; import org.sonar.api.utils.log.LogTester; import org.sonar.scanner.mediumtest.ScannerMediumTester; -import com.google.common.collect.ImmutableMap; +import static org.assertj.core.api.Assertions.assertThat; public class TasksMediumTest { @@ -47,9 +43,6 @@ public class TasksMediumTest { @Rule public LogTester logTester = new LogTester(); - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - @Rule public ScannerMediumTester tester = new ScannerMediumTester() .registerPlugin("faketask", new FakeTaskPlugin()); @@ -90,22 +83,6 @@ public class TasksMediumTest { .execute(); } - @Test - public void incrementalNotFound() throws Exception { - File baseDir = temp.newFolder(); - thrown.expect(MessageException.class); - thrown.expectMessage( - "Incremental mode is not available. Please contact your administrator."); - - tester.newTask() - .properties(ImmutableMap.builder() - .put("sonar.projectKey", "key") - .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) - .put("sonar.sources", ".") - .put("sonar.incremental", "true").build()) - .execute(); - } - private static class FakeTaskPlugin extends SonarPlugin { @Override