]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5245 - SONAR-5245 - Added ActiveRuleService#activeByRuleQuery and ActiveRuleSer...
authorStephane Gamard <stephane.gamard@searchbox.com>
Tue, 27 May 2014 08:09:28 +0000 (10:09 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Tue, 27 May 2014 09:57:56 +0000 (11:57 +0200)
sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleService.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRecreateBuiltInActionTest.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java

index 1831af846c7624202f8864cbbe7c8938881f24d5..f8ce1d36e3617e64f4def07bf993243cd0360400 100644 (file)
@@ -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<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);
@@ -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();
+    }
+
+  }
 }
index f3744c49dc9185026819eb274760b4a18a3d84a5..7db8206c29c4e056fd6a63f57b790bc3d670340d 100644 (file)
 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"));
   }
 }
index d973d5a023dc41ade534d1848b3f5376862cf98a..ebf04a98e8e13a4888038d4809f7db764ebdc7dc 100644 (file)
@@ -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
index 7b89c45aee1ec16a9675130053792b619c27f5cd..83a3563b567ae658aa3dbc884576e6f457e59642 100644 (file)
@@ -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");
   }