3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.issue;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Date;
25 import java.util.List;
27 import java.util.Optional;
29 import java.util.function.Function;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
32 import org.apache.commons.lang.StringUtils;
33 import org.sonar.api.resources.Qualifiers;
34 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
35 import org.sonar.ce.task.projectanalysis.component.Component;
36 import org.sonar.ce.task.projectanalysis.component.ReportModulesPath;
37 import org.sonar.core.issue.DefaultIssue;
38 import org.sonar.core.issue.IssueChangeContext;
39 import org.sonar.db.DbClient;
40 import org.sonar.db.DbSession;
41 import org.sonar.db.component.ComponentDto;
42 import org.sonar.server.issue.IssueFieldsSetter;
44 import static org.apache.commons.lang.StringUtils.trimToEmpty;
46 class ProjectTrackerBaseLazyInput extends BaseInputFactory.BaseLazyInput {
48 private AnalysisMetadataHolder analysisMetadataHolder;
49 private ComponentsWithUnprocessedIssues componentsWithUnprocessedIssues;
50 private DbClient dbClient;
51 private IssueFieldsSetter issueUpdater;
52 private ComponentIssuesLoader issuesLoader;
53 private ReportModulesPath reportModulesPath;
55 ProjectTrackerBaseLazyInput(AnalysisMetadataHolder analysisMetadataHolder, ComponentsWithUnprocessedIssues componentsWithUnprocessedIssues, DbClient dbClient,
56 IssueFieldsSetter issueUpdater, ComponentIssuesLoader issuesLoader, ReportModulesPath reportModulesPath, Component component) {
57 super(dbClient, component, null);
58 this.analysisMetadataHolder = analysisMetadataHolder;
59 this.componentsWithUnprocessedIssues = componentsWithUnprocessedIssues;
60 this.dbClient = dbClient;
61 this.issueUpdater = issueUpdater;
62 this.issuesLoader = issuesLoader;
63 this.reportModulesPath = reportModulesPath;
67 protected List<DefaultIssue> loadIssues() {
68 List<DefaultIssue> result = new ArrayList<>();
69 try (DbSession dbSession = dbClient.openSession(false)) {
70 Set<String> dirOrModulesUuidsWithIssues = dbClient.issueDao().selectModuleAndDirComponentUuidsOfOpenIssuesForProjectUuid(dbSession, component.getUuid());
71 if (!dirOrModulesUuidsWithIssues.isEmpty()) {
72 Map<String, String> pathByModuleKey = reportModulesPath.get();
73 // Migrate issues that were previously on modules or directories to the root project
74 Map<String, ComponentDto> modulesByUuid = dbClient.componentDao().selectProjectAndModulesFromProjectKey(dbSession, component.getDbKey(), true)
75 .stream().collect(Collectors.toMap(ComponentDto::uuid, Function.identity()));
76 List<ComponentDto> dirOrModulesWithIssues = dbClient.componentDao().selectByUuids(dbSession, dirOrModulesUuidsWithIssues);
77 dirOrModulesWithIssues.forEach(c -> {
78 List<DefaultIssue> issuesOnModuleOrDir = issuesLoader.loadOpenIssues(c.uuid());
79 String moduleOrDirProjectRelativePath = c.qualifier().equals(Qualifiers.MODULE) ? buildModuleProjectRelativePath(pathByModuleKey, c)
80 : buildDirectoryProjectRelativePath(pathByModuleKey, c, modulesByUuid.get(c.moduleUuid()));
81 result.addAll(migrateIssuesToTheRoot(issuesOnModuleOrDir, moduleOrDirProjectRelativePath));
82 componentsWithUnprocessedIssues.remove(c.uuid());
85 result.addAll(issuesLoader.loadOpenIssues(effectiveUuid));
90 private static String buildDirectoryProjectRelativePath(Map<String, String> pathByModuleKey, ComponentDto c, ComponentDto parentModule) {
91 String moduleProjectRelativePath = buildModuleProjectRelativePath(pathByModuleKey, parentModule);
92 return Stream.of(moduleProjectRelativePath, c.path())
93 .map(StringUtils::trimToNull)
94 .filter(s -> s != null && !"/".equals(s))
95 .collect(Collectors.joining("/"));
98 private static String buildModuleProjectRelativePath(Map<String, String> pathByModuleKey, ComponentDto parentModule) {
99 return Optional.ofNullable(pathByModuleKey.get(parentModule.getKey()))
100 // If module is not in the scanner report, we can't guess the path accurately. Fallback on what we have in DB
101 .orElse(trimToEmpty(parentModule.path()));
104 private Collection<? extends DefaultIssue> migrateIssuesToTheRoot(List<DefaultIssue> issuesOnModule, String modulePath) {
105 for (DefaultIssue i : issuesOnModule) {
106 // changes the issue's component uuid, add a change and set issue as changed to enforce it is persisted to DB
107 IssueChangeContext context = IssueChangeContext.createUser(new Date(analysisMetadataHolder.getAnalysisDate()), null);
108 if (StringUtils.isNotBlank(modulePath)) {
109 issueUpdater.setMessage(i, "[" + modulePath + "] " + i.getMessage(), context);
111 issueUpdater.setIssueMoved(i, component.getUuid(), context);
112 // other fields (such as module, modulePath, componentKey) are read-only and set/reset for consistency only
113 i.setComponentKey(component.getKey());
114 i.setModuleUuidPath(null);
116 return issuesOnModule;