diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-06-03 23:18:21 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-06-03 23:18:21 +0200 |
commit | f5bfa86e0f4c92d61f66dc965010db78823ebdad (patch) | |
tree | 5f703062a995b8afc3bf55183200c5e12591bd67 /sonar-server/src/main | |
parent | 65920936508883a128dab1b77f9a49332bb5d363 (diff) | |
download | sonarqube-f5bfa86e0f4c92d61f66dc965010db78823ebdad.tar.gz sonarqube-f5bfa86e0f4c92d61f66dc965010db78823ebdad.zip |
SONAR-5007 ignore warnings when restoring a backup
Diffstat (limited to 'sonar-server/src/main')
5 files changed, 51 insertions, 39 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java index 22569765d44..4d93115cd37 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java @@ -26,6 +26,7 @@ import org.apache.commons.lang.StringUtils; import org.codehaus.staxmate.SMInputFactory; import org.codehaus.staxmate.in.SMHierarchicCursor; import org.codehaus.staxmate.in.SMInputCursor; +import org.slf4j.LoggerFactory; import org.sonar.api.ServerComponent; import org.sonar.api.rule.RuleKey; import org.sonar.api.utils.text.XmlWriter; @@ -35,6 +36,7 @@ import org.sonar.core.qualityprofile.db.ActiveRuleKey; import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.core.qualityprofile.db.QualityProfileKey; import org.sonar.server.db.DbClient; +import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.qualityprofile.index.ActiveRuleIndex; import org.sonar.server.search.IndexClient; @@ -173,7 +175,12 @@ public class QProfileBackuper implements ServerComponent { RuleActivation activation = new RuleActivation(ActiveRuleKey.of(profileKey, ruleKey)); activation.setSeverity(severity); activation.setParameters(parameters); - activator.activate(dbSession, activation); + try { + activator.activate(dbSession, activation); + } catch (BadRequestException e) { + // TODO should return warnings instead of logging warnings + LoggerFactory.getLogger(getClass()).warn(e.getMessage()); + } rulesToDeactivate.remove(ruleKey); } for (RuleKey ruleKey : rulesToDeactivate) { diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivation.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivation.java index 255562e851e..e7c856962f5 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivation.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivation.java @@ -39,29 +39,44 @@ public class RuleActivation { this.key = key; } - public boolean isCascade(){ + /** + * For internal use + */ + boolean isCascade() { return this.cascade; } - public RuleActivation isCascade(boolean b){ + /** + * For internal use + */ + RuleActivation isCascade(boolean b) { this.cascade = b; return this; } - public boolean isReset() { - return severity==null && parameters.isEmpty(); + /** + * For internal use + */ + boolean isReset() { + return severity == null && parameters.isEmpty(); } public RuleActivation setSeverity(@Nullable String s) { - if (s != null) { - if (!Severity.ALL.contains(s)) { - throw new IllegalArgumentException("Unknown severity: " + s); - } + if (s != null && !Severity.ALL.contains(s)) { + throw new IllegalArgumentException("Unknown severity: " + s); } this.severity = s; return this; } + /** + * Optional severity. Use the parent severity or default rule severity if null. + */ + @CheckForNull + public String getSeverity() { + return severity; + } + public RuleActivation setParameter(String key, @Nullable String value) { String sanitizedValue = Strings.emptyToNull(value); if (value == null) { @@ -85,9 +100,4 @@ public class RuleActivation { public Map<String, String> getParameters() { return parameters; } - - @CheckForNull - public String getSeverity() { - return severity; - } } diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivationContextFactory.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivationContextFactory.java index 5188850c22a..272786895c0 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivationContextFactory.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivationContextFactory.java @@ -31,6 +31,7 @@ import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.core.qualityprofile.db.QualityProfileKey; import org.sonar.core.rule.RuleDto; import org.sonar.server.db.DbClient; +import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.rule.Rule; import java.util.Collection; @@ -51,7 +52,7 @@ public class RuleActivationContextFactory implements ServerComponent { QualityProfileDto profile = initProfile(key, context, session, false); initActiveRules(key, context, session, false); if (!profile.getLanguage().equals(rule.getLanguage())) { - throw new IllegalArgumentException(String.format("Rule %s and profile %s have different languages", rule.getKey(), profile.getKey())); + throw new BadRequestException(String.format("Rule %s and profile %s have different languages", rule.getKey(), profile.getKey())); } if (profile.getParent() != null) { @@ -66,16 +67,16 @@ public class RuleActivationContextFactory implements ServerComponent { private RuleDto initRule(RuleKey ruleKey, RuleActivationContext context, DbSession dbSession) { RuleDto rule = db.ruleDao().getNullableByKey(dbSession, ruleKey); if (rule == null) { - throw new IllegalArgumentException("Rule not found: " + ruleKey); + throw new BadRequestException("Rule not found: " + ruleKey); } if (RuleStatus.REMOVED == rule.getStatus()) { - throw new IllegalArgumentException("Rule was removed: " + ruleKey); + throw new BadRequestException("Rule was removed: " + ruleKey); } if (Cardinality.MULTIPLE.equals(rule.getCardinality())) { - throw new IllegalArgumentException("Rule template can't be activated on a Quality profile: " + ruleKey); + throw new BadRequestException("Rule template can't be activated on a Quality profile: " + ruleKey); } if (Rule.MANUAL_REPOSITORY_KEY.equals(rule.getRepositoryKey())) { - throw new IllegalArgumentException("Manual rule can't be activated on a Quality profile: " + ruleKey); + throw new BadRequestException("Manual rule can't be activated on a Quality profile: " + ruleKey); } context.setRule(rule); context.setRuleParams(db.ruleDao().findRuleParamsByRuleKey(dbSession, rule.getKey())); @@ -85,7 +86,7 @@ public class RuleActivationContextFactory implements ServerComponent { private QualityProfileDto initProfile(ActiveRuleKey key, RuleActivationContext context, DbSession session, boolean parent) { QualityProfileDto profile = db.qualityProfileDao().getByKey(key.qProfile(),session); if (profile == null) { - throw new IllegalArgumentException("Quality profile not found: " + key.qProfile()); + throw new BadRequestException("Quality profile not found: " + key.qProfile()); } if (parent) { context.setParentProfile(profile); diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java index dcab4ba292f..de87d2ac1da 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java @@ -77,6 +77,9 @@ public class RuleActivator implements ServerComponent { /** * Activate a rule on a Quality profile. Update configuration (severity/parameters) if the rule is already * activated. + * + * @throws org.sonar.server.exceptions.BadRequestException if the profile, the rule or a rule parameter does + * not exist */ List<ActiveRuleChange> activate(RuleActivation activation) { DbSession dbSession = db.openSession(false); @@ -96,7 +99,6 @@ public class RuleActivator implements ServerComponent { * Activate the rule WITHOUT committing db session */ List<ActiveRuleChange> activate(DbSession dbSession, RuleActivation activation) { - RuleActivationContext context = contextFactory.create(activation.getKey(), dbSession); List<ActiveRuleChange> changes = Lists.newArrayList(); ActiveRuleChange change; @@ -145,7 +147,6 @@ public class RuleActivator implements ServerComponent { } private List<ActiveRuleChange> cascadeActivation(DbSession session, RuleActivation activation) { - List<ActiveRuleChange> changes = Lists.newArrayList(); // get all inherited profiles @@ -284,20 +285,15 @@ public class RuleActivator implements ServerComponent { try { // TODO pb because limited to QueryOptions.MAX_LIMIT RuleResult result = ruleIndex.search(ruleQuery, - QueryOptions.DEFAULT.setOffset(0) - .setLimit(Integer.MAX_VALUE) - .setFieldsToReturn(ImmutableSet.of(RuleNormalizer.RuleField.IS_TEMPLATE.field(), RuleNormalizer.RuleField.SEVERITY.field())) - ); + new QueryOptions() + .setMaxLimit() + .setFieldsToReturn(ImmutableSet.of(RuleNormalizer.RuleField.IS_TEMPLATE.field()))); for (Rule rule : result.getHits()) { if (!rule.isTemplate()) { ActiveRuleKey key = ActiveRuleKey.of(profileKey, rule.key()); RuleActivation activation = new RuleActivation(key); - if (severity != null && !severity.isEmpty()) { - activation.setSeverity(severity); - } else { - activation.setSeverity(rule.severity()); - } + activation.setSeverity(severity); for (ActiveRuleChange active : this.activate(dbSession, activation)) { results.put("activated", active.getKey().ruleKey().toString()); } @@ -318,12 +314,7 @@ public class RuleActivator implements ServerComponent { DbSession dbSession = db.openSession(false); try { - RuleResult result = ruleIndex.search(ruleQuery, - QueryOptions.DEFAULT.setOffset(0) - // TODO pb because limited to QueryOptions.MAX_LIMIT - .setLimit(Integer.MAX_VALUE) - .setFieldsToReturn(ImmutableSet.of(RuleNormalizer.RuleField.IS_TEMPLATE.field(), RuleNormalizer.RuleField.SEVERITY.field())) - ); + RuleResult result = ruleIndex.search(ruleQuery, new QueryOptions()); for (Rule rule : result.getHits()) { ActiveRuleKey key = ActiveRuleKey.of(profile, rule.key()); diff --git a/sonar-server/src/main/java/org/sonar/server/search/QueryOptions.java b/sonar-server/src/main/java/org/sonar/server/search/QueryOptions.java index 881b4d87470..ae19e77a69f 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/QueryOptions.java +++ b/sonar-server/src/main/java/org/sonar/server/search/QueryOptions.java @@ -34,8 +34,6 @@ import java.util.Set; */ public class QueryOptions { - public static final QueryOptions DEFAULT = new QueryOptions().setLimit(Integer.MAX_VALUE); - public static final int DEFAULT_OFFSET = 0; public static final int DEFAULT_LIMIT = 10; public static final int MAX_LIMIT = 500; @@ -103,6 +101,11 @@ public class QueryOptions { return this; } + public QueryOptions setMaxLimit() { + this.limit = MAX_LIMIT; + return this; + } + public Set<String> getFieldsToReturn() { return fieldsToReturn; } |