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;
}
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());
}
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());
}
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);
}
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("<div>adhoc desc</div>").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", "<div>adhoc desc</div>", Severity.MAJOR, Common.RuleType.CODE_SMELL);
}
Aggregations