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.

ScanProperties.java 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 java.nio.file.Path;
  22. import java.nio.file.Paths;
  23. import java.util.Optional;
  24. import org.sonar.api.config.Configuration;
  25. import org.sonar.api.utils.MessageException;
  26. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  27. import static org.sonar.core.config.ScannerProperties.BRANCH_NAME;
  28. import static org.sonar.core.config.ScannerProperties.ORGANIZATION;
  29. /**
  30. * Properties that can be passed to the scanners and are not exposed in SonarQube.
  31. */
  32. public class ScanProperties {
  33. public static final String METADATA_FILE_PATH_KEY = "sonar.scanner.metadataFilePath";
  34. public static final String KEEP_REPORT_PROP_KEY = "sonar.scanner.keepReport";
  35. public static final String VERBOSE_KEY = "sonar.verbose";
  36. public static final String METADATA_DUMP_FILENAME = "report-task.txt";
  37. public static final String SONAR_REPORT_EXPORT_PATH = "sonar.report.export.path";
  38. public static final String PRELOAD_FILE_METADATA_KEY = "sonar.preloadFileMetadata";
  39. public static final String FORCE_RELOAD_KEY = "sonar.scm.forceReloadAll";
  40. public static final String SCM_REVISION = "sonar.scm.revision";
  41. private final Configuration configuration;
  42. private final DefaultInputProject project;
  43. public ScanProperties(Configuration configuration, DefaultInputProject project) {
  44. this.configuration = configuration;
  45. this.project = project;
  46. }
  47. public boolean shouldKeepReport() {
  48. return configuration.getBoolean(KEEP_REPORT_PROP_KEY).orElse(false) || configuration.getBoolean(VERBOSE_KEY).orElse(false);
  49. }
  50. public boolean preloadFileMetadata() {
  51. return configuration.getBoolean(PRELOAD_FILE_METADATA_KEY).orElse(false);
  52. }
  53. public Optional<String> organizationKey() {
  54. return configuration.get(ORGANIZATION);
  55. }
  56. public Optional<String> branch() {
  57. return configuration.get(BRANCH_NAME);
  58. }
  59. public Optional<String> get(String propertyKey) {
  60. return configuration.get(propertyKey);
  61. }
  62. public Path metadataFilePath() {
  63. Optional<String> metadataFilePath = configuration.get(METADATA_FILE_PATH_KEY);
  64. if (metadataFilePath.isPresent()) {
  65. Path metadataPath = Paths.get(metadataFilePath.get());
  66. if (!metadataPath.isAbsolute()) {
  67. throw MessageException.of(String.format("Property '%s' must point to an absolute path: %s", METADATA_FILE_PATH_KEY, metadataFilePath.get()));
  68. }
  69. return project.getBaseDir().resolve(metadataPath);
  70. } else {
  71. return project.getWorkDir().resolve(METADATA_DUMP_FILENAME);
  72. }
  73. }
  74. /**
  75. * This should be called in the beginning of the analysis to fail fast
  76. */
  77. public void validate() {
  78. metadataFilePath();
  79. }
  80. }