3 * Copyright (C) 2009-2024 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.qualityprofile.QProfileDto;
30 import org.sonar.server.qualityprofile.QProfileTree;
31 import org.sonar.server.user.UserSession;
32 import org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters;
34 import static org.apache.commons.lang.StringUtils.isEmpty;
35 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
37 public class ChangeParentAction implements QProfileWsAction {
39 private final DbClient dbClient;
40 private final QProfileTree ruleActivator;
41 private final Languages languages;
42 private final QProfileWsSupport wsSupport;
43 private final UserSession userSession;
45 public ChangeParentAction(DbClient dbClient, QProfileTree ruleActivator,
46 Languages languages, QProfileWsSupport wsSupport, UserSession userSession) {
47 this.dbClient = dbClient;
48 this.ruleActivator = ruleActivator;
49 this.languages = languages;
50 this.wsSupport = wsSupport;
51 this.userSession = userSession;
55 public void define(NewController context) {
56 NewAction inheritance = context.createAction("change_parent")
59 .setDescription("Change a quality profile's parent.<br>" +
60 "Requires one of the following permissions:" +
62 " <li>'Administer Quality Profiles'</li>" +
63 " <li>Edit right on the specified quality profile</li>" +
67 QProfileReference.defineParams(inheritance, languages);
69 inheritance.createParam(QualityProfileWsParameters.PARAM_PARENT_QUALITY_PROFILE)
70 .setDescription("New parent profile name. <br> " +
71 "If no profile is provided, the inheritance link with current parent profile (if any) is broken, which deactivates all rules " +
72 "which come from the parent and are not overridden.")
73 .setExampleValue("Sonar way");
77 public void handle(Request request, Response response) throws Exception {
78 userSession.checkLoggedIn();
79 QProfileReference reference = QProfileReference.fromName(request);
81 try (DbSession dbSession = dbClient.openSession(false)) {
82 QProfileDto profile = wsSupport.getProfile(dbSession, reference);
83 wsSupport.checkCanEdit(dbSession, profile);
85 String parentName = request.param(QualityProfileWsParameters.PARAM_PARENT_QUALITY_PROFILE);
86 if (isEmpty(parentName)) {
87 ruleActivator.removeParentAndCommit(dbSession, profile);
89 String parentLanguage = request.mandatoryParam(PARAM_LANGUAGE);
90 QProfileReference parentRef = QProfileReference.fromName(parentLanguage, parentName);
91 QProfileDto parent = wsSupport.getProfile(dbSession, parentRef);
92 ruleActivator.setParentAndCommit(dbSession, profile, parent);