use of org.sonar.api.batch.rule.Rule 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.Rule in project sonarqube by SonarSource.
the class ModuleIssues method validateRule.
private Rule validateRule(Issue issue) {
RuleKey ruleKey = issue.ruleKey();
Rule rule = rules.find(ruleKey);
if (rule == null) {
throw MessageException.of(String.format("The rule '%s' does not exist.", ruleKey));
}
if (Strings.isNullOrEmpty(rule.name()) && Strings.isNullOrEmpty(issue.primaryLocation().message())) {
throw MessageException.of(String.format("The rule '%s' has no name and the related issue has no message.", ruleKey));
}
return rule;
}
use of org.sonar.api.batch.rule.Rule in project sonarqube by SonarSource.
the class RuleNameProviderTest method setUp.
@Before
public void setUp() {
ruleKey = mock(RuleKey.class);
rule = mock(Rule.class);
rules = mock(Rules.class);
provider = new RuleNameProvider(rules);
when(ruleKey.rule()).thenReturn("ruleKey");
when(ruleKey.repository()).thenReturn("repoKey");
when(rule.name()).thenReturn("name");
when(rule.key()).thenReturn(ruleKey);
when(rules.find(any(RuleKey.class))).thenReturn(rule);
}
use of org.sonar.api.batch.rule.Rule in project sonarlint-core by SonarSource.
the class DefaultSensorStorage method validateRule.
private DefaultRule validateRule(Issue issue) {
RuleKey ruleKey = issue.ruleKey();
Rule rule = rules.find(ruleKey);
if (rule == null) {
throw MessageException.of(String.format("The rule '%s' does not exist.", ruleKey));
}
if (Strings.isNullOrEmpty(rule.name()) && Strings.isNullOrEmpty(issue.primaryLocation().message())) {
throw MessageException.of(String.format("The rule '%s' has no name and the related issue has no message.", ruleKey));
}
return (DefaultRule) rule;
}
use of org.sonar.api.batch.rule.Rule in project sonarlint-core by SonarSource.
the class SonarQubeActiveRulesProvider method createNewActiveRule.
private static void createNewActiveRule(ActiveRulesBuilder builder, ActiveRule activeRule, Sonarlint.Rules storageRules, String language, Rules rules) {
RuleKey ruleKey = RuleKey.of(activeRule.getRepo(), activeRule.getKey());
Rule rule = rules.find(ruleKey);
Sonarlint.Rules.Rule storageRule;
try {
storageRule = storageRules.getRulesByKeyOrThrow(ruleKey.toString());
} catch (IllegalArgumentException e) {
throw new MessageException("Unknown active rule in the quality profile of the project. Please update the SonarQube server binding.");
}
NewActiveRule newActiveRule = builder.create(ruleKey).setLanguage(language).setName(rule.name()).setInternalKey(rule.internalKey()).setSeverity(activeRule.getSeverity());
if (!StringUtils.isEmpty(storageRule.getTemplateKey())) {
RuleKey templateRuleKey = RuleKey.parse(storageRule.getTemplateKey());
newActiveRule.setTemplateRuleKey(templateRuleKey.rule());
}
for (Map.Entry<String, String> param : activeRule.getParamsMap().entrySet()) {
newActiveRule.setParam(param.getKey(), param.getValue());
}
newActiveRule.activate();
}
Aggregations