1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.scanner.rule;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.sonar.api.issue.impact.Severity;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.DateUtils;
import org.sonar.scanner.http.ScannerWsClient;
import org.sonarqube.ws.client.GetRequest;
import static java.util.Optional.ofNullable;
public class DefaultActiveRulesLoader implements ActiveRulesLoader {
private static final String RULES_ACTIVE_URL = "/api/v2/analysis/active_rules?";
private final ScannerWsClient wsClient;
public DefaultActiveRulesLoader(ScannerWsClient wsClient) {
this.wsClient = wsClient;
}
@Override
public List<LoadedActiveRule> load(String projectKey) {
GetRequest getRequest = new GetRequest(getUrl(projectKey));
List<ActiveRuleGson> jsonResponse;
try (Reader reader = wsClient.call(getRequest).contentReader()) {
jsonResponse = new Gson().fromJson(reader, new TypeToken<ArrayList<ActiveRuleGson>>() {
}.getType());
} catch (Exception e) {
throw new IllegalStateException("Unable to load active rules", e);
}
return convert(jsonResponse);
}
private static String getUrl(String projectKey) {
return RULES_ACTIVE_URL + "projectKey=" + projectKey;
}
private static List<LoadedActiveRule> convert(List<ActiveRuleGson> activeRuleGsonList) {
return activeRuleGsonList.stream()
.map(DefaultActiveRulesLoader::convertActiveRule)
.toList();
}
private static LoadedActiveRule convertActiveRule(ActiveRuleGson activeRule) {
LoadedActiveRule loadedRule = new LoadedActiveRule();
loadedRule.setRuleKey(convertRuleKey(activeRule.ruleKey()));
loadedRule.setName(activeRule.name());
loadedRule.setSeverity(activeRule.severity());
loadedRule.setCreatedAt(DateUtils.dateToLong(DateUtils.parseDateTime(activeRule.createdAt())));
loadedRule.setUpdatedAt(DateUtils.dateToLong(DateUtils.parseDateTime(activeRule.updatedAt())));
loadedRule.setLanguage(activeRule.language());
loadedRule.setInternalKey(activeRule.internalKey());
loadedRule.setQProfileKey(activeRule.qProfileKey());
ofNullable(activeRule.templateRuleKey())
.map(RuleKey::parse)
.map(RuleKey::rule)
.ifPresent(loadedRule::setTemplateRuleKey);
loadedRule.setParams(activeRule.params() != null ? convertParams(activeRule.params()) : Map.of());
loadedRule.setImpacts(activeRule.impacts() != null ? activeRule.impacts() : Map.of());
loadedRule.setDeprecatedKeys(convertDeprecatedKeys(activeRule.deprecatedKeys()));
return loadedRule;
}
private static Map<String, String> convertParams(List<ParamGson> params) {
return params.stream().collect(Collectors.toMap(ParamGson::key, ParamGson::value));
}
private static Set<RuleKey> convertDeprecatedKeys(@Nullable List<RuleKeyGson> deprecatedKeysList) {
return ofNullable(deprecatedKeysList)
.orElse(List.of())
.stream()
.map(value -> RuleKey.of(value.repository(), value.rule()))
.collect(Collectors.toSet());
}
private static RuleKey convertRuleKey(RuleKeyGson ruleKey) {
return RuleKey.of(ruleKey.repository(), ruleKey.rule());
}
record ActiveRuleGson(
@SerializedName("ruleKey") RuleKeyGson ruleKey,
@SerializedName("name") String name,
@SerializedName("severity") String severity,
@SerializedName("createdAt") String createdAt,
@SerializedName("updatedAt") String updatedAt,
@SerializedName("internalKey") @Nullable String internalKey,
@SerializedName("language") String language,
@SerializedName("templateRuleKey") @Nullable String templateRuleKey,
@SerializedName("qProfileKey") String qProfileKey,
@SerializedName("deprecatedKeys") @Nullable List<RuleKeyGson> deprecatedKeys,
@SerializedName("params") @Nullable List<ParamGson> params,
@SerializedName("impacts") @Nullable Map<SoftwareQuality, Severity> impacts) {
}
record RuleKeyGson(@SerializedName("repository") String repository, @SerializedName("rule") String rule) {
}
record ParamGson(@SerializedName("key") String key, @SerializedName("value") String value) {
}
}
|