Skip to content

Commit

Permalink
json-writer: print level key in SARIF output
Browse files Browse the repository at this point in the history
... 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]>
  • Loading branch information
lzaoral and kdudka committed Aug 26, 2022
1 parent d9ede63 commit a3eac76
Show file tree
Hide file tree
Showing 7 changed files with 12,242 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/json-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,24 @@ static void sarifEncodeMsg(PTree *pDst, const std::string& text)
pDst->put_child("message", msg);
}

static void sarifEncodeLevel(PTree *result, const std::string &event) {
std::string level = event;

// cut the [...] suffix from event if present
size_t pos = event.find('[');
if (std::string::npos != pos)
level = event.substr(0U, pos);

// go through events that denote warning level
for (const char *str : {"error", "warning", "note"}) {
if (str == level) {
// encode in the output if matched
result->put<std::string>("level", level);
return;
}
}
}

static void sarifEncodeLoc(PTree *pLoc, const Defect &def, unsigned idx)
{
// location ID within the result
Expand Down Expand Up @@ -291,6 +309,9 @@ void SarifTreeEncoder::appendDef(const Defect &def)
// update CWE map
cweMap_[ruleId] = def.cwe;

// key event severity level
sarifEncodeLevel(&result, keyEvt.event);

// key event location
PTree loc;
sarifEncodeLoc(&loc, def, def.keyEventIdx);
Expand Down
Loading

0 comments on commit a3eac76

Please sign in to comment.