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