]> source.dussan.org Git - sonarqube.git/blob
df9029b03288569f1371866f8d3cdef8b25a58a7
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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 java.util.Optional;
23 import org.sonar.ce.task.projectanalysis.component.Component;
24 import org.sonar.ce.task.projectanalysis.component.PathAwareVisitor;
25 import org.sonar.ce.task.projectanalysis.component.PathAwareVisitorAdapter;
26 import org.sonar.ce.task.projectanalysis.issue.ComponentIssuesRepository;
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_HOTSPOTS_REVIEWED_STATUS_KEY;
34 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_TO_REVIEW_STATUS_KEY;
35 import static org.sonar.api.measures.CoreMetrics.SECURITY_REVIEW_RATING_KEY;
36 import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT;
37 import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
38 import static org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit.FILE;
39 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
40 import static org.sonar.core.metric.SoftwareQualitiesMetrics.SOFTWARE_QUALITY_SECURITY_REVIEW_RATING_KEY;
41 import static org.sonar.server.security.SecurityReviewRating.computeAToDRating;
42 import static org.sonar.server.security.SecurityReviewRating.computePercent;
43 import static org.sonar.server.security.SecurityReviewRating.computeRating;
44
45 public class SecurityReviewMeasuresVisitor extends PathAwareVisitorAdapter<SecurityReviewCounter> {
46
47   private final ComponentIssuesRepository componentIssuesRepository;
48   private final MeasureRepository measureRepository;
49   private final Metric securityReviewRatingMetric;
50   private final Metric softwareQualitySecurityReviewRatingMetric;
51   private final Metric securityHotspotsReviewedMetric;
52   private final Metric securityHotspotsReviewedStatusMetric;
53   private final Metric securityHotspotsToReviewStatusMetric;
54
55   public SecurityReviewMeasuresVisitor(ComponentIssuesRepository componentIssuesRepository, MeasureRepository measureRepository, MetricRepository metricRepository) {
56     super(FILE, POST_ORDER, SecurityReviewMeasuresVisitor.CounterFactory.INSTANCE);
57     this.componentIssuesRepository = componentIssuesRepository;
58     this.measureRepository = measureRepository;
59     this.securityReviewRatingMetric = metricRepository.getByKey(SECURITY_REVIEW_RATING_KEY);
60     this.softwareQualitySecurityReviewRatingMetric = metricRepository.getByKey(SOFTWARE_QUALITY_SECURITY_REVIEW_RATING_KEY);
61     this.securityHotspotsReviewedMetric = metricRepository.getByKey(SECURITY_HOTSPOTS_REVIEWED_KEY);
62     this.securityHotspotsReviewedStatusMetric = metricRepository.getByKey(SECURITY_HOTSPOTS_REVIEWED_STATUS_KEY);
63     this.securityHotspotsToReviewStatusMetric = metricRepository.getByKey(SECURITY_HOTSPOTS_TO_REVIEW_STATUS_KEY);
64   }
65
66   @Override
67   public void visitProject(Component project, Path<SecurityReviewCounter> path) {
68     computeMeasure(project, path);
69   }
70
71   @Override
72   public void visitDirectory(Component directory, PathAwareVisitor.Path<SecurityReviewCounter> path) {
73     computeMeasure(directory, path);
74   }
75
76   @Override
77   public void visitFile(Component file, PathAwareVisitor.Path<SecurityReviewCounter> path) {
78     computeMeasure(file, path);
79   }
80
81   private void computeMeasure(Component component, PathAwareVisitor.Path<SecurityReviewCounter> path) {
82     componentIssuesRepository.getIssues(component)
83       .stream()
84       .filter(issue -> issue.type().equals(SECURITY_HOTSPOT))
85       .forEach(issue -> path.current().processHotspot(issue));
86
87     measureRepository.add(component, securityHotspotsReviewedStatusMetric, newMeasureBuilder().create(path.current().getHotspotsReviewed()));
88     measureRepository.add(component, securityHotspotsToReviewStatusMetric, newMeasureBuilder().create(path.current().getHotspotsToReview()));
89     Optional<Double> percent = computePercent(path.current().getHotspotsToReview(), path.current().getHotspotsReviewed());
90     measureRepository.add(component, securityReviewRatingMetric, RatingMeasures.get(computeRating(percent.orElse(null))));
91     measureRepository.add(component, softwareQualitySecurityReviewRatingMetric,
92       RatingMeasures.get(computeAToDRating(percent.orElse(null))));
93     percent.ifPresent(p -> measureRepository.add(component, securityHotspotsReviewedMetric, newMeasureBuilder().create(p, securityHotspotsReviewedMetric.getDecimalScale())));
94
95     if (!path.isRoot()) {
96       path.parent().add(path.current());
97     }
98   }
99
100   private static final class CounterFactory extends PathAwareVisitorAdapter.SimpleStackElementFactory<SecurityReviewCounter> {
101     public static final SecurityReviewMeasuresVisitor.CounterFactory INSTANCE = new SecurityReviewMeasuresVisitor.CounterFactory();
102
103     private CounterFactory() {
104       // prevents instantiation
105     }
106
107     @Override
108     public SecurityReviewCounter createForAny(Component component) {
109       return new SecurityReviewCounter();
110     }
111   }
112
113 }