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;
  }
 private void processProperties(SMInputCursor ruleCursor, ActiveRule activeRule)
     throws XMLStreamException {
   SMInputCursor propertyCursor = ruleCursor.childElementCursor(PROPERTY_NODE);
   while (propertyCursor.getNext() != null) {
     String key = propertyCursor.getAttrValue(PROPERTY_NAME_ATTR);
     String value = propertyCursor.getAttrValue(PROPERTY_VALUE_ATTR);
     activeRule.setParameter(key, value);
   }
 }
  private void processRules(
      SMInputCursor rulesCursor, RulesProfile profile, ValidationMessages messages)
      throws XMLStreamException {
    Map<String, String> parameters = new HashMap<>();
    while (rulesCursor.getNext() != null) {
      SMInputCursor ruleCursor = rulesCursor.childElementCursor();

      String repositoryKey = null;
      String key = null;
      RulePriority priority = null;
      parameters.clear();

      while (ruleCursor.getNext() != null) {
        String nodeName = ruleCursor.getLocalName();

        if (StringUtils.equals("repositoryKey", nodeName)) {
          repositoryKey = StringUtils.trim(ruleCursor.collectDescendantText(false));

        } else if (StringUtils.equals("key", nodeName)) {
          key = StringUtils.trim(ruleCursor.collectDescendantText(false));

        } else if (StringUtils.equals("priority", nodeName)) {
          priority =
              RulePriority.valueOf(StringUtils.trim(ruleCursor.collectDescendantText(false)));

        } else if (StringUtils.equals("parameters", nodeName)) {
          SMInputCursor propsCursor = ruleCursor.childElementCursor("parameter");
          processParameters(propsCursor, parameters);
        }
      }

      Rule rule = ruleFinder.findByKey(repositoryKey, key);
      if (rule == null) {
        messages.addWarningText("Rule not found: " + ruleToString(repositoryKey, key));

      } else {
        ActiveRule activeRule = profile.activateRule(rule, priority);
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
          if (rule.getParam(entry.getKey()) == null) {
            messages.addWarningText(
                "The parameter '"
                    + entry.getKey()
                    + "' does not exist in the rule: "
                    + ruleToString(repositoryKey, key));
          } else {
            activeRule.setParameter(entry.getKey(), entry.getValue());
          }
        }
      }
    }
  }
 private ProfileDefinition loadFromDeprecatedCheckProfile(CheckProfile cp) {
   DefaultProfileDefinition definition =
       DefaultProfileDefinition.create(cp.getName(), cp.getLanguage());
   for (Check check : cp.getChecks()) {
     RulePriority priority = null;
     if (check.getPriority() != null) {
       priority = RulePriority.fromCheckPriority(check.getPriority());
     }
     Rule rule = ruleFinder.findByKey(check.getRepositoryKey(), check.getTemplateKey());
     if (rule != null) {
       ActiveRule activeRule = definition.activateRule(rule, priority);
       for (Map.Entry<String, String> entry : check.getProperties().entrySet()) {
         activeRule.setParameter(entry.getKey(), entry.getValue());
       }
     }
   }
   return definition;
 }
Exemple #5
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;
 }
  @Test
  public void recreate_built_in_profiles_from_language() throws Exception {
    String name = "Default";
    String language = "java";

    RulesProfile profile = RulesProfile.create(name, language);
    Rule rule = Rule.create("pmd", "rule");
    rule.createParameter("max");
    ActiveRule activeRule = profile.activateRule(rule, RulePriority.BLOCKER);
    activeRule.setParameter("max", "10");

    ProfileDefinition profileDefinition = mock(ProfileDefinition.class);
    when(profileDefinition.createProfile(any(ValidationMessages.class))).thenReturn(profile);
    definitions.add(profileDefinition);

    when(ruleDao.getNullableByKey(session, RuleKey.of("pmd", "rule")))
        .thenReturn(new RuleDto().setId(10).setSeverity("INFO"));

    when(qProfileOperations.newProfile(
            eq(name), eq(language), eq(true), any(UserSession.class), eq(session)))
        .thenReturn(new QProfile().setId(1));

    backup.recreateBuiltInProfilesByLanguage(language);

    verify(qProfileActiveRuleOperations)
        .createActiveRule(
            eq(QualityProfileKey.of(name, language)),
            eq(RuleKey.of("pmd", "rule")),
            eq("BLOCKER"),
            eq(session));
    verify(qProfileActiveRuleOperations)
        .updateActiveRuleParam(any(ActiveRuleDto.class), eq("max"), eq("10"), eq(session));
    verifyNoMoreInteractions(qProfileActiveRuleOperations);

    verify(dryRunCache).reportGlobalModification(session);
    verify(session).commit();
  }