package org.sonar.server.qualityprofile;
import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.ServerComponent;
import org.sonar.server.db.DbClient;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.qualityprofile.persistence.ActiveRuleDao;
+import org.sonar.server.rule.Rule;
+import org.sonar.server.rule.index.RuleIndex;
+import org.sonar.server.rule.index.RuleQuery;
+import org.sonar.server.rule.index.RuleResult;
import org.sonar.server.search.IndexClient;
+import org.sonar.server.search.QueryOptions;
import org.sonar.server.user.UserSession;
import org.sonar.server.util.TypeValidations;
}
}
- public List<ActiveRuleChange> bulkActivate(BulkRuleActivation activation) {
- verifyPermission(UserSession.get());
- throw new UnsupportedOperationException("TODO");
+ /**
+ * Deactivate a rule on a Quality profile WITHOUT committing db session and WITHOUT checking permissions
+ */
+ public List<ActiveRuleChange> deactivate(ActiveRuleKey key, DbSession dbSession) {
+ List<ActiveRuleChange> changes = Lists.newArrayList();
+ RuleActivationContext context = contextFactory.create(key, dbSession);
+ ActiveRuleChange change;
+ if (context.activeRule() == null) {
+ // not activated !
+ return changes;
+ }
+ change = new ActiveRuleChange(ActiveRuleChange.Type.DEACTIVATED, key);
+ changes.add(change);
+ persist(changes, context, dbSession);
+ return changes;
}
-
private void verifyPermission(UserSession userSession) {
userSession.checkLoggedIn();
userSession.checkGlobalPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
}
}
}
+
+ public void activateByRuleQuery(RuleQuery ruleQuery, QualityProfileKey profile) {
+ verifyPermission(UserSession.get());
+ RuleIndex ruleIndex = index.get(RuleIndex.class);
+ DbSession dbSession = db.openSession(false);
+
+ try {
+ RuleResult result = ruleIndex.search(ruleQuery,
+ QueryOptions.DEFAULT.setOffset(0)
+ .setLimit(Integer.MAX_VALUE)
+ .setFieldsToReturn(ImmutableSet.of(""))
+ );
+
+ for (Rule rule : result.getHits()) {
+ ActiveRuleKey key = ActiveRuleKey.of(profile, rule.key());
+ RuleActivation activation = new RuleActivation(key);
+ activation.setSeverity(rule.severity());
+ this.activate(activation, dbSession);
+ }
+ dbSession.commit();
+ } finally {
+ dbSession.close();
+ }
+ }
+
+ public void deActivateByRuleQuery(RuleQuery ruleQuery, QualityProfileKey profile) {
+ verifyPermission(UserSession.get());
+ RuleIndex ruleIndex = index.get(RuleIndex.class);
+ DbSession dbSession = db.openSession(false);
+
+ try {
+ RuleResult result = ruleIndex.search(ruleQuery,
+ QueryOptions.DEFAULT.setOffset(0)
+ .setLimit(Integer.MAX_VALUE)
+ .setFieldsToReturn(ImmutableSet.of(""))
+ );
+
+ for (Rule rule : result.getHits()) {
+ ActiveRuleKey key = ActiveRuleKey.of(profile, rule.key());
+ this.deactivate(key, dbSession);
+ }
+ dbSession.commit();
+ } finally {
+ dbSession.close();
+ }
+
+ }
}
package org.sonar.server.qualityprofile.ws;
import org.sonar.api.ServerComponent;
+import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.RequestHandler;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
+import org.sonar.core.qualityprofile.db.QualityProfileKey;
import org.sonar.server.qualityprofile.ActiveRuleService;
-import org.sonar.server.qualityprofile.BulkRuleActivation;
+import org.sonar.server.rule.RuleService;
+import org.sonar.server.rule.index.RuleQuery;
import org.sonar.server.rule.ws.SearchAction;
+import org.sonar.server.search.ws.SearchOptions;
public class BulkRuleActivationActions implements ServerComponent {
private final ActiveRuleService service;
+ private final RuleService ruleService;
- public BulkRuleActivationActions(ActiveRuleService service) {
+ public BulkRuleActivationActions(ActiveRuleService service, RuleService ruleService) {
this.service = service;
+ this.ruleService = ruleService;
}
void define(WebService.NewController controller) {
}
private void defineProfileKeyParameters(WebService.NewAction action) {
- action.createParam("target_profile_lang")
- .setDescription("Profile language")
+ action.createParam("profile_key")
+ .setDescription("Quality Profile Key. To retrieve a profileKey for a given language please see the /api/qprofile documentation")
.setRequired(true)
- .setExampleValue("java");
-
- action.createParam("target_profile_name")
- .setDescription("Profile name")
- .setRequired(true)
- .setExampleValue("My profile");
+ .setExampleValue("java:My Profile");
}
private void bulkActivate(Request request, Response response) throws Exception {
- BulkRuleActivation activation = new BulkRuleActivation();
- // TODO
- service.bulkActivate(activation);
+ service.activateByRuleQuery(createRuleQuery(request), readKey(request));
}
private void bulkDeactivate(Request request, Response response) throws Exception {
- // TODO
+ service.deActivateByRuleQuery(createRuleQuery(request), readKey(request));
+ }
+
+ private RuleQuery createRuleQuery(Request request) {
+ RuleQuery query = ruleService.newRuleQuery();
+ query.setQueryText(request.param(SearchOptions.PARAM_TEXT_QUERY));
+ query.setSeverities(request.paramAsStrings(SearchAction.PARAM_SEVERITIES));
+ query.setRepositories(request.paramAsStrings(SearchAction.PARAM_REPOSITORIES));
+ query.setStatuses(request.paramAsEnums(SearchAction.PARAM_STATUSES, RuleStatus.class));
+ query.setLanguages(request.paramAsStrings(SearchAction.PARAM_LANGUAGES));
+ query.setDebtCharacteristics(request.paramAsStrings(SearchAction.PARAM_DEBT_CHARACTERISTICS));
+ query.setHasDebtCharacteristic(request.paramAsBoolean(SearchAction.PARAM_HAS_DEBT_CHARACTERISTIC));
+ query.setActivation(request.paramAsBoolean(SearchAction.PARAM_ACTIVATION));
+ query.setQProfileKey(request.param(SearchAction.PARAM_QPROFILE));
+ query.setTags(request.paramAsStrings(SearchAction.PARAM_TAGS));
+ query.setAllOfTags(request.paramAsStrings(SearchAction.PARAM_ALL_OF_TAGS));
+ return query;
+ }
+
+ private QualityProfileKey readKey(Request request) {
+ return QualityProfileKey.parse(request.mandatoryParam("profile_key"));
}
}
import org.sonar.server.qualityprofile.ActiveRuleService;
import org.sonar.server.qualityprofile.QProfileBackup;
import org.sonar.server.qualityprofile.QProfileResult;
+import org.sonar.server.rule.RuleService;
import org.sonar.server.ws.WsTester;
import static com.google.common.collect.Lists.newArrayList;
@Before
public void setUp() throws Exception {
ActiveRuleService activeRuleService = mock(ActiveRuleService.class);
+ RuleService ruleService = mock(RuleService.class);
tester = new WsTester(new QProfilesWs(
new QProfileRecreateBuiltInAction(qProfileBackup),
new RuleActivationActions(activeRuleService),
- new BulkRuleActivationActions(activeRuleService)));
+ new BulkRuleActivationActions(activeRuleService, ruleService)));
}
@Test
import org.sonar.api.server.ws.WebService;
import org.sonar.server.qualityprofile.ActiveRuleService;
import org.sonar.server.qualityprofile.QProfileBackup;
+import org.sonar.server.rule.RuleService;
import org.sonar.server.ws.WsTester;
import static org.fest.assertions.Assertions.assertThat;
@Before
public void setUp() {
ActiveRuleService activeRuleService = mock(ActiveRuleService.class);
+ RuleService ruleService = mock(RuleService.class);
controller = new WsTester(new QProfilesWs(new QProfileRecreateBuiltInAction(
mock(QProfileBackup.class)),
new RuleActivationActions(activeRuleService),
- new BulkRuleActivationActions(activeRuleService)
+ new BulkRuleActivationActions(activeRuleService, ruleService)
)).controller("api/qualityprofiles");
}