Tuesday, November 24, 2020

Create Sonar Plugin - Scanner Basic - Save Issues 2

 Need to create a InputFile (org.sonar.api.batch.fs.InputFile) to save the issue. 

Retrieve the input file from the file system ( org.sonar.api.batch.fs.FileSystem) by mmatching name and the type of the file (MAIN)

    Currently we have two types (MAIN and TEST)

-----------

public static enum Type {
MAIN,
TEST;

private Type() {
}
}

----------------------

below is the sample code snippet to get the Input file

-------------------------------------------------------------------------------------

private void getResourceAndSaveIssue(final Violation violation, String fileName) {
LOG.debug("Calling getResourceAndSaveIssue for file=" + fileName);
LOG.debug(violation.toString());

InputFile inputFile = fileSystem.inputFile(
fileSystem.predicates().and(
fileSystem.predicates().hasAbsolutePath(fileName),
fileSystem.predicates().hasType(InputFile.Type.MAIN)));

LOG.debug("inputFile null ? " + (inputFile == null));

if (inputFile != null) {
saveIssue(inputFile, violation.getLine(), violation.getRule().getId(), violation.getRule().getName());
} else {
LOG.error("Not able to find a InputFile with " + fileName);
}
}

No comments:

Post a Comment