Skip to content

Commit a3eac76

Browse files
lzaoralkdudka
andcommitted
json-writer: print level key in SARIF output
... so that users can easily filter results according to their severity. Fixes: #80 Co-authored-by: Kamil Dudka <[email protected]> Co-authored-by: Lukáš Zaoral <[email protected]>
1 parent d9ede63 commit a3eac76

7 files changed

+12242
-0
lines changed

src/json-writer.cc

+21
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,24 @@ static void sarifEncodeMsg(PTree *pDst, const std::string& text)
214214
pDst->put_child("message", msg);
215215
}
216216

217+
static void sarifEncodeLevel(PTree *result, const std::string &event) {
218+
std::string level = event;
219+
220+
// cut the [...] suffix from event if present
221+
size_t pos = event.find('[');
222+
if (std::string::npos != pos)
223+
level = event.substr(0U, pos);
224+
225+
// go through events that denote warning level
226+
for (const char *str : {"error", "warning", "note"}) {
227+
if (str == level) {
228+
// encode in the output if matched
229+
result->put<std::string>("level", level);
230+
return;
231+
}
232+
}
233+
}
234+
217235
static void sarifEncodeLoc(PTree *pLoc, const Defect &def, unsigned idx)
218236
{
219237
// location ID within the result
@@ -291,6 +309,9 @@ void SarifTreeEncoder::appendDef(const Defect &def)
291309
// update CWE map
292310
cweMap_[ruleId] = def.cwe;
293311

312+
// key event severity level
313+
sarifEncodeLevel(&result, keyEvt.event);
314+
294315
// key event location
295316
PTree loc;
296317
sarifEncodeLoc(&loc, def, def.keyEventIdx);

0 commit comments

Comments
 (0)