Skip to content

Commit

Permalink
Merge pull request #5 from qld-gov-au/cleanupImprovement
Browse files Browse the repository at this point in the history
Improve github package cleanup to handle more than 100 version/packag…
  • Loading branch information
zmacca authored Jul 31, 2024
2 parents ef0558b + eed1e1b commit a569ee6
Showing 1 changed file with 74 additions and 8 deletions.
82 changes: 74 additions & 8 deletions .github/bin/cleanupGitHubMavenPackage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,53 @@ echo RELEASE_VERSION=$RELEASE_VERSION
echo "Listing all package versions for organization $GITHUB_ORG..."

# Get the list of packages
packages_response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/orgs/$GITHUB_ORG/packages?package_type=maven&repository_id=$GITHUB_REPO_ID")
#packages_response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
# -H "Accept: application/vnd.github.v3+json" \
# "https://api.github.com/orgs/$GITHUB_ORG/packages?package_type=maven&repository_id=$GITHUB_REPO_ID")

page=1
packages_response=""
# Fetch all pages of packages
while :; do
response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/orgs/$GITHUB_ORG/packages?package_type=maven&repository_id=$GITHUB_REPO_ID&page=$page&per_page=100")

# Initialize variables for package versions
page=1
versions_response=""

# Fetch all pages of package versions
while :; do
response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/orgs/$GITHUB_ORG/packages/maven/$package/versions?page=$page&per_page=100")

if [ -z "$response" ] || [ "$response" == "[]" ]; then
break
fi

if [ -z "$versions_response" ]; then
versions_response="$response"
else
versions_response=$(echo "$versions_response" | jq -s 'add' - "$response")
fi

page=$((page + 1))
done

if [ -z "$response" ] || [ "$response" == "[]" ]; then
break
fi

if [ -z "$packages_response" ]; then
packages_response="$response"
else
packages_response=$(echo "$packages_response" | jq -s 'add' - "$response")
fi

page=$((page + 1))
done

# Extract package names
package_names=$(echo "$packages_response" | jq -r '.[].name')
Expand All @@ -27,12 +71,34 @@ for package in $package_names; do
echo "Processing package: $package"

# Get the list of package versions
response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/orgs/$GITHUB_ORG/packages/maven/$package/versions")
# versions_response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
# -H "Accept: application/vnd.github.v3+json" \
# "https://api.github.com/orgs/$GITHUB_ORG/packages/maven/$package/versions")

page=1
versions_response=""

# Fetch all pages of package versions
while :; do
response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/orgs/$GITHUB_ORG/packages/maven/$package/versions?page=$page&per_page=100")

if [ -z "$response" ] || [ "$response" == "[]" ]; then
break
fi

if [ -z "$versions_response" ]; then
versions_response="$response"
else
versions_response=$(echo "$versions_response" | jq -s 'add' - "$response")
fi

page=$((page + 1))
done

# Extract version IDs for the specified release version
version_ids=$(echo "$response" | jq -r --arg version "$RELEASE_VERSION" '.[] | select(.name == $version) | .id')
version_ids=$(echo "$versions_response" | jq -r --arg version "$RELEASE_VERSION" '.[] | select(.name == $version) | .id')

if [ -z "$version_ids" ]; then
echo "No versions found for $RELEASE_VERSION in package $package"
Expand All @@ -45,4 +111,4 @@ for package in $package_names; do
"https://api.github.com/orgs/$GITHUB_ORG/packages/maven/$package/versions/$version_id"
done
fi
done
done

0 comments on commit a569ee6

Please sign in to comment.