You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IssuesAction.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.server.batch;
  21. import org.sonar.api.resources.Scopes;
  22. import org.sonar.api.server.ws.Request;
  23. import org.sonar.api.server.ws.Response;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.batch.protocol.input.BatchInput;
  26. import org.sonar.db.component.ComponentDto;
  27. import org.sonar.core.permission.GlobalPermissions;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.MyBatis;
  30. import org.sonar.server.db.DbClient;
  31. import org.sonar.server.issue.index.IssueDoc;
  32. import org.sonar.server.issue.index.IssueIndex;
  33. import org.sonar.server.plugins.MimeTypes;
  34. import org.sonar.server.user.UserSession;
  35. import java.io.IOException;
  36. import java.io.OutputStream;
  37. import java.util.Iterator;
  38. import java.util.List;
  39. import java.util.Map;
  40. import static com.google.common.collect.Maps.newHashMap;
  41. public class IssuesAction implements BatchWsAction {
  42. private static final String PARAM_KEY = "key";
  43. private final DbClient dbClient;
  44. private final IssueIndex issueIndex;
  45. private final UserSession userSession;
  46. public IssuesAction(DbClient dbClient, IssueIndex issueIndex, UserSession userSession) {
  47. this.dbClient = dbClient;
  48. this.issueIndex = issueIndex;
  49. this.userSession = userSession;
  50. }
  51. @Override
  52. public void define(WebService.NewController controller) {
  53. WebService.NewAction action = controller.createAction("issues")
  54. .setDescription("Return open issues")
  55. .setSince("5.1")
  56. .setInternal(true)
  57. .setHandler(this);
  58. action
  59. .createParam(PARAM_KEY)
  60. .setRequired(true)
  61. .setDescription("Project, module or file key")
  62. .setExampleValue("org.codehaus.sonar:sonar");
  63. }
  64. @Override
  65. public void handle(Request request, Response response) throws Exception {
  66. userSession.checkGlobalPermission(GlobalPermissions.PREVIEW_EXECUTION);
  67. final String moduleKey = request.mandatoryParam(PARAM_KEY);
  68. response.stream().setMediaType(MimeTypes.PROTOBUF);
  69. DbSession session = dbClient.openSession(false);
  70. try {
  71. ComponentDto component = dbClient.componentDao().selectByKey(session, moduleKey);
  72. Map<String, String> keysByUUid = keysByUUid(session, component);
  73. BatchInput.ServerIssue.Builder issueBuilder = BatchInput.ServerIssue.newBuilder();
  74. for (Iterator<IssueDoc> issueDocIterator = issueIndex.selectIssuesForBatch(component); issueDocIterator.hasNext();) {
  75. handleIssue(issueDocIterator.next(), issueBuilder, keysByUUid, response.stream().output());
  76. }
  77. } finally {
  78. MyBatis.closeQuietly(session);
  79. }
  80. }
  81. private void handleIssue(IssueDoc issue, BatchInput.ServerIssue.Builder issueBuilder, Map<String, String> keysByUUid, OutputStream out) {
  82. issueBuilder.setKey(issue.key());
  83. issueBuilder.setModuleKey(keysByUUid.get(issue.moduleUuid()));
  84. String path = issue.filePath();
  85. if (path != null) {
  86. issueBuilder.setPath(path);
  87. }
  88. issueBuilder.setRuleRepository(issue.ruleKey().repository());
  89. issueBuilder.setRuleKey(issue.ruleKey().rule());
  90. String checksum = issue.checksum();
  91. if (checksum != null) {
  92. issueBuilder.setChecksum(checksum);
  93. }
  94. String assigneeLogin = issue.assignee();
  95. if (assigneeLogin != null) {
  96. issueBuilder.setAssigneeLogin(assigneeLogin);
  97. }
  98. Integer line = issue.line();
  99. if (line != null) {
  100. issueBuilder.setLine(line);
  101. }
  102. String message = issue.message();
  103. if (message != null) {
  104. issueBuilder.setMsg(message);
  105. }
  106. issueBuilder.setSeverity(org.sonar.batch.protocol.Constants.Severity.valueOf(issue.severity()));
  107. issueBuilder.setManualSeverity(issue.isManualSeverity());
  108. issueBuilder.setStatus(issue.status());
  109. String resolution = issue.resolution();
  110. if (resolution != null) {
  111. issueBuilder.setResolution(resolution);
  112. }
  113. issueBuilder.setCreationDate(issue.creationDate().getTime());
  114. try {
  115. issueBuilder.build().writeDelimitedTo(out);
  116. } catch (IOException e) {
  117. throw new IllegalStateException("Unable to serialize issue", e);
  118. }
  119. issueBuilder.clear();
  120. }
  121. private Map<String, String> keysByUUid(DbSession session, ComponentDto component) {
  122. Map<String, String> keysByUUid = newHashMap();
  123. if (Scopes.PROJECT.equals(component.scope())) {
  124. List<ComponentDto> modulesTree = dbClient.componentDao().selectDescendantModules(session, component.uuid());
  125. for (ComponentDto componentDto : modulesTree) {
  126. keysByUUid.put(componentDto.uuid(), componentDto.key());
  127. }
  128. } else {
  129. String moduleUuid = component.moduleUuid();
  130. if (moduleUuid == null) {
  131. throw new IllegalArgumentException(String.format("The component '%s' has no module uuid", component.uuid()));
  132. }
  133. ComponentDto module = dbClient.componentDao().selectByUuid(session, moduleUuid);
  134. keysByUUid.put(module.uuid(), module.key());
  135. }
  136. return keysByUUid;
  137. }
  138. }