您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ListAction.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  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.branch.pr.ws;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Objects;
  24. import java.util.Optional;
  25. import java.util.function.Function;
  26. import javax.annotation.Nullable;
  27. import org.sonar.api.server.ws.Request;
  28. import org.sonar.api.server.ws.Response;
  29. import org.sonar.api.server.ws.WebService;
  30. import org.sonar.api.web.UserRole;
  31. import org.sonar.db.DbClient;
  32. import org.sonar.db.DbSession;
  33. import org.sonar.db.component.BranchDto;
  34. import org.sonar.db.component.ComponentDto;
  35. import org.sonar.db.component.SnapshotDto;
  36. import org.sonar.db.protobuf.DbProjectBranches;
  37. import org.sonar.server.component.ComponentFinder;
  38. import org.sonar.server.issue.index.BranchStatistics;
  39. import org.sonar.server.issue.index.IssueIndex;
  40. import org.sonar.server.user.UserSession;
  41. import org.sonarqube.ws.ProjectPullRequests;
  42. import static com.google.common.base.Preconditions.checkArgument;
  43. import static java.util.Objects.requireNonNull;
  44. import static org.sonar.api.resources.Qualifiers.PROJECT;
  45. import static org.sonar.api.utils.DateUtils.formatDateTime;
  46. import static org.sonar.core.util.Protobuf.setNullable;
  47. import static org.sonar.core.util.stream.MoreCollectors.toList;
  48. import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
  49. import static org.sonar.db.component.BranchType.PULL_REQUEST;
  50. import static org.sonar.server.branch.pr.ws.PullRequestsWs.addProjectParam;
  51. import static org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PROJECT;
  52. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  53. public class ListAction implements PullRequestWsAction {
  54. private final DbClient dbClient;
  55. private final UserSession userSession;
  56. private final ComponentFinder componentFinder;
  57. private final IssueIndex issueIndex;
  58. public ListAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder, IssueIndex issueIndex) {
  59. this.dbClient = dbClient;
  60. this.userSession = userSession;
  61. this.componentFinder = componentFinder;
  62. this.issueIndex = issueIndex;
  63. }
  64. @Override
  65. public void define(WebService.NewController context) {
  66. WebService.NewAction action = context.createAction("list")
  67. .setSince("7.1")
  68. .setDescription("List the pull requests of a project.<br/>" +
  69. "Requires 'Administer' rights on the specified project.")
  70. .setResponseExample(getClass().getResource("list-example.json"))
  71. .setHandler(this);
  72. addProjectParam(action);
  73. }
  74. @Override
  75. public void handle(Request request, Response response) throws Exception {
  76. String projectKey = request.mandatoryParam(PARAM_PROJECT);
  77. try (DbSession dbSession = dbClient.openSession(false)) {
  78. ComponentDto project = componentFinder.getByKey(dbSession, projectKey);
  79. userSession.checkComponentPermission(UserRole.USER, project);
  80. checkArgument(project.isEnabled() && PROJECT.equals(project.qualifier()), "Invalid project key");
  81. List<BranchDto> pullRequests = dbClient.branchDao().selectByComponent(dbSession, project).stream()
  82. .filter(b -> b.getBranchType() == PULL_REQUEST)
  83. .collect(toList());
  84. List<String> pullRequestUuids = pullRequests.stream().map(BranchDto::getUuid).collect(toList());
  85. Map<String, BranchDto> mergeBranchesByUuid = dbClient.branchDao()
  86. .selectByUuids(dbSession, pullRequests.stream().map(BranchDto::getMergeBranchUuid).filter(Objects::nonNull).collect(toList()))
  87. .stream().collect(uniqueIndex(BranchDto::getUuid));
  88. Map<String, BranchStatistics> branchStatisticsByBranchUuid = issueIndex.searchBranchStatistics(project.uuid(), pullRequestUuids).stream()
  89. .collect(uniqueIndex(BranchStatistics::getBranchUuid, Function.identity()));
  90. Map<String, String> analysisDateByBranchUuid = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, pullRequestUuids).stream()
  91. .collect(uniqueIndex(SnapshotDto::getComponentUuid, s -> formatDateTime(s.getCreatedAt())));
  92. ProjectPullRequests.ListWsResponse.Builder protobufResponse = ProjectPullRequests.ListWsResponse.newBuilder();
  93. pullRequests
  94. .forEach(b -> addPullRequest(protobufResponse, b, mergeBranchesByUuid, branchStatisticsByBranchUuid.get(b.getUuid()),
  95. analysisDateByBranchUuid.get(b.getUuid())));
  96. writeProtobuf(protobufResponse.build(), request, response);
  97. }
  98. }
  99. private static void addPullRequest(ProjectPullRequests.ListWsResponse.Builder response, BranchDto branch, Map<String, BranchDto> mergeBranchesByUuid,
  100. BranchStatistics branchStatistics, @Nullable String analysisDate) {
  101. Optional<BranchDto> mergeBranch = Optional.ofNullable(mergeBranchesByUuid.get(branch.getMergeBranchUuid()));
  102. ProjectPullRequests.PullRequest.Builder builder = ProjectPullRequests.PullRequest.newBuilder();
  103. builder.setKey(branch.getKey());
  104. DbProjectBranches.PullRequestData pullRequestData = requireNonNull(branch.getPullRequestData(), "Pull request data should be available for branch type PULL_REQUEST");
  105. builder.setBranch(pullRequestData.getBranch());
  106. builder.setUrl(pullRequestData.getUrl());
  107. builder.setTitle(pullRequestData.getTitle());
  108. if (mergeBranch.isPresent()) {
  109. String mergeBranchKey = mergeBranch.get().getKey();
  110. builder.setBase(mergeBranchKey);
  111. } else {
  112. builder.setIsOrphan(true);
  113. }
  114. setNullable(analysisDate, builder::setAnalysisDate);
  115. setBranchStatus(builder, branchStatistics);
  116. response.addPullRequests(builder);
  117. }
  118. private static void setBranchStatus(ProjectPullRequests.PullRequest.Builder builder, @Nullable BranchStatistics branchStatistics) {
  119. ProjectPullRequests.Status.Builder statusBuilder = ProjectPullRequests.Status.newBuilder();
  120. statusBuilder.setBugs(branchStatistics == null ? 0L : branchStatistics.getBugs());
  121. statusBuilder.setVulnerabilities(branchStatistics == null ? 0L : branchStatistics.getVulnerabilities());
  122. statusBuilder.setCodeSmells(branchStatistics == null ? 0L : branchStatistics.getCodeSmells());
  123. builder.setStatus(statusBuilder);
  124. }
  125. }