3 * Copyright (C) 2009-2021 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.server.qualityprofile.ws;
22 import java.util.Arrays;
23 import java.util.List;
25 import org.sonar.api.resources.Language;
26 import org.sonar.api.resources.Languages;
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.core.util.stream.MoreCollectors;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.qualityprofile.QProfileDto;
34 import org.sonar.db.qualityprofile.SearchQualityProfileGroupsQuery;
35 import org.sonar.db.user.GroupDto;
36 import org.sonar.db.user.SearchGroupMembershipDto;
37 import org.sonarqube.ws.Common;
38 import org.sonarqube.ws.Qualityprofiles;
40 import static java.util.Optional.ofNullable;
41 import static org.sonar.api.server.ws.WebService.Param.PAGE;
42 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
43 import static org.sonar.api.server.ws.WebService.Param.SELECTED;
44 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
45 import static org.sonar.api.server.ws.WebService.SelectionMode.ALL;
46 import static org.sonar.api.server.ws.WebService.SelectionMode.DESELECTED;
47 import static org.sonar.api.server.ws.WebService.SelectionMode.fromParam;
48 import static org.sonar.core.util.stream.MoreCollectors.toList;
49 import static org.sonar.core.util.stream.MoreCollectors.toSet;
50 import static org.sonar.db.Pagination.forPage;
51 import static org.sonar.db.qualityprofile.SearchQualityProfileGroupsQuery.ANY;
52 import static org.sonar.db.qualityprofile.SearchQualityProfileGroupsQuery.IN;
53 import static org.sonar.db.qualityprofile.SearchQualityProfileGroupsQuery.OUT;
54 import static org.sonar.db.qualityprofile.SearchQualityProfileGroupsQuery.builder;
55 import static org.sonar.server.ws.WsUtils.writeProtobuf;
56 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_SEARCH_GROUPS;
57 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
58 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE;
60 public class SearchGroupsAction implements QProfileWsAction {
62 private static final Map<WebService.SelectionMode, String> MEMBERSHIP = Map.of(WebService.SelectionMode.SELECTED, IN, DESELECTED, OUT, ALL, ANY);
64 private final DbClient dbClient;
65 private final QProfileWsSupport wsSupport;
66 private final Languages languages;
68 public SearchGroupsAction(DbClient dbClient, QProfileWsSupport wsSupport, Languages languages) {
69 this.dbClient = dbClient;
70 this.wsSupport = wsSupport;
71 this.languages = languages;
75 public void define(WebService.NewController context) {
76 WebService.NewAction action = context
77 .createAction(ACTION_SEARCH_GROUPS)
78 .setDescription("List the groups that are allowed to edit a Quality Profile.<br>" +
79 "Requires one of the following permissions:" +
81 " <li>'Administer Quality Profiles'</li>" +
82 " <li>Edit right on the specified quality profile</li>" +
86 .addSelectionModeParam()
87 .addSearchQuery("sonar", "group names")
89 .setResponseExample(getClass().getResource("search_groups-example.json"))
92 action.createParam(PARAM_QUALITY_PROFILE)
93 .setDescription("Quality Profile name")
95 .setExampleValue("Recommended quality profile");
98 .createParam(PARAM_LANGUAGE)
99 .setDescription("Quality profile language")
101 .setPossibleValues(Arrays.stream(languages.all()).map(Language::getKey).collect(toSet()));
105 public void handle(Request request, Response response) throws Exception {
106 SearchQualityProfileUsersRequest wsRequest = buildRequest(request);
107 try (DbSession dbSession = dbClient.openSession(false)) {
108 QProfileDto profile = wsSupport.getProfile(dbSession, wsRequest.getQualityProfile(), wsRequest.getLanguage());
109 wsSupport.checkCanEdit(dbSession, profile);
111 SearchQualityProfileGroupsQuery query = builder()
113 .setQuery(wsRequest.getQuery())
114 .setMembership(MEMBERSHIP.get(fromParam(wsRequest.getSelected())))
116 int total = dbClient.qProfileEditGroupsDao().countByQuery(dbSession, query);
117 List<SearchGroupMembershipDto> groupMemberships = dbClient.qProfileEditGroupsDao().selectByQuery(dbSession, query,
118 forPage(wsRequest.getPage()).andSize(wsRequest.getPageSize()));
119 Map<String, GroupDto> groupsByUuid = dbClient.groupDao().selectByUuids(dbSession,
120 groupMemberships.stream().map(SearchGroupMembershipDto::getGroupUuid).collect(MoreCollectors.toList()))
122 .collect(MoreCollectors.uniqueIndex(GroupDto::getUuid));
124 Qualityprofiles.SearchGroupsResponse.newBuilder()
125 .addAllGroups(groupMemberships.stream()
126 .map(groupsMembership -> toGroup(groupsByUuid.get(groupsMembership.getGroupUuid()), groupsMembership.isSelected()))
128 .setPaging(buildPaging(wsRequest, total)).build(),
133 private static SearchQualityProfileUsersRequest buildRequest(Request request) {
134 return SearchQualityProfileUsersRequest.builder()
135 .setQualityProfile(request.mandatoryParam(PARAM_QUALITY_PROFILE))
136 .setLanguage(request.mandatoryParam(PARAM_LANGUAGE))
137 .setQuery(request.param(TEXT_QUERY))
138 .setSelected(request.mandatoryParam(SELECTED))
139 .setPage(request.mandatoryParamAsInt(PAGE))
140 .setPageSize(request.mandatoryParamAsInt(PAGE_SIZE))
144 private static Qualityprofiles.SearchGroupsResponse.Group toGroup(GroupDto group, boolean isSelected) {
145 Qualityprofiles.SearchGroupsResponse.Group.Builder builder = Qualityprofiles.SearchGroupsResponse.Group.newBuilder()
146 .setName(group.getName())
147 .setSelected(isSelected);
148 ofNullable(group.getDescription()).ifPresent(builder::setDescription);
149 return builder.build();
152 private static Common.Paging buildPaging(SearchQualityProfileUsersRequest wsRequest, int total) {
153 return Common.Paging.newBuilder()
154 .setPageIndex(wsRequest.getPage())
155 .setPageSize(wsRequest.getPageSize())