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.

DeprecatedPropertiesWarningGenerator.java 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.scan;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import java.util.Optional;
  23. import org.apache.commons.lang3.StringUtils;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import org.sonar.api.CoreProperties;
  27. import org.sonar.api.config.Configuration;
  28. import org.sonar.api.notifications.AnalysisWarnings;
  29. import org.sonar.batch.bootstrapper.EnvironmentInformation;
  30. import org.sonar.scanner.http.ScannerWsClientProvider;
  31. public class DeprecatedPropertiesWarningGenerator {
  32. private static final Logger LOG = LoggerFactory.getLogger(DeprecatedPropertiesWarningGenerator.class);
  33. @VisibleForTesting
  34. static final String PASSWORD_WARN_MESSAGE = String.format("The properties '%s' and '%s' are deprecated and will be removed in the " +
  35. "future. Please pass a token with the '%s' property instead.", CoreProperties.LOGIN, CoreProperties.PASSWORD,
  36. ScannerWsClientProvider.TOKEN_PROPERTY);
  37. @VisibleForTesting
  38. static final String LOGIN_WARN_MESSAGE = String.format("The property '%s' is deprecated and will be removed in the future. " +
  39. "Please use the '%s' property instead when passing a token.", CoreProperties.LOGIN, ScannerWsClientProvider.TOKEN_PROPERTY);
  40. @VisibleForTesting
  41. static final String SCANNER_DOTNET_WARN_MESSAGE = String.format(" The '%s' property is available from SonarScanner " +
  42. "for .NET version 5.13.", ScannerWsClientProvider.TOKEN_PROPERTY);
  43. private static final String ENV_KEY_SCANNER_DOTNET = "ScannerMSBuild";
  44. private final Configuration configuration;
  45. private final AnalysisWarnings analysisWarnings;
  46. private final EnvironmentInformation environmentInformation;
  47. public DeprecatedPropertiesWarningGenerator(Configuration configuration, AnalysisWarnings analysisWarnings,
  48. EnvironmentInformation environmentInformation) {
  49. this.configuration = configuration;
  50. this.analysisWarnings = analysisWarnings;
  51. this.environmentInformation = environmentInformation;
  52. }
  53. public void execute() {
  54. Optional<String> login = configuration.get(CoreProperties.LOGIN);
  55. Optional<String> password = configuration.get(CoreProperties.PASSWORD);
  56. String warningMessage = null;
  57. if (password.isPresent()) {
  58. warningMessage = PASSWORD_WARN_MESSAGE;
  59. } else if (login.isPresent()) {
  60. warningMessage = LOGIN_WARN_MESSAGE;
  61. }
  62. if (warningMessage != null) {
  63. if (isScannerDotNet()) {
  64. warningMessage += SCANNER_DOTNET_WARN_MESSAGE;
  65. }
  66. LOG.warn(warningMessage);
  67. analysisWarnings.addUnique(warningMessage);
  68. }
  69. }
  70. private boolean isScannerDotNet() {
  71. return StringUtils.containsIgnoreCase(environmentInformation.getKey(), ENV_KEY_SCANNER_DOTNET);
  72. }
  73. }