diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-02-27 17:22:15 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-02-27 17:27:55 +0100 |
commit | 34bbb693d3012f97fd7041a97a14223fec2445b6 (patch) | |
tree | 0518eec4a7512ce5b40cd9c48e07b8e527d98a9b /sonar-server/src/main | |
parent | 75e7a86e62284c92185824b126e0efc99cda9a4d (diff) | |
download | sonarqube-34bbb693d3012f97fd7041a97a14223fec2445b6.tar.gz sonarqube-34bbb693d3012f97fd7041a97a14223fec2445b6.zip |
Return information on groupId and rootId on issue WS
Diffstat (limited to 'sonar-server/src/main')
6 files changed, 88 insertions, 13 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/issue/DefaultIssueFinder.java b/sonar-server/src/main/java/org/sonar/server/issue/DefaultIssueFinder.java index 5293602aaca..eb3263fcbe8 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/DefaultIssueFinder.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/DefaultIssueFinder.java @@ -19,6 +19,8 @@ */ package org.sonar.server.issue; +import com.google.common.base.Function; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import org.apache.ibatis.session.SqlSession; @@ -32,6 +34,7 @@ import org.sonar.api.rules.Rule; import org.sonar.api.user.User; import org.sonar.api.user.UserFinder; import org.sonar.api.utils.Paging; +import org.sonar.core.component.ComponentDto; import org.sonar.core.issue.DefaultIssueQueryResult; import org.sonar.core.issue.db.IssueChangeDao; import org.sonar.core.issue.db.IssueDao; @@ -42,10 +45,12 @@ import org.sonar.core.rule.DefaultRuleFinder; import org.sonar.server.user.UserSession; import javax.annotation.CheckForNull; + import java.util.*; import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Maps.newHashMap; +import static com.google.common.collect.Sets.newHashSet; /** * @since 3.6 @@ -110,18 +115,16 @@ public class DefaultIssueFinder implements IssueFinder { Map<String, DefaultIssue> issuesByKey = newHashMap(); List<Issue> issues = newArrayList(); - Set<Integer> ruleIds = Sets.newHashSet(); - Set<Long> componentIds = Sets.newHashSet(); - Set<Long> projectIds = Sets.newHashSet(); - Set<String> actionPlanKeys = Sets.newHashSet(); - Set<String> users = Sets.newHashSet(); + Set<Integer> ruleIds = newHashSet(); + Set<Long> componentIds = newHashSet(); + Set<String> actionPlanKeys = newHashSet(); + Set<String> users = newHashSet(); for (IssueDto dto : pagedSortedIssues) { DefaultIssue defaultIssue = dto.toDefaultIssue(); issuesByKey.put(dto.getKee(), defaultIssue); issues.add(defaultIssue); ruleIds.add(dto.getRuleId()); componentIds.add(dto.getComponentId()); - projectIds.add(dto.getRootComponentId()); actionPlanKeys.add(dto.getActionPlanKey()); if (dto.getReporter() != null) { users.add(dto.getReporter()); @@ -139,12 +142,19 @@ public class DefaultIssueFinder implements IssueFinder { } } + Collection<Component> components = findComponents(componentIds); + Collection<Component> groupComponents = findGroupComponents(components); + Collection<Component> rootComponents = findRootComponents(components); + + Set<Component> allComponents = newHashSet(components); + allComponents.addAll(groupComponents); + allComponents.addAll(rootComponents); return new DefaultIssueQueryResult(issues) .setMaxResultsReached(authorizedIssues.size() == query.maxResults()) .addRules(hideRules(query) ? Collections.<Rule>emptyList() : findRules(ruleIds)) - .addComponents(findComponents(componentIds)) - .addProjects(findComponents(projectIds)) + .addComponents(allComponents) + .addProjects(rootComponents) .addActionPlans(findActionPlans(actionPlanKeys)) .addUsers(findUsers(users)) .setPaging(paging); @@ -189,7 +199,25 @@ public class DefaultIssueFinder implements IssueFinder { } private Collection<Component> findComponents(Set<Long> componentIds) { - return resourceDao.findByIds(componentIds); + return Lists.<Component>newArrayList(resourceDao.selectComponentsByIds(componentIds)); + } + + private Collection<Component> findGroupComponents(Collection<Component> components) { + return findComponents(newHashSet(Iterables.transform(components, new Function<Component, Long>() { + @Override + public Long apply(Component input) { + return ((ComponentDto) input).groupId(); + } + }))); + } + + private Collection<Component> findRootComponents(Collection<Component> components) { + return findComponents(newHashSet(Iterables.transform(components, new Function<Component, Long>() { + @Override + public Long apply(Component input) { + return ((ComponentDto) input).rootId(); + } + }))); } private Collection<ActionPlan> findActionPlans(Set<String> actionPlanKeys) { diff --git a/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java b/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java index cca54668f0b..8d137827771 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java @@ -210,6 +210,7 @@ public class IssueService implements ServerComponent { } // Force use of correct key in case deprecated key is used issue.setComponentKey(resourceDto.getKey()); + issue.setComponentId(resourceDto.getId()); if (!authorizationDao.isAuthorizedComponentKey(resourceDto.getKey(), userSession.userId(), UserRole.USER)) { // TODO throw unauthorized throw new IllegalStateException("User does not have the required role"); diff --git a/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowWsHandler.java b/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowWsHandler.java index c153bf67e61..f74135f2e72 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowWsHandler.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowWsHandler.java @@ -19,6 +19,8 @@ */ package org.sonar.server.issue.ws; +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; import org.sonar.api.component.Component; import org.sonar.api.i18n.I18n; import org.sonar.api.issue.*; @@ -33,6 +35,7 @@ import org.sonar.api.user.User; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.text.JsonWriter; import org.sonar.api.web.UserRole; +import org.sonar.core.component.ComponentDto; import org.sonar.core.issue.workflow.Transition; import org.sonar.core.technicaldebt.DefaultTechnicalDebtManager; import org.sonar.markdown.Markdown; @@ -46,6 +49,7 @@ import org.sonar.server.user.UserSession; import javax.annotation.CheckForNull; import javax.annotation.Nullable; + import java.util.Arrays; import java.util.Date; import java.util.List; @@ -95,8 +99,11 @@ public class IssueShowWsHandler implements RequestHandler { } private void writeIssue(IssueQueryResult result, DefaultIssue issue, JsonWriter json) { - Component component = result.component(issue); - Component project = result.project(issue); + // component, module and project can be null if they were removed + ComponentDto component = (ComponentDto) result.component(issue); + ComponentDto module = (ComponentDto) getModule(result, component); + ComponentDto project = (ComponentDto) geProject(result, component); + String actionPlanKey = issue.actionPlanKey(); ActionPlan actionPlan = result.actionPlan(issue); Long technicalDebt = issue.debt(); @@ -108,6 +115,8 @@ public class IssueShowWsHandler implements RequestHandler { .prop("component", issue.componentKey()) .prop("componentLongName", component != null ? component.longName() : null) .prop("componentQualifier", component != null ? component.qualifier() : null) + // Do not display module long name if module and project are the same + .prop("moduleLongName", module != null && project != null && !module.getId().equals(project.getId()) ? module.longName() : null) .prop("project", issue.projectKey()) .prop("projectLongName", project != null ? project.longName() : null) .prop("rule", issue.ruleKey().toString()) @@ -134,6 +143,39 @@ public class IssueShowWsHandler implements RequestHandler { addCharacteristics(result, issue, json); } + /** + * Can be null on project or on removed component + */ + @CheckForNull + private Component getModule(IssueQueryResult result, @Nullable final ComponentDto component){ + if (component != null) { + return Iterables.find(result.components(), new Predicate<Component>() { + @Override + public boolean apply(Component input) { + Long groupId = component.groupId(); + return groupId != null && groupId.equals(((ComponentDto) input).getId()); + } + }, null); + } + return null; + } + + /** + * Can be null on removed component + */ + @CheckForNull + private Component geProject(IssueQueryResult result, @Nullable final ComponentDto component){ + if (component != null) { + return Iterables.find(result.components(), new Predicate<Component>() { + @Override + public boolean apply(Component input) { + return component.rootId().equals(((ComponentDto) input).getId()); + } + }, null); + } + return null; + } + private void addCharacteristics(IssueQueryResult result, DefaultIssue issue, JsonWriter json) { Characteristic requirement = technicalDebtManager.findRequirementByRule(result.rule(issue)); // Requirement can be null if it has been disabled diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/issues_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/issues_controller.rb index 2df0551a2e5..2eae8b6625b 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/issues_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/issues_controller.rb @@ -338,12 +338,15 @@ class Api::IssuesController < Api::ApiController def component_to_hash(component) hash = { :key => component.key, + :id => component.id, :qualifier => component.qualifier } hash[:name] = component.name if component.name hash[:longName] = component.longName if component.longName hash[:path] = component.path if component.path - hash[:moduleKey] = component.moduleKey if component.moduleKey + hash[:groupId] = component.groupId if component.groupId + # On a root project, groupId is null but rootId is equal to itself, which make no sense. + hash[:rootId] = component.rootId if component.groupId && component.rootId hash end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/issue.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/issue.rb index 50d118fbec4..59b3edb73d6 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/issue.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/issue.rb @@ -24,6 +24,7 @@ class Issue hash = { :key => issue.key, :component => issue.componentKey, + :componentId => issue.componentId, :project => issue.projectKey, :rule => issue.ruleKey.toString(), :status => issue.status diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/issue/_show.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/issue/_show.html.erb index 54f8f0ea89a..0e3ac01900a 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/issue/_show.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/issue/_show.html.erb @@ -5,7 +5,7 @@ component = @issue_results.component(@issue) %> - <% if @issue.componentKey() != project.key() %> + <% if project && @issue.componentKey() != project.key() %> <div class="subtitle"> <%= h project.longName() -%> </div> |