3 * Copyright (C) 2009-2019 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 org.sonar.api.resources.Languages;
23 import org.sonar.api.server.ws.Request;
24 import org.sonar.api.server.ws.Response;
25 import org.sonar.api.server.ws.WebService.NewAction;
26 import org.sonar.api.server.ws.WebService.NewController;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.organization.OrganizationDto;
30 import org.sonar.db.qualityprofile.QProfileDto;
31 import org.sonar.server.qualityprofile.QProfileTree;
32 import org.sonar.server.user.UserSession;
33 import org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters;
35 import static org.apache.commons.lang.StringUtils.isEmpty;
36 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02;
37 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
38 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PARENT_KEY;
40 public class ChangeParentAction implements QProfileWsAction {
42 private final DbClient dbClient;
43 private final QProfileTree ruleActivator;
44 private final Languages languages;
45 private final QProfileWsSupport wsSupport;
46 private final UserSession userSession;
48 public ChangeParentAction(DbClient dbClient, QProfileTree ruleActivator,
49 Languages languages, QProfileWsSupport wsSupport, UserSession userSession) {
50 this.dbClient = dbClient;
51 this.ruleActivator = ruleActivator;
52 this.languages = languages;
53 this.wsSupport = wsSupport;
54 this.userSession = userSession;
58 public void define(NewController context) {
59 NewAction inheritance = context.createAction("change_parent")
62 .setDescription("Change a quality profile's parent.<br>" +
63 "Requires one of the following permissions:" +
65 " <li>'Administer Quality Profiles'</li>" +
66 " <li>Edit right on the specified quality profile</li>" +
70 QProfileWsSupport.createOrganizationParam(inheritance)
72 QProfileReference.defineParams(inheritance, languages);
74 inheritance.createParam(PARAM_PARENT_KEY)
75 .setDescription("New parent profile key.<br> " +
76 "If no profile is provided, the inheritance link with current parent profile (if any) is broken, which deactivates all rules " +
77 "which come from the parent and are not overridden.")
78 .setDeprecatedSince("6.6")
79 .setExampleValue(UUID_EXAMPLE_02);
81 inheritance.createParam(QualityProfileWsParameters.PARAM_PARENT_QUALITY_PROFILE)
82 .setDescription("Quality profile name. If this parameter is set, '%s' must not be set and '%s' must be set to disambiguate.", PARAM_PARENT_KEY, PARAM_LANGUAGE)
83 .setExampleValue("Sonar way");
87 public void handle(Request request, Response response) throws Exception {
88 userSession.checkLoggedIn();
89 QProfileReference reference = QProfileReference.from(request);
91 try (DbSession dbSession = dbClient.openSession(false)) {
92 QProfileDto profile = wsSupport.getProfile(dbSession, reference);
93 OrganizationDto organization = wsSupport.getOrganization(dbSession, profile);
94 wsSupport.checkCanEdit(dbSession, organization, profile);
96 String parentKey = request.param(PARAM_PARENT_KEY);
97 String parentName = request.param(QualityProfileWsParameters.PARAM_PARENT_QUALITY_PROFILE);
98 if (isEmpty(parentKey) && isEmpty(parentName)) {
99 ruleActivator.removeParentAndCommit(dbSession, profile);
101 String parentOrganizationKey = parentKey == null ? organization.getKey() : null;
102 String parentLanguage = parentKey == null ? request.param(PARAM_LANGUAGE) : null;
103 QProfileReference parentRef = QProfileReference.from(parentKey, parentOrganizationKey, parentLanguage, parentName);
104 QProfileDto parent = wsSupport.getProfile(dbSession, parentRef);
105 ruleActivator.setParentAndCommit(dbSession, profile, parent);
108 response.noContent();