If the static analysis tool or output format is not currently supported there are 3 options:
- The simplest thing is to write a script to convert the tool's output to the SARB format (see below).
- Add support to SARB by creating a ResultsParser.
- See if the tool will add support for the SARB format.
The output from tool is piped into the converter (e.g. tool2sarb
) which is then piped into SARB:
tool | tool2sarb | sarb create tool.baseline
This example will show how to write a simple PHP script to convert ESLint's JSON format to the SARB format.
Here is a snippet from ESLint's JSON output...
[
{
"filePath": "/var/lib/jenkins/workspace/Releases/eslint Release/eslint/fullOfProblems.js",
"messages": [
{
"ruleId": "no-unused-vars",
"severity": 2,
"message": "'addOne' is defined but never used.",
"line": 1,
"column": 10,
"nodeType": "Identifier",
"messageId": "unusedVar",
"endLine": 1,
"endColumn": 16
} ,
... next issue in file ...
]
},
... next file ...
]
The SARB format is JSON. For each issue the following information is needed:
SARB field name | Description | Field mapped to in ESLint's output |
---|---|---|
file |
Full path to file | filePath |
line |
Line number of issue | line |
type |
Type of issue (e.g. rule that was violated | ruleId |
message |
Human readable description of problem | message |
The script needs to do the following:
- Read JSON input from STDIN
- Convert JSON to an array
- Loop through the outer array (which has an entry for each file)
- Loop through the
messages
for each file to get the file's issues. - Add these to an array of issues.
- Output array of issues as JSON.
<?php
// See `example/eslint2sarb.php`
// 1. Read JSON input from STDIN
$inputData = stream_get_contents(STDIN);
if ($inputData === false) die("Could not read input");
// 2.Convert JSON to an array
$asJson = json_decode($inputData, true);
if (!is_array($asJson)) die ("Could not parse JSON");
$issues = [];
// 3. Loop through the outer array (which has an entry for each file)
foreach($asJson as $fileWithIssues) {
$fileName = $fileWithIssues['filePath'];
// 4. Loop through the `messages` for each file to get the file's issues.
foreach($fileWithIssues['messages'] as $issue) {
// 5. Add these to an array of issues.
$issues[] = [
'file' => $fileName,
'line' => $issue['line'],
'type' => $issue['ruleId'],
'message' => $issue['message'],
];
}
}
// 6. Output array of issues as JSON.
echo json_encode($issues, JSON_PRETTY_PRINT);
Combining simple scripts like the above to convert to the SARB format gives you the ability to use almost any tool for any language.