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.

ProjectInfo.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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;
  21. import java.time.Clock;
  22. import java.util.Date;
  23. import java.util.Optional;
  24. import java.util.function.Predicate;
  25. import org.apache.commons.lang.StringUtils;
  26. import org.picocontainer.Startable;
  27. import org.sonar.api.CoreProperties;
  28. import org.sonar.api.config.Configuration;
  29. import org.sonar.api.utils.DateUtils;
  30. import org.sonar.api.utils.MessageException;
  31. import static java.lang.String.format;
  32. import static org.sonar.api.CoreProperties.BUILD_STRING_PROPERTY;
  33. import static org.sonar.api.CoreProperties.PROJECT_VERSION_PROPERTY;
  34. /**
  35. * @since 6.3
  36. *
  37. * Immutable after {@link #start()}
  38. */
  39. public class ProjectInfo implements Startable {
  40. private final Clock clock;
  41. private Configuration settings;
  42. private Date analysisDate;
  43. private String projectVersion;
  44. private String buildString;
  45. public ProjectInfo(Configuration settings, Clock clock) {
  46. this.settings = settings;
  47. this.clock = clock;
  48. }
  49. public Date getAnalysisDate() {
  50. return analysisDate;
  51. }
  52. public Optional<String> getProjectVersion() {
  53. return Optional.ofNullable(projectVersion);
  54. }
  55. public Optional<String> getBuildString() {
  56. return Optional.ofNullable(buildString);
  57. }
  58. private Date loadAnalysisDate() {
  59. Optional<String> value = settings.get(CoreProperties.PROJECT_DATE_PROPERTY);
  60. if (value.isEmpty()) {
  61. return Date.from(clock.instant());
  62. }
  63. try {
  64. // sonar.projectDate may have been specified as a time
  65. return DateUtils.parseDateTime(value.get());
  66. } catch (RuntimeException e) {
  67. // this is probably just a date
  68. }
  69. try {
  70. // sonar.projectDate may have been specified as a date
  71. return DateUtils.parseDate(value.get());
  72. } catch (RuntimeException e) {
  73. throw new IllegalArgumentException("Illegal value for '" + CoreProperties.PROJECT_DATE_PROPERTY + "'", e);
  74. }
  75. }
  76. @Override
  77. public void start() {
  78. this.analysisDate = loadAnalysisDate();
  79. this.projectVersion = settings.get(PROJECT_VERSION_PROPERTY)
  80. .map(StringUtils::trimToNull)
  81. .filter(validateLengthLimit("project version"))
  82. .orElse(null);
  83. this.buildString = settings.get(BUILD_STRING_PROPERTY)
  84. .map(StringUtils::trimToNull)
  85. .filter(validateLengthLimit("buildString"))
  86. .orElse(null);
  87. }
  88. private static Predicate<String> validateLengthLimit(String label) {
  89. return value -> {
  90. if (value.length() > 100) {
  91. throw MessageException.of(format("\"%s\" is not a valid %s. " +
  92. "The maximum length is 100 characters.", value, label));
  93. }
  94. return true;
  95. };
  96. }
  97. @Override
  98. public void stop() {
  99. // nothing to do
  100. }
  101. }