]> source.dussan.org Git - sonarqube.git/blob
6ecb9f0c3e7c8c6120d7199a3c41088632184cbe
[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.Map;
23 import org.sonar.api.issue.impact.Severity;
24 import org.sonar.api.issue.impact.SoftwareQuality;
25 import org.sonar.api.measures.CoreMetrics;
26 import org.sonar.ce.task.projectanalysis.component.Component;
27 import org.sonar.ce.task.projectanalysis.component.PathAwareVisitorAdapter;
28 import org.sonar.ce.task.projectanalysis.formula.counter.RatingValue;
29 import org.sonar.ce.task.projectanalysis.issue.ComponentIssuesRepository;
30 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
31 import org.sonar.ce.task.projectanalysis.measure.RatingMeasures;
32 import org.sonar.ce.task.projectanalysis.metric.Metric;
33 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
34 import org.sonar.core.issue.DefaultIssue;
35 import org.sonar.server.measure.Rating;
36 import org.sonar.server.metric.SoftwareQualitiesMetrics;
37
38 import static org.sonar.api.measures.CoreMetrics.RELIABILITY_RATING_KEY;
39 import static org.sonar.api.measures.CoreMetrics.SECURITY_RATING_KEY;
40 import static org.sonar.api.rules.RuleType.BUG;
41 import static org.sonar.api.rules.RuleType.VULNERABILITY;
42 import static org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit.FILE;
43 import static org.sonar.server.measure.Rating.RATING_BY_SEVERITY;
44 import static org.sonar.server.metric.SoftwareQualitiesMetrics.SOFTWARE_QUALITY_RELIABILITY_RATING_KEY;
45 import static org.sonar.server.metric.SoftwareQualitiesMetrics.SOFTWARE_QUALITY_SECURITY_RATING_KEY;
46
47 /**
48  * Compute following measures for projects and descendants:
49  * {@link CoreMetrics#RELIABILITY_RATING_KEY}
50  * {@link CoreMetrics#SECURITY_RATING_KEY}
51  * {@link SoftwareQualitiesMetrics#SOFTWARE_QUALITY_RELIABILITY_RATING_KEY}
52  * {@link SoftwareQualitiesMetrics#SOFTWARE_QUALITY_SECURITY_RATING_KEY}
53  */
54 public class ReliabilityAndSecurityRatingMeasuresVisitor extends PathAwareVisitorAdapter<ReliabilityAndSecurityRatingMeasuresVisitor.Counter> {
55
56   private final MeasureRepository measureRepository;
57   private final ComponentIssuesRepository componentIssuesRepository;
58   private final Map<String, Metric> metricsByKey;
59
60   public ReliabilityAndSecurityRatingMeasuresVisitor(MetricRepository metricRepository, MeasureRepository measureRepository, ComponentIssuesRepository componentIssuesRepository) {
61     super(FILE, Order.POST_ORDER, CounterFactory.INSTANCE);
62     this.measureRepository = measureRepository;
63     this.componentIssuesRepository = componentIssuesRepository;
64
65     // Output metrics
66     Metric reliabilityRatingMetric = metricRepository.getByKey(RELIABILITY_RATING_KEY);
67     Metric securityRatingMetric = metricRepository.getByKey(SECURITY_RATING_KEY);
68     Metric softwareQualityReliabilityRatingMetric = metricRepository.getByKey(SOFTWARE_QUALITY_RELIABILITY_RATING_KEY);
69     Metric softwareQualitySecurityRatingMetric = metricRepository.getByKey(SOFTWARE_QUALITY_SECURITY_RATING_KEY);
70
71     this.metricsByKey = Map.of(
72       RELIABILITY_RATING_KEY, reliabilityRatingMetric,
73       SECURITY_RATING_KEY, securityRatingMetric,
74       SOFTWARE_QUALITY_RELIABILITY_RATING_KEY, softwareQualityReliabilityRatingMetric,
75       SOFTWARE_QUALITY_SECURITY_RATING_KEY, softwareQualitySecurityRatingMetric)
76     ;
77   }
78
79   @Override
80   public void visitProject(Component project, Path<Counter> path) {
81     computeAndSaveMeasures(project, path);
82   }
83
84   @Override
85   public void visitDirectory(Component directory, Path<Counter> path) {
86     computeAndSaveMeasures(directory, path);
87   }
88
89   @Override
90   public void visitFile(Component file, Path<Counter> path) {
91     computeAndSaveMeasures(file, path);
92   }
93
94   private void computeAndSaveMeasures(Component component, Path<Counter> path) {
95     processIssues(component, path);
96     path.current().ratingValueByMetric.forEach((key, value) -> {
97       Rating rating = value.getValue();
98       measureRepository.add(component, metricsByKey.get(key), RatingMeasures.get(rating));
99     });
100     if (!path.isRoot()) {
101       path.parent().add(path.current());
102     }
103   }
104
105   private void processIssues(Component component, Path<Counter> path) {
106     componentIssuesRepository.getIssues(component)
107       .stream()
108       .filter(issue -> issue.resolution() == null)
109       .forEach(issue -> {
110         processIssue(path, issue);
111       });
112   }
113
114   private static void processIssue(Path<Counter> path, DefaultIssue issue) {
115     Rating rating = RATING_BY_SEVERITY.get(issue.severity());
116     if (issue.type().equals(BUG)) {
117       path.current().ratingValueByMetric.get(RELIABILITY_RATING_KEY).increment(rating);
118     } else if (issue.type().equals(VULNERABILITY)) {
119       path.current().ratingValueByMetric.get(SECURITY_RATING_KEY).increment(rating);
120     }
121
122     processSoftwareQualityRating(path, issue, SoftwareQuality.RELIABILITY, SOFTWARE_QUALITY_RELIABILITY_RATING_KEY);
123     processSoftwareQualityRating(path, issue, SoftwareQuality.SECURITY, SOFTWARE_QUALITY_SECURITY_RATING_KEY);
124   }
125
126   private static void processSoftwareQualityRating(Path<Counter> path, DefaultIssue issue, SoftwareQuality softwareQuality, String metricKey) {
127     Severity severity = issue.impacts().get(softwareQuality);
128     if (severity != null) {
129       Rating rating = Rating.RATING_BY_SOFTWARE_QUALITY_SEVERITY.get(severity);
130
131       if (rating != null) {
132         path.current().ratingValueByMetric.get(metricKey).increment(rating);
133       }
134     }
135   }
136
137   static final class Counter {
138     private final Map<String, RatingValue> ratingValueByMetric = Map.of(
139       RELIABILITY_RATING_KEY, new RatingValue(),
140       SECURITY_RATING_KEY, new RatingValue(),
141       SOFTWARE_QUALITY_RELIABILITY_RATING_KEY, new RatingValue(),
142       SOFTWARE_QUALITY_SECURITY_RATING_KEY, new RatingValue());
143
144     private Counter() {
145       // prevents instantiation
146     }
147
148     void add(Counter otherCounter) {
149       ratingValueByMetric.forEach((key, value) -> value.increment(otherCounter.ratingValueByMetric.get(key)));
150     }
151
152   }
153
154   private static final class CounterFactory extends PathAwareVisitorAdapter.SimpleStackElementFactory<ReliabilityAndSecurityRatingMeasuresVisitor.Counter> {
155     public static final CounterFactory INSTANCE = new CounterFactory();
156
157     private CounterFactory() {
158       // prevents instantiation
159     }
160
161     @Override
162     public Counter createForAny(Component component) {
163       return new Counter();
164     }
165   }
166 }