-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprune.sh
More file actions
66 lines (55 loc) · 2.17 KB
/
prune.sh
File metadata and controls
66 lines (55 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/bash
while getopts 'd' opt; do
case "$opt" in
d) DRY_RUN=true ;;
*) echo "Usage $0 [-d]" >&2; exit 1
esac
done
shift "$((OPTIND-1))"
export GH_PAGER=
DAYS_OLD=14
packages=(buildroot org.kde.sdk org.kde.sdk.locale org.kde.sdk.debug org.kde.platform org.kde.platform.locale)
now=$(date +%s)
for pkg in "${packages[@]}"; do
gh api --paginate -H "Accept: application/vnd.github+json" \
"/users/$REPOSITORY_OWNER/packages/container/$REPOSITORY%2F$pkg/versions" | \
jq -c '.[]' | while read -r version; do
id=$(echo "$version" | jq -r '.id')
tags=$(echo "$version" | jq -r '.metadata.container.tags | join(",")')
updated_at=$(echo "$version" | jq -r '.updated_at')
updated_epoch=$(date -d "$updated_at" +%s)
age_days=$(( (now - updated_epoch) / 86400 ))
pkgname="${pkg//%2F//}"
should_delete=false
if [[ -z "$tags" ]]; then
echo "🗑 Deleting untagged $pkgname $id"
should_delete=true
fi
if echo "$tags" | grep -Eq '\bpr-[0-9]+\b'; then
pr_num=$(echo "$tags" | grep -o '[0-9]\+')
state=$(gh api "/repos/$REPOSITORY_OWNER/$REPOSITORY/pulls/$pr_num" -q '.state')
if [[ "$state" == "closed" ]]; then
echo "🗑 Deleting PRs artifacts: $pkgname $tags (ID: $id)"
should_delete=true
else
echo "⏩ Skipping PR-$pr_num (still open) in $pkgname: $tags"
continue
fi
fi
if [[ "$tags" =~ latest ]]; then
# echo "⏩ Skipping: $pkgname $tags (contains 'latest')"
continue
fi
if [[ "$age_days" -ge "$DAYS_OLD" ]]; then
echo "🗑 Deleting: $pkgname $tags (ID: $id, $age_days days old)"
should_delete=true
fi
if [[ "$should_delete" == true ]]; then
if [[ "$DRY_RUN" == "true" ]]; then
echo "💤 Dry-run: would delete /users/$REPOSITORY_OWNER/packages/container/$REPOSITORY%2F$pkg/versions/$id"
else
gh api -X DELETE "/users/$REPOSITORY_OWNER/packages/container/$REPOSITORY%2F$pkg/versions/$id"
fi
fi
done
done