3 * Copyright (C) 2009-2023 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.qualityprofile.QProfileDto;
32 import org.sonar.db.rule.RuleDto;
33 import org.sonar.server.qualityprofile.QProfileRules;
34 import org.sonar.server.qualityprofile.RuleActivation;
35 import org.sonar.server.user.UserSession;
37 import static java.lang.String.format;
38 import static java.util.Collections.singletonList;
39 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
40 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_ACTIVATE_RULE;
41 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
42 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PARAMS;
43 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RESET;
44 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE;
45 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_SEVERITY;
47 public class ActivateRuleAction implements QProfileWsAction {
49 private final DbClient dbClient;
50 private final QProfileRules ruleActivator;
51 private final UserSession userSession;
52 private final QProfileWsSupport wsSupport;
54 public ActivateRuleAction(DbClient dbClient, QProfileRules ruleActivator, UserSession userSession, QProfileWsSupport wsSupport) {
55 this.dbClient = dbClient;
56 this.ruleActivator = ruleActivator;
57 this.userSession = userSession;
58 this.wsSupport = wsSupport;
61 public void define(WebService.NewController controller) {
62 WebService.NewAction activate = controller
63 .createAction(ACTION_ACTIVATE_RULE)
64 .setDescription("Activate a rule on a Quality Profile.<br> " +
65 "Requires one of the following permissions:" +
67 " <li>'Administer Quality Profiles'</li>" +
68 " <li>Edit right on the specified quality profile</li>" +
74 activate.createParam(PARAM_KEY)
75 .setDescription("Quality Profile key. Can be obtained through <code>api/qualityprofiles/search</code>")
77 .setExampleValue(UUID_EXAMPLE_01);
79 activate.createParam(PARAM_RULE)
80 .setDescription("Rule key")
82 .setExampleValue("java:AvoidCycles");
84 activate.createParam(PARAM_SEVERITY)
85 .setDescription(format("Severity. Ignored if parameter %s is true.", PARAM_RESET))
86 .setPossibleValues(Severity.ALL);
88 activate.createParam(PARAM_PARAMS)
89 .setDescription(format("Parameters as semi-colon list of <code>key=value</code>. Ignored if parameter %s is true.", PARAM_RESET))
90 .setExampleValue("params=key1=v1;key2=v2");
92 activate.createParam(PARAM_RESET)
93 .setDescription("Reset severity and parameters of activated rule. Set the values defined on parent profile or from rule default values.")
94 .setBooleanPossibleValues();
98 public void handle(Request request, Response response) throws Exception {
99 userSession.checkLoggedIn();
100 try (DbSession dbSession = dbClient.openSession(false)) {
101 String profileKey = request.mandatoryParam(PARAM_KEY);
102 QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromKey(profileKey));
103 wsSupport.checkCanEdit(dbSession, profile);
104 RuleActivation activation = readActivation(dbSession, request);
105 ruleActivator.activateAndCommit(dbSession, profile, singletonList(activation));
108 response.noContent();
111 private RuleActivation readActivation(DbSession dbSession, Request request) {
112 RuleKey ruleKey = RuleKey.parse(request.mandatoryParam(PARAM_RULE));
113 RuleDto ruleDto = wsSupport.getRule(dbSession, ruleKey);
114 boolean reset = Boolean.TRUE.equals(request.paramAsBoolean(PARAM_RESET));
116 return RuleActivation.createReset(ruleDto.getUuid());
118 String severity = request.param(PARAM_SEVERITY);
119 Map<String, String> params = null;
120 String paramsAsString = request.param(PARAM_PARAMS);
121 if (paramsAsString != null) {
122 params = KeyValueFormat.parse(paramsAsString);
124 return RuleActivation.create(ruleDto.getUuid(), severity, params);