example

#1005
Raw
Author
Anonymous
Created
Jan. 29, 2024, 8:58 p.m.
Expires
Never
Size
1.6 KB
Hits
98
Syntax
PowerShell
Private
✗ No
$ErrorActionPreference = "Stop"
$exit_code = 0

$PSVersionTable

# 1. run snyk auth
snyk auth d19c7d43-2dfc-4b41-a4ae-3e10e4b5b5a3

# 2. run snyk test
Write-Host "Running: snyk test --all-projects --json --org=6628429c-f2ec-427a-9aea-c5cbedd4dbc7 --project-name-prefix=BuildFolder:"
$snyk_test_json = snyk test --all-projects --json --org=6628429c-f2ec-427a-9aea-c5cbedd4dbc7 --project-name-prefix=BuildFolder:

# 3. format results to support single & multiple results returned
Write-Host "Procesing snyk test --json output"
$formatted_json = $snyk_test_json | ConvertFrom-Json

# 4. call snyk-delta for each result
foreach ($project_test_result in $formatted_json) {
 $project_name = $project_test_result.displayTargetFile
 Write-Host "Processing:   $project_name"

 $project_test_json_result = $project_test_result | ConvertTo-Json -Compress -depth 100
    if (cmd /C 'echo "$project_test_json_result" | snyk-delta -d') {
            $project_exit_code = $?
            Write-Host "Finished processing"
        } else {
            $project_exit_code = $?
            if ($project_exit_code -gt 1) {
                Write-Host "snyk-delta encountered an error, retrying"
                cmd /C 'echo "$project_test_json_result" | snyk-delta -d'
            }
            Write-Host "Finished processing"
        }

 if ($project_exit_code -gt $exit_code) {
   $exit_code = $project_exit_code
 }
 Write-Host "Project: ${project_name} | Exit code: ${project_exit_code}"
 Write-Host "";
}

Write-Host "Overall exit code for snyk-delta-all-projects.ps1: ${exit_code}"
exit $exit_code