3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.analysis;
22 import java.util.Date;
24 import java.util.Optional;
25 import javax.annotation.CheckForNull;
26 import javax.annotation.Nullable;
27 import org.junit.rules.ExternalResource;
28 import org.sonar.ce.task.util.InitializedProperty;
29 import org.sonar.db.component.BranchType;
30 import org.sonar.db.organization.OrganizationDto;
31 import org.sonar.server.project.Project;
32 import org.sonar.server.qualityprofile.QualityProfile;
34 import static com.google.common.base.Preconditions.checkNotNull;
35 import static com.google.common.base.Preconditions.checkState;
36 import static java.util.Objects.requireNonNull;
38 public class AnalysisMetadataHolderRule extends ExternalResource implements MutableAnalysisMetadataHolder {
40 private final InitializedProperty<Boolean> organizationsEnabled = new InitializedProperty<>();
42 private final InitializedProperty<Organization> organization = new InitializedProperty<>();
44 private final InitializedProperty<String> uuid = new InitializedProperty<>();
46 private final InitializedProperty<Long> analysisDate = new InitializedProperty<>();
48 private final InitializedProperty<Analysis> baseAnalysis = new InitializedProperty<>();
50 private final InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();
52 private final InitializedProperty<Branch> branch = new InitializedProperty<>();
54 private final InitializedProperty<String> pullRequestId = new InitializedProperty<>();
56 private final InitializedProperty<Project> project = new InitializedProperty<>();
58 private final InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
60 private final InitializedProperty<Map<String, QualityProfile>> qProfilesPerLanguage = new InitializedProperty<>();
62 private final InitializedProperty<Map<String, ScannerPlugin>> pluginsByKey = new InitializedProperty<>();
64 private final InitializedProperty<String> scmRevisionId = new InitializedProperty<>();
67 public AnalysisMetadataHolderRule setOrganizationsEnabled(boolean isOrganizationsEnabled) {
68 this.organizationsEnabled.setProperty(isOrganizationsEnabled);
73 public boolean isOrganizationsEnabled() {
74 checkState(organizationsEnabled.isInitialized(), "Organizations enabled flag has not been set");
75 return organizationsEnabled.getProperty();
79 public AnalysisMetadataHolderRule setOrganization(Organization organization) {
80 requireNonNull(organization, "organization can't be null");
81 this.organization.setProperty(organization);
85 public AnalysisMetadataHolderRule setOrganizationUuid(String uuid, String defaultQualityGateUuid) {
86 requireNonNull(uuid, "organization uuid can't be null");
88 .setProperty(Organization.from(new OrganizationDto().setUuid(uuid).setKey("key_" + uuid).setName("name_" + uuid).setDefaultQualityGateUuid(defaultQualityGateUuid)));
93 public Organization getOrganization() {
94 checkState(organization.isInitialized(), "Organization has not been set");
95 return this.organization.getProperty();
99 public AnalysisMetadataHolderRule setUuid(String s) {
100 checkNotNull(s, "UUID must not be null");
101 this.uuid.setProperty(s);
106 public String getUuid() {
107 checkState(uuid.isInitialized(), "Analysis UUID has not been set");
108 return this.uuid.getProperty();
111 public AnalysisMetadataHolderRule setAnalysisDate(Date date) {
112 checkNotNull(date, "Date must not be null");
113 this.analysisDate.setProperty(date.getTime());
118 public AnalysisMetadataHolderRule setAnalysisDate(long date) {
119 checkNotNull(date, "Date must not be null");
120 this.analysisDate.setProperty(date);
125 public long getAnalysisDate() {
126 checkState(analysisDate.isInitialized(), "Analysis date has not been set");
127 return this.analysisDate.getProperty();
131 public boolean hasAnalysisDateBeenSet() {
132 return analysisDate.isInitialized();
136 public boolean isFirstAnalysis() {
137 return getBaseAnalysis() == null;
141 public AnalysisMetadataHolderRule setBaseAnalysis(@Nullable Analysis baseAnalysis) {
142 this.baseAnalysis.setProperty(baseAnalysis);
148 public Analysis getBaseAnalysis() {
149 checkState(baseAnalysis.isInitialized(), "Base analysis has not been set");
150 return baseAnalysis.getProperty();
154 public AnalysisMetadataHolderRule setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
155 this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
160 public boolean isCrossProjectDuplicationEnabled() {
161 checkState(crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has not been set");
162 return crossProjectDuplicationEnabled.getProperty();
166 public AnalysisMetadataHolderRule setBranch(Branch branch) {
167 this.branch.setProperty(branch);
172 public Branch getBranch() {
173 checkState(branch.isInitialized(), "Branch has not been set");
174 return branch.getProperty();
178 public MutableAnalysisMetadataHolder setPullRequestKey(String pullRequestKey) {
179 this.pullRequestId.setProperty(pullRequestKey);
184 public String getPullRequestKey() {
185 checkState(pullRequestId.isInitialized(), "Pull request id has not been set");
186 return pullRequestId.getProperty();
190 public AnalysisMetadataHolderRule setProject(Project p) {
191 this.project.setProperty(p);
196 public Project getProject() {
197 checkState(project.isInitialized(), "Project has not been set");
198 return project.getProperty();
202 public AnalysisMetadataHolderRule setRootComponentRef(int rootComponentRef) {
203 this.rootComponentRef.setProperty(rootComponentRef);
208 public int getRootComponentRef() {
209 checkState(rootComponentRef.isInitialized(), "Root component ref has not been set");
210 return rootComponentRef.getProperty();
214 public AnalysisMetadataHolderRule setQProfilesByLanguage(Map<String, QualityProfile> qProfilesPerLanguage) {
215 this.qProfilesPerLanguage.setProperty(qProfilesPerLanguage);
220 public Map<String, QualityProfile> getQProfilesByLanguage() {
221 checkState(qProfilesPerLanguage.isInitialized(), "QProfile per language has not been set");
222 return qProfilesPerLanguage.getProperty();
226 public AnalysisMetadataHolderRule setScannerPluginsByKey(Map<String, ScannerPlugin> plugins) {
227 this.pluginsByKey.setProperty(plugins);
232 public Map<String, ScannerPlugin> getScannerPluginsByKey() {
233 checkState(pluginsByKey.isInitialized(), "Plugins per key has not been set");
234 return pluginsByKey.getProperty();
238 public MutableAnalysisMetadataHolder setScmRevisionId(String scmRevisionId) {
239 checkState(!this.scmRevisionId.isInitialized(), "ScmRevisionId has already been set");
240 this.scmRevisionId.setProperty(scmRevisionId);
245 public Optional<String> getScmRevisionId() {
246 if (!scmRevisionId.isInitialized()) {
247 return Optional.empty();
249 return Optional.of(scmRevisionId.getProperty());
253 public boolean isShortLivingBranch() {
254 Branch property = this.branch.getProperty();
255 return property != null && property.getType() == BranchType.SHORT;
259 public boolean isLongLivingBranch() {
260 Branch property = this.branch.getProperty();
261 return property != null && property.getType() == BranchType.LONG;
265 public boolean isPullRequest() {
266 Branch property = this.branch.getProperty();
267 return property != null && property.getType() == BranchType.PULL_REQUEST;