Browse Source

fixup! SONAR-11741 fix NPE when importing issues from external report

tags/7.7
Simon Brandhof 5 years ago
parent
commit
6f593749e3

+ 23
- 29
sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/ExternalIssueImporter.java View File

*/ */
package org.sonar.scanner.externalissue; package org.sonar.scanner.externalissue;


import com.google.common.base.MoreObjects;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@CheckForNull @CheckForNull
private static NewIssueLocation fillLocation(SensorContext context, NewIssueLocation newLocation, Location location) { private static NewIssueLocation fillLocation(SensorContext context, NewIssueLocation newLocation, Location location) {
InputFile file = findFile(context, location.filePath); InputFile file = findFile(context, location.filePath);
if (file != null) {
newLocation
.on(file);
if (file == null) {
return null;
}
newLocation.on(file);


if (location.message != null) {
newLocation.message(location.message);
}
if (location.message != null) {
newLocation.message(location.message);
}


if (location.textRange != null) {
if (location.textRange.startColumn != null) {
TextPointer start = file.newPointer(location.textRange.startLine, location.textRange.startColumn);
int endLine;
int endColumn;

if (location.textRange.endLine == null) {
// assume it's on a single line
endLine = location.textRange.startLine;
} else {
endLine = location.textRange.endLine;
}
if (location.textRange.endColumn == null) {
// assume it's until the last character of the end line
endColumn = file.selectLine(endLine).end().lineOffset();
} else {
endColumn = location.textRange.endColumn;
}
TextPointer end = file.newPointer(endLine, endColumn);
newLocation.at(file.newRange(start, end));
if (location.textRange != null) {
if (location.textRange.startColumn != null) {
TextPointer start = file.newPointer(location.textRange.startLine, location.textRange.startColumn);
int endLine = MoreObjects.firstNonNull(location.textRange.endLine, location.textRange.startLine);
int endColumn;

if (location.textRange.endColumn == null) {
// assume it's until the last character of the end line
endColumn = file.selectLine(endLine).end().lineOffset();
} else { } else {
newLocation.at(file.selectLine(location.textRange.startLine));
endColumn = location.textRange.endColumn;
} }
TextPointer end = file.newPointer(endLine, endColumn);
newLocation.at(file.newRange(start, end));
} else {
newLocation.at(file.selectLine(location.textRange.startLine));
} }
return newLocation;
} }
return null;
return newLocation;
} }


@CheckForNull @CheckForNull

+ 0
- 1
sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/ReportParserTest.java View File

public void parse_sample() { public void parse_sample() {
ReportParser parser = new ReportParser(Paths.get("src/test/resources/org/sonar/scanner/externalissue/report.json")); ReportParser parser = new ReportParser(Paths.get("src/test/resources/org/sonar/scanner/externalissue/report.json"));


System.out.println(Paths.get("org/sonar/scanner/externalissue/report.json").toAbsolutePath());
Report report = parser.parse(); Report report = parser.parse();


assertThat(report.issues).hasSize(4); assertThat(report.issues).hasSize(4);

Loading…
Cancel
Save