]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9670 Introduce sonar.branch.longLivedBranches.regex
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Tue, 12 Sep 2017 09:37:55 +0000 (11:37 +0200)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Tue, 12 Sep 2017 09:41:16 +0000 (11:41 +0200)
sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/DefaultAnalysisMode.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/IncrementalScannerHandler.java [new file with mode: 0644]
sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/ValidateIncremental.java [deleted file]
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectSettingsProvider.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/analysis/DefaultAnalysisModeTest.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/tasks/TasksMediumTest.java

index 534dad293adef2c6d8fdd7049763743d479da52d..1e669c1a5a2d15550ebcd986de35fe634cc01648 100644 (file)
@@ -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 (file)
index 0000000..f657598
--- /dev/null
@@ -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 (file)
index 0db769b..0000000
+++ /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();
-}
index 4823c8ec0063a41e9bf4ba244ed9c80da2132efd..9029c3ed48677dfeced67c6a7c35129702b84b59 100644 (file)
@@ -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<String, String> 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<String, String> loadProjectSettings(SettingsLoader settingsLoader, String projectKey) {
-    return settingsLoader.load(projectKey);
-  }
 }
index 5b68fd567a4ab53db27ba3d0bf87c889ca5ac01d..d0f27a192453ad99d63da07359eb0ab6012c75c3 100644 (file)
 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);
index d7cb80f787e9c5eb88f4e80e868e2d17dc7991f7..49f1c995652b7b67de48d8d60fdc5c3799e0f54e 100644 (file)
  */
 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.<String, String>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