use of org.sonar.api.batch.fs.TextRange 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.fs.TextRange in project sonarqube by SonarSource.
the class DefaultSensorStorage method store.
@Override
public void store(DefaultSymbolTable symbolTable) {
ScannerReportWriter writer = reportPublisher.getWriter();
DefaultInputFile inputFile = (DefaultInputFile) symbolTable.inputFile();
inputFile.setPublish(true);
int componentRef = inputFile.batchId();
if (writer.hasComponentData(FileStructure.Domain.SYMBOLS, componentRef)) {
throw new UnsupportedOperationException("Trying to save symbol table twice for the same file is not supported: " + symbolTable.inputFile().absolutePath());
}
final ScannerReport.Symbol.Builder builder = ScannerReport.Symbol.newBuilder();
final ScannerReport.TextRange.Builder rangeBuilder = ScannerReport.TextRange.newBuilder();
writer.writeComponentSymbols(componentRef, symbolTable.getReferencesBySymbol().entrySet().stream().map(input -> {
builder.clear();
rangeBuilder.clear();
TextRange declaration = input.getKey();
builder.setDeclaration(rangeBuilder.setStartLine(declaration.start().line()).setStartOffset(declaration.start().lineOffset()).setEndLine(declaration.end().line()).setEndOffset(declaration.end().lineOffset()).build());
for (TextRange reference : input.getValue()) {
builder.addReference(rangeBuilder.setStartLine(reference.start().line()).setStartOffset(reference.start().lineOffset()).setEndLine(reference.end().line()).setEndOffset(reference.end().lineOffset()).build());
}
return builder.build();
}).collect(Collectors.toList()));
}
use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.
the class OneBugIssuePerTestLineSensor method createIssues.
private static void createIssues(InputFile file, SensorContext context, String repo) {
RuleKey ruleKey = RuleKey.of(repo, RULE_KEY);
for (int line = 1; line <= file.lines(); line++) {
TextRange text = file.selectLine(line);
// do not count empty lines, which can be a pain with end-of-file return
if (text.end().lineOffset() == 0) {
continue;
}
NewIssue newIssue = context.newIssue();
newIssue.forRule(ruleKey).at(newIssue.newLocation().on(file).at(text).message("This bug issue is generated on each line of a test file")).save();
}
}
use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.
the class OneBugIssuePerLineSensor method createIssues.
private static void createIssues(InputFile file, SensorContext context, String repo) {
RuleKey ruleKey = RuleKey.of(repo, RULE_KEY);
for (int line = 1; line <= file.lines(); line++) {
TextRange text = file.selectLine(line);
// do not count empty lines, which can be a pain with end-of-file return
if (text.end().lineOffset() == 0) {
continue;
}
NewIssue newIssue = context.newIssue();
newIssue.forRule(ruleKey).at(newIssue.newLocation().on(file).at(text).message("This bug issue is generated on each line")).save();
}
}
use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.
the class ExternalIssueImporterTest method import_issue_with_complete_primary_location.
@Test
public void import_issue_with_complete_primary_location() {
ReportParser.TextRange input = new ReportParser.TextRange();
input.startLine = 1;
input.startColumn = 4;
input.endLine = 2;
input.endColumn = 3;
runOn(newIssue(input));
assertThat(context.allExternalIssues()).hasSize(1);
ExternalIssue output = context.allExternalIssues().iterator().next();
assertSameRange(input, output.primaryLocation().textRange());
}
Aggregations