示例#1
0
  private void execute(RulesDefinition... defs) {
    RuleDefinitionsLoader loader =
        new RuleDefinitionsLoader(
            mock(DeprecatedRulesDefinitionLoader.class),
            new RuleRepositories(),
            mock(CommonRuleDefinitionsImpl.class),
            defs);
    Languages languages = mock(Languages.class);
    when(languages.get("java")).thenReturn(mock(Language.class));

    RegisterRules task = new RegisterRules(loader, ruleActivator, dbClient, languages);
    task.start();
    // Execute a commit to refresh session state as the task is using its own session
    dbTester.getSession().commit();
  }
示例#2
0
  private void writeResult(JsonWriter json, QProfileResult result) {
    String language = result.profile().getLanguage();
    json.beginObject()
        .name("profile")
        .beginObject()
        .prop("key", result.profile().getKey())
        .prop("name", result.profile().getName())
        .prop("language", language)
        .prop("languageName", languages.get(result.profile().getLanguage()).getName())
        .prop("isDefault", false)
        .prop("isInherited", false)
        .endObject();

    if (!result.infos().isEmpty()) {
      json.name("infos").beginArray();
      for (String info : result.infos()) {
        json.value(info);
      }
      json.endArray();
    }

    if (!result.warnings().isEmpty()) {
      json.name("warnings").beginArray();
      for (String warning : result.warnings()) {
        json.value(warning);
      }
      json.endArray();
    }

    json.endObject().close();
  }
  @Override
  public ProjectReferentials load(ProjectReactor reactor, Settings settings, Languages languages) {
    ProjectReferentials ref = new ProjectReferentials();

    String defaultName = settings.getString(ModuleQProfiles.SONAR_PROFILE_PROP);

    for (Language language : languages.all()) {
      org.sonar.batch.protocol.input.QProfile profile = null;
      if (StringUtils.isNotBlank(defaultName)) {
        profile = loadDefaultQProfile(defaultName, language.getKey());
      }
      if (profile == null) {
        profile = loadQProfile(settings, language.getKey());
      }
      if (profile != null) {
        ref.addQProfile(profile);
      }
    }

    for (QProfile qProfile : ref.qProfiles()) {
      ListMultimap<Integer, ActiveRuleParamDto> paramDtosByActiveRuleId =
          ArrayListMultimap.create();
      for (ActiveRuleParamDto dto : activeRuleDao.selectParamsByProfileKey(qProfile.key())) {
        paramDtosByActiveRuleId.put(dto.getActiveRuleId(), dto);
      }

      for (ActiveRuleDto activeDto : activeRuleDao.selectByProfileKey(qProfile.key())) {
        Rule rule = ruleFinder.findById(activeDto.getRuleId());
        if (rule != null) {
          String internalKey;
          Rule template = rule.getTemplate();
          if (template != null) {
            internalKey = template.getConfigKey();
          } else {
            internalKey = rule.getConfigKey();
          }
          ActiveRule activeRule =
              new ActiveRule(
                  rule.ruleKey().repository(),
                  rule.ruleKey().rule(),
                  rule.getName(),
                  activeDto.getSeverityString(),
                  internalKey,
                  rule.getLanguage());

          // load parameter values
          for (ActiveRuleParamDto paramDto : paramDtosByActiveRuleId.get(activeDto.getId())) {
            activeRule.params().put(paramDto.getKey(), paramDto.getValue());
          }

          // load default values
          for (RuleParam param : rule.getParams()) {
            if (!activeRule.params().containsKey(param.getKey())) {
              activeRule.params().put(param.getKey(), param.getDefaultValue());
            }
          }

          ref.addActiveRule(activeRule);
        }
      }
    }

    return ref;
  }