Search in sources :

Example 11 with RuleMetadataDto

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

the class AdHocRuleCreator method persistAndIndex.

/**
 * Persists a new add hoc rule in the DB and indexes it.
 * @return the rule that was inserted in the DB, which <b>includes the generated ID</b>.
 */
public RuleDto persistAndIndex(DbSession dbSession, NewAdHocRule adHoc) {
    RuleDao dao = dbClient.ruleDao();
    Optional<RuleDto> existingRuleDtoOpt = dao.selectByKey(dbSession, adHoc.getKey());
    RuleMetadataDto metadata;
    long now = system2.now();
    if (!existingRuleDtoOpt.isPresent()) {
        RuleDefinitionDto dto = new RuleDefinitionDto().setUuid(uuidFactory.create()).setRuleKey(adHoc.getKey()).setIsExternal(true).setIsAdHoc(true).setName(adHoc.getEngineId() + ":" + adHoc.getRuleId()).setScope(ALL).setStatus(READY).setCreatedAt(now).setUpdatedAt(now);
        dao.insert(dbSession, dto);
        metadata = new RuleMetadataDto().setRuleUuid(dto.getUuid());
    } else {
        // No need to update the rule, only org specific metadata
        RuleDto ruleDto = existingRuleDtoOpt.get();
        Preconditions.checkState(ruleDto.isExternal() && ruleDto.isAdHoc());
        metadata = ruleDto.getMetadata();
    }
    if (adHoc.hasDetails()) {
        boolean changed = false;
        if (!Objects.equals(metadata.getAdHocName(), adHoc.getName())) {
            metadata.setAdHocName(substring(adHoc.getName(), 0, MAX_LENGTH_AD_HOC_NAME));
            changed = true;
        }
        if (!Objects.equals(metadata.getAdHocDescription(), adHoc.getDescription())) {
            metadata.setAdHocDescription(substring(adHoc.getDescription(), 0, MAX_LENGTH_AD_HOC_DESC));
            changed = true;
        }
        if (!Objects.equals(metadata.getAdHocSeverity(), adHoc.getSeverity())) {
            metadata.setAdHocSeverity(adHoc.getSeverity());
            changed = true;
        }
        RuleType ruleType = requireNonNull(adHoc.getRuleType(), "Rule type should not be null");
        if (!Objects.equals(metadata.getAdHocType(), ruleType.getDbConstant())) {
            metadata.setAdHocType(ruleType);
            changed = true;
        }
        if (changed) {
            metadata.setUpdatedAt(now);
            metadata.setCreatedAt(now);
            dao.insertOrUpdate(dbSession, metadata);
        }
    }
    RuleDto ruleDto = dao.selectOrFailByKey(dbSession, adHoc.getKey());
    ruleIndexer.commitAndIndex(dbSession, ruleDto.getUuid());
    return ruleDto;
}
Also used : RuleDao(org.sonar.db.rule.RuleDao) RuleDto(org.sonar.db.rule.RuleDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleMetadataDto(org.sonar.db.rule.RuleMetadataDto) RuleType(org.sonar.api.rules.RuleType)

Example 12 with RuleMetadataDto

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

the class AdHocRuleCreatorTest method does_not_update_rule_when_no_change.

@Test
public void does_not_update_rule_when_no_change() {
    RuleDefinitionDto rule = db.rules().insert(r -> r.setRepositoryKey("external_eslint").setIsExternal(true).setIsAdHoc(true));
    RuleMetadataDto ruleMetadata = db.rules().insertOrUpdateMetadata(rule);
    RuleDto ruleUpdated = underTest.persistAndIndex(dbSession, new NewAdHocRule(ScannerReport.AdHocRule.newBuilder().setEngineId("eslint").setRuleId(rule.getKey().rule()).setName(ruleMetadata.getAdHocName()).setDescription(ruleMetadata.getAdHocDescription()).setSeverity(Constants.Severity.valueOf(ruleMetadata.getAdHocSeverity())).setType(ScannerReport.IssueType.forNumber(ruleMetadata.getAdHocType())).build()));
    assertThat(ruleUpdated).isNotNull();
    assertThat(ruleUpdated.isExternal()).isTrue();
    assertThat(ruleUpdated.isAdHoc()).isTrue();
    assertThat(ruleUpdated.getKey()).isEqualTo(rule.getKey());
    assertThat(ruleUpdated.getName()).isEqualTo(rule.getName());
    assertThat(ruleUpdated.getDescription()).isEqualTo(rule.getDescription());
    assertThat(ruleUpdated.getSeverity()).isEqualTo(rule.getSeverity());
    assertThat(ruleUpdated.getType()).isEqualTo(rule.getType());
    assertThat(ruleUpdated.getDefinition().getCreatedAt()).isEqualTo(rule.getCreatedAt());
    assertThat(ruleUpdated.getDefinition().getUpdatedAt()).isEqualTo(rule.getUpdatedAt());
    assertThat(ruleUpdated.getMetadata().getAdHocName()).isEqualTo(ruleMetadata.getAdHocName());
    assertThat(ruleUpdated.getMetadata().getAdHocDescription()).isEqualTo(ruleMetadata.getAdHocDescription());
    assertThat(ruleUpdated.getMetadata().getAdHocSeverity()).isEqualTo(ruleMetadata.getAdHocSeverity());
    assertThat(ruleUpdated.getMetadata().getAdHocType()).isEqualTo(ruleMetadata.getAdHocType());
    assertThat(ruleUpdated.getMetadata().getCreatedAt()).isEqualTo(rule.getCreatedAt());
    assertThat(ruleUpdated.getMetadata().getUpdatedAt()).isEqualTo(rule.getUpdatedAt());
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleMetadataDto(org.sonar.db.rule.RuleMetadataDto) Test(org.junit.Test)

Example 13 with RuleMetadataDto

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

the class SearchActionTest method search_debt_rules_with_default_linear_offset_and_overridden_linear_debt.

@Test
public void search_debt_rules_with_default_linear_offset_and_overridden_linear_debt() {
    RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage("java").setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()).setDefRemediationGapMultiplier("1h").setDefRemediationBaseEffort("15min"));
    RuleMetadataDto metadata = insertMetadata(rule, r -> r.setRemediationFunction(DebtRemediationFunction.Type.LINEAR.name()).setRemediationGapMultiplier("1h").setRemediationBaseEffort(null));
    indexRules();
    SearchResponse result = ws.newRequest().setParam("f", "debtRemFn,debtOverloaded,defaultDebtRemFn").executeProtobuf(SearchResponse.class);
    assertThat(result.getTotal()).isOne();
    assertThat(result.getRulesCount()).isOne();
    Rule searchedRule = result.getRules(0);
    assertThat(searchedRule).isNotNull();
    assertThat(searchedRule.getDefaultDebtRemFnCoeff()).isEqualTo("1h");
    assertThat(searchedRule.getDefaultDebtRemFnOffset()).isEqualTo("15min");
    assertThat(searchedRule.getDefaultDebtRemFnType()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name());
    assertThat(searchedRule.getDefaultRemFnBaseEffort()).isEqualTo("15min");
    assertThat(searchedRule.getDefaultRemFnGapMultiplier()).isEqualTo("1h");
    assertThat(searchedRule.getDefaultRemFnType()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name());
    assertThat(searchedRule.getDebtOverloaded()).isTrue();
    assertThat(searchedRule.getDebtRemFnCoeff()).isEqualTo("1h");
    assertThat(searchedRule.getDebtRemFnOffset()).isEmpty();
    assertThat(searchedRule.getDebtRemFnType()).isEqualTo(DebtRemediationFunction.Type.LINEAR.name());
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleMetadataDto(org.sonar.db.rule.RuleMetadataDto) Rule(org.sonarqube.ws.Rules.Rule) UserSessionRule(org.sonar.server.tester.UserSessionRule) SearchResponse(org.sonarqube.ws.Rules.SearchResponse) Test(org.junit.Test)

Example 14 with RuleMetadataDto

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

the class SearchActionTest method should_filter_on_specific_tags.

@Test
public void should_filter_on_specific_tags() {
    RuleDefinitionDto rule1 = db.rules().insert(r -> r.setLanguage("java"));
    RuleMetadataDto metadata1 = insertMetadata(rule1, setTags("tag1", "tag2"));
    RuleDefinitionDto rule2 = db.rules().insert(r -> r.setLanguage("java"));
    RuleMetadataDto metadata2 = insertMetadata(rule2);
    indexRules();
    Consumer<TestRequest> request = r -> r.setParam("f", "repo,name").setParam("tags", metadata1.getTags().stream().collect(Collectors.joining(",")));
    verify(request, rule1);
}
Also used : RandomStringUtils.randomAlphanumeric(org.apache.commons.lang.RandomStringUtils.randomAlphanumeric) ActiveRuleIndexer(org.sonar.server.qualityprofile.index.ActiveRuleIndexer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collections.singletonList(java.util.Collections.singletonList) Rules(org.sonarqube.ws.Rules) Languages(org.sonar.api.resources.Languages) WebService(org.sonar.api.server.ws.WebService) Collections.singleton(java.util.Collections.singleton) Assertions.entry(org.assertj.guava.api.Assertions.entry) BLOCKER(org.sonar.api.rule.Severity.BLOCKER) Arrays.asList(java.util.Arrays.asList) DebtRemediationFunction(org.sonar.api.server.debt.DebtRemediationFunction) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleTesting.setSystemTags(org.sonar.db.rule.RuleTesting.setSystemTags) RuleIndexer(org.sonar.server.rule.index.RuleIndexer) Mockito.doReturn(org.mockito.Mockito.doReturn) Rule(org.sonarqube.ws.Rules.Rule) DbTester(org.sonar.db.DbTester) QProfileRulesImpl(org.sonar.server.qualityprofile.QProfileRulesImpl) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) System2(org.sonar.api.utils.System2) AlwaysIncreasingSystem2(org.sonar.api.impl.utils.AlwaysIncreasingSystem2) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleMetadataDto(org.sonar.db.rule.RuleMetadataDto) Collectors(java.util.stream.Collectors) NotFoundException(org.sonar.server.exceptions.NotFoundException) Common(org.sonarqube.ws.Common) QProfileRules(org.sonar.server.qualityprofile.QProfileRules) RuleTesting.setTags(org.sonar.db.rule.RuleTesting.setTags) List(java.util.List) LanguageTesting(org.sonar.server.language.LanguageTesting) RuleKey(org.sonar.api.rule.RuleKey) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) Arrays.stream(java.util.Arrays.stream) Mockito.mock(org.mockito.Mockito.mock) SearchResponse(org.sonarqube.ws.Rules.SearchResponse) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) EsTester(org.sonar.server.es.EsTester) RuleActivator(org.sonar.server.qualityprofile.builtin.RuleActivator) RuleStatus(org.sonar.api.rule.RuleStatus) PARAM_ACTIVATION(org.sonar.server.rule.ws.RulesWsParameters.PARAM_ACTIVATION) TypeValidations(org.sonar.server.util.TypeValidations) RuleType(org.sonar.api.rules.RuleType) HashSet(java.util.HashSet) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Extractor(org.assertj.core.api.iterable.Extractor) IntegerTypeValidation(org.sonar.server.util.IntegerTypeValidation) PARAM_COMPARE_TO_PROFILE(org.sonar.server.rule.ws.RulesWsParameters.PARAM_COMPARE_TO_PROFILE) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Before(org.junit.Before) QualityProfileChangeEventService(org.sonar.server.pushapi.qualityprofile.QualityProfileChangeEventService) UserSessionRule(org.sonar.server.tester.UserSessionRule) Assertions.tuple(org.assertj.core.api.Assertions.tuple) PARAM_QPROFILE(org.sonar.server.rule.ws.RulesWsParameters.PARAM_QPROFILE) MacroInterpreter(org.sonar.server.text.MacroInterpreter) TestRequest(org.sonar.server.ws.TestRequest) Test(org.junit.Test) RuleIndex(org.sonar.server.rule.index.RuleIndex) WsActionTester(org.sonar.server.ws.WsActionTester) PARAM_RULE_KEY(org.sonar.server.rule.ws.RulesWsParameters.PARAM_RULE_KEY) Consumer(java.util.function.Consumer) RuleParamDto(org.sonar.db.rule.RuleParamDto) StringTypeValidation(org.sonar.server.util.StringTypeValidation) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleMetadataDto(org.sonar.db.rule.RuleMetadataDto) TestRequest(org.sonar.server.ws.TestRequest) Test(org.junit.Test)

Example 15 with RuleMetadataDto

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

the class ShowActionTest method ignore_predefined_info_on_adhoc_rule.

@Test
public void ignore_predefined_info_on_adhoc_rule() {
    RuleDefinitionDto externalRule = db.rules().insert(r -> r.setIsExternal(true).setIsAdHoc(true).setName("predefined name").setDescription("<div>predefined desc</div>").setSeverity(Severity.BLOCKER).setType(RuleType.VULNERABILITY));
    RuleMetadataDto metadata = db.rules().insertOrUpdateMetadata(externalRule, m -> m.setAdHocName("adhoc name").setAdHocDescription("<div>adhoc desc</div>").setAdHocSeverity(Severity.MAJOR).setAdHocType(RuleType.CODE_SMELL).setNoteData(null).setNoteUserUuid(null));
    doReturn("&lt;div&gt;adhoc desc&lt;/div&gt;").when(macroInterpreter).interpret(metadata.getAdHocDescription());
    ShowResponse result = ws.newRequest().setParam(PARAM_KEY, externalRule.getKey().toString()).executeProtobuf(ShowResponse.class);
    Rule resultRule = result.getRule();
    assertThat(resultRule).extracting(Rule::getName, Rule::getHtmlDesc, Rule::getSeverity, Rule::getType).containsExactlyInAnyOrder("adhoc name", "&lt;div&gt;adhoc desc&lt;/div&gt;", Severity.MAJOR, Common.RuleType.CODE_SMELL);
}
Also used : ShowResponse(org.sonarqube.ws.Rules.ShowResponse) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleMetadataDto(org.sonar.db.rule.RuleMetadataDto) UserSessionRule(org.sonar.server.tester.UserSessionRule) Rule(org.sonarqube.ws.Rules.Rule) RuleTesting.newTemplateRule(org.sonar.db.rule.RuleTesting.newTemplateRule) RuleTesting.newCustomRule(org.sonar.db.rule.RuleTesting.newCustomRule) Test(org.junit.Test)

Aggregations

RuleMetadataDto (org.sonar.db.rule.RuleMetadataDto)17 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)15 Test (org.junit.Test)13 UserSessionRule (org.sonar.server.tester.UserSessionRule)7 Rule (org.sonarqube.ws.Rules.Rule)7 SearchResponse (org.sonarqube.ws.Rules.SearchResponse)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 Rules (org.sonarqube.ws.Rules)3 ShowResponse (org.sonarqube.ws.Rules.ShowResponse)3 RuleType (org.sonar.api.rules.RuleType)2 RuleDto (org.sonar.db.rule.RuleDto)2 Common (org.sonarqube.ws.Common)2 Arrays.asList (java.util.Arrays.asList)1 Arrays.stream (java.util.Arrays.stream)1 Collections.singleton (java.util.Collections.singleton)1 Collections.singletonList (java.util.Collections.singletonList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1