Пример #1
0
 private void importActiveRules(RulesDao rulesDao, RulesProfile profile) {
   for (Iterator<ActiveRule> iar = profile.getActiveRules(true).iterator(); iar.hasNext(); ) {
     ActiveRule activeRule = iar.next();
     Rule unMarshalledRule = activeRule.getRule();
     Rule matchingRuleInDb =
         rulesDao.getRuleByKey(unMarshalledRule.getRepositoryKey(), unMarshalledRule.getKey());
     if (matchingRuleInDb == null) {
       LoggerFactory.getLogger(getClass())
           .error(
               "Unable to find active rule "
                   + unMarshalledRule.getRepositoryKey()
                   + ":"
                   + unMarshalledRule.getKey());
       iar.remove();
       continue;
     }
     activeRule.setRule(matchingRuleInDb);
     activeRule.setRulesProfile(profile);
     activeRule.getActiveRuleParams();
     for (Iterator<ActiveRuleParam> irp = activeRule.getActiveRuleParams().iterator();
         irp.hasNext(); ) {
       ActiveRuleParam activeRuleParam = irp.next();
       RuleParam unMarshalledRP = activeRuleParam.getRuleParam();
       RuleParam matchingRPInDb = rulesDao.getRuleParam(matchingRuleInDb, unMarshalledRP.getKey());
       if (matchingRPInDb == null) {
         LoggerFactory.getLogger(getClass())
             .error("Unable to find active rule parameter " + unMarshalledRP.getKey());
         irp.remove();
         continue;
       }
       activeRuleParam.setActiveRule(activeRule);
       activeRuleParam.setRuleParam(matchingRPInDb);
     }
   }
 }
Пример #2
0
 /** Sets the rule parameters */
 public Rule setParams(List<RuleParam> params) {
   this.params.clear();
   for (RuleParam param : params) {
     param.setRule(this);
     this.params.add(param);
   }
   return this;
 }
Пример #3
0
 public RuleParam getParam(String key) {
   for (RuleParam param : params) {
     if (StringUtils.equals(key, param.getKey())) {
       return param;
     }
   }
   return null;
 }
Пример #4
0
 /** Deal with creation of ActiveRuleChange item when a rule is disabled on a profile */
 private void ruleDisabled(RulesProfile profile, ActiveRule disabledRule, String userName) {
   incrementProfileVersionIfNeeded(profile);
   ActiveRuleChange rc = new ActiveRuleChange(userName, profile, disabledRule.getRule());
   rc.setEnabled(false);
   rc.setOldSeverity(disabledRule.getSeverity());
   if (disabledRule.getRule().getParams() != null) {
     for (RuleParam p : disabledRule.getRule().getParams()) {
       String oldParam = disabledRule.getParameter(p.getKey());
       if (oldParam != null) {
         rc.setParameterChange(p.getKey(), oldParam, null);
       }
     }
   }
   getSession().saveWithoutFlush(rc);
 }
Пример #5
0
 /** Deal with creation of ActiveRuleChange item when a rule is enabled on a profile */
 private void ruleEnabled(RulesProfile profile, ActiveRule newActiveRule, String userName) {
   incrementProfileVersionIfNeeded(profile);
   ActiveRuleChange rc = new ActiveRuleChange(userName, profile, newActiveRule.getRule());
   rc.setEnabled(true);
   rc.setNewSeverity(newActiveRule.getSeverity());
   if (newActiveRule.getRule().getParams() != null) {
     for (RuleParam p : newActiveRule.getRule().getParams()) {
       String newParam = newActiveRule.getParameter(p.getKey());
       if (newParam != null) {
         rc.setParameterChange(p.getKey(), null, newParam);
       }
     }
   }
   getSession().saveWithoutFlush(rc);
 }
Пример #6
0
 private void addRuleProperty(Rule rule, Field field) {
   org.sonar.check.RuleProperty propertyAnnotation =
       field.getAnnotation(org.sonar.check.RuleProperty.class);
   if (propertyAnnotation != null) {
     String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName());
     RuleParam param = rule.createParameter(fieldKey);
     param.setDescription(propertyAnnotation.description());
     param.setDefaultValue(propertyAnnotation.defaultValue());
     if (!StringUtils.isBlank(propertyAnnotation.type())) {
       try {
         param.setType(PropertyType.valueOf(propertyAnnotation.type().trim()).name());
       } catch (IllegalArgumentException e) {
         throw new SonarException("Invalid property type [" + propertyAnnotation.type() + "]", e);
       }
     } else {
       param.setType(guessType(field.getType()).name());
     }
   }
 }
Пример #7
0
  /**
   * Deal with creation of ActiveRuleChange item when a rule is changed (severity and/or param(s))
   * on a profile
   */
  private void ruleChanged(
      RulesProfile profile, ActiveRule oldActiveRule, ActiveRule newActiveRule, String userName) {
    incrementProfileVersionIfNeeded(profile);
    ActiveRuleChange rc = new ActiveRuleChange(userName, profile, newActiveRule.getRule());

    if (oldActiveRule.getSeverity() != newActiveRule.getSeverity()) {
      rc.setOldSeverity(oldActiveRule.getSeverity());
      rc.setNewSeverity(newActiveRule.getSeverity());
    }
    if (oldActiveRule.getRule().getParams() != null) {
      for (RuleParam p : oldActiveRule.getRule().getParams()) {
        String oldParam = oldActiveRule.getParameter(p.getKey());
        String newParam = newActiveRule.getParameter(p.getKey());
        if (!StringUtils.equals(oldParam, newParam)) {
          rc.setParameterChange(p.getKey(), oldParam, newParam);
        }
      }
    }

    getSession().saveWithoutFlush(rc);
  }
Пример #8
0
  @Test
  public void update_custom_rule_with_empty_parameter() {
    // Create template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
    ruleDao.insert(dbSession, templateRule);
    RuleParamDto templateRuleParam =
        RuleParamDto.createFor(templateRule)
            .setName("regex")
            .setType("STRING")
            .setDescription("Reg ex");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam);

    // Create custom rule
    RuleDto customRule =
        RuleTesting.newCustomRule(templateRule)
            .setName("Old name")
            .setDescription("Old description")
            .setSeverity(Severity.MINOR)
            .setStatus(RuleStatus.BETA);
    ruleDao.insert(dbSession, customRule);
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam);

    dbSession.commit();

    // Update custom rule without setting a value for the parameter
    RuleUpdate update =
        RuleUpdate.createForCustomRule(customRule.getKey())
            .setName("New name")
            .setMarkdownDescription("New description")
            .setSeverity("MAJOR")
            .setStatus(RuleStatus.READY);
    updater.update(update, userSessionRule);

    dbSession.clearCache();

    // Verify custom rule is updated
    Rule customRuleReloaded = ruleIndex.getByKey(customRule.getKey());
    RuleParam param = customRuleReloaded.params().get(0);
    assertThat(param.defaultValue()).isNull();
  }
Пример #9
0
  private static void processParameter(Rule rule, SMInputCursor ruleC) throws XMLStreamException {
    RuleParam param = rule.createParameter();

    String keyAttribute = ruleC.getAttrValue("key");
    if (StringUtils.isNotBlank(keyAttribute)) {
      /* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */
      param.setKey(StringUtils.trim(keyAttribute));
    }

    String typeAttribute = ruleC.getAttrValue("type");
    if (StringUtils.isNotBlank(typeAttribute)) {
      /* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */
      param.setType(type(StringUtils.trim(typeAttribute)));
    }

    SMInputCursor paramC = ruleC.childElementCursor();
    while (paramC.getNext() != null) {
      String propNodeName = paramC.getLocalName();
      String propText = StringUtils.trim(paramC.collectDescendantText(false));
      if (StringUtils.equalsIgnoreCase("key", propNodeName)) {
        param.setKey(propText);

      } else if (StringUtils.equalsIgnoreCase("description", propNodeName)) {
        param.setDescription(propText);

      } else if (StringUtils.equalsIgnoreCase("type", propNodeName)) {
        param.setType(type(propText));

      } else if (StringUtils.equalsIgnoreCase("defaultValue", propNodeName)) {
        param.setDefaultValue(propText);
      }
    }
    if (Strings.isNullOrEmpty(param.getKey())) {
      throw new SonarException("Node <key> is missing in <param>");
    }
  }
Пример #10
0
  @Test
  public void update_active_rule_parameters_when_updating_custom_rule() {
    // Create template rule with 3 parameters
    RuleDto templateRule =
        RuleTesting.newTemplateRule(RuleKey.of("java", "S001")).setLanguage("xoo");
    ruleDao.insert(dbSession, templateRule);
    RuleParamDto templateRuleParam1 =
        RuleParamDto.createFor(templateRule)
            .setName("regex")
            .setType("STRING")
            .setDescription("Reg ex")
            .setDefaultValue(".*");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam1);
    RuleParamDto templateRuleParam2 =
        RuleParamDto.createFor(templateRule)
            .setName("format")
            .setType("STRING")
            .setDescription("format")
            .setDefaultValue("csv");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam2);
    RuleParamDto templateRuleParam3 =
        RuleParamDto.createFor(templateRule)
            .setName("message")
            .setType("STRING")
            .setDescription("message");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam3);

    // Create custom rule
    RuleDto customRule =
        RuleTesting.newCustomRule(templateRule).setSeverity(Severity.MAJOR).setLanguage("xoo");
    ruleDao.insert(dbSession, customRule);
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam1.setDefaultValue("a.*"));
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam2.setDefaultValue("txt"));
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam3);

    // Create a quality profile
    QualityProfileDto profileDto = QProfileTesting.newXooP1();
    db.qualityProfileDao().insert(dbSession, profileDto);
    dbSession.commit();

    // Activate the custom rule
    RuleActivation activation =
        new RuleActivation(customRule.getKey()).setSeverity(Severity.BLOCKER);
    tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_NAME);
    dbSession.commit();
    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);

    dbSession.clearCache();

    // Verify custom rule parameters has been updated
    Rule customRuleReloaded = ruleIndex.getByKey(customRule.getKey());
    assertThat(customRuleReloaded.params()).hasSize(3);
    assertThat(customRuleReloaded.param("regex")).isNotNull();
    assertThat(customRuleReloaded.param("regex").defaultValue()).isEqualTo("b.*");
    assertThat(customRuleReloaded.param("message")).isNotNull();
    assertThat(customRuleReloaded.param("message").defaultValue()).isEqualTo("a message");
    assertThat(customRuleReloaded.param("format")).isNotNull();
    assertThat(customRuleReloaded.param("format").defaultValue()).isNull();

    RuleParam param = customRuleReloaded.params().get(0);
    assertThat(param.defaultValue()).isEqualTo("b.*");

    // Verify active rule parameters has been updated
    ActiveRule activeRule =
        tester
            .get(ActiveRuleIndex.class)
            .getByKey(ActiveRuleKey.of(profileDto.getKey(), customRule.getKey()));
    assertThat(activeRule.params()).hasSize(2);
    assertThat(activeRule.params().get("regex")).isEqualTo("b.*");
    assertThat(activeRule.params().get("message")).isEqualTo("a message");
    assertThat(activeRule.params().get("format")).isNull();

    // Verify that severity has not changed
    assertThat(activeRule.severity()).isEqualTo(Severity.BLOCKER);
  }