Skip to content

Commit 920e96b

Browse files
committed
fix: handle json array existence when single or multi-project results
1 parent f6ab727 commit 920e96b

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed

.github/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ To release a major you need to add `BREAKING CHANGE: ` to the start of the body
7979

8080
Ensure that your code adheres to the included `.eslintrc` config by running `npm run test:checks`.
8181

82-
Check your code is formatted by running `prettier`m e.g. `npx prettier --check "the-search-path"`
82+
Check your code is formatted by running `prettier` e.g. `npx prettier --check snykTask/**/*.ts` and auto-fix any violations by performing `npx prettier --write snykTask/**/*.ts`
8383

8484
## Sending pull requests
8585

snykTask/src/__tests__/task-lib.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,18 @@ test('getOptionsToExecuteSnyk builds IExecOptions like we need it', () => {
4646
expect(options.ignoreReturnCode).toBe(true);
4747
});
4848

49-
test('finds vulnerabilities greater than medium threshold', () => {
49+
test('finds vulnerabilities greater than medium threshold in single-project results', () => {
50+
const fixturePath =
51+
'snykTask/test/fixtures/single-project-high-vulnerabilities.json';
52+
const itemsFound = doVulnerabilitiesExistForFailureThreshold(
53+
fixturePath,
54+
'medium',
55+
);
56+
57+
expect(itemsFound).toBe(true);
58+
});
59+
60+
test('finds vulnerabilities greater than medium threshold in multi-project results', () => {
5061
const fixturePath = 'snykTask/test/fixtures/high-vulnerabilities.json';
5162
const itemsFound = doVulnerabilitiesExistForFailureThreshold(
5263
fixturePath,

snykTask/src/task-lib.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,29 @@ export function doVulnerabilitiesExistForFailureThreshold(
123123
const json = JSON.parse(file);
124124
const thresholdOrdinal = getSeverityOrdinal(threshold);
125125

126-
for (let i = 0; i < json.length; i++) {
127-
let project = json[i];
128-
for (const vulnerability of project['vulnerabilities']) {
129-
if (getSeverityOrdinal(vulnerability['severity']) >= thresholdOrdinal) {
126+
if (Array.isArray(json)) {
127+
for (let i = 0; i < json.length; i++) {
128+
if (hasMatchingVulnerabilities(json[i], thresholdOrdinal)) {
130129
return true;
131130
}
132131
}
132+
} else {
133+
if (hasMatchingVulnerabilities(json, thresholdOrdinal)) {
134+
return true;
135+
}
133136
}
134137

135138
console.log(
136139
`no vulnerabilities of at least '${threshold}' severity were detected, not failing build`,
137140
);
138141
return false;
139142
}
143+
144+
function hasMatchingVulnerabilities(project: any, thresholdOrdinal: number) {
145+
for (const vulnerability of project['vulnerabilities']) {
146+
if (getSeverityOrdinal(vulnerability['severity']) >= thresholdOrdinal) {
147+
return true;
148+
}
149+
}
150+
return false;
151+
}

snykTask/test/fixtures/high-vulnerabilities.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,18 @@
1111
"ok": true,
1212
"dependencyCount": 0,
1313
"org": "demo-applications"
14-
}
14+
},
15+
{
16+
"vulnerabilities": [
17+
{
18+
"severity": "medium"
19+
},
20+
{
21+
"severity": "high"
22+
}
23+
],
24+
"ok": true,
25+
"dependencyCount": 0,
26+
"org": "demo-applications"
27+
}
1528
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"vulnerabilities": [
3+
{
4+
"severity": "critical"
5+
},
6+
{
7+
"severity": "high"
8+
}
9+
],
10+
"ok": true,
11+
"dependencyCount": 0,
12+
"org": "demo-applications"
13+
}
14+

0 commit comments

Comments
 (0)