]> source.dussan.org Git - sonarqube.git/blob
27904e164bb54f6fe2dd75fc4c819be3901dc754
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.api.posttask;
21
22 import com.google.common.base.Optional;
23 import java.util.Collection;
24 import java.util.Date;
25 import java.util.Map;
26 import java.util.Set;
27 import javax.annotation.CheckForNull;
28 import javax.annotation.Nullable;
29 import org.sonar.api.ce.posttask.CeTask;
30 import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
31 import org.sonar.api.ce.posttask.Project;
32 import org.sonar.api.ce.posttask.QualityGate;
33 import org.sonar.api.ce.posttask.ScannerContext;
34 import org.sonar.api.utils.System2;
35 import org.sonar.api.utils.log.Logger;
36 import org.sonar.api.utils.log.Loggers;
37 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
38 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
39 import org.sonar.server.computation.task.projectanalysis.qualitygate.Condition;
40 import org.sonar.server.computation.task.projectanalysis.qualitygate.ConditionStatus;
41 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGateHolder;
42 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGateStatus;
43 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGateStatusHolder;
44 import org.sonar.server.computation.task.step.ComputationStepExecutor;
45
46 import static com.google.common.collect.FluentIterable.from;
47 import static java.lang.String.format;
48 import static java.util.Objects.requireNonNull;
49 import static org.sonar.api.ce.posttask.CeTask.Status.FAILED;
50 import static org.sonar.api.ce.posttask.CeTask.Status.SUCCESS;
51
52 /**
53  * Responsible for calling {@link PostProjectAnalysisTask} implementations (if any).
54  */
55 public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor.Listener {
56   private static final PostProjectAnalysisTask[] NO_POST_PROJECT_ANALYSIS_TASKS = new PostProjectAnalysisTask[0];
57
58   private static final Logger LOG = Loggers.get(PostProjectAnalysisTasksExecutor.class);
59
60   private final org.sonar.ce.queue.CeTask ceTask;
61   private final AnalysisMetadataHolder analysisMetadataHolder;
62   private final QualityGateHolder qualityGateHolder;
63   private final QualityGateStatusHolder qualityGateStatusHolder;
64   private final PostProjectAnalysisTask[] postProjectAnalysisTasks;
65   private final BatchReportReader reportReader;
66   private final System2 system2;
67
68   /**
69    * Constructor used by Pico when there is no {@link PostProjectAnalysisTask} in the container.
70    */
71   public PostProjectAnalysisTasksExecutor(org.sonar.ce.queue.CeTask ceTask,
72     AnalysisMetadataHolder analysisMetadataHolder,
73     QualityGateHolder qualityGateHolder, QualityGateStatusHolder qualityGateStatusHolder,
74     BatchReportReader reportReader, System2 system2) {
75     this(ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder, reportReader, system2, null);
76   }
77
78   public PostProjectAnalysisTasksExecutor(org.sonar.ce.queue.CeTask ceTask,
79     AnalysisMetadataHolder analysisMetadataHolder,
80     QualityGateHolder qualityGateHolder, QualityGateStatusHolder qualityGateStatusHolder,
81     BatchReportReader reportReader, System2 system2,
82     @Nullable PostProjectAnalysisTask[] postProjectAnalysisTasks) {
83     this.analysisMetadataHolder = analysisMetadataHolder;
84     this.qualityGateHolder = qualityGateHolder;
85     this.qualityGateStatusHolder = qualityGateStatusHolder;
86     this.ceTask = ceTask;
87     this.reportReader = reportReader;
88     this.postProjectAnalysisTasks = postProjectAnalysisTasks == null ? NO_POST_PROJECT_ANALYSIS_TASKS : postProjectAnalysisTasks;
89     this.system2 = system2;
90   }
91
92   @Override
93   public void finished(boolean allStepsExecuted) {
94     if (postProjectAnalysisTasks.length == 0) {
95       return;
96     }
97
98     ProjectAnalysis projectAnalysis = createProjectAnalysis(allStepsExecuted ? SUCCESS : FAILED);
99     for (PostProjectAnalysisTask postProjectAnalysisTask : postProjectAnalysisTasks) {
100       executeTask(projectAnalysis, postProjectAnalysisTask);
101     }
102   }
103
104   private static void executeTask(ProjectAnalysis projectAnalysis, PostProjectAnalysisTask postProjectAnalysisTask) {
105     try {
106       postProjectAnalysisTask.finished(projectAnalysis);
107     } catch (Exception e) {
108       LOG.error("Execution of task " + postProjectAnalysisTask.getClass() + " failed", e);
109     }
110   }
111
112   private ProjectAnalysis createProjectAnalysis(CeTask.Status status) {
113     Long analysisDate = getAnalysisDate();
114     return new ProjectAnalysis(
115       new CeTaskImpl(this.ceTask.getUuid(), status),
116       createProject(this.ceTask),
117       analysisDate,
118       analysisDate == null ? system2.now() : analysisDate,
119       ScannerContextImpl.from(reportReader.readContextProperties()),
120       status == SUCCESS ? createQualityGate(this.qualityGateHolder) : null);
121   }
122
123   private static Project createProject(org.sonar.ce.queue.CeTask ceTask) {
124     return new ProjectImpl(
125       ceTask.getComponentUuid(),
126       ceTask.getComponentKey(),
127       ceTask.getComponentName());
128   }
129
130   @CheckForNull
131   private Long getAnalysisDate() {
132     if (this.analysisMetadataHolder.hasAnalysisDateBeenSet()) {
133       return this.analysisMetadataHolder.getAnalysisDate();
134     }
135     return null;
136   }
137
138   @CheckForNull
139   private QualityGateImpl createQualityGate(QualityGateHolder qualityGateHolder) {
140     Optional<org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGate> qualityGateOptional = qualityGateHolder.getQualityGate();
141     if (qualityGateOptional.isPresent()) {
142       org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGate qualityGate = qualityGateOptional.get();
143
144       return new QualityGateImpl(
145         String.valueOf(qualityGate.getId()),
146         qualityGate.getName(),
147         convert(qualityGateStatusHolder.getStatus()),
148         convert(qualityGate.getConditions(), qualityGateStatusHolder.getStatusPerConditions()));
149     }
150     return null;
151   }
152
153   private static QualityGate.Status convert(QualityGateStatus status) {
154     switch (status) {
155       case OK:
156         return QualityGate.Status.OK;
157       case WARN:
158         return QualityGate.Status.WARN;
159       case ERROR:
160         return QualityGate.Status.ERROR;
161       default:
162         throw new IllegalArgumentException(format(
163           "Unsupported value '%s' of QualityGateStatus can not be converted to QualityGate.Status",
164           status));
165     }
166   }
167
168   private static Collection<QualityGate.Condition> convert(Set<Condition> conditions, Map<Condition, ConditionStatus> statusPerConditions) {
169     return from(conditions)
170       .transform(new ConditionToCondition(statusPerConditions))
171       .toList();
172   }
173
174   private static class ProjectAnalysis implements PostProjectAnalysisTask.ProjectAnalysis {
175     private final CeTask ceTask;
176     private final Project project;
177     @CheckForNull
178     private final Long analysisDate;
179     private final long date;
180     private final ScannerContext scannerContext;
181     @CheckForNull
182     private final QualityGate qualityGate;
183
184     private ProjectAnalysis(CeTask ceTask, Project project,
185       @Nullable Long analysisDate, long date,
186       ScannerContext scannerContext, @Nullable QualityGate qualityGate) {
187       this.ceTask = requireNonNull(ceTask, "ceTask can not be null");
188       this.project = requireNonNull(project, "project can not be null");
189       this.analysisDate = analysisDate;
190       this.date = date;
191       this.scannerContext = requireNonNull(scannerContext, "scannerContext can not be null");
192       this.qualityGate = qualityGate;
193     }
194
195     @Override
196     public CeTask getCeTask() {
197       return ceTask;
198     }
199
200     @Override
201     public Project getProject() {
202       return project;
203     }
204
205     @Override
206     @CheckForNull
207     public QualityGate getQualityGate() {
208       return qualityGate;
209     }
210
211     @Override
212     public Date getDate() {
213       return new Date(date);
214     }
215
216     @Override
217     public java.util.Optional<Date> getAnalysisDate() {
218       if (analysisDate == null) {
219         return java.util.Optional.empty();
220       }
221       return java.util.Optional.of(new Date(analysisDate));
222     }
223
224     @Override
225     public ScannerContext getScannerContext() {
226       return scannerContext;
227     }
228
229     @Override
230     public String toString() {
231       return "ProjectAnalysis{" +
232         "ceTask=" + ceTask +
233         ", project=" + project +
234         ", analysisDate=" + analysisDate +
235         ", date=" + date +
236         ", scannerContext=" + scannerContext +
237         ", qualityGate=" + qualityGate +
238         '}';
239     }
240   }
241
242 }