Skip to content

Commit 62d85a1

Browse files
committed
json-parser: decode JSON format produced by Snyk Code
1 parent d733a6e commit 62d85a1

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

src/json-parser.cc

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,62 @@ void SnykTreeDecoder::readRoot(
411411
pDefList = nullptr;
412412
}
413413

414+
void snykReadLocation(DefEvent *pEvt, const pt::ptree &defNode)
415+
{
416+
const pt::ptree *locs;
417+
if (!findChildOf(&locs, defNode, "locations") || locs->empty())
418+
// no location info available
419+
return;
420+
421+
const pt::ptree *pl;
422+
if (!findChildOf(&pl, locs->begin()->second, "physicalLocation"))
423+
// unknown location info format
424+
return;
425+
426+
const pt::ptree *al;
427+
if (findChildOf(&al, *pl, "artifactLocation")) {
428+
const auto uri = valueOf<std::string>(*al, "uri", "");
429+
if (!uri.empty())
430+
// read file name
431+
pEvt->fileName = uri;
432+
}
433+
434+
const pt::ptree *reg;
435+
if (findChildOf(&reg, *pl, "region")) {
436+
// read line/col if available
437+
pEvt->line = valueOf<int>(*reg, "startLine", 0);
438+
pEvt->column = valueOf<int>(*reg, "startColumn", 0);
439+
}
440+
}
441+
414442
bool SnykTreeDecoder::readNode(
415443
Defect *def,
416444
pt::ptree::const_iterator defIter)
417445
{
418-
// TODO
419-
return false;
446+
// initialize the defect structure
447+
*def = Defect("SNYK_CODE_WARNING");
448+
449+
// the current node representing a single snyk's report
450+
const pt::ptree &defNode = defIter->second;
451+
452+
// initialize the key event
453+
const auto level = valueOf<std::string>(defNode, "level", "warning");
454+
def->events.push_back(DefEvent(level));
455+
DefEvent &keyEvent = def->events.back();
456+
457+
// read "rule" that triggered the report
458+
const auto rule = valueOf<std::string>(defNode, "ruleId", "");
459+
if (!rule.empty())
460+
keyEvent.event += "[" + rule + "]";
461+
462+
// read location
463+
keyEvent.fileName = "<unknown>";
464+
snykReadLocation(&keyEvent, defNode);
465+
466+
// read diagnostic message
467+
const pt::ptree *msgNode;
468+
if (findChildOf(&msgNode, defNode, "message"))
469+
keyEvent.msg = valueOf<std::string>(*msgNode, "text", "<unknown>");
470+
471+
return true;
420472
}

0 commit comments

Comments
 (0)