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.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.organization.OrganizationDto;
29 import org.sonar.db.qualityprofile.QProfileDto;
30 import org.sonar.db.rule.RuleDefinitionDto;
31 import org.sonar.server.qualityprofile.QProfileRules;
32 import org.sonar.server.user.UserSession;
34 import static java.util.Collections.singletonList;
35 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
36 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_DEACTIVATE_RULE;
37 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
38 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE;
40 public class DeactivateRuleAction implements QProfileWsAction {
42 private final DbClient dbClient;
43 private final QProfileRules ruleActivator;
44 private final UserSession userSession;
45 private final QProfileWsSupport wsSupport;
47 public DeactivateRuleAction(DbClient dbClient, QProfileRules ruleActivator, UserSession userSession, QProfileWsSupport wsSupport) {
48 this.dbClient = dbClient;
49 this.ruleActivator = ruleActivator;
50 this.userSession = userSession;
51 this.wsSupport = wsSupport;
54 public void define(WebService.NewController controller) {
55 WebService.NewAction deactivate = controller
56 .createAction(ACTION_DEACTIVATE_RULE)
57 .setDescription("Deactivate a rule on a quality profile.<br> " +
58 "Requires one of the following permissions:" +
60 " <li>'Administer Quality Profiles'</li>" +
61 " <li>Edit right on the specified quality profile</li>" +
67 deactivate.createParam(PARAM_KEY)
68 .setDescription("Quality Profile key. Can be obtained through <code>api/qualityprofiles/search</code>")
69 .setDeprecatedKey("profile_key", "6.5")
71 .setExampleValue(UUID_EXAMPLE_01);
73 deactivate.createParam(PARAM_RULE)
74 .setDescription("Rule key")
75 .setDeprecatedKey("rule_key", "6.5")
77 .setExampleValue("squid:AvoidCycles");
81 public void handle(Request request, Response response) throws Exception {
82 RuleKey ruleKey = RuleKey.parse(request.mandatoryParam(PARAM_RULE));
83 String qualityProfileKey = request.mandatoryParam(PARAM_KEY);
84 userSession.checkLoggedIn();
85 try (DbSession dbSession = dbClient.openSession(false)) {
86 RuleDefinitionDto rule = wsSupport.getRule(dbSession, ruleKey);
87 QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromKey(qualityProfileKey));
88 OrganizationDto organization = wsSupport.getOrganization(dbSession, profile);
89 wsSupport.checkCanEdit(dbSession, organization, profile);
90 ruleActivator.deactivateAndCommit(dbSession, profile, singletonList(rule.getId()));