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.api.posttask;
22 import java.util.Collection;
23 import java.util.Date;
25 import java.util.Optional;
27 import javax.annotation.CheckForNull;
28 import javax.annotation.Nullable;
29 import org.sonar.api.ce.posttask.Analysis;
30 import org.sonar.api.ce.posttask.Branch;
31 import org.sonar.api.ce.posttask.CeTask;
32 import org.sonar.api.ce.posttask.Organization;
33 import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
34 import org.sonar.api.ce.posttask.Project;
35 import org.sonar.api.ce.posttask.QualityGate;
36 import org.sonar.api.ce.posttask.ScannerContext;
37 import org.sonar.api.utils.System2;
38 import org.sonar.api.utils.log.Logger;
39 import org.sonar.api.utils.log.Loggers;
40 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
41 import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
42 import org.sonar.ce.task.projectanalysis.qualitygate.Condition;
43 import org.sonar.ce.task.projectanalysis.qualitygate.ConditionStatus;
44 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateHolder;
45 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateStatus;
46 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateStatusHolder;
47 import org.sonar.ce.task.step.ComputationStepExecutor;
48 import org.sonar.core.util.stream.MoreCollectors;
50 import static java.lang.String.format;
51 import static java.util.Objects.requireNonNull;
52 import static java.util.Optional.empty;
53 import static java.util.Optional.of;
54 import static java.util.Optional.ofNullable;
55 import static org.sonar.api.ce.posttask.CeTask.Status.FAILED;
56 import static org.sonar.api.ce.posttask.CeTask.Status.SUCCESS;
57 import static org.sonar.db.component.BranchType.PULL_REQUEST;
60 * Responsible for calling {@link PostProjectAnalysisTask} implementations (if any).
62 public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor.Listener {
63 private static final PostProjectAnalysisTask[] NO_POST_PROJECT_ANALYSIS_TASKS = new PostProjectAnalysisTask[0];
65 private static final Logger LOG = Loggers.get(PostProjectAnalysisTasksExecutor.class);
67 private final org.sonar.ce.task.CeTask ceTask;
68 private final AnalysisMetadataHolder analysisMetadataHolder;
69 private final QualityGateHolder qualityGateHolder;
70 private final QualityGateStatusHolder qualityGateStatusHolder;
71 private final PostProjectAnalysisTask[] postProjectAnalysisTasks;
72 private final BatchReportReader reportReader;
73 private final System2 system2;
76 * Constructor used by Pico when there is no {@link PostProjectAnalysisTask} in the container.
78 public PostProjectAnalysisTasksExecutor(org.sonar.ce.task.CeTask ceTask,
79 AnalysisMetadataHolder analysisMetadataHolder,
80 QualityGateHolder qualityGateHolder, QualityGateStatusHolder qualityGateStatusHolder,
81 BatchReportReader reportReader, System2 system2) {
82 this(ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder, reportReader, system2, null);
85 public PostProjectAnalysisTasksExecutor(org.sonar.ce.task.CeTask ceTask,
86 AnalysisMetadataHolder analysisMetadataHolder,
87 QualityGateHolder qualityGateHolder, QualityGateStatusHolder qualityGateStatusHolder,
88 BatchReportReader reportReader, System2 system2,
89 @Nullable PostProjectAnalysisTask[] postProjectAnalysisTasks) {
90 this.analysisMetadataHolder = analysisMetadataHolder;
91 this.qualityGateHolder = qualityGateHolder;
92 this.qualityGateStatusHolder = qualityGateStatusHolder;
94 this.reportReader = reportReader;
95 this.postProjectAnalysisTasks = postProjectAnalysisTasks == null ? NO_POST_PROJECT_ANALYSIS_TASKS : postProjectAnalysisTasks;
96 this.system2 = system2;
100 public void finished(boolean allStepsExecuted) {
101 if (postProjectAnalysisTasks.length == 0) {
105 ProjectAnalysisImpl projectAnalysis = createProjectAnalysis(allStepsExecuted ? SUCCESS : FAILED);
106 for (PostProjectAnalysisTask postProjectAnalysisTask : postProjectAnalysisTasks) {
107 executeTask(projectAnalysis, postProjectAnalysisTask);
111 private static void executeTask(ProjectAnalysisImpl projectAnalysis, PostProjectAnalysisTask postProjectAnalysisTask) {
113 postProjectAnalysisTask.finished(projectAnalysis);
114 } catch (Exception e) {
115 LOG.error("Execution of task " + postProjectAnalysisTask.getClass() + " failed", e);
119 private ProjectAnalysisImpl createProjectAnalysis(CeTask.Status status) {
120 return new ProjectAnalysisImpl(
121 createOrganization(),
122 new CeTaskImpl(this.ceTask.getUuid(), status),
123 createProject(this.ceTask),
124 getAnalysis().orElse(null),
125 getAnalysis().map(a -> a.getDate().getTime()).orElse(system2.now()),
126 ScannerContextImpl.from(reportReader.readContextProperties()),
127 status == SUCCESS ? createQualityGate() : null,
129 reportReader.readMetadata().getScmRevisionId());
133 private Organization createOrganization() {
134 if (!analysisMetadataHolder.isOrganizationsEnabled()) {
137 org.sonar.ce.task.projectanalysis.analysis.Organization organization = analysisMetadataHolder.getOrganization();
138 return new OrganizationImpl(organization.getName(), organization.getKey());
141 private Optional<Analysis> getAnalysis() {
142 Long analysisDate = getAnalysisDate();
144 if (analysisDate != null) {
145 return of(new AnalysisImpl(analysisMetadataHolder.getUuid(), analysisDate));
150 private static Project createProject(org.sonar.ce.task.CeTask ceTask) {
151 return ceTask.getMainComponent()
152 .map(c -> new ProjectImpl(
154 c.getKey().orElseThrow(() -> new IllegalStateException("Missing project key")),
155 c.getName().orElseThrow(() -> new IllegalStateException("Missing project name"))))
156 .orElseThrow(() -> new IllegalStateException("Report processed for a task of a deleted component"));
160 private Long getAnalysisDate() {
161 if (this.analysisMetadataHolder.hasAnalysisDateBeenSet()) {
162 return this.analysisMetadataHolder.getAnalysisDate();
168 private QualityGate createQualityGate() {
169 Optional<org.sonar.ce.task.projectanalysis.qualitygate.QualityGate> qualityGateOptional = this.qualityGateHolder.getQualityGate();
170 if (qualityGateOptional.isPresent()) {
171 org.sonar.ce.task.projectanalysis.qualitygate.QualityGate qualityGate = qualityGateOptional.get();
173 return new QualityGateImpl(
174 String.valueOf(qualityGate.getId()),
175 qualityGate.getName(),
176 convert(qualityGateStatusHolder.getStatus()),
177 convert(qualityGate.getConditions(), qualityGateStatusHolder.getStatusPerConditions()));
183 private BranchImpl createBranch() {
184 org.sonar.ce.task.projectanalysis.analysis.Branch analysisBranch = analysisMetadataHolder.getBranch();
185 if (!analysisBranch.isLegacyFeature()) {
186 String branchKey = analysisBranch.getType() == PULL_REQUEST ? analysisBranch.getPullRequestKey() : analysisBranch.getName();
187 return new BranchImpl(analysisBranch.isMain(), branchKey, Branch.Type.valueOf(analysisBranch.getType().name()));
192 private static QualityGate.Status convert(QualityGateStatus status) {
195 return QualityGate.Status.OK;
197 return QualityGate.Status.ERROR;
199 throw new IllegalArgumentException(format(
200 "Unsupported value '%s' of QualityGateStatus can not be converted to QualityGate.Status",
205 private static Collection<QualityGate.Condition> convert(Set<Condition> conditions, Map<Condition, ConditionStatus> statusPerConditions) {
206 return conditions.stream()
207 .map(new ConditionToCondition(statusPerConditions)::apply)
208 .collect(MoreCollectors.toList(statusPerConditions.size()));
211 private static class ProjectAnalysisImpl implements PostProjectAnalysisTask.ProjectAnalysis {
213 private final Organization organization;
214 private final CeTask ceTask;
215 private final Project project;
216 private final long date;
217 private final ScannerContext scannerContext;
219 private final QualityGate qualityGate;
221 private final Branch branch;
223 private final Analysis analysis;
224 private final String scmRevisionId;
226 private ProjectAnalysisImpl(@Nullable Organization organization, CeTask ceTask, Project project,
227 @Nullable Analysis analysis, long date,
228 ScannerContext scannerContext, @Nullable QualityGate qualityGate, @Nullable Branch branch, String scmRevisionId) {
229 this.organization = organization;
230 this.ceTask = requireNonNull(ceTask, "ceTask can not be null");
231 this.project = requireNonNull(project, "project can not be null");
232 this.analysis = analysis;
234 this.scannerContext = requireNonNull(scannerContext, "scannerContext can not be null");
235 this.qualityGate = qualityGate;
236 this.branch = branch;
237 this.scmRevisionId = scmRevisionId;
241 public Optional<Organization> getOrganization() {
242 return Optional.ofNullable(organization);
246 public CeTask getCeTask() {
251 public Project getProject() {
256 public Optional<Branch> getBranch() {
257 return ofNullable(branch);
262 public QualityGate getQualityGate() {
267 public Date getDate() {
268 return new Date(date);
272 public Optional<Date> getAnalysisDate() {
273 return analysis == null ? empty() : ofNullable(analysis.getDate());
277 public Optional<Analysis> getAnalysis() {
278 return ofNullable(analysis);
282 public ScannerContext getScannerContext() {
283 return scannerContext;
287 public String getScmRevisionId() {
288 return scmRevisionId;
292 public String toString() {
293 return "ProjectAnalysis{" +
295 ", project=" + project +
297 ", scannerContext=" + scannerContext +
298 ", qualityGate=" + qualityGate +
299 ", analysis=" + analysis +
304 private static class AnalysisImpl implements Analysis {
306 private final String analysisUuid;
307 private final long date;
309 private AnalysisImpl(String analysisUuid, long date) {
310 this.analysisUuid = analysisUuid;
315 public String getAnalysisUuid() {
320 public Date getDate() {
321 return new Date(date);
325 private static class OrganizationImpl implements Organization {
326 private final String name;
327 private final String key;
329 private OrganizationImpl(String name, String key) {
330 this.name = requireNonNull(name, "name can't be null");
331 this.key = requireNonNull(key, "key can't be null");
335 public String getName() {
340 public String getKey() {