diff --git a/rdjson_formatter/rdjson_formatter.rb b/rdjson_formatter/rdjson_formatter.rb index 9f077b3..68ae9bc 100755 --- a/rdjson_formatter/rdjson_formatter.rb +++ b/rdjson_formatter/rdjson_formatter.rb @@ -34,7 +34,7 @@ package_name = advisory['module_name'] title = advisory['title'] - cve_id = advisory['cves']&.first || advisory['id'] + cve_id = (advisory['cves']&.first || advisory['id'])&.to_s url = advisory['url'] severity = advisory['severity'].to_s.downcase diff --git a/test/rdjson_formatter/test.sh b/test/rdjson_formatter/test.sh index 89e77ab..006c1fb 100755 --- a/test/rdjson_formatter/test.sh +++ b/test/rdjson_formatter/test.sh @@ -3,6 +3,10 @@ set -eux CWD=$(pwd) +compare_json() { + diff -u <(jq -S . "$1") <(jq -S . "$2") +} + cd ./test/rdjson_formatter/testdata yarn audit --json 2>/dev/null \ @@ -12,4 +16,15 @@ yarn audit --json 2>/dev/null \ > result.out cd "${CWD}" -diff -u ./test/rdjson_formatter/testdata/result.ok ./test/rdjson_formatter/testdata/result.out +compare_json ./test/rdjson_formatter/testdata/result.ok ./test/rdjson_formatter/testdata/result.out + +cd ./test/rdjson_formatter/testdata + +ruby "${CWD}/rdjson_formatter/rdjson_formatter.rb" yarn.lock \ + < advisory_without_cve.jsonl \ + | jq . \ + | sed -e "s!${CWD}/!!g" \ + > advisory_without_cve.out + +cd "${CWD}" +compare_json ./test/rdjson_formatter/testdata/advisory_without_cve.ok ./test/rdjson_formatter/testdata/advisory_without_cve.out diff --git a/test/rdjson_formatter/testdata/advisory_without_cve.jsonl b/test/rdjson_formatter/testdata/advisory_without_cve.jsonl new file mode 100644 index 0000000..0d2c577 --- /dev/null +++ b/test/rdjson_formatter/testdata/advisory_without_cve.jsonl @@ -0,0 +1 @@ +{"type":"auditAdvisory","data":{"advisory":{"id":1116454,"module_name":"minimist","severity":"moderate","title":"Prototype Pollution in minimist","url":"https://github.com/advisories/GHSA-vh95-rmgr-6w4m","recommendation":"Upgrade to version 0.2.1 or later","cves":[]}}} diff --git a/test/rdjson_formatter/testdata/advisory_without_cve.ok b/test/rdjson_formatter/testdata/advisory_without_cve.ok new file mode 100644 index 0000000..5998515 --- /dev/null +++ b/test/rdjson_formatter/testdata/advisory_without_cve.ok @@ -0,0 +1,26 @@ +{ + "source": { + "name": "yarn audit", + "url": "https://github.com/yarnpkg/yarn" + }, + "severity": "WARNING", + "diagnostics": [ + { + "message": "[moderate] Prototype Pollution in minimist\nSolution: Upgrade to version 0.2.1 or later", + "location": { + "path": "yarn.lock", + "range": { + "start": { + "line": 5, + "column": 0 + } + } + }, + "severity": "WARNING", + "code": { + "value": "1116454", + "url": "https://github.com/advisories/GHSA-vh95-rmgr-6w4m" + } + } + ] +}