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;
22 import org.sonar.api.rule.Severity;
23 import org.sonar.api.server.ws.Change;
24 import org.sonar.api.server.ws.Request;
25 import org.sonar.api.server.ws.Response;
26 import org.sonar.api.server.ws.WebService;
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.BulkChangeResult;
31 import org.sonar.server.qualityprofile.QProfileRules;
32 import org.sonar.server.rule.index.RuleQuery;
33 import org.sonar.server.rule.ws.RuleQueryFactory;
34 import org.sonar.server.user.UserSession;
36 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_03;
37 import static org.sonar.server.qualityprofile.ws.BulkChangeWsResponse.writeResponse;
38 import static org.sonar.server.qualityprofile.ws.QProfileReference.fromKey;
39 import static org.sonar.server.rule.ws.RuleWsSupport.defineGenericRuleSearchParameters;
40 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_ACTIVATE_RULES;
41 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TARGET_KEY;
42 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TARGET_SEVERITY;
44 public class ActivateRulesAction implements QProfileWsAction {
46 private final RuleQueryFactory ruleQueryFactory;
47 private final UserSession userSession;
48 private final QProfileRules qProfileRules;
49 private final DbClient dbClient;
50 private final QProfileWsSupport wsSupport;
52 public ActivateRulesAction(RuleQueryFactory ruleQueryFactory, UserSession userSession, QProfileRules qProfileRules, QProfileWsSupport wsSupport, DbClient dbClient) {
53 this.ruleQueryFactory = ruleQueryFactory;
54 this.userSession = userSession;
55 this.qProfileRules = qProfileRules;
56 this.dbClient = dbClient;
57 this.wsSupport = wsSupport;
60 public void define(WebService.NewController controller) {
61 WebService.NewAction activate = controller
62 .createAction(ACTION_ACTIVATE_RULES)
63 .setDescription("Bulk-activate rules on one quality profile.<br> " +
64 "Requires one of the following permissions:" +
66 " <li>'Administer Quality Profiles'</li>" +
67 " <li>Edit right on the specified quality profile</li>" +
71 .setChangelog(new Change("10.0", "Parameter 'sansTop25' is deprecated"))
74 defineGenericRuleSearchParameters(activate);
76 activate.createParam(PARAM_TARGET_KEY)
77 .setDescription("Quality Profile key on which the rule activation is done. To retrieve a quality profile key please see <code>api/qualityprofiles/search</code>")
79 .setExampleValue(UUID_EXAMPLE_03);
81 activate.createParam(PARAM_TARGET_SEVERITY)
82 .setDescription("Severity to set on the activated rules")
83 .setPossibleValues(Severity.ALL);
87 public void handle(Request request, Response response) throws Exception {
88 String qualityProfileKey = request.mandatoryParam(PARAM_TARGET_KEY);
89 userSession.checkLoggedIn();
90 BulkChangeResult result;
91 try (DbSession dbSession = dbClient.openSession(false)) {
92 QProfileDto profile = wsSupport.getProfile(dbSession, fromKey(qualityProfileKey));
93 wsSupport.checkCanEdit(dbSession, profile);
94 wsSupport.checkNotBuiltIn(profile);
95 RuleQuery ruleQuery = ruleQueryFactory.createRuleQuery(dbSession, request);
96 ruleQuery.setIncludeExternal(false);
97 result = qProfileRules.bulkActivateAndCommit(dbSession, profile, ruleQuery, request.param(PARAM_TARGET_SEVERITY));
100 writeResponse(result, response);