3 * Copyright (C) 2009-2022 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.rule.RuleKey;
23 import org.sonar.api.server.ws.Request;
24 import org.sonar.api.server.ws.Response;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.qualityprofile.QProfileDto;
29 import org.sonar.db.rule.RuleDefinitionDto;
30 import org.sonar.server.qualityprofile.QProfileRules;
31 import org.sonar.server.user.UserSession;
33 import static java.util.Collections.singletonList;
34 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
35 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_DEACTIVATE_RULE;
36 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
37 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE;
39 public class DeactivateRuleAction implements QProfileWsAction {
41 private final DbClient dbClient;
42 private final QProfileRules ruleActivator;
43 private final UserSession userSession;
44 private final QProfileWsSupport wsSupport;
46 public DeactivateRuleAction(DbClient dbClient, QProfileRules ruleActivator, UserSession userSession, QProfileWsSupport wsSupport) {
47 this.dbClient = dbClient;
48 this.ruleActivator = ruleActivator;
49 this.userSession = userSession;
50 this.wsSupport = wsSupport;
53 public void define(WebService.NewController controller) {
54 WebService.NewAction deactivate = controller
55 .createAction(ACTION_DEACTIVATE_RULE)
56 .setDescription("Deactivate a rule on a quality profile.<br> " +
57 "Requires one of the following permissions:" +
59 " <li>'Administer Quality Profiles'</li>" +
60 " <li>Edit right on the specified quality profile</li>" +
66 deactivate.createParam(PARAM_KEY)
67 .setDescription("Quality Profile key. Can be obtained through <code>api/qualityprofiles/search</code>")
69 .setExampleValue(UUID_EXAMPLE_01);
71 deactivate.createParam(PARAM_RULE)
72 .setDescription("Rule key")
74 .setExampleValue("java:S1144");
78 public void handle(Request request, Response response) throws Exception {
79 RuleKey ruleKey = RuleKey.parse(request.mandatoryParam(PARAM_RULE));
80 String qualityProfileKey = request.mandatoryParam(PARAM_KEY);
81 userSession.checkLoggedIn();
82 try (DbSession dbSession = dbClient.openSession(false)) {
83 RuleDefinitionDto rule = wsSupport.getRule(dbSession, ruleKey);
84 QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromKey(qualityProfileKey));
85 wsSupport.checkCanEdit(dbSession, profile);
86 ruleActivator.deactivateAndCommit(dbSession, profile, singletonList(rule.getUuid()));