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.
21 package org.sonar.ce.task.projectanalysis.qualitymodel;
23 import java.util.Optional;
24 import org.sonar.ce.task.projectanalysis.component.Component;
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.metric.Metric;
29 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
30 import org.sonar.server.measure.Rating;
31 import org.sonar.server.security.SecurityReviewRating;
33 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
34 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_KEY;
35 import static org.sonar.api.measures.CoreMetrics.SECURITY_REVIEW_RATING_KEY;
36 import static org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit.PROJECT;
37 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
39 public class SecurityReviewRatingVisitor extends TypeAwareVisitorAdapter {
41 private final MeasureRepository measureRepository;
42 private final Metric nclocMetric;
43 private final Metric securityHostspotsMetric;
44 private final Metric securityReviewRatingMetric;
46 public SecurityReviewRatingVisitor(MeasureRepository measureRepository, MetricRepository metricRepository) {
47 super(PROJECT, Order.POST_ORDER);
48 this.measureRepository = measureRepository;
49 this.nclocMetric = metricRepository.getByKey(NCLOC_KEY);
50 this.securityHostspotsMetric = metricRepository.getByKey(SECURITY_HOTSPOTS_KEY);
51 this.securityReviewRatingMetric = metricRepository.getByKey(SECURITY_REVIEW_RATING_KEY);
55 public void visitProject(Component project) {
56 Optional<Measure> nclocMeasure = measureRepository.getRawMeasure(project, nclocMetric);
57 Optional<Measure> securityHostspotsMeasure = measureRepository.getRawMeasure(project, securityHostspotsMetric);
58 if (!nclocMeasure.isPresent() || !securityHostspotsMeasure.isPresent()) {
61 int ncloc = nclocMeasure.get().getIntValue();
62 int securityHotspots = securityHostspotsMeasure.get().getIntValue();
63 Rating rating = SecurityReviewRating.compute(ncloc, securityHotspots);
64 measureRepository.add(project, securityReviewRatingMetric, newMeasureBuilder().create(rating.getIndex(), rating.name()));