From: Stephane Gamard Date: Tue, 27 May 2014 08:09:28 +0000 (+0200) Subject: SONAR-5245 - SONAR-5245 - Added ActiveRuleService#activeByRuleQuery and ActiveRuleSer... X-Git-Tag: 4.4-RC1~816 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c9bdd428036550a94588ad1b4a3039c3ec604df7;p=sonarqube.git SONAR-5245 - SONAR-5245 - Added ActiveRuleService#activeByRuleQuery and ActiveRuleService#deActiveByRuleQuery --- diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleService.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleService.java index 1831af846c7..f8ce1d36e36 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleService.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleService.java @@ -20,6 +20,7 @@ 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; @@ -36,7 +37,12 @@ import org.sonar.core.rule.RuleParamDto; 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; @@ -191,12 +197,23 @@ public class ActiveRuleService implements ServerComponent { } } - public List 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 deactivate(ActiveRuleKey key, DbSession dbSession) { + List 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); @@ -213,4 +230,51 @@ public class ActiveRuleService implements ServerComponent { } } } + + 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(); + } + + } } diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java index f3744c49dc9..7db8206c29c 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java @@ -20,21 +20,27 @@ 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) { @@ -80,24 +86,37 @@ public class BulkRuleActivationActions implements ServerComponent { } 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")); } } diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRecreateBuiltInActionTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRecreateBuiltInActionTest.java index d973d5a023d..ebf04a98e8e 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRecreateBuiltInActionTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRecreateBuiltInActionTest.java @@ -28,6 +28,7 @@ import org.mockito.runners.MockitoJUnitRunner; 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; @@ -45,10 +46,11 @@ public class QProfileRecreateBuiltInActionTest { @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 diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java index 7b89c45aee1..83a3563b567 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java @@ -25,6 +25,7 @@ import org.junit.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; @@ -37,10 +38,11 @@ public class QProfilesWsTest { @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"); }