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.Severity;
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.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>" +
73 defineGenericRuleSearchParameters(activate);
75 activate.createParam(PARAM_TARGET_KEY)
76 .setDescription("Quality Profile key on which the rule activation is done. To retrieve a quality profile key please see <code>api/qualityprofiles/search</code>")
77 .setDeprecatedKey("profile_key", "6.5")
79 .setExampleValue(UUID_EXAMPLE_03);
81 activate.createParam(PARAM_TARGET_SEVERITY)
82 .setDescription("Severity to set on the activated rules")
83 .setDeprecatedKey("activation_severity", "6.5")
84 .setPossibleValues(Severity.ALL);
88 public void handle(Request request, Response response) throws Exception {
89 String qualityProfileKey = request.mandatoryParam(PARAM_TARGET_KEY);
90 userSession.checkLoggedIn();
91 BulkChangeResult result;
92 try (DbSession dbSession = dbClient.openSession(false)) {
93 QProfileDto profile = wsSupport.getProfile(dbSession, fromKey(qualityProfileKey));
94 OrganizationDto organization = wsSupport.getOrganization(dbSession, profile);
95 wsSupport.checkCanEdit(dbSession, organization, profile);
96 wsSupport.checkNotBuiltIn(profile);
97 RuleQuery ruleQuery = ruleQueryFactory.createRuleQuery(dbSession, request);
98 ruleQuery.setIncludeExternal(false);
99 result = qProfileRules.bulkActivateAndCommit(dbSession, profile, ruleQuery, request.param(PARAM_TARGET_SEVERITY));
102 writeResponse(result, response);