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 com.google.common.collect.ImmutableMap;
23 import java.util.Arrays;
24 import java.util.List;
26 import org.sonar.api.resources.Language;
27 import org.sonar.api.resources.Languages;
28 import org.sonar.api.server.ws.Request;
29 import org.sonar.api.server.ws.Response;
30 import org.sonar.api.server.ws.WebService;
31 import org.sonar.core.util.stream.MoreCollectors;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.qualityprofile.GroupMembershipDto;
35 import org.sonar.db.qualityprofile.QProfileDto;
36 import org.sonar.db.qualityprofile.SearchGroupsQuery;
37 import org.sonar.db.user.GroupDto;
38 import org.sonarqube.ws.Common;
39 import org.sonarqube.ws.Qualityprofiles;
41 import static java.util.Optional.ofNullable;
42 import static org.sonar.api.server.ws.WebService.Param.PAGE;
43 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
44 import static org.sonar.api.server.ws.WebService.Param.SELECTED;
45 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
46 import static org.sonar.api.server.ws.WebService.SelectionMode.ALL;
47 import static org.sonar.api.server.ws.WebService.SelectionMode.DESELECTED;
48 import static org.sonar.api.server.ws.WebService.SelectionMode.fromParam;
49 import static org.sonar.core.util.stream.MoreCollectors.toList;
50 import static org.sonar.core.util.stream.MoreCollectors.toSet;
51 import static org.sonar.db.Pagination.forPage;
52 import static org.sonar.db.qualityprofile.SearchGroupsQuery.ANY;
53 import static org.sonar.db.qualityprofile.SearchGroupsQuery.IN;
54 import static org.sonar.db.qualityprofile.SearchGroupsQuery.OUT;
55 import static org.sonar.db.qualityprofile.SearchGroupsQuery.builder;
56 import static org.sonar.server.ws.WsUtils.writeProtobuf;
57 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_SEARCH_GROUPS;
58 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
59 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE;
61 public class SearchGroupsAction implements QProfileWsAction {
63 private static final Map<WebService.SelectionMode, String> MEMBERSHIP = ImmutableMap.of(WebService.SelectionMode.SELECTED, IN, DESELECTED, OUT, ALL, ANY);
65 private final DbClient dbClient;
66 private final QProfileWsSupport wsSupport;
67 private final Languages languages;
69 public SearchGroupsAction(DbClient dbClient, QProfileWsSupport wsSupport, Languages languages) {
70 this.dbClient = dbClient;
71 this.wsSupport = wsSupport;
72 this.languages = languages;
76 public void define(WebService.NewController context) {
77 WebService.NewAction action = context
78 .createAction(ACTION_SEARCH_GROUPS)
79 .setDescription("List the groups that are allowed to edit a Quality Profile.<br>" +
80 "Requires one of the following permissions:" +
82 " <li>'Administer Quality Profiles'</li>" +
83 " <li>Edit right on the specified quality profile</li>" +
87 .addSelectionModeParam()
88 .addSearchQuery("sonar", "group names")
90 .setResponseExample(getClass().getResource("search_groups-example.json"))
93 action.createParam(PARAM_QUALITY_PROFILE)
94 .setDescription("Quality Profile name")
96 .setExampleValue("Recommended quality profile");
99 .createParam(PARAM_LANGUAGE)
100 .setDescription("Quality profile language")
102 .setPossibleValues(Arrays.stream(languages.all()).map(Language::getKey).collect(toSet()));
106 public void handle(Request request, Response response) throws Exception {
107 SearchUsersRequest wsRequest = buildRequest(request);
108 try (DbSession dbSession = dbClient.openSession(false)) {
109 QProfileDto profile = wsSupport.getProfile(dbSession, wsRequest.getQualityProfile(), wsRequest.getLanguage());
110 wsSupport.checkCanEdit(dbSession, profile);
112 SearchGroupsQuery query = builder()
114 .setQuery(wsRequest.getQuery())
115 .setMembership(MEMBERSHIP.get(fromParam(wsRequest.getSelected())))
117 int total = dbClient.qProfileEditGroupsDao().countByQuery(dbSession, query);
118 List<GroupMembershipDto> groupMemberships = dbClient.qProfileEditGroupsDao().selectByQuery(dbSession, query,
119 forPage(wsRequest.getPage()).andSize(wsRequest.getPageSize()));
120 Map<String, GroupDto> groupsByUuid = dbClient.groupDao().selectByUuids(dbSession,
121 groupMemberships.stream().map(GroupMembershipDto::getGroupUuid).collect(MoreCollectors.toList()))
123 .collect(MoreCollectors.uniqueIndex(GroupDto::getUuid));
125 Qualityprofiles.SearchGroupsResponse.newBuilder()
126 .addAllGroups(groupMemberships.stream()
127 .map(groupsMembership -> toGroup(groupsByUuid.get(groupsMembership.getGroupUuid()), groupsMembership.isSelected()))
129 .setPaging(buildPaging(wsRequest, total)).build(),
134 private static SearchUsersRequest buildRequest(Request request) {
135 return SearchUsersRequest.builder()
136 .setQualityProfile(request.mandatoryParam(PARAM_QUALITY_PROFILE))
137 .setLanguage(request.mandatoryParam(PARAM_LANGUAGE))
138 .setQuery(request.param(TEXT_QUERY))
139 .setSelected(request.mandatoryParam(SELECTED))
140 .setPage(request.mandatoryParamAsInt(PAGE))
141 .setPageSize(request.mandatoryParamAsInt(PAGE_SIZE))
145 private static Qualityprofiles.SearchGroupsResponse.Group toGroup(GroupDto group, boolean isSelected) {
146 Qualityprofiles.SearchGroupsResponse.Group.Builder builder = Qualityprofiles.SearchGroupsResponse.Group.newBuilder()
147 .setName(group.getName())
148 .setSelected(isSelected);
149 ofNullable(group.getDescription()).ifPresent(builder::setDescription);
150 return builder.build();
153 private static Common.Paging buildPaging(SearchUsersRequest wsRequest, int total) {
154 return Common.Paging.newBuilder()
155 .setPageIndex(wsRequest.getPage())
156 .setPageSize(wsRequest.getPageSize())