3 * Copyright (C) 2009-2020 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.qualitymodel;
22 import java.util.Optional;
23 import org.sonar.ce.task.projectanalysis.component.Component;
24 import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
25 import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
26 import org.sonar.ce.task.projectanalysis.measure.Measure;
27 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
28 import org.sonar.ce.task.projectanalysis.measure.RatingMeasures;
29 import org.sonar.ce.task.projectanalysis.metric.Metric;
30 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
31 import org.sonar.server.measure.Rating;
32 import org.sonar.server.security.SecurityReviewRating;
34 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
35 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_KEY;
36 import static org.sonar.api.measures.CoreMetrics.SECURITY_REVIEW_RATING_KEY;
38 public class SecurityReviewRatingVisitorForPortfoliosAndApplications extends TypeAwareVisitorAdapter {
40 private final MeasureRepository measureRepository;
41 private final Metric nclocMetric;
42 private final Metric securityHostspotsMetric;
43 private final Metric securityReviewRatingMetric;
45 public SecurityReviewRatingVisitorForPortfoliosAndApplications(MeasureRepository measureRepository, MetricRepository metricRepository) {
46 super(CrawlerDepthLimit.SUBVIEW, Order.POST_ORDER);
47 this.measureRepository = measureRepository;
48 this.nclocMetric = metricRepository.getByKey(NCLOC_KEY);
49 this.securityHostspotsMetric = metricRepository.getByKey(SECURITY_HOTSPOTS_KEY);
50 this.securityReviewRatingMetric = metricRepository.getByKey(SECURITY_REVIEW_RATING_KEY);
54 public void visitProject(Component project) {
59 public void visitView(Component view) {
64 public void visitSubView(Component subView) {
65 computeMeasure(subView);
68 private void computeMeasure(Component component) {
69 Optional<Measure> nclocMeasure = measureRepository.getRawMeasure(component, nclocMetric);
70 Optional<Measure> securityHostspotsMeasure = measureRepository.getRawMeasure(component, securityHostspotsMetric);
71 if (!nclocMeasure.isPresent() || !securityHostspotsMeasure.isPresent()) {
74 int ncloc = nclocMeasure.get().getIntValue();
75 int securityHotspots = securityHostspotsMeasure.get().getIntValue();
76 Rating rating = SecurityReviewRating.computeForPortfolios(ncloc, securityHotspots);
77 measureRepository.add(component, securityReviewRatingMetric, RatingMeasures.get(rating));