Skip to content

Instantly share code, notes, and snippets.

@abhi2495
Created February 9, 2022 20:57
Show Gist options
  • Save abhi2495/19082f4f1550864bef2846bf8adac620 to your computer and use it in GitHub Desktop.
Save abhi2495/19082f4f1550864bef2846bf8adac620 to your computer and use it in GitHub Desktop.
Shell Script to delete a bunch of blackduck versions for a given project id
#! /usr/bin/env bash
blackduckHostName="${1}"
blackduckApiKey="${2}"
blackduckProjectId="${3}"
BEARER_TOKEN_RESPONSE=$(curl -X POST -H "Content-Length: 0" -H "Authorization: token $blackduckApiKey" "$blackduckHostName/api/tokens/authenticate")
BEARER_TOKEN=$(echo $BEARER_TOKEN_RESPONSE | jq -r '.bearerToken')
PROJECT_VERSIONS_LIST_RESPONSE=$(curl -X GET -H "Authorization: Bearer $BEARER_TOKEN" "$blackduckHostName/api/projects/$blackduckProjectId/versions?limit=100")
PROJECT_VERSION_ENDPOINTS=($(echo $PROJECT_VERSIONS_LIST_RESPONSE | jq -r '.items[] | select(.versionName != "develop" and .versionName != "release") | ._meta.href'))
echo "Array size: " ${#PROJECT_VERSION_ENDPOINTS[@]}
for endpoint in "${PROJECT_VERSION_ENDPOINTS[@]}"; do
# echo "Calling the following project version endpoint to delete:-$endpoint."
if [ $endpoint == "null" ]; then
echo "Project version not found for current project. Skipping delete."
else
# echo "Deleting project version"
curl -X DELETE -H "Authorization: Bearer $BEARER_TOKEN" $endpoint
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment