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;
23 import org.sonar.api.rule.RuleKey;
24 import org.sonar.api.rule.Severity;
25 import org.sonar.api.server.ws.Request;
26 import org.sonar.api.server.ws.Response;
27 import org.sonar.api.server.ws.WebService;
28 import org.sonar.api.utils.KeyValueFormat;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.organization.OrganizationDto;
32 import org.sonar.db.qualityprofile.QProfileDto;
33 import org.sonar.db.rule.RuleDefinitionDto;
34 import org.sonar.server.qualityprofile.QProfileRules;
35 import org.sonar.server.qualityprofile.RuleActivation;
36 import org.sonar.server.user.UserSession;
38 import static java.lang.String.format;
39 import static java.util.Collections.singletonList;
40 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
41 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_ACTIVATE_RULE;
42 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
43 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PARAMS;
44 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RESET;
45 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE;
46 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_SEVERITY;
48 public class ActivateRuleAction implements QProfileWsAction {
50 private final DbClient dbClient;
51 private final QProfileRules ruleActivator;
52 private final UserSession userSession;
53 private final QProfileWsSupport wsSupport;
55 public ActivateRuleAction(DbClient dbClient, QProfileRules ruleActivator, UserSession userSession, QProfileWsSupport wsSupport) {
56 this.dbClient = dbClient;
57 this.ruleActivator = ruleActivator;
58 this.userSession = userSession;
59 this.wsSupport = wsSupport;
62 public void define(WebService.NewController controller) {
63 WebService.NewAction activate = controller
64 .createAction(ACTION_ACTIVATE_RULE)
65 .setDescription("Activate a rule on a Quality Profile.<br> " +
66 "Requires one of the following permissions:" +
68 " <li>'Administer Quality Profiles'</li>" +
69 " <li>Edit right on the specified quality profile</li>" +
75 activate.createParam(PARAM_KEY)
76 .setDescription("Quality Profile key. Can be obtained through <code>api/qualityprofiles/search</code>")
77 .setDeprecatedKey("profile_key", "6.5")
79 .setExampleValue(UUID_EXAMPLE_01);
81 activate.createParam(PARAM_RULE)
82 .setDescription("Rule key")
83 .setDeprecatedKey("rule_key", "6.5")
85 .setExampleValue("squid:AvoidCycles");
87 activate.createParam(PARAM_SEVERITY)
88 .setDescription(format("Severity. Ignored if parameter %s is true.", PARAM_RESET))
89 .setPossibleValues(Severity.ALL);
91 activate.createParam(PARAM_PARAMS)
92 .setDescription(format("Parameters as semi-colon list of <code>key=value</code>. Ignored if parameter %s is true.", PARAM_RESET))
93 .setExampleValue("params=key1=v1;key2=v2");
95 activate.createParam(PARAM_RESET)
96 .setDescription("Reset severity and parameters of activated rule. Set the values defined on parent profile or from rule default values.")
97 .setBooleanPossibleValues();
101 public void handle(Request request, Response response) throws Exception {
102 userSession.checkLoggedIn();
103 try (DbSession dbSession = dbClient.openSession(false)) {
104 String profileKey = request.mandatoryParam(PARAM_KEY);
105 QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromKey(profileKey));
106 OrganizationDto organization = wsSupport.getOrganization(dbSession, profile);
107 wsSupport.checkCanEdit(dbSession, organization, profile);
108 RuleActivation activation = readActivation(dbSession, request);
109 ruleActivator.activateAndCommit(dbSession, profile, singletonList(activation));
112 response.noContent();
115 private RuleActivation readActivation(DbSession dbSession, Request request) {
116 RuleKey ruleKey = RuleKey.parse(request.mandatoryParam(PARAM_RULE));
117 RuleDefinitionDto ruleDefinition = wsSupport.getRule(dbSession, ruleKey);
118 boolean reset = Boolean.TRUE.equals(request.paramAsBoolean(PARAM_RESET));
120 return RuleActivation.createReset(ruleDefinition.getId());
122 String severity = request.param(PARAM_SEVERITY);
123 Map<String, String> params = null;
124 String paramsAsString = request.param(PARAM_PARAMS);
125 if (paramsAsString != null) {
126 params = KeyValueFormat.parse(paramsAsString);
128 return RuleActivation.create(ruleDefinition.getId(), severity, params);