Search in sources :

Example 21 with ActiveRuleParamDto

use of org.sonar.db.qualityprofile.ActiveRuleParamDto in project sonarqube by SonarSource.

the class RegisterRules method mergeParams.

private void mergeParams(RulesDefinition.Rule ruleDef, RuleDto rule, DbSession session) {
    List<RuleParamDto> paramDtos = dbClient.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey());
    Map<String, RuleParamDto> existingParamsByName = Maps.newHashMap();
    for (RuleParamDto paramDto : paramDtos) {
        RulesDefinition.Param paramDef = ruleDef.param(paramDto.getName());
        if (paramDef == null) {
            dbClient.activeRuleDao().deleteParamsByRuleParam(session, rule.getId(), paramDto.getName());
            dbClient.ruleDao().deleteRuleParam(session, paramDto.getId());
        } else {
            if (mergeParam(paramDto, paramDef)) {
                dbClient.ruleDao().updateRuleParam(session, rule, paramDto);
            }
            existingParamsByName.put(paramDto.getName(), paramDto);
        }
    }
    // Create newly parameters
    for (RulesDefinition.Param param : ruleDef.params()) {
        RuleParamDto paramDto = existingParamsByName.get(param.key());
        if (paramDto != null) {
            continue;
        }
        paramDto = RuleParamDto.createFor(rule).setName(param.key()).setDescription(param.description()).setDefaultValue(param.defaultValue()).setType(param.type().toString());
        dbClient.ruleDao().insertRuleParam(session, rule, paramDto);
        if (StringUtils.isEmpty(param.defaultValue())) {
            continue;
        }
        // Propagate the default value to existing active rule parameters
        for (ActiveRuleDto activeRule : dbClient.activeRuleDao().selectByRuleId(session, rule.getId())) {
            ActiveRuleParamDto activeParam = ActiveRuleParamDto.createFor(paramDto).setValue(param.defaultValue());
            dbClient.activeRuleDao().insertParam(session, activeRule, activeParam);
        }
    }
}
Also used : RulesDefinition(org.sonar.api.server.rule.RulesDefinition) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 22 with ActiveRuleParamDto

use of org.sonar.db.qualityprofile.ActiveRuleParamDto in project sonarqube by SonarSource.

the class ActiveRuleCompleter method activeRuleDtosToActiveRuleParamDtos.

private ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleDtosToActiveRuleParamDtos(DbSession dbSession, List<ActiveRuleDto> activeRuleDtos) {
    Map<Integer, ActiveRuleKey> activeRuleIdsByKey = new HashMap<>();
    for (ActiveRuleDto activeRuleDto : activeRuleDtos) {
        activeRuleIdsByKey.put(activeRuleDto.getId(), activeRuleDto.getKey());
    }
    List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDto::getId));
    ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = ArrayListMultimap.create(activeRuleDtos.size(), 10);
    for (ActiveRuleParamDto activeRuleParamDto : activeRuleParamDtos) {
        ActiveRuleKey activeRuleKey = activeRuleIdsByKey.get(activeRuleParamDto.getActiveRuleId());
        activeRuleParamsByActiveRuleKey.put(activeRuleKey, activeRuleParamDto);
    }
    return activeRuleParamsByActiveRuleKey;
}
Also used : HashMap(java.util.HashMap) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 23 with ActiveRuleParamDto

use of org.sonar.db.qualityprofile.ActiveRuleParamDto in project sonarqube by SonarSource.

the class ActiveRuleCompleter method buildActiveRuleResponse.

private static Rules.Active buildActiveRuleResponse(ActiveRuleDto activeRule, List<ActiveRuleParamDto> parameters) {
    Rules.Active.Builder activeRuleResponse = Rules.Active.newBuilder();
    activeRuleResponse.setQProfile(activeRule.getKey().qProfile());
    String inheritance = activeRule.getInheritance();
    activeRuleResponse.setInherit(inheritance != null ? inheritance : ActiveRule.Inheritance.NONE.name());
    activeRuleResponse.setSeverity(activeRule.getSeverityString());
    activeRuleResponse.setCreatedAt(DateUtils.formatDateTime(activeRule.getCreatedAt()));
    Rules.Active.Param.Builder paramBuilder = Rules.Active.Param.newBuilder();
    for (ActiveRuleParamDto parameter : parameters) {
        activeRuleResponse.addParams(paramBuilder.clear().setKey(parameter.getKey()).setValue(nullToEmpty(parameter.getValue())));
    }
    return activeRuleResponse.build();
}
Also used : ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) Rules(org.sonarqube.ws.Rules)

Example 24 with ActiveRuleParamDto

use of org.sonar.db.qualityprofile.ActiveRuleParamDto in project sonarqube by SonarSource.

the class QProfileBackuperMediumTest method restore_and_create_profile.

@Test
public void restore_and_create_profile() throws Exception {
    // Backup file declares profile P1 on xoo
    tester.get(QProfileBackuper.class).restore(new StringReader(Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)), null);
    // Check in db
    QualityProfileDto profile = db.qualityProfileDao().selectByNameAndLanguage("P1", "xoo", dbSession);
    assertThat(profile).isNotNull();
    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, profile.getKey());
    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(profile.getKey()).setActivation(true))).containsOnly(XOO_X1);
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) RuleIndex(org.sonar.server.rule.index.RuleIndex) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleQuery(org.sonar.server.rule.index.RuleQuery) StringReader(java.io.StringReader) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 25 with ActiveRuleParamDto

use of org.sonar.db.qualityprofile.ActiveRuleParamDto in project sonarqube by SonarSource.

the class QProfileCopierMediumTest method verifyOneActiveRule.

private void verifyOneActiveRule(String profileKey, String expectedSeverity, @Nullable String expectedInheritance, Map<String, String> expectedParams) {
    // check in db
    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, profileKey);
    assertThat(activeRules).hasSize(1);
    ActiveRuleDto activeRule = activeRules.get(0);
    assertThat(activeRule.getSeverityString()).isEqualTo(expectedSeverity);
    assertThat(activeRule.getInheritance()).isEqualTo(expectedInheritance);
    // verify parameters
    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRule.getKey());
    List<ActiveRuleParamDto> params = db.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
    assertThat(params).hasSize(expectedParams.size());
    Map<String, ActiveRuleParamDto> paramsByKey = ActiveRuleParamDto.groupByKey(params);
    for (Map.Entry<String, String> entry : expectedParams.entrySet()) {
        String value = paramsByKey.get(entry.getKey()).getValue();
        assertThat(value).isEqualTo(entry.getValue());
    }
    // check in es
    assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(profileKey).setActivation(true))).hasSize(1);
}
Also used : ActiveRuleIndex(org.sonar.server.qualityprofile.index.ActiveRuleIndex) RuleIndex(org.sonar.server.rule.index.RuleIndex) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleQuery(org.sonar.server.rule.index.RuleQuery) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)29 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)26 Test (org.junit.Test)13 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)13 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)9 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)8 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)7 RuleParamDto (org.sonar.db.rule.RuleParamDto)7 RuleDto (org.sonar.db.rule.RuleDto)6 Map (java.util.Map)5 StringReader (java.io.StringReader)4 RuleKey (org.sonar.api.rule.RuleKey)4 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 RuleQuery (org.sonar.server.rule.index.RuleQuery)4 RulesProfile (org.sonar.api.profiles.RulesProfile)3 RuleIndex (org.sonar.server.rule.index.RuleIndex)3 WsTester (org.sonar.server.ws.WsTester)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 HashMap (java.util.HashMap)2 RuleActivation (org.sonar.server.qualityprofile.RuleActivation)2