assertThat(issue.getEffort()).isEqualTo(50l); assertThat(issue.getType()).isEqualTo(IssueType.CODE_SMELL); assertThat(issue.getTextRange().getStartLine()).isEqualTo(5); assertThat(issue.getTextRange().getEndLine()).isEqualTo(5); assertThat(issue.getTextRange().getStartOffset()).isEqualTo(3); assertThat(issue.getTextRange().getEndOffset()).isEqualTo(41); assertThat(issue.getType()).isEqualTo(IssueType.BUG); assertThat(issue.getEffort()).isZero(); assertThat(issue.getTextRange().getStartLine()).isEqualTo(3); assertThat(issue.getTextRange().getEndLine()).isEqualTo(3); assertThat(issue.getTextRange().getStartOffset()).isEqualTo(0); assertThat(issue.getTextRange().getEndOffset()).isEqualTo(24); assertThat(issue.hasTextRange()).isFalse(); assertThat(issue.getFlow(0).getLocationCount()).isOne(); assertThat(issue.getFlow(0).getLocation(0).getTextRange().getStartLine()).isOne(); assertThat(issue.getFlow(1).getLocationCount()).isOne(); assertThat(issue.getFlow(1).getLocation(0).getTextRange().getStartLine()).isEqualTo(3);
.setTextRange(TextRange.newBuilder().setStartLine(2).build()) .setMsg("the message") .setRuleRepository(ruleKey.repository())
int startLine = reportIssue.getTextRange().getStartLine(); issue.setLine(startLine); issue.setChecksum(lineHashSeq.getHashForLine(startLine));
issue.setRuleKey(RuleKey.of(reportIssue.getRuleRepository(), reportIssue.getRuleKey())); if (reportIssue.hasTextRange()) { int startLine = reportIssue.getTextRange().getStartLine(); issue.setLine(startLine); issue.setChecksum(lineHashSeq.getHashForLine(startLine));
final ScannerReport.TextRange.Builder rangeBuilder = ScannerReport.TextRange.newBuilder(); writer.writeComponentSymbols(componentRef, symbolTable.getReferencesBySymbol().entrySet().stream()
ClonePart originBlock = input.getOriginPart(); blockBuilder.clear(); dupBuilder.setOriginPosition(ScannerReport.TextRange.newBuilder() .setStartLine(originBlock.getStartLine()) .setEndLine(originBlock.getEndLine()) .setRange(ScannerReport.TextRange.newBuilder() .setStartLine(duplicate.getStartLine()) .setEndLine(duplicate.getEndLine())
@Override public void store(DefaultHighlighting highlighting) { ScannerReportWriter writer = reportPublisher.getWriter(); DefaultInputFile inputFile = (DefaultInputFile) highlighting.inputFile(); if (shouldSkipStorage(inputFile)) { return; } inputFile.setPublished(true); int componentRef = inputFile.scannerId(); if (writer.hasComponentData(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, componentRef)) { throw new UnsupportedOperationException("Trying to save highlighting twice for the same file is not supported: " + inputFile); } final ScannerReport.SyntaxHighlightingRule.Builder builder = ScannerReport.SyntaxHighlightingRule.newBuilder(); final ScannerReport.TextRange.Builder rangeBuilder = ScannerReport.TextRange.newBuilder(); writer.writeComponentSyntaxHighlighting(componentRef, highlighting.getSyntaxHighlightingRuleSet().stream() .map(input -> { builder.setRange(rangeBuilder.setStartLine(input.range().start().line()) .setStartOffset(input.range().start().lineOffset()) .setEndLine(input.range().end().line()) .setEndOffset(input.range().end().lineOffset()) .build()); builder.setType(ScannerReportUtils.toProtocolType(input.getTextType())); return builder.build(); }).collect(toList())); }
private static ScannerReport.Issue createReportIssue(Issue issue, int componentRef, String activeRuleSeverity) { String primaryMessage = nullToEmpty(issue.primaryLocation().message()); org.sonar.api.batch.rule.Severity overriddenSeverity = issue.overriddenSeverity(); Severity severity = overriddenSeverity != null ? Severity.valueOf(overriddenSeverity.name()) : Severity.valueOf(activeRuleSeverity); 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(componentRef); 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::addFlow, locationBuilder, textRangeBuilder, issue.flows()); return builder.build(); }
private static ScannerReport.ExternalIssue createReportExternalIssue(ExternalIssue issue, int componentRef) { // primary location of an external issue must have a message String primaryMessage = issue.primaryLocation().message(); Severity severity = Severity.valueOf(issue.severity().name()); IssueType issueType = IssueType.valueOf(issue.type().name()); ScannerReport.ExternalIssue.Builder builder = ScannerReport.ExternalIssue.newBuilder(); ScannerReport.IssueLocation.Builder locationBuilder = IssueLocation.newBuilder(); ScannerReport.TextRange.Builder textRangeBuilder = ScannerReport.TextRange.newBuilder(); // non-null fields builder.setSeverity(severity); builder.setType(issueType); builder.setEngineId(issue.engineId()); builder.setRuleId(issue.ruleId()); builder.setMsg(primaryMessage); locationBuilder.setMsg(primaryMessage); locationBuilder.setComponentRef(componentRef); TextRange primaryTextRange = issue.primaryLocation().textRange(); if (primaryTextRange != null) { builder.setTextRange(toProtobufTextRange(textRangeBuilder, primaryTextRange)); } Long effort = issue.remediationEffort(); if (effort != null) { builder.setEffort(effort); } applyFlows(builder::addFlow, locationBuilder, textRangeBuilder, issue.flows()); return builder.build(); }
private void processHighlighting(Iterator<ScannerReport.SyntaxHighlightingRule> syntaxHighlightingIterator, StringBuilder highlighting, DbFileSources.Line.Builder lineBuilder) { ScannerReport.SyntaxHighlightingRule syntaxHighlighting = syntaxHighlightingIterator.next(); int line = lineBuilder.getLine(); ScannerReport.TextRange range = syntaxHighlighting.getRange(); if (range.getStartLine() <= line) { String offsets = rangeOffsetConverter.offsetToString(syntaxHighlighting.getRange(), line, lineBuilder.getSource().length()); if (offsets.isEmpty()) { if (range.getEndLine() == line) { syntaxHighlightingIterator.remove(); } } else { if (highlighting.length() > 0) { highlighting.append(SYMBOLS_SEPARATOR); } highlighting.append(offsets) .append(OFFSET_SEPARATOR) .append(getCssClass(syntaxHighlighting.getType())); if (range.getEndLine() == line) { syntaxHighlightingIterator.remove(); } } } }
public static TrackedIssue toTrackedIssue(InputComponent component, ScannerReport.Issue rawIssue, @Nullable SourceHashHolder hashes) { RuleKey ruleKey = RuleKey.of(rawIssue.getRuleRepository(), rawIssue.getRuleKey()); Preconditions.checkNotNull(component.key(), "Component key must be set"); Preconditions.checkNotNull(ruleKey, "Rule key must be set"); TrackedIssue issue = new TrackedIssue(hashes != null ? hashes.getHashedSource() : null); issue.setKey(Uuids.createFast()); issue.setComponentKey(component.key()); issue.setRuleKey(ruleKey); issue.setGap(rawIssue.getGap() != 0 ? rawIssue.getGap() : null); issue.setSeverity(rawIssue.getSeverity().name()); issue.setMessage(StringUtils.trimToNull(rawIssue.getMsg())); issue.setResolution(null); issue.setStatus(Issue.STATUS_OPEN); issue.setNew(true); if (rawIssue.hasTextRange()) { TextRange r = rawIssue.getTextRange(); issue.setStartLine(r.getStartLine()); issue.setStartLineOffset(r.getStartOffset()); issue.setEndLine(r.getEndLine()); issue.setEndLineOffset(r.getEndOffset()); } return issue; }
/** * Get list of all start positions of a symbol in an inputfile * * @param symbolStartLine 0-based start offset for the symbol in file * @param symbolStartLineOffset 0-based end offset for the symbol in file */ @CheckForNull public List<ScannerReport.TextRange> symbolReferencesFor(InputFile file, int symbolStartLine, int symbolStartLineOffset) { int ref = ((DefaultInputComponent) file).scannerId(); try (CloseableIterator<Symbol> symbols = getReportReader().readComponentSymbols(ref)) { while (symbols.hasNext()) { Symbol symbol = symbols.next(); if (symbol.getDeclaration().getStartLine() == symbolStartLine && symbol.getDeclaration().getStartOffset() == symbolStartLineOffset) { return symbol.getReferenceList(); } } } return Collections.emptyList(); }
private static DetailedTextBlock convert(ScannerReport.TextRange textRange, int id) { return new DetailedTextBlock(id, textRange.getStartLine(), textRange.getEndLine()); }
private static boolean matchLine(ScannerReport.TextRange range, int line) { return range.getStartLine() <= line && range.getEndLine() >= line; }
private static TextRange toRange(InputFile file, ScannerReport.TextRange reportRange) { return file.newRange(file.newPointer(reportRange.getStartLine(), reportRange.getStartOffset()), file.newPointer(reportRange.getEndLine(), reportRange.getEndOffset())); }
private static void validateEndOffsetNotGreaterThanLineLength(ScannerReport.TextRange range, int lineLength, int line) { checkExpression(range.getEndLine() != line || range.getEndOffset() <= lineLength, "End offset %s is defined outside the length (%s) of the line %s", range.getEndOffset(), lineLength, line); }
private static TextBlock convert(ScannerReport.TextRange textRange) { return new TextBlock(textRange.getStartLine(), textRange.getEndLine()); }
private static void validateStartOffsetNotGreaterThanLineLength(ScannerReport.TextRange range, int lineLength, int line) { checkExpression(range.getStartLine() != line || range.getStartOffset() <= lineLength, "Start offset %s is defined outside the length (%s) of the line %s", range.getStartOffset(), lineLength, line); }
@Override public int compare(ScannerReport.Symbol o1, ScannerReport.Symbol o2) { if (o1.getDeclaration().getStartLine() == o2.getDeclaration().getStartLine()) { return Integer.compare(o1.getDeclaration().getStartOffset(), o2.getDeclaration().getStartOffset()); } else { return Integer.compare(o1.getDeclaration().getStartLine(), o2.getDeclaration().getStartLine()); } } }
@CheckForNull private ScannerReport.SyntaxHighlightingRule getNextHighlightingMatchingLine(int line) { // Get next element (if exists) if (currentItem == null && lineHighlightingIterator.hasNext()) { currentItem = lineHighlightingIterator.next(); } // Return current element if lines match if (currentItem != null && currentItem.getRange().getStartLine() == line) { return currentItem; } return null; }