You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScmConfiguration.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.scm;
  21. import java.util.LinkedHashMap;
  22. import java.util.Map;
  23. import java.util.stream.Collectors;
  24. import javax.annotation.CheckForNull;
  25. import org.apache.commons.lang.StringUtils;
  26. import org.sonar.api.CoreProperties;
  27. import org.sonar.api.Properties;
  28. import org.sonar.api.Property;
  29. import org.sonar.api.PropertyType;
  30. import org.sonar.api.Startable;
  31. import org.sonar.api.batch.scm.ScmProvider;
  32. import org.sonar.api.config.Configuration;
  33. import org.sonar.api.notifications.AnalysisWarnings;
  34. import org.sonar.api.utils.MessageException;
  35. import org.sonar.api.utils.log.Logger;
  36. import org.sonar.api.utils.log.Loggers;
  37. import org.sonar.core.config.ScannerProperties;
  38. import org.sonar.scanner.fs.InputModuleHierarchy;
  39. import static org.sonar.api.CoreProperties.SCM_PROVIDER_KEY;
  40. @Properties({
  41. @Property(
  42. key = ScmConfiguration.FORCE_RELOAD_KEY,
  43. defaultValue = "false",
  44. name = "Force reloading of SCM information for all files",
  45. description = "By default only files modified since previous analysis are inspected. Set this parameter to true to force the reloading.",
  46. category = CoreProperties.CATEGORY_SCM,
  47. project = false,
  48. module = false,
  49. global = false,
  50. type = PropertyType.BOOLEAN)
  51. })
  52. public class ScmConfiguration implements Startable {
  53. private static final Logger LOG = Loggers.get(ScmConfiguration.class);
  54. public static final String FORCE_RELOAD_KEY = "sonar.scm.forceReloadAll";
  55. static final String MESSAGE_SCM_STEP_IS_DISABLED_BY_CONFIGURATION = "SCM Step is disabled by configuration";
  56. static final String MESSAGE_SCM_EXCLUSIONS_IS_DISABLED_BY_CONFIGURATION = "Exclusions based on SCM info is disabled by configuration";
  57. private final Configuration settings;
  58. private final AnalysisWarnings analysisWarnings;
  59. private final Map<String, ScmProvider> providerPerKey = new LinkedHashMap<>();
  60. private final InputModuleHierarchy moduleHierarchy;
  61. private ScmProvider provider;
  62. public ScmConfiguration(InputModuleHierarchy moduleHierarchy, Configuration settings, AnalysisWarnings analysisWarnings,
  63. ScmProvider... providers) {
  64. this.moduleHierarchy = moduleHierarchy;
  65. this.settings = settings;
  66. this.analysisWarnings = analysisWarnings;
  67. for (ScmProvider scmProvider : providers) {
  68. providerPerKey.put(scmProvider.key(), scmProvider);
  69. }
  70. }
  71. @Override
  72. public void start() {
  73. if (isDisabled()) {
  74. LOG.debug(MESSAGE_SCM_STEP_IS_DISABLED_BY_CONFIGURATION);
  75. return;
  76. }
  77. if (settings.hasKey(SCM_PROVIDER_KEY)) {
  78. settings.get(SCM_PROVIDER_KEY).ifPresent(this::setProviderIfSupported);
  79. } else {
  80. autodetection();
  81. if (this.provider == null) {
  82. considerOldScmUrl();
  83. }
  84. if (this.provider == null) {
  85. String message = "SCM provider autodetection failed. Please use \"" + SCM_PROVIDER_KEY + "\" to define SCM of " +
  86. "your project, or disable the SCM Sensor in the project settings.";
  87. LOG.warn(message);
  88. analysisWarnings.addUnique(message);
  89. }
  90. }
  91. if (isExclusionDisabled()) {
  92. LOG.info(MESSAGE_SCM_EXCLUSIONS_IS_DISABLED_BY_CONFIGURATION);
  93. }
  94. }
  95. private void setProviderIfSupported(String forcedProviderKey) {
  96. if (providerPerKey.containsKey(forcedProviderKey)) {
  97. this.provider = providerPerKey.get(forcedProviderKey);
  98. } else {
  99. String supportedProviders = providerPerKey.isEmpty() ? "No SCM provider installed"
  100. : ("Supported SCM providers are " + providerPerKey.keySet().stream().collect(Collectors.joining(",")));
  101. throw new IllegalArgumentException("SCM provider was set to \"" + forcedProviderKey + "\" but no SCM provider found for this key. " + supportedProviders);
  102. }
  103. }
  104. private void considerOldScmUrl() {
  105. settings.get(ScannerProperties.LINKS_SOURCES_DEV).ifPresent(url -> {
  106. if (StringUtils.startsWith(url, "scm:")) {
  107. String[] split = url.split(":");
  108. if (split.length > 1) {
  109. setProviderIfSupported(split[1]);
  110. }
  111. }
  112. });
  113. }
  114. private void autodetection() {
  115. for (ScmProvider installedProvider : providerPerKey.values()) {
  116. if (installedProvider.supports(moduleHierarchy.root().getBaseDir().toFile())) {
  117. if (this.provider == null) {
  118. this.provider = installedProvider;
  119. } else {
  120. throw MessageException.of("SCM provider autodetection failed. Both " + this.provider.key() + " and " + installedProvider.key()
  121. + " claim to support this project. Please use \"" + SCM_PROVIDER_KEY + "\" to define SCM of your project.");
  122. }
  123. }
  124. }
  125. }
  126. @CheckForNull
  127. public ScmProvider provider() {
  128. return provider;
  129. }
  130. public boolean isDisabled() {
  131. return settings.getBoolean(CoreProperties.SCM_DISABLED_KEY).orElse(false);
  132. }
  133. public boolean isExclusionDisabled() {
  134. return isDisabled() || settings.getBoolean(CoreProperties.SCM_EXCLUSIONS_DISABLED_KEY).orElse(false);
  135. }
  136. public boolean forceReloadAll() {
  137. return settings.getBoolean(FORCE_RELOAD_KEY).orElse(false);
  138. }
  139. @Override
  140. public void stop() {
  141. // Nothing to do
  142. }
  143. }