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.

AnalysisMetadataHolderImpl.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.ce.task.projectanalysis.analysis;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.util.Map;
  23. import java.util.Optional;
  24. import javax.annotation.CheckForNull;
  25. import javax.annotation.Nullable;
  26. import org.sonar.ce.task.util.InitializedProperty;
  27. import org.sonar.core.platform.EditionProvider;
  28. import org.sonar.core.platform.PlatformEditionProvider;
  29. import org.sonar.db.component.BranchType;
  30. import org.sonar.server.project.Project;
  31. import org.sonar.server.qualityprofile.QualityProfile;
  32. import static com.google.common.base.Preconditions.checkState;
  33. import static java.util.Objects.requireNonNull;
  34. import static org.apache.commons.lang.StringUtils.defaultIfBlank;
  35. public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder {
  36. private static final String BRANCH_NOT_SET = "Branch has not been set";
  37. private final InitializedProperty<Boolean> organizationsEnabled = new InitializedProperty<>();
  38. private final InitializedProperty<Organization> organization = new InitializedProperty<>();
  39. private final InitializedProperty<String> uuid = new InitializedProperty<>();
  40. private final InitializedProperty<Long> analysisDate = new InitializedProperty<>();
  41. private final InitializedProperty<Analysis> baseProjectSnapshot = new InitializedProperty<>();
  42. private final InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();
  43. private final InitializedProperty<Branch> branch = new InitializedProperty<>();
  44. private final InitializedProperty<String> pullRequestKey = new InitializedProperty<>();
  45. private final InitializedProperty<Project> project = new InitializedProperty<>();
  46. private final InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
  47. private final InitializedProperty<Map<String, QualityProfile>> qProfilesPerLanguage = new InitializedProperty<>();
  48. private final InitializedProperty<Map<String, ScannerPlugin>> pluginsByKey = new InitializedProperty<>();
  49. private final InitializedProperty<String> scmRevision = new InitializedProperty<>();
  50. private final PlatformEditionProvider editionProvider;
  51. public AnalysisMetadataHolderImpl(PlatformEditionProvider editionProvider) {
  52. this.editionProvider = editionProvider;
  53. }
  54. @Override
  55. public MutableAnalysisMetadataHolder setOrganizationsEnabled(boolean isOrganizationsEnabled) {
  56. checkState(!this.organizationsEnabled.isInitialized(), "Organization enabled flag has already been set");
  57. this.organizationsEnabled.setProperty(isOrganizationsEnabled);
  58. return this;
  59. }
  60. @Override
  61. public boolean isOrganizationsEnabled() {
  62. checkState(organizationsEnabled.isInitialized(), "Organizations enabled flag has not been set");
  63. return organizationsEnabled.getProperty();
  64. }
  65. @Override
  66. public MutableAnalysisMetadataHolder setOrganization(Organization organization) {
  67. checkState(!this.organization.isInitialized(), "Organization has already been set");
  68. requireNonNull(organization, "Organization can't be null");
  69. this.organization.setProperty(organization);
  70. return this;
  71. }
  72. @Override
  73. public Organization getOrganization() {
  74. checkState(organization.isInitialized(), "Organization has not been set");
  75. return organization.getProperty();
  76. }
  77. @Override
  78. public MutableAnalysisMetadataHolder setUuid(String s) {
  79. checkState(!uuid.isInitialized(), "Analysis uuid has already been set");
  80. requireNonNull(s, "Analysis uuid can't be null");
  81. this.uuid.setProperty(s);
  82. return this;
  83. }
  84. @Override
  85. public String getUuid() {
  86. checkState(uuid.isInitialized(), "Analysis uuid has not been set");
  87. return this.uuid.getProperty();
  88. }
  89. @Override
  90. public MutableAnalysisMetadataHolder setAnalysisDate(long date) {
  91. checkState(!analysisDate.isInitialized(), "Analysis date has already been set");
  92. this.analysisDate.setProperty(date);
  93. return this;
  94. }
  95. @Override
  96. public long getAnalysisDate() {
  97. checkState(analysisDate.isInitialized(), "Analysis date has not been set");
  98. return this.analysisDate.getProperty();
  99. }
  100. @Override
  101. public boolean hasAnalysisDateBeenSet() {
  102. return analysisDate.isInitialized();
  103. }
  104. @Override
  105. public boolean isFirstAnalysis() {
  106. return getBaseAnalysis() == null;
  107. }
  108. @Override
  109. public MutableAnalysisMetadataHolder setBaseAnalysis(@Nullable Analysis baseAnalysis) {
  110. checkState(!this.baseProjectSnapshot.isInitialized(), "Base project snapshot has already been set");
  111. this.baseProjectSnapshot.setProperty(baseAnalysis);
  112. return this;
  113. }
  114. @Override
  115. @CheckForNull
  116. public Analysis getBaseAnalysis() {
  117. checkState(baseProjectSnapshot.isInitialized(), "Base project snapshot has not been set");
  118. return baseProjectSnapshot.getProperty();
  119. }
  120. @Override
  121. public MutableAnalysisMetadataHolder setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
  122. checkState(!this.crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has already been set");
  123. this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
  124. return this;
  125. }
  126. @Override
  127. public boolean isCrossProjectDuplicationEnabled() {
  128. checkState(crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has not been set");
  129. return crossProjectDuplicationEnabled.getProperty();
  130. }
  131. @Override
  132. public MutableAnalysisMetadataHolder setBranch(Branch branch) {
  133. checkState(!this.branch.isInitialized(), "Branch has already been set");
  134. boolean isCommunityEdition = editionProvider.get().filter(t -> t == EditionProvider.Edition.COMMUNITY).isPresent();
  135. checkState(
  136. !isCommunityEdition || branch.isMain(),
  137. "Branches and Pull Requests are not supported in Community Edition");
  138. this.branch.setProperty(branch);
  139. return this;
  140. }
  141. @Override
  142. public Branch getBranch() {
  143. checkState(branch.isInitialized(), BRANCH_NOT_SET);
  144. return branch.getProperty();
  145. }
  146. @Override
  147. public MutableAnalysisMetadataHolder setPullRequestKey(String pullRequestKey) {
  148. checkState(!this.pullRequestKey.isInitialized(), "Pull request key has already been set");
  149. this.pullRequestKey.setProperty(pullRequestKey);
  150. return this;
  151. }
  152. @Override
  153. public String getPullRequestKey() {
  154. checkState(pullRequestKey.isInitialized(), "Pull request key has not been set");
  155. return pullRequestKey.getProperty();
  156. }
  157. @Override
  158. public MutableAnalysisMetadataHolder setProject(Project project) {
  159. checkState(!this.project.isInitialized(), "Project has already been set");
  160. this.project.setProperty(project);
  161. return this;
  162. }
  163. @Override
  164. public Project getProject() {
  165. checkState(project.isInitialized(), "Project has not been set");
  166. return project.getProperty();
  167. }
  168. @Override
  169. public MutableAnalysisMetadataHolder setRootComponentRef(int rootComponentRef) {
  170. checkState(!this.rootComponentRef.isInitialized(), "Root component ref has already been set");
  171. this.rootComponentRef.setProperty(rootComponentRef);
  172. return this;
  173. }
  174. @Override
  175. public int getRootComponentRef() {
  176. checkState(rootComponentRef.isInitialized(), "Root component ref has not been set");
  177. return rootComponentRef.getProperty();
  178. }
  179. @Override
  180. public MutableAnalysisMetadataHolder setQProfilesByLanguage(Map<String, QualityProfile> qprofilesByLanguage) {
  181. checkState(!this.qProfilesPerLanguage.isInitialized(), "QProfiles by language has already been set");
  182. this.qProfilesPerLanguage.setProperty(ImmutableMap.copyOf(qprofilesByLanguage));
  183. return this;
  184. }
  185. @Override
  186. public Map<String, QualityProfile> getQProfilesByLanguage() {
  187. checkState(qProfilesPerLanguage.isInitialized(), "QProfiles by language has not been set");
  188. return qProfilesPerLanguage.getProperty();
  189. }
  190. @Override
  191. public MutableAnalysisMetadataHolder setScannerPluginsByKey(Map<String, ScannerPlugin> pluginsByKey) {
  192. checkState(!this.pluginsByKey.isInitialized(), "Plugins by key has already been set");
  193. this.pluginsByKey.setProperty(ImmutableMap.copyOf(pluginsByKey));
  194. return this;
  195. }
  196. @Override
  197. public Map<String, ScannerPlugin> getScannerPluginsByKey() {
  198. checkState(pluginsByKey.isInitialized(), "Plugins by key has not been set");
  199. return pluginsByKey.getProperty();
  200. }
  201. @Override
  202. public MutableAnalysisMetadataHolder setScmRevision(@Nullable String s) {
  203. checkState(!this.scmRevision.isInitialized(), "ScmRevision has already been set");
  204. this.scmRevision.setProperty(defaultIfBlank(s, null));
  205. return this;
  206. }
  207. @Override
  208. public Optional<String> getScmRevision() {
  209. if (!scmRevision.isInitialized()) {
  210. return Optional.empty();
  211. }
  212. return Optional.ofNullable(scmRevision.getProperty());
  213. }
  214. @Override
  215. public boolean isShortLivingBranch() {
  216. checkState(this.branch.isInitialized(), BRANCH_NOT_SET);
  217. Branch prop = branch.getProperty();
  218. return prop != null && prop.getType() == BranchType.SHORT;
  219. }
  220. @Override
  221. public boolean isLongLivingBranch() {
  222. checkState(this.branch.isInitialized(), BRANCH_NOT_SET);
  223. Branch prop = branch.getProperty();
  224. return prop != null && prop.getType() == BranchType.LONG;
  225. }
  226. @Override
  227. public boolean isPullRequest() {
  228. checkState(this.branch.isInitialized(), BRANCH_NOT_SET);
  229. Branch prop = branch.getProperty();
  230. return prop != null && prop.getType() == BranchType.PULL_REQUEST;
  231. }
  232. }