Example #1
0
 private void appendRule(ActiveRule activeRule, Writer writer) throws IOException {
   writer.append("<rule><repositoryKey>");
   writer.append(activeRule.getRepositoryKey());
   writer.append("</repositoryKey><key>");
   StringEscapeUtils.escapeXml(writer, activeRule.getRuleKey());
   writer.append("</key>");
   if (activeRule.getSeverity() != null) {
     writer.append("<priority>");
     writer.append(activeRule.getSeverity().name());
     writer.append("</priority>");
   }
   appendRuleParameters(activeRule, writer);
   writer.append("</rule>");
 }
  private List<ProfileDefinition> loadFromDeprecatedRepository(RulesRepository repository) {
    List<ProfileDefinition> result = new ArrayList<ProfileDefinition>();

    for (int index = 0; index < repository.getProvidedProfiles().size(); index++) {
      RulesProfile deprecated = (RulesProfile) repository.getProvidedProfiles().get(index);
      DefaultProfileDefinition providedProfile =
          DefaultProfileDefinition.create(deprecated.getName(), repository.getLanguage().getKey());
      for (ActiveRule deprecatedActiveRule : deprecated.getActiveRules(true)) {
        String repositoryKey = deprecatedActiveRule.getRepositoryKey();
        if (StringUtils.isBlank(repositoryKey)) {
          repositoryKey = getPluginKey(repository);
        }
        Rule rule = ruleFinder.findByKey(repositoryKey, deprecatedActiveRule.getRuleKey());
        if (rule != null) {
          ActiveRule activeRule =
              providedProfile.activateRule(rule, deprecatedActiveRule.getSeverity());
          for (ActiveRuleParam arp : deprecatedActiveRule.getActiveRuleParams()) {
            activeRule.setParameter(arp.getKey(), arp.getValue());
          }
        }
      }
      result.add(providedProfile);
    }
    return result;
  }
  @Test
  public void testImportingPriority() {
    Reader reader =
        new StringReader(
            TestUtils.getResourceContent("/org/sonar/plugins/php/pmd/simple-ruleset.xml"));
    RulesProfile profile = importer.importProfile(reader, messages);

    ActiveRule activeRule =
        profile.getActiveRuleByConfigKey(
            REPOSITORY_KEY, "rulesets/codesize.xml/CyclomaticComplexity");
    assertThat(activeRule.getSeverity(), is(RulePriority.MINOR));

    activeRule =
        profile.getActiveRuleByConfigKey(
            REPOSITORY_KEY, "rulesets/codesize.xml/ExcessiveMethodLength");
    assertThat(activeRule.getSeverity(), is(RulePriority.CRITICAL));
  }
Example #4
0
 /**
  * Note: disabled rules are excluded.
  *
  * @return the list of active rules for a given severity
  */
 public List<ActiveRule> getActiveRules(RulePriority severity) {
   List<ActiveRule> result = new ArrayList<>();
   for (ActiveRule activeRule : activeRules) {
     if (activeRule.getSeverity().equals(severity) && activeRule.isEnabled()) {
       result.add(activeRule);
     }
   }
   return result;
 }
  @Test
  public void testImportingDefaultPriority() {
    Reader reader =
        new StringReader(
            TestUtils.getResourceContent("/org/sonar/plugins/php/pmd/simple-ruleset.xml"));
    RulesProfile profile = importer.importProfile(reader, messages);

    ActiveRule activeRule =
        profile.getActiveRuleByConfigKey(REPOSITORY_KEY, "rulesets/codesize.xml/NPathComplexity");
    assertThat(activeRule.getSeverity(), is(RulePriority.MAJOR)); // reuse the rule default priority
  }
  @Test
  public void importPriorities() {
    Reader reader =
        new StringReader(
            TestUtils.getResourceContent(
                "/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml"));
    RulesProfile profile = importer.importProfile(reader, messages);

    ActiveRule javadocCheck =
        profile.getActiveRuleByConfigKey("checkstyle", "Checker/JavadocPackage");
    assertThat(javadocCheck.getSeverity()).isEqualTo(RulePriority.BLOCKER);
  }
Example #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);
  }
  @Test
  public void properties_should_be_inherited() {
    Reader reader =
        new StringReader(
            TestUtils.getResourceContent(
                "/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/inheritance_of_properties.xml"));
    RulesProfile profile = importer.importProfile(reader, messages);

    ActiveRule activeRule =
        profile.getActiveRuleByConfigKey("checkstyle", "Checker/TreeWalker/MissingOverride");
    assertThat(activeRule.getSeverity()).isEqualTo(RulePriority.BLOCKER);
    assertThat(activeRule.getParameter("javaFiveCompatibility")).isEqualTo("true");
  }
  @Test
  public void priorityIsOptional() {
    Reader reader =
        new StringReader(
            TestUtils.getResourceContent(
                "/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml"));
    RulesProfile profile = importer.importProfile(reader, messages);

    ActiveRule activeRule =
        profile.getActiveRuleByConfigKey("checkstyle", "Checker/TreeWalker/EqualsHashCode");
    assertThat(activeRule.getSeverity())
        .isEqualTo(RulePriority.BLOCKER); // reuse the rule default priority
  }
Example #10
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);
 }
Example #11
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);
 }
 private LintProfile createLintProfile(List<ActiveRule> activeRules) {
   LintProfile profile = new LintProfile();
   Map<String, RulePriority> activeKeys = new HashMap<>();
   List<LintIssue> issues = Lists.newArrayList();
   for (ActiveRule rule : activeRules) {
     activeKeys.put(rule.getRuleKey(), rule.getSeverity());
   }
   for (String ruleKey : ruleKeys) {
     issues.add(getLintIssue(ruleKey, activeKeys));
   }
   // ensure order of issues in output, sort by key.
   Collections.sort(issues, new IssueComparator());
   profile.issues = issues;
   return profile;
 }
Example #13
0
 /** Important : the ruby controller has already create the profile */
 public ValidationMessages importProfile(
     String profileName, String language, String importerKey, String profileDefinition) {
   ValidationMessages messages = ValidationMessages.create();
   ProfileImporter importer = getProfileImporter(importerKey);
   RulesProfile profile = importer.importProfile(new StringReader(profileDefinition), messages);
   if (!messages.hasErrors()) {
     DatabaseSession session = sessionFactory.getSession();
     RulesProfile persistedProfile =
         session.getSingleResult(RulesProfile.class, "name", profileName, "language", language);
     for (ActiveRule activeRule : profile.getActiveRules()) {
       ActiveRule persistedActiveRule =
           persistedProfile.activateRule(activeRule.getRule(), activeRule.getSeverity());
       for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) {
         persistedActiveRule.setParameter(activeRuleParam.getKey(), activeRuleParam.getValue());
       }
     }
     session.saveWithoutFlush(persistedProfile);
     session.commit();
   }
   return messages;
 }
Example #14
0
  private boolean initAndAddIssue(DefaultIssue issue, @Nullable Violation violation) {
    // TODO fail fast : if rule does not exist

    ActiveRule activeRule =
        qProfile.getActiveRule(issue.ruleKey().repository(), issue.ruleKey().rule());
    if (activeRule == null || activeRule.getRule() == null) {
      // rule does not exist or is not enabled -> ignore the issue
      return false;
    }
    issue.setCreationDate(project.getAnalysisDate());
    issue.setUpdateDate(project.getAnalysisDate());
    if (issue.severity() == null) {
      issue.setSeverity(activeRule.getSeverity().name());
    }
    issue.setRemediationCost(technicalDebtCalculator.cost(issue));

    if (filters.accept(issue, violation)) {
      cache.put(issue);
      return true;
    }
    return false;
  }