]> source.dussan.org Git - sonarqube.git/blob
79330bbbe919b35eaeb254fbdcc21a5fd874f017
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 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.qualityprofile.ws;
21
22 import com.google.common.collect.ImmutableMap;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Map;
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;
40
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;
60
61 public class SearchGroupsAction implements QProfileWsAction {
62
63   private static final Map<WebService.SelectionMode, String> MEMBERSHIP = ImmutableMap.of(WebService.SelectionMode.SELECTED, IN, DESELECTED, OUT, ALL, ANY);
64
65   private final DbClient dbClient;
66   private final QProfileWsSupport wsSupport;
67   private final Languages languages;
68
69   public SearchGroupsAction(DbClient dbClient, QProfileWsSupport wsSupport, Languages languages) {
70     this.dbClient = dbClient;
71     this.wsSupport = wsSupport;
72     this.languages = languages;
73   }
74
75   @Override
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:" +
81         "<ul>" +
82         "  <li>'Administer Quality Profiles'</li>" +
83         "  <li>Edit right on the specified quality profile</li>" +
84         "</ul>")
85       .setHandler(this)
86       .setInternal(true)
87       .addSelectionModeParam()
88       .addSearchQuery("sonar", "group names")
89       .addPagingParams(25)
90       .setResponseExample(getClass().getResource("search_groups-example.json"))
91       .setSince("6.6");
92
93     action.createParam(PARAM_QUALITY_PROFILE)
94       .setDescription("Quality Profile name")
95       .setRequired(true)
96       .setExampleValue("Recommended quality profile");
97
98     action
99       .createParam(PARAM_LANGUAGE)
100       .setDescription("Quality profile language")
101       .setRequired(true)
102       .setPossibleValues(Arrays.stream(languages.all()).map(Language::getKey).collect(toSet()));
103   }
104
105   @Override
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);
111
112       SearchGroupsQuery query = builder()
113         .setProfile(profile)
114         .setQuery(wsRequest.getQuery())
115         .setMembership(MEMBERSHIP.get(fromParam(wsRequest.getSelected())))
116         .build();
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()))
122         .stream()
123         .collect(MoreCollectors.uniqueIndex(GroupDto::getUuid));
124       writeProtobuf(
125         Qualityprofiles.SearchGroupsResponse.newBuilder()
126           .addAllGroups(groupMemberships.stream()
127             .map(groupsMembership -> toGroup(groupsByUuid.get(groupsMembership.getGroupUuid()), groupsMembership.isSelected()))
128             .collect(toList()))
129           .setPaging(buildPaging(wsRequest, total)).build(),
130         request, response);
131     }
132   }
133
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))
142       .build();
143   }
144
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();
151   }
152
153   private static Common.Paging buildPaging(SearchUsersRequest wsRequest, int total) {
154     return Common.Paging.newBuilder()
155       .setPageIndex(wsRequest.getPage())
156       .setPageSize(wsRequest.getPageSize())
157       .setTotal(total)
158       .build();
159   }
160 }