|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | + |
| 4 | +PROJECTPATH=$1 |
| 5 | +PROJECT=$2 |
| 6 | +VERSION=$3 |
| 7 | +SUFFIX=$4 |
| 8 | + |
| 9 | +DETECT_URL_PATH=https://detect.blackduck.com/detect${DETECT_MAJOR_VERSION:-10}.sh |
| 10 | + |
| 11 | +if [ "$PROJECTPATH" = "" ] |
| 12 | +then |
| 13 | + echo "specify folder to scan" |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +if [ "$PROJECT" = "" ] |
| 18 | +then |
| 19 | + PROJECT=${PROJECTPATH} |
| 20 | +fi |
| 21 | + |
| 22 | +if [ "$VERSION" = "" ] |
| 23 | +then |
| 24 | + VERSION=LATEST |
| 25 | +fi |
| 26 | + |
| 27 | +# SET BD_URL and BD_API_TOKEN variables to point to your instance of Black Duck |
| 28 | +# |
| 29 | +get_bearer_token() { |
| 30 | + local response |
| 31 | + response=$(curl -s -X POST -H "Authorization: token $BD_API_TOKEN" "$BD_URL/api/tokens/authenticate") |
| 32 | + echo "$response" | grep -oP '"bearerToken"\s*:\s*"\K[^"]+' |
| 33 | +} |
| 34 | + |
| 35 | +get_scan_readiness() { |
| 36 | + local api_url="${BD_URL}/api/codelocations?q=name:${PROJECT}_${VERSION}_${SUFFIX}_code%20binary" |
| 37 | + echo "API URL: $api_url" |
| 38 | + local response |
| 39 | + local bearer_token=$(get_bearer_token) |
| 40 | + response=$(curl -s -H "Authorization: Bearer $bearer_token" "$api_url") |
| 41 | + echo "Response: $response" |
| 42 | + |
| 43 | + # Extract count of IN_PROGRESS status items |
| 44 | + local count |
| 45 | + count=$(echo "$response" | grep -o '"status":[^]]*' | grep -c 'IN_PROGRESS') |
| 46 | + echo "Count of IN_PROGRESS scans: $count" |
| 47 | + |
| 48 | + # Return 0 if no scans are in progress (ready), 1 otherwise (not ready) |
| 49 | + if [ "$count" -eq 0 ]; then |
| 50 | + echo "No scans in progress. Scan is ready." |
| 51 | + return 0 |
| 52 | + else |
| 53 | + echo "Scans are still in progress." |
| 54 | + return 1 |
| 55 | + fi |
| 56 | +} |
| 57 | + |
| 58 | +# bash <(curl -s -L $DETECT_URL_PATH) \ |
| 59 | +# --blackduck.url=$BD_URL \ |
| 60 | +# --blackduck.api.token=$BD_API_TOKEN \ |
| 61 | +# --blackduck.trust.cert=true \ |
| 62 | +# --detect.binary.scan.file.path=${PROJECTPATH} \ |
| 63 | +# --detect.tools=BINARY_SCAN \ |
| 64 | +# --detect.project.name=${PROJECT} \ |
| 65 | +# --detect.project.version.name=${VERSION} \ |
| 66 | +# --detect.code.location.name=${PROJECT}_${VERSION}_${SUFFIX}_code \ |
| 67 | + |
| 68 | +if [ "$DETECT_SERIAL_MODE" = "true" ] || [ "$DETECT_SERIAL_MODE" = "TRUE" ]; then |
| 69 | + # Wait for scan readiness |
| 70 | + echo "Waiting for scan readiness..." |
| 71 | + while ! get_scan_readiness; do |
| 72 | + echo "Scan is still in progress. Waiting 30 seconds before rechecking..." |
| 73 | + sleep 30 |
| 74 | + done |
| 75 | + echo "Scan completed." |
| 76 | +fi |
0 commit comments