From 743143fed8dc9cd6f23ebfa2b3bd1ecd3fc8899e Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 12 Sep 2017 13:30:16 +0200 Subject: [PATCH] SONAR-9670 Introduce sonar.branch.longLivedBranches.regex --- .../scanner/analysis/DefaultAnalysisMode.java | 16 +++++---- .../scanner/bootstrap/GlobalAnalysisMode.java | 34 +++++++++---------- .../analysis/DefaultAnalysisModeTest.java | 17 ++++++---- 3 files changed, 37 insertions(+), 30 deletions(-) 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 1e669c1a5a2..77fd14c6351 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 IncrementalScannerHandler validateIncremental; + private final IncrementalScannerHandler incrementalScannerHandler; private boolean scanAllFiles; private boolean incremental; @@ -51,13 +51,14 @@ public class DefaultAnalysisMode implements AnalysisMode { } public DefaultAnalysisMode(AnalysisProperties props, BranchConfiguration branchConfig, - GlobalAnalysisMode analysisMode, ProjectRepositories projectRepos, @Nullable IncrementalScannerHandler validateIncremental) { + GlobalAnalysisMode analysisMode, ProjectRepositories projectRepos, @Nullable IncrementalScannerHandler incrementalScannerHandler) { this.branchConfig = branchConfig; this.analysisMode = analysisMode; this.projectRepos = projectRepos; - this.validateIncremental = validateIncremental; + this.incrementalScannerHandler = incrementalScannerHandler; this.analysisProps = props.properties(); load(); + printFlags(); } @Override @@ -69,7 +70,10 @@ public class DefaultAnalysisMode implements AnalysisMode { return scanAllFiles; } - public void printFlags() { + private void printFlags() { + if (incremental) { + LOG.info("Incremental mode"); + } if (!scanAllFiles) { LOG.info("Scanning only changed files"); } @@ -77,14 +81,14 @@ public class DefaultAnalysisMode implements AnalysisMode { private void load() { String scanAllStr = analysisProps.get(KEY_SCAN_ALL); - scanAllFiles = !branchConfig.isShortLivingBranch() && (!analysisMode.isIssues() || "true".equals(scanAllStr)); incremental = incremental(); + scanAllFiles = !incremental && !branchConfig.isShortLivingBranch() && (!analysisMode.isIssues() || "true".equals(scanAllStr)); } private boolean incremental() { String inc = analysisProps.get(KEY_INCREMENTAL); if ("true".equals(inc)) { - if (validateIncremental == null || !validateIncremental.execute()) { + if (incrementalScannerHandler == null || !incrementalScannerHandler.execute()) { throw MessageException.of("Incremental mode is not available. Please contact your administrator."); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalAnalysisMode.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalAnalysisMode.java index a6dddc7f92e..8bc053566e5 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalAnalysisMode.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalAnalysisMode.java @@ -36,6 +36,23 @@ public class GlobalAnalysisMode { protected boolean issues; protected boolean mediumTestMode; + public GlobalAnalysisMode(GlobalProperties props) { + String mode = props.property(CoreProperties.ANALYSIS_MODE); + validate(mode); + issues = CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode); + mediumTestMode = "true".equals(props.property(MEDIUM_TEST_ENABLED)); + if (preview) { + LOG.info("Preview mode"); + } else if (issues) { + LOG.info("Issues mode"); + } else { + LOG.info("Publish mode"); + } + if (mediumTestMode) { + LOG.info("Medium test mode"); + } + } + public boolean isPreview() { return preview; } @@ -66,21 +83,4 @@ public class GlobalAnalysisMode { } } - - public GlobalAnalysisMode(GlobalProperties props) { - String mode = props.property(CoreProperties.ANALYSIS_MODE); - validate(mode); - issues = CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode); - mediumTestMode = "true".equals(props.property(MEDIUM_TEST_ENABLED)); - if (preview) { - LOG.debug("Preview mode"); - } else if (issues) { - LOG.debug("Issues mode"); - } else { - LOG.debug("Publish mode"); - } - if (mediumTestMode) { - LOG.info("Medium test mode"); - } - } } 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 d0f27a19245..44ce642e99b 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 @@ -38,14 +38,14 @@ public class DefaultAnalysisModeTest { private BranchConfiguration branchConfig; private ProjectRepositories projectRepos; private GlobalAnalysisMode globalMode; - private IncrementalScannerHandler validateIncremental; + private IncrementalScannerHandler incrementalScannerHandler; @Before public void setUp() { branchConfig = mock(BranchConfiguration.class); projectRepos = mock(ProjectRepositories.class); globalMode = mock(GlobalAnalysisMode.class); - validateIncremental = mock(IncrementalScannerHandler.class); + incrementalScannerHandler = mock(IncrementalScannerHandler.class); } @Rule @@ -82,7 +82,7 @@ public class DefaultAnalysisModeTest { @Test public void no_incremental_if_not_publish() { - when(validateIncremental.execute()).thenReturn(true); + when(incrementalScannerHandler.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"); @@ -92,31 +92,34 @@ public class DefaultAnalysisModeTest { @Test public void no_incremental_mode_if_branches() { when(globalMode.isPublish()).thenReturn(true); - when(validateIncremental.execute()).thenReturn(true); + when(incrementalScannerHandler.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(); + assertThat(analysisMode.scanAllFiles()).isTrue(); } @Test public void no_incremental_mode_if_no_previous_analysis() { - when(validateIncremental.execute()).thenReturn(true); + when(incrementalScannerHandler.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(); + assertThat(analysisMode.scanAllFiles()).isTrue(); } @Test public void incremental_mode() { - when(validateIncremental.execute()).thenReturn(true); + when(incrementalScannerHandler.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(); + assertThat(analysisMode.scanAllFiles()).isFalse(); } @Test @@ -144,7 +147,7 @@ public class DefaultAnalysisModeTest { } private DefaultAnalysisMode createmode(AnalysisProperties analysisProps) { - return new DefaultAnalysisMode(analysisProps, branchConfig, globalMode, projectRepos, validateIncremental); + return new DefaultAnalysisMode(analysisProps, branchConfig, globalMode, projectRepos, incrementalScannerHandler); } } -- 2.39.5