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 org.sonar.ce.task.projectanalysis.component.Component;
23 import org.sonar.ce.task.projectanalysis.component.PathAwareVisitor;
24 import org.sonar.ce.task.projectanalysis.component.PathAwareVisitorAdapter;
25 import org.sonar.ce.task.projectanalysis.issue.ComponentIssuesRepository;
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;
32 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_REVIEWED_KEY;
33 import static org.sonar.api.measures.CoreMetrics.SECURITY_REVIEW_RATING_KEY;
34 import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT;
35 import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
36 import static org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit.FILE;
37 import static org.sonar.server.security.SecurityReviewRating.computePercent;
38 import static org.sonar.server.security.SecurityReviewRating.computeRating;
40 public class SecurityReviewMeasuresVisitor extends PathAwareVisitorAdapter<SecurityReviewCounter> {
42 private final ComponentIssuesRepository componentIssuesRepository;
43 private final MeasureRepository measureRepository;
44 private final Metric securityReviewRatingMetric;
45 private final Metric securityHotspotsReviewedMetric;
47 public SecurityReviewMeasuresVisitor(ComponentIssuesRepository componentIssuesRepository, MeasureRepository measureRepository, MetricRepository metricRepository) {
48 super(FILE, POST_ORDER, SecurityReviewMeasuresVisitor.CounterFactory.INSTANCE);
49 this.componentIssuesRepository = componentIssuesRepository;
50 this.measureRepository = measureRepository;
51 this.securityReviewRatingMetric = metricRepository.getByKey(SECURITY_REVIEW_RATING_KEY);
52 this.securityHotspotsReviewedMetric = metricRepository.getByKey(SECURITY_HOTSPOTS_REVIEWED_KEY);
56 public void visitProject(Component project, Path<SecurityReviewCounter> path) {
57 computeMeasure(project, path);
61 public void visitDirectory(Component directory, PathAwareVisitor.Path<SecurityReviewCounter> path) {
62 computeMeasure(directory, path);
66 public void visitFile(Component file, PathAwareVisitor.Path<SecurityReviewCounter> path) {
67 computeMeasure(file, path);
70 private void computeMeasure(Component component, PathAwareVisitor.Path<SecurityReviewCounter> path) {
71 componentIssuesRepository.getIssues(component)
73 .filter(issue -> issue.type().equals(SECURITY_HOTSPOT))
74 .forEach(issue -> path.current().processHotspot(issue));
76 Double percent = computePercent(path.current().getHotspotsToReview(), path.current().getHotspotsReviewed());
77 measureRepository.add(component, securityHotspotsReviewedMetric, Measure.newMeasureBuilder().create(percent, securityHotspotsReviewedMetric.getDecimalScale()));
78 measureRepository.add(component, securityReviewRatingMetric, RatingMeasures.get(computeRating(percent)));
81 path.parent().add(path.current());
85 private static final class CounterFactory extends PathAwareVisitorAdapter.SimpleStackElementFactory<SecurityReviewCounter> {
86 public static final SecurityReviewMeasuresVisitor.CounterFactory INSTANCE = new SecurityReviewMeasuresVisitor.CounterFactory();
88 private CounterFactory() {
89 // prevents instantiation
93 public SecurityReviewCounter createForAny(Component component) {
94 return new SecurityReviewCounter();