use of org.sonar.db.qualityprofile.ActiveRuleParamDto in project sonarqube by SonarSource.
the class RuleUpdater method updateParameters.
private void updateParameters(DbSession dbSession, RuleUpdate update, Context context) {
if (update.isChangeParameters() && update.isCustomRule()) {
RuleDto customRule = context.rule;
Integer templateId = customRule.getTemplateId();
checkNotNull(templateId, "Rule '%s' has no persisted template!", customRule);
Optional<RuleDto> templateRule = dbClient.ruleDao().selectById(templateId, dbSession);
if (!templateRule.isPresent()) {
throw new IllegalStateException(String.format("Template %s of rule %s does not exist", customRule.getTemplateId(), customRule.getKey()));
}
List<String> paramKeys = newArrayList();
// Load active rules and its parameters in cache
Multimap<ActiveRuleDto, ActiveRuleParamDto> activeRuleParamsByActiveRule = getActiveRuleParamsByActiveRule(dbSession, customRule);
// Browse custom rule parameters to create, update or delete them
deleteOrUpdateParameters(dbSession, update, customRule, paramKeys, activeRuleParamsByActiveRule);
}
}
use of org.sonar.db.qualityprofile.ActiveRuleParamDto in project sonarqube by SonarSource.
the class RuleActivatorContextFactory method initActiveRules.
private void initActiveRules(String profileKey, RuleKey ruleKey, RuleActivatorContext context, DbSession session, boolean parent) {
ActiveRuleKey key = ActiveRuleKey.of(profileKey, ruleKey);
Optional<ActiveRuleDto> activeRule = db.activeRuleDao().selectByKey(session, key);
Collection<ActiveRuleParamDto> activeRuleParams = null;
if (activeRule.isPresent()) {
activeRuleParams = db.activeRuleDao().selectParamsByActiveRuleId(session, activeRule.get().getId());
}
if (parent) {
context.setParentActiveRule(activeRule.orNull());
context.setParentActiveRuleParams(activeRuleParams);
} else {
context.setActiveRule(activeRule.orNull());
context.setActiveRuleParams(activeRuleParams);
}
}
use of org.sonar.db.qualityprofile.ActiveRuleParamDto in project sonarqube by SonarSource.
the class CompareActionMediumTest method createActiveRuleWithParam.
private ActiveRuleDto createActiveRuleWithParam(RuleDto rule, QualityProfileDto profile, String value) {
ActiveRuleDto activeRule = createActiveRule(rule, profile);
RuleParamDto paramDto = db.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey()).get(0);
ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(paramDto).setValue(value);
db.activeRuleDao().insertParam(session, activeRule, activeRuleParam);
return activeRule;
}
use of org.sonar.db.qualityprofile.ActiveRuleParamDto in project sonarqube by SonarSource.
the class QProfileBackuperMediumTest method restore_parent_profile.
@Test
public void restore_parent_profile() throws Exception {
// define two parent/child profiles
db.qualityProfileDao().insert(dbSession, newXooP1("org-123"), newXooP2("org-123").setParentKee(XOO_P1_KEY));
dbSession.commit();
// rule x1 is activated on parent profile (so inherited by child profile)
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(Severity.INFO);
activation.setParameter("max", "10");
tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_KEY);
dbSession.commit();
dbSession.clearCache();
activeRuleIndexer.index();
// restore backup of parent profile -> update x1 and propagates to child
tester.get(QProfileBackuper.class).restore(new StringReader(Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore-parent.xml"), StandardCharsets.UTF_8)), null);
// parent profile is updated
List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY);
assertThat(activeRules).hasSize(1);
ActiveRuleDto activeRuleDoc = activeRules.get(0);
assertThat(activeRuleDoc.getSeverityString()).isEqualTo("BLOCKER");
assertThat(activeRuleDoc.getInheritance()).isNull();
ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(params).hasSize(1);
assertThat(params.get(0).getKey()).isEqualTo("max");
assertThat(params.get(0).getValue()).isEqualTo("7");
// child profile is inherited
activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P2_KEY);
assertThat(activeRules).hasSize(1);
activeRuleDoc = activeRules.get(0);
assertThat(activeRuleDoc.getSeverityString()).isEqualTo("BLOCKER");
assertThat(activeRuleDoc.getInheritance()).isEqualTo(INHERITED);
activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(params).hasSize(1);
assertThat(params.get(0).getKey()).isEqualTo("max");
assertThat(params.get(0).getValue()).isEqualTo("7");
}
use of org.sonar.db.qualityprofile.ActiveRuleParamDto in project sonarqube by SonarSource.
the class QProfileBackuperMediumTest method restore_and_update_profile.
@Test
public void restore_and_update_profile() throws Exception {
// create profile P1 with rules x1 and x2 activated
db.qualityProfileDao().insert(dbSession, newXooP1("org-123"));
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(Severity.INFO);
activation.setParameter("max", "10");
tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_NAME);
activation = new RuleActivation(XOO_X2);
activation.setSeverity(Severity.INFO);
tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_NAME);
dbSession.commit();
dbSession.clearCache();
activeRuleIndexer.index();
// restore backup, which activates only x1
// -> update x1 and deactivate x2
tester.get(QProfileBackuper.class).restore(new StringReader(Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)), null);
// Check in db
List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY);
assertThat(activeRules).hasSize(1);
ActiveRuleDto activeRuleDoc = activeRules.get(0);
assertThat(activeRuleDoc.getSeverityString()).isEqualTo("BLOCKER");
assertThat(activeRuleDoc.getInheritance()).isNull();
ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(params).hasSize(1);
assertThat(params.get(0).getKey()).isEqualTo("max");
assertThat(params.get(0).getValue()).isEqualTo("7");
// Check in es
assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(XOO_P1_KEY).setActivation(true))).containsOnly(XOO_X1);
}
Aggregations