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.

AuthorsAction.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.issue.ws;
  21. import com.google.common.collect.ImmutableSet;
  22. import com.google.common.io.Resources;
  23. import java.util.EnumSet;
  24. import java.util.List;
  25. import java.util.Optional;
  26. import java.util.stream.Collectors;
  27. import javax.annotation.Nullable;
  28. import org.sonar.api.resources.Qualifiers;
  29. import org.sonar.api.resources.Scopes;
  30. import org.sonar.api.rules.RuleType;
  31. import org.sonar.api.server.ws.Change;
  32. import org.sonar.api.server.ws.Request;
  33. import org.sonar.api.server.ws.Response;
  34. import org.sonar.api.server.ws.WebService;
  35. import org.sonar.api.server.ws.WebService.NewAction;
  36. import org.sonar.db.DbClient;
  37. import org.sonar.db.DbSession;
  38. import org.sonar.db.component.ComponentDto;
  39. import org.sonar.db.organization.OrganizationDto;
  40. import org.sonar.server.component.ComponentFinder;
  41. import org.sonar.server.issue.index.IssueIndex;
  42. import org.sonar.server.issue.index.IssueQuery;
  43. import org.sonar.server.organization.DefaultOrganizationProvider;
  44. import org.sonar.server.user.UserSession;
  45. import org.sonarqube.ws.Issues.AuthorsResponse;
  46. import static com.google.common.base.Preconditions.checkArgument;
  47. import static java.util.Optional.ofNullable;
  48. import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
  49. import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
  50. import static org.sonar.server.exceptions.NotFoundException.checkFoundWithOptional;
  51. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  52. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  53. public class AuthorsAction implements IssuesWsAction {
  54. private static final EnumSet<RuleType> ALL_RULE_TYPES_EXCEPT_SECURITY_HOTSPOTS = EnumSet.complementOf(EnumSet.of(RuleType.SECURITY_HOTSPOT));
  55. private static final String PARAM_ORGANIZATION = "organization";
  56. private static final String PARAM_PROJECT = "project";
  57. private final UserSession userSession;
  58. private final DbClient dbClient;
  59. private final IssueIndex issueIndex;
  60. private final ComponentFinder componentFinder;
  61. private final DefaultOrganizationProvider defaultOrganizationProvider;
  62. public AuthorsAction(UserSession userSession, DbClient dbClient, IssueIndex issueIndex, ComponentFinder componentFinder,
  63. DefaultOrganizationProvider defaultOrganizationProvider) {
  64. this.userSession = userSession;
  65. this.dbClient = dbClient;
  66. this.issueIndex = issueIndex;
  67. this.componentFinder = componentFinder;
  68. this.defaultOrganizationProvider = defaultOrganizationProvider;
  69. }
  70. @Override
  71. public void define(WebService.NewController controller) {
  72. NewAction action = controller.createAction("authors")
  73. .setSince("5.1")
  74. .setDescription("Search SCM accounts which match a given query.<br/>" +
  75. "Requires authentication.")
  76. .setResponseExample(Resources.getResource(this.getClass(), "authors-example.json"))
  77. .setChangelog(new Change("7.4", "The maximum size of 'ps' is set to 100"))
  78. .setHandler(this);
  79. action.createSearchQuery("luke", "authors");
  80. action.createPageSize(10, 100);
  81. action.createParam(PARAM_ORGANIZATION)
  82. .setDescription("Organization key")
  83. .setRequired(false)
  84. .setInternal(true)
  85. .setExampleValue("my-org")
  86. .setSince("7.4");
  87. action.createParam(PARAM_PROJECT)
  88. .setDescription("Project key")
  89. .setRequired(false)
  90. .setExampleValue(KEY_PROJECT_EXAMPLE_001)
  91. .setSince("7.4");
  92. }
  93. @Override
  94. public void handle(Request request, Response response) throws Exception {
  95. userSession.checkLoggedIn();
  96. try (DbSession dbSession = dbClient.openSession(false)) {
  97. OrganizationDto organization = getOrganization(dbSession, request.param(PARAM_ORGANIZATION));
  98. userSession.checkMembership(organization);
  99. Optional<ComponentDto> project = getProject(dbSession, organization, request.param(PARAM_PROJECT));
  100. List<String> authors = getAuthors(organization, project, request);
  101. AuthorsResponse wsResponse = AuthorsResponse.newBuilder().addAllAuthors(authors).build();
  102. writeProtobuf(wsResponse, request, response);
  103. }
  104. }
  105. private OrganizationDto getOrganization(DbSession dbSession, @Nullable String organizationKey) {
  106. String organizationOrDefaultKey = ofNullable(organizationKey).orElseGet(defaultOrganizationProvider.get()::getKey);
  107. return checkFoundWithOptional(
  108. dbClient.organizationDao().selectByKey(dbSession, organizationOrDefaultKey),
  109. "No organization with key '%s'", organizationOrDefaultKey);
  110. }
  111. private Optional<ComponentDto> getProject(DbSession dbSession, OrganizationDto organization, @Nullable String projectKey) {
  112. if (projectKey == null) {
  113. return Optional.empty();
  114. }
  115. ComponentDto project = componentFinder.getByKey(dbSession, projectKey);
  116. checkArgument(project.scope().equals(Scopes.PROJECT), "Component '%s' must be a project", projectKey);
  117. checkArgument(project.getOrganizationUuid().equals(organization.getUuid()), "Project '%s' is not part of the organization '%s'", projectKey, organization.getKey());
  118. return Optional.of(project);
  119. }
  120. private List<String> getAuthors(OrganizationDto organization, Optional<ComponentDto> project, Request request) {
  121. IssueQuery.Builder issueQueryBuilder = IssueQuery.builder()
  122. .organizationUuid(organization.getUuid());
  123. project.ifPresent(p -> {
  124. switch (p.qualifier()) {
  125. case Qualifiers.PROJECT:
  126. issueQueryBuilder.projectUuids(ImmutableSet.of(p.uuid()));
  127. return;
  128. case Qualifiers.APP:
  129. case Qualifiers.VIEW:
  130. issueQueryBuilder.viewUuids(ImmutableSet.of(p.uuid()));
  131. return;
  132. default:
  133. throw new IllegalArgumentException(String.format("Component of type '%s' is not supported", p.qualifier()));
  134. }
  135. });
  136. return issueIndex.searchAuthors(
  137. issueQueryBuilder
  138. .types(ALL_RULE_TYPES_EXCEPT_SECURITY_HOTSPOTS.stream().map(Enum::name).collect(Collectors.toList()))
  139. .build(),
  140. request.param(TEXT_QUERY),
  141. request.mandatoryParamAsInt(PAGE_SIZE));
  142. }
  143. }