3 * Copyright (C) 2009-2024 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.jupiter.api.extension.AfterEachCallback;
28 import org.junit.jupiter.api.extension.ExtensionContext;
29 import org.junit.rules.ExternalResource;
30 import org.sonar.ce.task.util.InitializedProperty;
31 import org.sonar.db.component.BranchType;
32 import org.sonar.server.project.Project;
33 import org.sonar.server.qualityprofile.QualityProfile;
35 import static com.google.common.base.Preconditions.checkNotNull;
36 import static com.google.common.base.Preconditions.checkState;
37 import static org.apache.commons.lang.StringUtils.defaultIfBlank;
39 public class AnalysisMetadataHolderRule extends ExternalResource implements MutableAnalysisMetadataHolder, AfterEachCallback {
41 private final InitializedProperty<String> uuid = new InitializedProperty<>();
42 private final InitializedProperty<Long> analysisDate = new InitializedProperty<>();
43 private final InitializedProperty<Analysis> baseAnalysis = new InitializedProperty<>();
44 private final InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();
45 private final InitializedProperty<Branch> branch = new InitializedProperty<>();
46 private final InitializedProperty<String> pullRequestId = new InitializedProperty<>();
47 private final InitializedProperty<Project> project = new InitializedProperty<>();
48 private final InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
49 private final InitializedProperty<Map<String, QualityProfile>> qProfilesPerLanguage = new InitializedProperty<>();
50 private final InitializedProperty<Map<String, ScannerPlugin>> pluginsByKey = new InitializedProperty<>();
51 private final InitializedProperty<String> scmRevision = new InitializedProperty<>();
52 private final InitializedProperty<String> newCodeReferenceBranch = new InitializedProperty<>();
55 public AnalysisMetadataHolderRule setUuid(String s) {
56 checkNotNull(s, "UUID must not be null");
57 this.uuid.setProperty(s);
62 public String getUuid() {
63 checkState(uuid.isInitialized(), "Analysis UUID has not been set");
64 return this.uuid.getProperty();
67 public AnalysisMetadataHolderRule setAnalysisDate(Date date) {
68 checkNotNull(date, "Date must not be null");
69 this.analysisDate.setProperty(date.getTime());
74 public AnalysisMetadataHolderRule setAnalysisDate(long date) {
75 this.analysisDate.setProperty(date);
80 public long getAnalysisDate() {
81 checkState(analysisDate.isInitialized(), "Analysis date has not been set");
82 return this.analysisDate.getProperty();
86 public boolean hasAnalysisDateBeenSet() {
87 return analysisDate.isInitialized();
91 public boolean isFirstAnalysis() {
92 return getBaseAnalysis() == null;
96 public AnalysisMetadataHolderRule setBaseAnalysis(@Nullable Analysis baseAnalysis) {
97 this.baseAnalysis.setProperty(baseAnalysis);
103 public Analysis getBaseAnalysis() {
104 checkState(baseAnalysis.isInitialized(), "Base analysis has not been set");
105 return baseAnalysis.getProperty();
109 public AnalysisMetadataHolderRule setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
110 this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
115 public boolean isCrossProjectDuplicationEnabled() {
116 checkState(crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has not been set");
117 return crossProjectDuplicationEnabled.getProperty();
121 public AnalysisMetadataHolderRule setBranch(Branch branch) {
122 this.branch.setProperty(branch);
127 public Branch getBranch() {
128 checkState(branch.isInitialized(), "Branch has not been set");
129 return branch.getProperty();
133 public MutableAnalysisMetadataHolder setPullRequestKey(String pullRequestKey) {
134 this.pullRequestId.setProperty(pullRequestKey);
139 public String getPullRequestKey() {
140 checkState(pullRequestId.isInitialized(), "Pull request id has not been set");
141 return pullRequestId.getProperty();
145 public AnalysisMetadataHolderRule setProject(Project p) {
146 this.project.setProperty(p);
151 public Project getProject() {
152 checkState(project.isInitialized(), "Project has not been set");
153 return project.getProperty();
157 public AnalysisMetadataHolderRule setRootComponentRef(int rootComponentRef) {
158 this.rootComponentRef.setProperty(rootComponentRef);
163 public int getRootComponentRef() {
164 checkState(rootComponentRef.isInitialized(), "Root component ref has not been set");
165 return rootComponentRef.getProperty();
169 public AnalysisMetadataHolderRule setQProfilesByLanguage(Map<String, QualityProfile> qProfilesPerLanguage) {
170 this.qProfilesPerLanguage.setProperty(qProfilesPerLanguage);
175 public Map<String, QualityProfile> getQProfilesByLanguage() {
176 checkState(qProfilesPerLanguage.isInitialized(), "QProfile per language has not been set");
177 return qProfilesPerLanguage.getProperty();
181 public AnalysisMetadataHolderRule setScannerPluginsByKey(Map<String, ScannerPlugin> plugins) {
182 this.pluginsByKey.setProperty(plugins);
187 public Map<String, ScannerPlugin> getScannerPluginsByKey() {
188 checkState(pluginsByKey.isInitialized(), "Plugins per key has not been set");
189 return pluginsByKey.getProperty();
193 public MutableAnalysisMetadataHolder setScmRevision(@Nullable String s) {
194 checkState(!this.scmRevision.isInitialized(), "ScmRevisionId has already been set");
195 this.scmRevision.setProperty(defaultIfBlank(s, null));
200 public MutableAnalysisMetadataHolder setNewCodeReferenceBranch(String newCodeReferenceBranch) {
201 checkState(!this.newCodeReferenceBranch.isInitialized(), "ScmRevisionId has already been set");
202 this.newCodeReferenceBranch.setProperty(defaultIfBlank(newCodeReferenceBranch, null));
207 public Optional<String> getScmRevision() {
208 if (!scmRevision.isInitialized()) {
209 return Optional.empty();
211 return Optional.ofNullable(scmRevision.getProperty());
215 public Optional<String> getNewCodeReferenceBranch() {
216 if (!newCodeReferenceBranch.isInitialized()) {
217 return Optional.empty();
219 return Optional.ofNullable(newCodeReferenceBranch.getProperty());
223 public boolean isBranch() {
224 Branch property = this.branch.getProperty();
225 return property != null && property.getType() == BranchType.BRANCH;
229 public boolean isPullRequest() {
230 Branch property = this.branch.getProperty();
231 return property != null && property.getType() == BranchType.PULL_REQUEST;
235 public void afterEach(ExtensionContext context) {