Search in sources :

Example 6 with RuleParamDto

use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.

the class ShowAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    RuleKey key = RuleKey.parse(request.mandatoryParam(PARAM_KEY));
    try (DbSession dbSession = dbClient.openSession(false)) {
        Optional<RuleDto> optionalRule = dbClient.ruleDao().selectByKey(dbSession, key);
        checkFoundWithOptional(optionalRule, "Rule not found: " + key);
        RuleDto rule = optionalRule.get();
        List<RuleDto> templateRules = new ArrayList<>();
        if (rule.getTemplateId() != null) {
            Optional<RuleDto> templateRule = dbClient.ruleDao().selectById(rule.getTemplateId(), dbSession);
            if (templateRule.isPresent()) {
                templateRules.add(templateRule.get());
            }
        }
        List<RuleParamDto> ruleParameters = dbClient.ruleDao().selectRuleParamsByRuleIds(dbSession, singletonList(rule.getId()));
        ShowResponse showResponse = buildResponse(dbSession, request, new SearchAction.SearchResult().setRules(singletonList(rule)).setTemplateRules(templateRules).setRuleParameters(ruleParameters).setTotal(1L));
        writeProtobuf(showResponse, request, response);
    }
}
Also used : DbSession(org.sonar.db.DbSession) ShowResponse(org.sonarqube.ws.Rules.ShowResponse) RuleKey(org.sonar.api.rule.RuleKey) RuleDto(org.sonar.db.rule.RuleDto) ArrayList(java.util.ArrayList) RuleParamDto(org.sonar.db.rule.RuleParamDto)

Example 7 with RuleParamDto

use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.

the class UpdateAction method buildResponse.

private UpdateResponse buildResponse(DbSession dbSession, RuleKey key) {
    Optional<RuleDto> optionalRule = dbClient.ruleDao().selectByKey(dbSession, key);
    checkFoundWithOptional(optionalRule, "Rule not found: " + key);
    RuleDto rule = optionalRule.get();
    List<RuleDto> templateRules = new ArrayList<>();
    if (rule.getTemplateId() != null) {
        Optional<RuleDto> templateRule = dbClient.ruleDao().selectById(rule.getTemplateId(), dbSession);
        if (templateRule.isPresent()) {
            templateRules.add(templateRule.get());
        }
    }
    List<RuleParamDto> ruleParameters = dbClient.ruleDao().selectRuleParamsByRuleIds(dbSession, singletonList(rule.getId()));
    UpdateResponse.Builder responseBuilder = UpdateResponse.newBuilder();
    SearchAction.SearchResult searchResult = new SearchAction.SearchResult().setRules(singletonList(rule)).setTemplateRules(templateRules).setRuleParameters(ruleParameters).setTotal(1L);
    responseBuilder.setRule(mapper.toWsRule(rule, searchResult, Collections.<String>emptySet()));
    return responseBuilder.build();
}
Also used : UpdateResponse(org.sonarqube.ws.Rules.UpdateResponse) RuleDto(org.sonar.db.rule.RuleDto) ArrayList(java.util.ArrayList) RuleParamDto(org.sonar.db.rule.RuleParamDto)

Example 8 with RuleParamDto

use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.

the class CompareActionMediumTest method createActiveRuleWithParam.

private ActiveRuleDto createActiveRuleWithParam(RuleDto rule, QualityProfileDto profile, String value) {
    ActiveRuleDto activeRule = createActiveRule(rule, profile);
    RuleParamDto paramDto = db.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey()).get(0);
    ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(paramDto).setValue(value);
    db.activeRuleDao().insertParam(session, activeRule, activeRuleParam);
    return activeRule;
}
Also used : 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 9 with RuleParamDto

use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.

the class SearchActionMediumTest method search_all_active_rules_params.

@Test
public void search_all_active_rules_params() throws Exception {
    QualityProfileDto profile = QProfileTesting.newXooP1("org-123");
    tester.get(QualityProfileDao.class).insert(dbSession, profile);
    RuleDto rule = RuleTesting.newXooX1();
    ruleDao.insert(dbSession, rule);
    dbSession.commit();
    RuleParamDto param = RuleParamDto.createFor(rule).setDefaultValue("some value").setType("string").setDescription("My small description").setName("my_var");
    ruleDao.insertRuleParam(dbSession, rule, param);
    RuleParamDto param2 = RuleParamDto.createFor(rule).setDefaultValue("other value").setType("integer").setDescription("My small description").setName("the_var");
    ruleDao.insertRuleParam(dbSession, rule, param2);
    ActiveRuleDto activeRule = newActiveRule(profile, rule);
    tester.get(ActiveRuleDao.class).insert(dbSession, activeRule);
    ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(param).setValue("The VALUE");
    tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule, activeRuleParam);
    ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(param2).setValue("The Other Value");
    tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule, activeRuleParam2);
    dbSession.commit();
    ruleIndexer.index();
    activeRuleIndexer.index();
    WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD);
    request.setParam(WebService.Param.TEXT_QUERY, "x1");
    request.setParam(PARAM_ACTIVATION, "true");
    request.setParam(WebService.Param.FIELDS, "params");
    WsTester.Result result = request.execute();
    result.assertJson(this.getClass(), "search_active_rules_params.json");
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) WsTester(org.sonar.server.ws.WsTester) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 10 with RuleParamDto

use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.

the class CreateActionMediumTest method create_custom_rule.

@Test
public void create_custom_rule() throws Exception {
    // Template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
    ruleDao.insert(session, templateRule);
    RuleParamDto param = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*");
    ruleDao.insertRuleParam(session, templateRule, param);
    session.commit();
    WsTester.TestRequest request = wsTester.newPostRequest("api/rules", "create").setParam("custom_key", "MY_CUSTOM").setParam("template_key", templateRule.getKey().toString()).setParam("name", "My custom rule").setParam("markdown_description", "Description").setParam("severity", "MAJOR").setParam("status", "BETA").setParam("params", "regex=a.*");
    request.execute().assertJson(getClass(), "create_custom_rule.json");
}
Also used : WsTester(org.sonar.server.ws.WsTester) RuleDto(org.sonar.db.rule.RuleDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) Test(org.junit.Test)

Aggregations

RuleParamDto (org.sonar.db.rule.RuleParamDto)37 RuleDto (org.sonar.db.rule.RuleDto)29 Test (org.junit.Test)17 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)14 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)12 RuleKey (org.sonar.api.rule.RuleKey)10 ArrayList (java.util.ArrayList)6 WsTester (org.sonar.server.ws.WsTester)6 Date (java.util.Date)5 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)5 SearchOptions (org.sonar.server.es.SearchOptions)4 RuleQuery (org.sonar.server.rule.index.RuleQuery)4 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)3 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 DbSession (org.sonar.db.DbSession)2 Collection (java.util.Collection)1 Map (java.util.Map)1 Before (org.junit.Before)1