Skip to content

Commit

Permalink
handle if results is null
Browse files Browse the repository at this point in the history
Signed-off-by: Sertac Ozercan <[email protected]>
  • Loading branch information
sozercan committed Jan 16, 2024
1 parent 13efe67 commit 175f984
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/patch.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Patch images
name: Patch vulnerable images
on:
# change this to your preferred trigger
workflow_dispatch:
jobs:
patch:
Expand Down Expand Up @@ -32,7 +33,7 @@ jobs:
id: vuln_count
run: |
report_file="report.json"
vuln_count=$(jq '[.Results[] | select(.Class=="os-pkgs" and .Vulnerabilities!=null) | .Vulnerabilities[]] | length' "$report_file")
vuln_count=$(jq 'if .Results then [.Results[] | select(.Class=="os-pkgs" and .Vulnerabilities!=null) | .Vulnerabilities[]] | length else 0 end' "$report_file")
echo "vuln_count=$vuln_count" >> $GITHUB_OUTPUT
# copa action will only run if there are vulnerabilities
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ Copacetic Action is supported with Copa version 0.3.0 and later.

## Example usage

https://github.com/sozercan/copa-action/blob/1318614969da67670fc36a7be0f55c4d5c3f1de8/.github/workflows/patch.yaml#L1-L64
https://github.com/sozercan/copa-action/blob/1318614969da67670fc36a7be0f55c4d5c3f1de8/.github/workflows/patch.yaml#L1-L64
2 changes: 1 addition & 1 deletion test/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ teardown_file() {
@test "Run trivy on patched image" {
run trivy image --exit-code 1 --vuln-type os --ignore-unfixed -f json -o nginx.1.21.6-patched.json 'docker.io/library/nginx:1.21.6-patched'
[ "$status" -eq 0 ]
vulns=$(jq '[.Results[] | select(.Class=="os-pkgs" and .Vulnerabilities!=null) | .Vulnerabilities[]] | length' nginx.1.21.6-patched.json)
vulns=$(jq 'if .Results then [.Results[] | select(.Class=="os-pkgs" and .Vulnerabilities!=null) | .Vulnerabilities[]] | length else 0 end' nginx.1.21.6-patched.json)
assert_equal "$vulns" "0"
}

0 comments on commit 175f984

Please sign in to comment.