/
ProjectRepositoryLoader.java
325 lines (286 loc) · 14.1 KB
/
ProjectRepositoryLoader.java
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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.server.batch;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import org.sonar.api.ServerComponent;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.rule.RuleKey;
import org.sonar.batch.protocol.input.FileData;
import org.sonar.batch.protocol.input.ProjectRepositories;
import org.sonar.core.UtcDateUtils;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.component.FilePathWithHashDto;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.properties.PropertyDto;
import org.sonar.core.qualityprofile.db.QualityProfileDto;
import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.qualityprofile.ActiveRule;
import org.sonar.server.qualityprofile.QProfileFactory;
import org.sonar.server.qualityprofile.QProfileLoader;
import org.sonar.server.rule.Rule;
import org.sonar.server.rule.RuleService;
import org.sonar.server.rule.index.RuleNormalizer;
import org.sonar.server.rule.index.RuleQuery;
import org.sonar.server.search.QueryContext;
import org.sonar.server.search.Result;
import org.sonar.server.user.UserSession;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;
public class ProjectRepositoryLoader implements ServerComponent {
private final DbClient dbClient;
private final QProfileFactory qProfileFactory;
private final QProfileLoader qProfileLoader;
private final RuleService ruleService;
private final Languages languages;
public ProjectRepositoryLoader(DbClient dbClient, QProfileFactory qProfileFactory, QProfileLoader qProfileLoader, RuleService ruleService,
Languages languages) {
this.dbClient = dbClient;
this.qProfileFactory = qProfileFactory;
this.qProfileLoader = qProfileLoader;
this.ruleService = ruleService;
this.languages = languages;
}
public ProjectRepositories load(ProjectRepositoryQuery query) {
boolean hasScanPerm = UserSession.get().hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION);
checkPermission(query.isPreview());
DbSession session = dbClient.openSession(false);
try {
ProjectRepositories ref = new ProjectRepositories();
String projectKey = query.getModuleKey();
ComponentDto module = dbClient.componentDao().getNullableByKey(session, query.getModuleKey());
// Current project/module can be null when analysing a new project
if (module != null) {
ComponentDto project = getProject(module, session);
if (!project.key().equals(module.key())) {
addSettings(ref, module.getKey(), getSettingsFromParents(module, hasScanPerm, session));
projectKey = project.key();
}
List<ComponentDto> modulesTree = dbClient.componentDao().selectModulesTree(session, module.uuid());
Map<String, String> moduleUuidsByKey = moduleUuidsByKey(module, modulesTree);
Map<String, Long> moduleIdsByKey = moduleIdsByKey(module, modulesTree);
List<PropertyDto> modulesTreeSettings = dbClient.propertiesDao().selectModulePropertiesTree(module.uuid(), session);
TreeModuleSettings treeModuleSettings = new TreeModuleSettings(moduleUuidsByKey, moduleIdsByKey, modulesTree, modulesTreeSettings, module);
addSettingsToChildrenModules(ref, query.getModuleKey(), Maps.<String, String>newHashMap(), treeModuleSettings, hasScanPerm, session);
addFileData(session, ref, modulesTree, module.uuid());
}
addProfiles(ref, projectKey, query.getProfileName(), session);
addActiveRules(ref);
addManualRules(ref);
return ref;
} finally {
MyBatis.closeQuietly(session);
}
}
private ComponentDto getProject(ComponentDto module, DbSession session) {
if (!module.isRootProject()) {
return dbClient.componentDao().getNullableByUuid(session, module.projectUuid());
} else {
return module;
}
}
private Map<String, String> getSettingsFromParents(ComponentDto module, boolean hasScanPerm, DbSession session) {
List<ComponentDto> parents = newArrayList();
aggregateParentModules(module, parents, session);
Collections.reverse(parents);
Map<String, String> parentProperties = newHashMap();
for (ComponentDto parent : parents) {
parentProperties.putAll(getPropertiesMap(dbClient.propertiesDao().selectProjectProperties(parent.key(), session), hasScanPerm));
}
return parentProperties;
}
private void aggregateParentModules(ComponentDto component, List<ComponentDto> parents, DbSession session) {
String moduleUuid = component.moduleUuid();
if (moduleUuid != null) {
ComponentDto parent = dbClient.componentDao().getByUuid(session, moduleUuid);
if (parent != null) {
parents.add(parent);
aggregateParentModules(parent, parents, session);
}
}
}
private void addSettingsToChildrenModules(ProjectRepositories ref, String moduleKey, Map<String, String> parentProperties, TreeModuleSettings treeModuleSettings,
boolean hasScanPerm, DbSession session) {
Map<String, String> currentParentProperties = newHashMap();
currentParentProperties.putAll(parentProperties);
currentParentProperties.putAll(getPropertiesMap(treeModuleSettings.findModuleSettings(moduleKey), hasScanPerm));
addSettings(ref, moduleKey, currentParentProperties);
for (ComponentDto childModule : treeModuleSettings.findChildrenModule(moduleKey)) {
addSettings(ref, childModule.getKey(), currentParentProperties);
addSettingsToChildrenModules(ref, childModule.getKey(), currentParentProperties, treeModuleSettings, hasScanPerm, session);
}
}
private void addSettings(ProjectRepositories ref, String module, Map<String, String> properties) {
if (!properties.isEmpty()) {
ref.addSettings(module, properties);
}
}
private Map<String, String> getPropertiesMap(List<PropertyDto> propertyDtos, boolean hasScanPerm) {
Map<String, String> properties = newHashMap();
for (PropertyDto propertyDto : propertyDtos) {
String key = propertyDto.getKey();
String value = propertyDto.getValue();
if (isPropertyAllowed(key, hasScanPerm)) {
properties.put(key, value);
}
}
return properties;
}
private static boolean isPropertyAllowed(String key, boolean hasScanPerm) {
return !key.contains(".secured") || hasScanPerm;
}
private void addProfiles(ProjectRepositories ref, @Nullable String projectKey, @Nullable String profileName, DbSession session) {
for (Language language : languages.all()) {
String languageKey = language.getKey();
QualityProfileDto qualityProfileDto = getProfile(languageKey, projectKey, profileName, session);
ref.addQProfile(new org.sonar.batch.protocol.input.QProfile(
qualityProfileDto.getKey(),
qualityProfileDto.getName(),
qualityProfileDto.getLanguage(),
UtcDateUtils.parseDateTime(qualityProfileDto.getRulesUpdatedAt())));
}
}
/**
* First try to find a quality profile matching the given name (if provided) and current language
* If no profile found, try to find the quality profile set on the project (if provided)
* If still no profile found, try to find the default profile of the language
* <p/>
* Never return null because a default profile should always be set on ech language
*/
private QualityProfileDto getProfile(String languageKey, @Nullable String projectKey, @Nullable String profileName, DbSession session) {
QualityProfileDto qualityProfileDto = profileName != null ? qProfileFactory.getByNameAndLanguage(session, profileName, languageKey) : null;
if (qualityProfileDto == null && projectKey != null) {
qualityProfileDto = qProfileFactory.getByProjectAndLanguage(session, projectKey, languageKey);
}
qualityProfileDto = qualityProfileDto != null ? qualityProfileDto : qProfileFactory.getDefault(session, languageKey);
if (qualityProfileDto != null) {
return qualityProfileDto;
} else {
throw new IllegalStateException(String.format("No quality profile can been found on language '%s' for project '%s'", languageKey, projectKey));
}
}
private void addActiveRules(ProjectRepositories ref) {
for (org.sonar.batch.protocol.input.QProfile qProfile : ref.qProfiles()) {
for (ActiveRule activeRule : qProfileLoader.findActiveRulesByProfile(qProfile.key())) {
Rule rule = ruleService.getNonNullByKey(activeRule.key().ruleKey());
org.sonar.batch.protocol.input.ActiveRule inputActiveRule = new org.sonar.batch.protocol.input.ActiveRule(
activeRule.key().ruleKey().repository(),
activeRule.key().ruleKey().rule(),
rule.name(),
activeRule.severity(),
rule.internalKey(),
qProfile.language());
for (Map.Entry<String, String> entry : activeRule.params().entrySet()) {
inputActiveRule.addParam(entry.getKey(), entry.getValue());
}
ref.addActiveRule(inputActiveRule);
}
}
}
private void addManualRules(ProjectRepositories ref) {
Result<Rule> ruleSearchResult = ruleService.search(new RuleQuery().setRepositories(newArrayList(RuleKey.MANUAL_REPOSITORY_KEY)), new QueryContext().setScroll(true)
.setFieldsToReturn(newArrayList(RuleNormalizer.RuleField.KEY.field(), RuleNormalizer.RuleField.NAME.field())));
Iterator<Rule> rules = ruleSearchResult.scroll();
while (rules.hasNext()) {
Rule rule = rules.next();
ref.addActiveRule(new org.sonar.batch.protocol.input.ActiveRule(
RuleKey.MANUAL_REPOSITORY_KEY,
rule.key().rule(),
rule.name(),
null, null, null));
}
}
private void addFileData(DbSession session, ProjectRepositories ref, List<ComponentDto> moduleChildren, String moduleKey) {
Map<String, String> moduleKeysByUuid = newHashMap();
for (ComponentDto module : moduleChildren) {
moduleKeysByUuid.put(module.uuid(), module.key());
}
for (FilePathWithHashDto file : dbClient.componentDao().selectModuleFilesTree(session, moduleKey)) {
FileData fileData = new FileData(file.getSrcHash(), false, null, null, null);
ref.addFileData(moduleKeysByUuid.get(file.getModuleUuid()), file.getPath(), fileData);
}
}
private void checkPermission(boolean preview) {
UserSession userSession = UserSession.get();
boolean hasScanPerm = userSession.hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION);
boolean hasPreviewPerm = userSession.hasGlobalPermission(GlobalPermissions.PREVIEW_EXECUTION);
if (!hasPreviewPerm && !hasScanPerm) {
throw new ForbiddenException("You're not authorized to execute any SonarQube analysis. Please contact your SonarQube administrator.");
}
if (!preview && !hasScanPerm) {
throw new ForbiddenException("You're only authorized to execute a local (dry run) SonarQube analysis without pushing the results to the SonarQube server. " +
"Please contact your SonarQube administrator.");
}
}
private Map<String, String> moduleUuidsByKey(ComponentDto module, List<ComponentDto> moduleChildren) {
Map<String, String> moduleUuidsByKey = newHashMap();
for (ComponentDto componentDto : moduleChildren) {
moduleUuidsByKey.put(componentDto.key(), componentDto.uuid());
}
return moduleUuidsByKey;
}
private Map<String, Long> moduleIdsByKey(ComponentDto module, List<ComponentDto> moduleChildren) {
Map<String, Long> moduleIdsByKey = newHashMap();
for (ComponentDto componentDto : moduleChildren) {
moduleIdsByKey.put(componentDto.key(), componentDto.getId());
}
return moduleIdsByKey;
}
private static class TreeModuleSettings {
private Map<String, Long> moduleIdsByKey;
private Map<String, String> moduleUuidsByKey;
private Multimap<Long, PropertyDto> propertiesByModuleId;
private Multimap<String, ComponentDto> moduleChildrenByModuleUuid;
private TreeModuleSettings(Map<String, String> moduleUuidsByKey, Map<String, Long> moduleIdsByKey, List<ComponentDto> moduleChildren,
List<PropertyDto> moduleChildrenSettings, ComponentDto module) {
this.moduleIdsByKey = moduleIdsByKey;
this.moduleUuidsByKey = moduleUuidsByKey;
propertiesByModuleId = ArrayListMultimap.create();
moduleChildrenByModuleUuid = ArrayListMultimap.create();
for (PropertyDto settings : moduleChildrenSettings) {
propertiesByModuleId.put(settings.getResourceId(), settings);
}
for (ComponentDto componentDto : moduleChildren) {
String moduleUuid = componentDto.moduleUuid();
if (moduleUuid != null) {
moduleChildrenByModuleUuid.put(moduleUuid, componentDto);
}
}
}
List<PropertyDto> findModuleSettings(String moduleKey) {
Long moduleId = moduleIdsByKey.get(moduleKey);
return newArrayList(propertiesByModuleId.get(moduleId));
}
List<ComponentDto> findChildrenModule(String moduleKey) {
String moduleUuid = moduleUuidsByKey.get(moduleKey);
return newArrayList(moduleChildrenByModuleUuid.get(moduleUuid));
}
}
}