use of org.sonar.api.batch.rule.ActiveRule in project sonarqube by SonarSource.
the class CreateIssueByInternalKeySensor method createIssues.
private static void createIssues(InputFile file, SensorContext context) {
ActiveRule rule = context.activeRules().findByInternalKey(XooRulesDefinition.XOO_REPOSITORY, context.settings().getString(INTERNAL_KEY_PROPERTY));
if (rule != null) {
NewIssue newIssue = context.newIssue();
newIssue.forRule(rule.ruleKey()).at(newIssue.newLocation().on(file).message("This issue is generated on each file")).save();
}
}
use of org.sonar.api.batch.rule.ActiveRule in project sonarqube by SonarSource.
the class ActiveRulesBuilderTest method build_rules.
@Test
public void build_rules() {
ActiveRules activeRules = new ActiveRulesBuilder().create(RuleKey.of("squid", "S0001")).setName("My Rule").setSeverity(Severity.CRITICAL).setInternalKey("__S0001__").setParam("min", "20").activate().create(RuleKey.of("squid", "S0002")).activate().create(RuleKey.of("findbugs", "NPE")).setInternalKey(null).setSeverity(null).setParam("foo", null).activate().build();
assertThat(activeRules.findAll()).hasSize(3);
assertThat(activeRules.findByRepository("squid")).hasSize(2);
assertThat(activeRules.findByRepository("findbugs")).hasSize(1);
assertThat(activeRules.findByInternalKey("squid", "__S0001__")).isNotNull();
assertThat(activeRules.findByRepository("unknown")).isEmpty();
ActiveRule squid1 = activeRules.find(RuleKey.of("squid", "S0001"));
assertThat(squid1.ruleKey().repository()).isEqualTo("squid");
assertThat(squid1.ruleKey().rule()).isEqualTo("S0001");
assertThat(squid1.severity()).isEqualTo(Severity.CRITICAL);
assertThat(squid1.internalKey()).isEqualTo("__S0001__");
assertThat(squid1.params()).hasSize(1);
assertThat(squid1.param("min")).isEqualTo("20");
ActiveRule squid2 = activeRules.find(RuleKey.of("squid", "S0002"));
assertThat(squid2.ruleKey().repository()).isEqualTo("squid");
assertThat(squid2.ruleKey().rule()).isEqualTo("S0002");
assertThat(squid2.severity()).isEqualTo(Severity.defaultSeverity());
assertThat(squid2.params()).isEmpty();
ActiveRule findbugsRule = activeRules.find(RuleKey.of("findbugs", "NPE"));
assertThat(findbugsRule.severity()).isEqualTo(Severity.defaultSeverity());
assertThat(findbugsRule.internalKey()).isNull();
assertThat(findbugsRule.params()).isEmpty();
}
use of org.sonar.api.batch.rule.ActiveRule in project sonarqube by SonarSource.
the class ModuleIssues method initAndAddIssue.
public boolean initAndAddIssue(Issue issue) {
DefaultInputComponent inputComponent = (DefaultInputComponent) issue.primaryLocation().inputComponent();
Rule rule = validateRule(issue);
ActiveRule activeRule = activeRules.find(issue.ruleKey());
if (activeRule == null) {
// rule does not exist or is not enabled -> ignore the issue
return false;
}
String primaryMessage = Strings.isNullOrEmpty(issue.primaryLocation().message()) ? rule.name() : issue.primaryLocation().message();
org.sonar.api.batch.rule.Severity overriddenSeverity = issue.overriddenSeverity();
Severity severity = overriddenSeverity != null ? Severity.valueOf(overriddenSeverity.name()) : Severity.valueOf(activeRule.severity());
ScannerReport.Issue.Builder builder = ScannerReport.Issue.newBuilder();
ScannerReport.IssueLocation.Builder locationBuilder = IssueLocation.newBuilder();
ScannerReport.TextRange.Builder textRangeBuilder = ScannerReport.TextRange.newBuilder();
// non-null fields
builder.setSeverity(severity);
builder.setRuleRepository(issue.ruleKey().repository());
builder.setRuleKey(issue.ruleKey().rule());
builder.setMsg(primaryMessage);
locationBuilder.setMsg(primaryMessage);
locationBuilder.setComponentRef(inputComponent.batchId());
TextRange primaryTextRange = issue.primaryLocation().textRange();
if (primaryTextRange != null) {
builder.setTextRange(toProtobufTextRange(textRangeBuilder, primaryTextRange));
}
Double gap = issue.gap();
if (gap != null) {
builder.setGap(gap);
}
applyFlows(builder, locationBuilder, textRangeBuilder, issue);
ScannerReport.Issue rawIssue = builder.build();
if (filters.accept(inputComponent.key(), rawIssue)) {
write(inputComponent.batchId(), rawIssue);
return true;
}
return false;
}
use of org.sonar.api.batch.rule.ActiveRule in project sonarqube by SonarSource.
the class LocalIssueTracking method copyServerIssues.
private void copyServerIssues(Collection<ServerIssueFromWs> serverIssues, List<TrackedIssue> trackedIssues) {
for (ServerIssueFromWs serverIssue : serverIssues) {
org.sonar.scanner.protocol.input.ScannerInput.ServerIssue unmatchedPreviousIssue = serverIssue.getDto();
TrackedIssue unmatched = IssueTransformer.toTrackedIssue(unmatchedPreviousIssue);
ActiveRule activeRule = activeRules.find(unmatched.getRuleKey());
unmatched.setNew(false);
if (activeRule == null) {
// rule removed
IssueTransformer.resolveRemove(unmatched);
}
trackedIssues.add(unmatched);
}
}
use of org.sonar.api.batch.rule.ActiveRule in project sonarqube by SonarSource.
the class LocalIssueTracking method updateUnmatchedIssue.
private void updateUnmatchedIssue(TrackedIssue issue) {
ActiveRule activeRule = activeRules.find(issue.getRuleKey());
issue.setNew(false);
boolean isRemovedRule = activeRule == null;
if (isRemovedRule) {
IssueTransformer.resolveRemove(issue);
} else {
IssueTransformer.close(issue);
}
}
Aggregations