]> source.dussan.org Git - sonarqube.git/blob
2bb1fdc3d3fc26b1d7ead0568599d88426b7897d
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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.ce.task.projectanalysis.qualitymodel;
21
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;
31
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;
39
40 public class SecurityReviewMeasuresVisitor extends PathAwareVisitorAdapter<SecurityReviewCounter> {
41
42   private final ComponentIssuesRepository componentIssuesRepository;
43   private final MeasureRepository measureRepository;
44   private final Metric securityReviewRatingMetric;
45   private final Metric securityHotspotsReviewedMetric;
46
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);
53   }
54
55   @Override
56   public void visitProject(Component project, Path<SecurityReviewCounter> path) {
57     computeMeasure(project, path);
58   }
59
60   @Override
61   public void visitDirectory(Component directory, PathAwareVisitor.Path<SecurityReviewCounter> path) {
62     computeMeasure(directory, path);
63   }
64
65   @Override
66   public void visitFile(Component file, PathAwareVisitor.Path<SecurityReviewCounter> path) {
67     computeMeasure(file, path);
68   }
69
70   private void computeMeasure(Component component, PathAwareVisitor.Path<SecurityReviewCounter> path) {
71     componentIssuesRepository.getIssues(component)
72       .stream()
73       .filter(issue -> issue.type().equals(SECURITY_HOTSPOT))
74       .forEach(issue -> path.current().processHotspot(issue));
75
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)));
79
80     if (!path.isRoot()) {
81       path.parent().add(path.current());
82     }
83   }
84
85   private static final class CounterFactory extends PathAwareVisitorAdapter.SimpleStackElementFactory<SecurityReviewCounter> {
86     public static final SecurityReviewMeasuresVisitor.CounterFactory INSTANCE = new SecurityReviewMeasuresVisitor.CounterFactory();
87
88     private CounterFactory() {
89       // prevents instantiation
90     }
91
92     @Override
93     public SecurityReviewCounter createForAny(Component component) {
94       return new SecurityReviewCounter();
95     }
96   }
97
98 }