return index.terms(RuleNormalizer.RuleField.ALL_TAGS.field(), query, size);
}
- public void update(RuleUpdate update) {
- checkPermission();
- ruleUpdater.update(update, userSession);
- }
-
public RuleKey create(NewRule newRule) {
checkPermission();
return ruleCreator.create(newRule);
/**
* Update manual rules and custom rules (rules instantiated from templates)
*/
- public boolean update(RuleUpdate update, UserSession userSession) {
+ public boolean update(DbSession dbSession, RuleUpdate update, UserSession userSession) {
if (update.isEmpty()) {
return false;
}
- DbSession dbSession = dbClient.openSession(false);
- try {
- Context context = newContext(update);
- // validate only the changes, not all the rule fields
- apply(update, context, userSession);
- dbClient.deprecatedRuleDao().update(dbSession, context.rule);
- updateParameters(dbSession, update, context);
- dbSession.commit();
- return true;
-
- } finally {
- dbSession.close();
- }
+ Context context = newContext(update);
+ // validate only the changes, not all the rule fields
+ apply(update, context, userSession);
+ dbClient.deprecatedRuleDao().update(dbSession, context.rule);
+ updateParameters(dbSession, update, context);
+ dbSession.commit();
+ return true;
}
/**
*/
package org.sonar.server.rule.ws;
+import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import com.google.common.collect.Sets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.KeyValueFormat;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.rule.Rule;
-import org.sonar.server.rule.RuleService;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.rule.RuleDto;
+import org.sonar.db.rule.RuleParamDto;
import org.sonar.server.rule.RuleUpdate;
+import org.sonar.server.rule.RuleUpdater;
+import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Rules.UpdateResponse;
+import static java.util.Collections.singletonList;
+import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
public class UpdateAction implements RulesWsAction {
public static final String PARAM_STATUS = "status";
public static final String PARAMS = "params";
- private final RuleService service;
- private final RuleMapping mapping;
+ private final DbClient dbClient;
+ private final RuleUpdater ruleUpdater;
+ private final RuleMapper mapper;
+ private final UserSession userSession;
- public UpdateAction(RuleService service, RuleMapping mapping) {
- this.service = service;
- this.mapping = mapping;
+ public UpdateAction(DbClient dbClient, RuleUpdater ruleUpdater, RuleMapper mapper, UserSession userSession) {
+ this.dbClient = dbClient;
+ this.ruleUpdater = ruleUpdater;
+ this.mapper = mapper;
+ this.userSession = userSession;
}
@Override
@Override
public void handle(Request request, Response response) throws Exception {
- RuleUpdate update = readRequest(request);
- service.update(update);
- UpdateResponse updateResponse = buildResponse(update.getRuleKey());
+ checkPermission();
+ DbSession dbSession = dbClient.openSession(false);
+ try {
+ RuleUpdate update = readRequest(dbSession, request);
+ ruleUpdater.update(dbSession, update, userSession);
+ UpdateResponse updateResponse = buildResponse(dbSession, update.getRuleKey());
- writeProtobuf(updateResponse, request, response);
+ writeProtobuf(updateResponse, request, response);
+ } finally {
+ dbClient.closeSession(dbSession);
+ }
}
- private RuleUpdate readRequest(Request request) {
+ private RuleUpdate readRequest(DbSession dbSession, Request request) {
RuleKey key = RuleKey.parse(request.mandatoryParam(PARAM_KEY));
- RuleUpdate update = createRuleUpdate(key);
+ RuleUpdate update = createRuleUpdate(dbSession, key);
readTags(request, update);
readMarkdownNote(request, update);
readDebt(request, update);
return update;
}
- private RuleUpdate createRuleUpdate(RuleKey key) {
- Rule rule = service.getByKey(key);
- if (rule == null) {
- throw new NotFoundException("This rule does not exists : " + key);
- }
- if (rule.templateKey() != null) {
+ private RuleUpdate createRuleUpdate(DbSession dbSession, RuleKey key) {
+ Optional<RuleDto> optionalRule = dbClient.ruleDao().selectByKey(dbSession, key);
+ checkFoundWithOptional(optionalRule, "This rule does not exists : " + key);
+ RuleDto rule = optionalRule.get();
+ if (rule.getTemplateId() != null) {
return RuleUpdate.createForCustomRule(key);
- } else if (rule.isManual()) {
+ } else if (RuleKey.MANUAL_REPOSITORY_KEY.equals(rule.getRepositoryKey())) {
return RuleUpdate.createForManualRule(key);
} else {
return RuleUpdate.createForPluginRule(key);
}
}
- private UpdateResponse buildResponse(RuleKey ruleKey) {
- Rule rule = service.getNonNullByKey(ruleKey);
+ private UpdateResponse buildResponse(DbSession dbSession, RuleKey key) {
+ Optional<RuleDto> optionalRule = dbClient.ruleDao().selectByKey(dbSession, key);
+ checkFoundWithOptional(optionalRule, "Rule not found: " + key);
+ RuleDto rule = optionalRule.get();
+ List<RuleDto> templateRules = new ArrayList<>();
+ if (rule.getTemplateId() != null) {
+ Optional<RuleDto> templateRule = dbClient.ruleDao().selectById(rule.getTemplateId(), dbSession);
+ if (templateRule.isPresent()) {
+ templateRules.add(templateRule.get());
+ }
+ }
+ List<RuleParamDto> ruleParameters = dbClient.ruleDao().selectRuleParamsByRuleIds(dbSession, singletonList(rule.getId()));
UpdateResponse.Builder responseBuilder = UpdateResponse.newBuilder();
- responseBuilder.setRule(mapping.buildRuleResponse(rule, null /* TODO replace by SearchOptions immutable constant */));
+ SearchAction.SearchResult searchResult = new SearchAction.SearchResult()
+ .setRules(singletonList(rule))
+ .setTemplateRules(templateRules)
+ .setRuleParameters(ruleParameters)
+ .setTotal(1L);
+ responseBuilder.setRule(mapper.toWsRule(rule, searchResult, Collections.<String>emptySet()));
return responseBuilder.build();
}
+
+ private void checkPermission() {
+ userSession.checkLoggedIn();
+ userSession.checkPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
+ }
}
import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.api.server.rule.RulesDefinition;
-import org.sonar.api.utils.MessageException;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.ActiveRuleKey;
assertThat(rule.tags()).isEmpty();
// User adds tag
- TESTER.get(RuleUpdater.class).update(RuleUpdate.createForPluginRule(RuleTesting.XOO_X1).setTags(newHashSet("tag2")), userSessionRule);
+ TESTER.get(RuleUpdater.class).update(dbSession, RuleUpdate.createForPluginRule(RuleTesting.XOO_X1).setTags(newHashSet("tag2")), userSessionRule);
dbSession.clearCache();
rule = ruleIndex.getByKey(RuleTesting.XOO_X1);
assertThat(rule.systemTags()).containsOnly("tag1");
assertThat(tags).containsOnly("sys1", "sys2");
}
- @Test
- public void update_rule() {
- userSessionRule.login("me").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
-
- RuleKey key = RuleKey.of("java", "S001");
-
- dao.insert(dbSession, RuleTesting.newDto(key));
- dbSession.commit();
-
- RuleUpdate update = RuleUpdate.createForCustomRule(key);
- update.setMarkdownNote("my *note*");
- service.update(update);
-
- dbSession.clearCache();
-
- RuleDto rule = dao.getNullableByKey(dbSession, key);
- assertThat(rule.getNoteData()).isEqualTo("my *note*");
- assertThat(rule.getNoteUserLogin()).isEqualTo("me");
- }
-
- @Test(expected = UnauthorizedException.class)
- public void do_not_update_if_not_granted() {
- userSessionRule.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
- RuleKey key = RuleKey.of("java", "S001");
-
- dao.insert(dbSession, RuleTesting.newDto(key)
- .setTags(Sets.newHashSet("security"))
- .setSystemTags(Sets.newHashSet("java8", "javadoc")));
- dbSession.commit();
-
- RuleUpdate update = RuleUpdate.createForPluginRule(key).setMarkdownNote("my *note*");
- service.update(update);
- }
-
@Test
public void create_rule() {
userSessionRule.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setTags(Sets.newHashSet("java9"));
try {
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Rule with REMOVED status cannot be updated: squid:S001");
RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY);
assertThat(update.isEmpty()).isTrue();
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
dbSession.clearCache();
RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY);
RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY);
update.setMarkdownNote("my *note*");
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
dbSession.clearCache();
RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY);
dbSession.commit();
RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setMarkdownNote(null);
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
dbSession.clearCache();
RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY);
// java8 is a system tag -> ignore
RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setTags(Sets.newHashSet("bug", "java8"));
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
dbSession.clearCache();
RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY);
dbSession.commit();
RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setTags(null);
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
dbSession.clearCache();
RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY);
DefaultDebtRemediationFunction fn = new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, "1min");
RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY)
.setDebtRemediationFunction(fn);
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
dbSession.clearCache();
// verify debt is overridden
RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY)
.setDebtRemediationFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR, "2d", null));
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
dbSession.clearCache();
// verify debt is overridden
RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY)
.setDebtRemediationFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, "10min"));
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
dbSession.clearCache();
// verify debt is overridden
dbSession.commit();
RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setDebtRemediationFunction(null);
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
dbSession.clearCache();
// verify debt is coming from default values
.setSeverity("MAJOR")
.setStatus(RuleStatus.READY)
.setParameters(ImmutableMap.of("regex", "b.*"));
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
dbSession.clearCache();
.setMarkdownDescription("New description")
.setSeverity("MAJOR")
.setStatus(RuleStatus.READY);
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
dbSession.clearCache();
// Update custom rule parameter 'regex', add 'message' and remove 'format'
RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey())
.setParameters(ImmutableMap.of("regex", "b.*", "message", "a message"));
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
dbSession.clearCache();
.setName("")
.setMarkdownDescription("New desc");
try {
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The name is missing");
.setName("New name")
.setMarkdownDescription("");
try {
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The description is missing");
.setName("New name")
.setMarkdownDescription("New description")
.setSeverity(Severity.CRITICAL);
- updater.update(update, userSessionRule);
+ updater.update(dbSession, update, userSessionRule);
dbSession.clearCache();