aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/sonar-webserver-auth/src/main/java/org/sonar/server/qualityprofile/RuleActivationContext.java2
-rw-r--r--server/sonar-webserver-auth/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java3
-rw-r--r--server/sonar-webserver-auth/src/test/java/org/sonar/server/qualityprofile/RuleActivatorTest.java28
3 files changed, 30 insertions, 3 deletions
diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/qualityprofile/RuleActivationContext.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/qualityprofile/RuleActivationContext.java
index e18c7cfeea9..a2b6dea96c3 100644
--- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/qualityprofile/RuleActivationContext.java
+++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/qualityprofile/RuleActivationContext.java
@@ -224,8 +224,6 @@ class RuleActivationContext {
checkRequest(this.currentRule != null, "Rule with UUID %s not found", ruleUuid);
RuleKey ruleKey = currentRule.get().getKey();
- checkRequest(ruleProfile.getLanguage().equals(currentRule.get().getLanguage()),
- "%s rule %s cannot be activated on %s profile %s", currentRule.get().getLanguage(), ruleKey, ruleProfile.getLanguage(), ruleProfile.getName());
this.currentRulesProfile = ruleProfile;
this.currentProfiles = profilesByUuid.values().stream()
.filter(p -> p.getRulesProfileUuid().equals(ruleProfile.getUuid()))
diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java
index 80fe80d8423..c8b9d4d3e05 100644
--- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java
+++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java
@@ -81,7 +81,8 @@ public class RuleActivator {
RuleDefinitionDto rule = context.getRule().get();
checkRequest(RuleStatus.REMOVED != rule.getStatus(), "Rule was removed: %s", rule.getKey());
checkRequest(!rule.isTemplate(), "Rule template can't be activated on a Quality profile: %s", rule.getKey());
-
+ checkRequest(context.getRulesProfile().getLanguage().equals(rule.getLanguage()),
+ "%s rule %s cannot be activated on %s profile %s", rule.getLanguage(), rule.getKey(), context.getRulesProfile().getLanguage(),context.getRulesProfile().getName());
List<ActiveRuleChange> changes = new ArrayList<>();
ActiveRuleChange change;
boolean stopCascading = false;
diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/qualityprofile/RuleActivatorTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/qualityprofile/RuleActivatorTest.java
index a36173fa87b..27768cfea81 100644
--- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/qualityprofile/RuleActivatorTest.java
+++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/qualityprofile/RuleActivatorTest.java
@@ -38,6 +38,7 @@ import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.db.qualityprofile.RulesProfileDto;
import org.sonar.db.rule.RuleDefinitionDto;
import org.sonar.db.rule.RuleParamDto;
+import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.qualityprofile.DescendantProfilesSupplier.Result;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.util.IntegerTypeValidation;
@@ -48,6 +49,7 @@ import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.assertThrows;
import static org.sonar.server.qualityprofile.ActiveRuleInheritance.INHERITED;
import static org.sonar.server.qualityprofile.ActiveRuleInheritance.OVERRIDES;
@@ -171,6 +173,32 @@ public class RuleActivatorTest {
assertThat(result.get(0).getInheritance()).isNull();
}
+ @Test
+ public void fail_if_rule_language_doesnt_match_qp() {
+ RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage("xoo")
+ .setRepositoryKey("repo")
+ .setRuleKey("rule")
+ .setSeverity(Severity.BLOCKER));
+ QProfileDto qp = db.qualityProfiles().insert(p -> p.setLanguage("xoo2").setKee("qp").setIsBuiltIn(true));
+
+ DbSession session = db.getSession();
+ RuleActivation resetRequest = RuleActivation.create(rule.getUuid());
+ RuleActivationContext context = new RuleActivationContext.Builder()
+ .setProfiles(singletonList(qp))
+ .setBaseProfile(RulesProfileDto.from(qp))
+ .setDate(NOW)
+ .setDescendantProfilesSupplier((profiles, ruleUuids) -> new Result(emptyList(), emptyList(), emptyList()))
+ .setRules(singletonList(rule))
+ .setRuleParams(emptyList())
+ .setActiveRules(emptyList())
+ .setActiveRuleParams(emptyList())
+ .build();
+
+
+ assertThrows("xoo rule repo:rule cannot be activated on xoo2 profile qp", BadRequestException.class,
+ () -> underTest.activate(session, resetRequest, context));
+ }
+
private ActiveRuleDto activateRuleInDb(RulesProfileDto ruleProfile, RuleDefinitionDto rule, RulePriority severity, @Nullable ActiveRuleInheritance inheritance) {
ActiveRuleDto dto = new ActiveRuleDto()