@@ -30,27 +30,52 @@ jobs:
3030 env :
3131 GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
3232 run : |
33+ set -euo pipefail
34+
35+ PACKAGE_NAME="com.uploadcare.uploadcare"
3336 SNAPSHOT_VERSION="${{ steps.get-version.outputs.base_version }}-PR-${{ github.event.pull_request.number }}-SNAPSHOT"
3437 echo "Looking for package version: ${SNAPSHOT_VERSION}"
3538
36- # Retrieve the numeric ID of any published version matching this snapshot.
37- # --paginate ensures all pages are scanned; --jq filters on each page.
39+ # Fetch ALL versions so we can detect whether the snapshot is the last remaining version.
40+ ALL_VERSION_IDS=$(gh api \
41+ --paginate \
42+ -H "Accept: application/vnd.github+json" \
43+ "/orgs/${{ github.repository_owner }}/packages/maven/${PACKAGE_NAME}/versions" \
44+ --jq ".[].id" 2>/dev/null || true)
45+
46+ # Retrieve the numeric ID(s) of any published version matching this snapshot.
3847 VERSION_IDS=$(gh api \
3948 --paginate \
4049 -H "Accept: application/vnd.github+json" \
41- "/orgs/${{ github.repository_owner }}/packages/maven/com.uploadcare.uploadcare /versions" \
50+ "/orgs/${{ github.repository_owner }}/packages/maven/${PACKAGE_NAME} /versions" \
4251 --jq ".[] | select(.name == \"${SNAPSHOT_VERSION}\") | .id" 2>/dev/null || true)
4352
44- if [ -z "$VERSION_IDS" ]; then
53+ if [ -z "${ VERSION_IDS} " ]; then
4554 echo "No published version found for ${SNAPSHOT_VERSION} — nothing to delete."
4655 exit 0
4756 fi
4857
58+ TOTAL_VERSIONS=$(printf "%s\n" "${ALL_VERSION_IDS}" | sed '/^$/d' | wc -l | tr -d ' ')
59+ echo "Total versions in package ${PACKAGE_NAME}: ${TOTAL_VERSIONS}"
60+
61+ # GitHub Packages does not allow deleting the LAST version of a package (HTTP 400).
62+ # If this is the last remaining version, delete the whole package instead.
63+ if [ "${TOTAL_VERSIONS}" -le 1 ]; then
64+ echo "Snapshot is the last version; deleting the whole package ${PACKAGE_NAME} …"
65+ gh api --method DELETE \
66+ -H "Accept: application/vnd.github+json" \
67+ "/orgs/${{ github.repository_owner }}/packages/maven/${PACKAGE_NAME}"
68+ echo "Deleted package ${PACKAGE_NAME}."
69+ exit 0
70+ fi
71+
4972 while IFS= read -r VERSION_ID; do
73+ [ -z "${VERSION_ID}" ] && continue
5074 echo "Deleting version ID ${VERSION_ID} (${SNAPSHOT_VERSION}) …"
5175 gh api --method DELETE \
5276 -H "Accept: application/vnd.github+json" \
53- "/orgs/${{ github.repository_owner }}/packages/maven/com.uploadcare.uploadcare /versions/${VERSION_ID}"
77+ "/orgs/${{ github.repository_owner }}/packages/maven/${PACKAGE_NAME} /versions/${VERSION_ID}"
5478 echo "Deleted version ID ${VERSION_ID}."
55- done <<< "$VERSION_IDS"
79+ done <<< "${VERSION_IDS}"
80+
5681 echo "Done cleaning up ${SNAPSHOT_VERSION}."
0 commit comments