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.

AnalysisMetadataHolderRule.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.ce.task.projectanalysis.analysis;
  21. import java.util.Date;
  22. import java.util.Map;
  23. import java.util.Optional;
  24. import javax.annotation.CheckForNull;
  25. import javax.annotation.Nullable;
  26. import org.junit.rules.ExternalResource;
  27. import org.sonar.ce.task.util.InitializedProperty;
  28. import org.sonar.db.component.BranchType;
  29. import org.sonar.server.project.Project;
  30. import org.sonar.server.qualityprofile.QualityProfile;
  31. import static com.google.common.base.Preconditions.checkNotNull;
  32. import static com.google.common.base.Preconditions.checkState;
  33. import static org.apache.commons.lang.StringUtils.defaultIfBlank;
  34. public class AnalysisMetadataHolderRule extends ExternalResource implements MutableAnalysisMetadataHolder {
  35. private final InitializedProperty<String> uuid = new InitializedProperty<>();
  36. private final InitializedProperty<Long> analysisDate = new InitializedProperty<>();
  37. private final InitializedProperty<Analysis> baseAnalysis = new InitializedProperty<>();
  38. private final InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();
  39. private final InitializedProperty<Branch> branch = new InitializedProperty<>();
  40. private final InitializedProperty<String> pullRequestId = new InitializedProperty<>();
  41. private final InitializedProperty<Project> project = new InitializedProperty<>();
  42. private final InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
  43. private final InitializedProperty<Map<String, QualityProfile>> qProfilesPerLanguage = new InitializedProperty<>();
  44. private final InitializedProperty<Map<String, ScannerPlugin>> pluginsByKey = new InitializedProperty<>();
  45. private final InitializedProperty<String> scmRevision = new InitializedProperty<>();
  46. @Override
  47. public AnalysisMetadataHolderRule setUuid(String s) {
  48. checkNotNull(s, "UUID must not be null");
  49. this.uuid.setProperty(s);
  50. return this;
  51. }
  52. @Override
  53. public String getUuid() {
  54. checkState(uuid.isInitialized(), "Analysis UUID has not been set");
  55. return this.uuid.getProperty();
  56. }
  57. public AnalysisMetadataHolderRule setAnalysisDate(Date date) {
  58. checkNotNull(date, "Date must not be null");
  59. this.analysisDate.setProperty(date.getTime());
  60. return this;
  61. }
  62. @Override
  63. public AnalysisMetadataHolderRule setAnalysisDate(long date) {
  64. this.analysisDate.setProperty(date);
  65. return this;
  66. }
  67. @Override
  68. public long getAnalysisDate() {
  69. checkState(analysisDate.isInitialized(), "Analysis date has not been set");
  70. return this.analysisDate.getProperty();
  71. }
  72. @Override
  73. public boolean hasAnalysisDateBeenSet() {
  74. return analysisDate.isInitialized();
  75. }
  76. @Override
  77. public boolean isFirstAnalysis() {
  78. return getBaseAnalysis() == null;
  79. }
  80. @Override
  81. public AnalysisMetadataHolderRule setBaseAnalysis(@Nullable Analysis baseAnalysis) {
  82. this.baseAnalysis.setProperty(baseAnalysis);
  83. return this;
  84. }
  85. @Override
  86. @CheckForNull
  87. public Analysis getBaseAnalysis() {
  88. checkState(baseAnalysis.isInitialized(), "Base analysis has not been set");
  89. return baseAnalysis.getProperty();
  90. }
  91. @Override
  92. public AnalysisMetadataHolderRule setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
  93. this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
  94. return this;
  95. }
  96. @Override
  97. public boolean isCrossProjectDuplicationEnabled() {
  98. checkState(crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has not been set");
  99. return crossProjectDuplicationEnabled.getProperty();
  100. }
  101. @Override
  102. public AnalysisMetadataHolderRule setBranch(Branch branch) {
  103. this.branch.setProperty(branch);
  104. return this;
  105. }
  106. @Override
  107. public Branch getBranch() {
  108. checkState(branch.isInitialized(), "Branch has not been set");
  109. return branch.getProperty();
  110. }
  111. @Override
  112. public MutableAnalysisMetadataHolder setPullRequestKey(String pullRequestKey) {
  113. this.pullRequestId.setProperty(pullRequestKey);
  114. return this;
  115. }
  116. @Override
  117. public String getPullRequestKey() {
  118. checkState(pullRequestId.isInitialized(), "Pull request id has not been set");
  119. return pullRequestId.getProperty();
  120. }
  121. @Override
  122. public AnalysisMetadataHolderRule setProject(Project p) {
  123. this.project.setProperty(p);
  124. return this;
  125. }
  126. @Override
  127. public Project getProject() {
  128. checkState(project.isInitialized(), "Project has not been set");
  129. return project.getProperty();
  130. }
  131. @Override
  132. public AnalysisMetadataHolderRule setRootComponentRef(int rootComponentRef) {
  133. this.rootComponentRef.setProperty(rootComponentRef);
  134. return this;
  135. }
  136. @Override
  137. public int getRootComponentRef() {
  138. checkState(rootComponentRef.isInitialized(), "Root component ref has not been set");
  139. return rootComponentRef.getProperty();
  140. }
  141. @Override
  142. public AnalysisMetadataHolderRule setQProfilesByLanguage(Map<String, QualityProfile> qProfilesPerLanguage) {
  143. this.qProfilesPerLanguage.setProperty(qProfilesPerLanguage);
  144. return this;
  145. }
  146. @Override
  147. public Map<String, QualityProfile> getQProfilesByLanguage() {
  148. checkState(qProfilesPerLanguage.isInitialized(), "QProfile per language has not been set");
  149. return qProfilesPerLanguage.getProperty();
  150. }
  151. @Override
  152. public AnalysisMetadataHolderRule setScannerPluginsByKey(Map<String, ScannerPlugin> plugins) {
  153. this.pluginsByKey.setProperty(plugins);
  154. return this;
  155. }
  156. @Override
  157. public Map<String, ScannerPlugin> getScannerPluginsByKey() {
  158. checkState(pluginsByKey.isInitialized(), "Plugins per key has not been set");
  159. return pluginsByKey.getProperty();
  160. }
  161. @Override
  162. public MutableAnalysisMetadataHolder setScmRevision(@Nullable String s) {
  163. checkState(!this.scmRevision.isInitialized(), "ScmRevisionId has already been set");
  164. this.scmRevision.setProperty(defaultIfBlank(s, null));
  165. return this;
  166. }
  167. @Override
  168. public Optional<String> getScmRevision() {
  169. if (!scmRevision.isInitialized()) {
  170. return Optional.empty();
  171. }
  172. return Optional.ofNullable(scmRevision.getProperty());
  173. }
  174. @Override
  175. public boolean isBranch() {
  176. Branch property = this.branch.getProperty();
  177. return property != null && property.getType() == BranchType.BRANCH;
  178. }
  179. @Override
  180. public boolean isPullRequest() {
  181. Branch property = this.branch.getProperty();
  182. return property != null && property.getType() == BranchType.PULL_REQUEST;
  183. }
  184. }