Skip to content

Commit

Permalink
SONAR-9670 Introduce sonar.branch.longLivedBranches.regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Gyerik committed Sep 12, 2017
1 parent b559ffd commit 6c1cf2b
Show file tree
Hide file tree
Showing 81 changed files with 805 additions and 458 deletions.
Expand Up @@ -31,13 +31,10 @@ public void sanityCheck() {
assertThat(mode.isIssues()).isFalse();
assertThat(mode.isPreview()).isFalse();
assertThat(mode.isPublish()).isTrue();
assertThat(mode.isIncremental()).isFalse();
mode.setPreviewOrIssue(true);
mode.setIncremental(true);
assertThat(mode.isIssues()).isTrue();
assertThat(mode.isPreview()).isTrue();
assertThat(mode.isPublish()).isFalse();
assertThat(mode.isIncremental()).isTrue();
}

}
Expand Up @@ -20,85 +20,107 @@
package org.sonar.scanner.analysis;

import java.util.Map;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.scanner.bootstrap.AbstractAnalysisMode;
import org.sonar.scanner.bootstrap.GlobalProperties;
import org.sonar.scanner.scan.BranchConfiguration;
import org.sonar.scanner.bootstrap.GlobalAnalysisMode;
import org.sonar.scanner.repository.ProjectRepositories;
import org.sonar.scanner.scan.branch.BranchConfiguration;

@Immutable
public class DefaultAnalysisMode extends AbstractAnalysisMode {

public class DefaultAnalysisMode implements AnalysisMode {
private static final Logger LOG = Loggers.get(DefaultAnalysisMode.class);
private static final String KEY_SCAN_ALL = "sonar.scanAllFiles";
private static final String KEY_INCREMENTAL = "sonar.incremental";

private final Map<String, String> analysisProps;
private final GlobalAnalysisMode analysisMode;
private final BranchConfiguration branchConfig;
private final ProjectRepositories projectRepos;
private final ValidateIncremental validateIncremental;

private boolean scanAllFiles;
private boolean incremental;

public DefaultAnalysisMode(GlobalProperties globalProps, AnalysisProperties props, BranchConfiguration branchConfig) {
init(globalProps.properties(), props.properties(), branchConfig);
public DefaultAnalysisMode(AnalysisProperties props, BranchConfiguration branchConfig, GlobalAnalysisMode analysisMode, ProjectRepositories projectRepos) {
this(props, branchConfig, analysisMode, projectRepos, null);
}

public boolean scanAllFiles() {
return scanAllFiles;
public DefaultAnalysisMode(AnalysisProperties props, BranchConfiguration branchConfig,
GlobalAnalysisMode analysisMode, ProjectRepositories projectRepos, @Nullable ValidateIncremental validateIncremental) {
this.branchConfig = branchConfig;
this.analysisMode = analysisMode;
this.projectRepos = projectRepos;
this.validateIncremental = validateIncremental;
this.analysisProps = props.properties();
load();
}

private void init(Map<String, String> globalProps, Map<String, String> analysisProps, BranchConfiguration branchConfig) {
// make sure analysis is consistent with global properties
boolean globalPreview = isIssues(globalProps);
boolean analysisPreview = isIssues(analysisProps);

if (!globalPreview && analysisPreview) {
throw new IllegalStateException("Inconsistent properties: global properties doesn't enable issues mode while analysis properties enables it");
}

load(globalProps, analysisProps, branchConfig.isShortLivingBranch());
@Override
public boolean isIncremental() {
return incremental;
}

private void load(Map<String, String> globalProps, Map<String, String> analysisProps, boolean isShortLivingBranch) {
String mode = getPropertyWithFallback(analysisProps, globalProps, CoreProperties.ANALYSIS_MODE);
validate(mode);
issues = CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode);
mediumTestMode = "true".equals(getPropertyWithFallback(analysisProps, globalProps, MEDIUM_TEST_ENABLED));
String scanAllStr = getPropertyWithFallback(analysisProps, globalProps, KEY_SCAN_ALL);
scanAllFiles = !isShortLivingBranch && (!issues || "true".equals(scanAllStr));
public boolean scanAllFiles() {
return scanAllFiles;
}

public void printMode() {
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 void printFlags() {
if (!scanAllFiles) {
LOG.info("Scanning only changed files");
}
}

@Override
public boolean isIncremental() {
return false;
private void load() {
String scanAllStr = analysisProps.get(KEY_SCAN_ALL);
scanAllFiles = !branchConfig.isShortLivingBranch() && (!analysisMode.isIssues() || "true".equals(scanAllStr));
incremental = incremental();
}

@CheckForNull
private static String getPropertyWithFallback(Map<String, String> props1, Map<String, String> props2, String key) {
if (props1.containsKey(key)) {
return props1.get(key);
private boolean incremental() {
String inc = analysisProps.get(KEY_INCREMENTAL);
if ("true".equals(inc)) {
if (validateIncremental == null || !validateIncremental.validate()) {
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");
}

if (branchConfig.branchName() != null) {
LOG.warn("Incremental analysis mode has been activated but it's not compatible with branches so a full analysis will be done.");
return false;
}

if (!projectRepos.exists() || projectRepos.lastAnalysisDate() == null) {
LOG.warn("Incremental analysis mode has been activated but the project was never analyzed before so a full analysis is about to be done.");
return false;
}

LOG.debug("Reference analysis is {}", DateUtils.formatDateTime(projectRepos.lastAnalysisDate()));
return true;
}

return props2.get(key);
return false;
}

private static boolean isIssues(Map<String, String> props) {
String mode = props.get(CoreProperties.ANALYSIS_MODE);
@Override
public boolean isPreview() {
return analysisMode.isPreview();
}

return CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode);
@Override
public boolean isIssues() {
return analysisMode.isIssues();
}

@Override
public boolean isPublish() {
return analysisMode.isPublish();
}
}
@@ -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 ValidateIncremental {
boolean validate();
}
Expand Up @@ -22,7 +22,6 @@
import com.google.common.collect.Lists;
import java.util.Collection;
import java.util.List;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.core.component.DefaultResourceTypes;
import org.sonar.core.config.CorePropertyDefinitions;
import org.sonar.core.issue.tracking.Tracker;
Expand All @@ -49,7 +48,7 @@ private BatchComponents() {
// only static stuff
}

public static Collection<Object> all(AnalysisMode analysisMode) {
public static Collection<Object> all(GlobalAnalysisMode analysisMode) {
List<Object> components = Lists.newArrayList(
DefaultResourceTypes.get(),

Expand Down
Expand Up @@ -24,7 +24,6 @@
import org.sonar.api.ExtensionProvider;
import org.sonar.api.Plugin;
import org.sonar.api.SonarRuntime;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.core.platform.ComponentContainer;
import org.sonar.core.platform.PluginInfo;
import org.sonar.core.platform.PluginRepository;
Expand All @@ -33,9 +32,9 @@ public class ExtensionInstaller {

private final SonarRuntime sonarRuntime;
private final PluginRepository pluginRepository;
private final AnalysisMode analysisMode;
private final GlobalAnalysisMode analysisMode;

public ExtensionInstaller(SonarRuntime sonarRuntime, PluginRepository pluginRepository, AnalysisMode analysisMode) {
public ExtensionInstaller(SonarRuntime sonarRuntime, PluginRepository pluginRepository, GlobalAnalysisMode analysisMode) {
this.sonarRuntime = sonarRuntime;
this.pluginRepository = pluginRepository;
this.analysisMode = analysisMode;
Expand Down
Expand Up @@ -20,33 +20,30 @@
package org.sonar.scanner.bootstrap;

import java.util.Arrays;
import javax.annotation.concurrent.Immutable;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.AnalysisMode;

public abstract class AbstractAnalysisMode implements AnalysisMode {

private static final String[] VALID_MODES = {CoreProperties.ANALYSIS_MODE_PREVIEW, CoreProperties.ANALYSIS_MODE_PUBLISH, CoreProperties.ANALYSIS_MODE_ISSUES};
@Immutable
public class GlobalAnalysisMode {
public static final String MEDIUM_TEST_ENABLED = "sonar.mediumTest.enabled";
private static final Logger LOG = LoggerFactory.getLogger(GlobalAnalysisMode.class);
private static final String[] VALID_MODES = {CoreProperties.ANALYSIS_MODE_PREVIEW, CoreProperties.ANALYSIS_MODE_PUBLISH, CoreProperties.ANALYSIS_MODE_ISSUES};

protected boolean preview;
protected boolean issues;
protected boolean mediumTestMode;

protected AbstractAnalysisMode() {
}

@Override
public boolean isPreview() {
return preview;
}

@Override
public boolean isIssues() {
return issues;
}

@Override
public boolean isPublish() {
return !preview && !issues;
}
Expand All @@ -70,4 +67,20 @@ protected static void validate(String mode) {

}

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");
}
}
}
Expand Up @@ -26,7 +26,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.config.Encryption;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.scanner.config.DefaultConfiguration;
Expand All @@ -48,7 +47,7 @@ public class GlobalConfiguration extends DefaultConfiguration {

private final Map<String, String> serverSideSettings;

public GlobalConfiguration(PropertyDefinitions propertyDefinitions, Encryption encryption, AnalysisMode mode,
public GlobalConfiguration(PropertyDefinitions propertyDefinitions, Encryption encryption, GlobalAnalysisMode mode,
Map<String, String> settings, Map<String, String> serverSideSettings) {
super(propertyDefinitions, encryption, mode, settings);
this.serverSideSettings = serverSideSettings;
Expand Down
Expand Up @@ -29,7 +29,7 @@ public class GlobalConfigurationProvider extends ProviderAdapter {

private GlobalConfiguration globalSettings;

public GlobalConfiguration provide(SettingsLoader loader, GlobalProperties globalProps, PropertyDefinitions propertyDefinitions, GlobalMode mode) {
public GlobalConfiguration provide(SettingsLoader loader, GlobalProperties globalProps, PropertyDefinitions propertyDefinitions, GlobalAnalysisMode mode) {
if (globalSettings == null) {

Map<String, String> serverSideSettings = loader.load(null);
Expand Down
Expand Up @@ -65,7 +65,7 @@ public static GlobalContainer create(Map<String, String> bootstrapProperties, Li
@Override
protected void doBeforeStart() {
GlobalProperties bootstrapProps = new GlobalProperties(bootstrapProperties);
GlobalMode globalMode = new GlobalMode(bootstrapProps);
GlobalAnalysisMode globalMode = new GlobalAnalysisMode(bootstrapProps);
add(bootstrapProps);
add(globalMode);
addBootstrapComponents();
Expand Down

This file was deleted.

0 comments on commit 6c1cf2b

Please sign in to comment.