-
Notifications
You must be signed in to change notification settings - Fork 10
143 lines (127 loc) · 4.8 KB
/
clean_repo.yml
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
name: Clean Container Registry
on:
# schedule:
# - cron: '0 21 * * 0'
workflow_dispatch:
jobs:
delete_untagged:
name: Delete Untagged Packages
runs-on: ubuntu-latest
steps:
- name: Delete Untagged Packages
uses: Chizkiyahu/delete-untagged-ghcr-action@v5
with:
token: ${{ secrets.PACKAGE_DELETER }}
repository_owner: ${{ github.repository_owner }}
repository: ${{ github.repository }}
untagged_only: true
owner_type: user
delete_old_pr:
name: Delete Old PR Packages
runs-on: ubuntu-latest
permissions:
packages: read
steps:
- name: Delete Old PR Packages
id: all_tags
run: |
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/user/packages/container/build-container-installer/versions" > all_packages.json
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/pulls | \
jq -r '.[] | select(.state == "open") | .number' | \
sed 's/^/pr-/g' > open_prs
cat << EOF | python
import json
import re
prs = open("open_prs", "r")
open_prs = prs.readlines()
open_prs = [x.strip() for x in open_prs]
all_packages = open('all_packages.json')
data = json.load(all_packages)
delete_versions = open("delete_versions", "w")
for i in data:
delete = True
for tag in i['metadata']['container']['tags']:
if not re.match('pr-.*', tag):
delete = False
continue
if tag in open_prs:
delete = False
if delete:
print("delete", i['id'])
delete_versions.write(str(i['id']))
delete_versions.write("\n")
print(i['metadata']['container']['tags'])
EOF
for id in $(cat delete_versions)
do
curl -L \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.PACKAGE_DELETER }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/user/packages/container/build-container-installer/versions/${id}
done
delete_old_branches:
name: Delete Old Branch Packages
runs-on: ubuntu-latest
permissions:
packages: read
steps:
- name: Delete Old Branch Packages
run: |
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/user/packages/container/build-container-installer/versions" > all_packages.json
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/branches | jq -r '.[].name' > branches
cat << EOF | python
import json
import re
branches_f = open("branches", "r")
branches = branches_f.readlines()
branches = [x.strip() for x in branches]
all_packages_f = open('all_packages.json')
data = json.load(all_packages_f)
delete_versions = open("delete_versions", "w")
for i in data:
delete = True
for tag in i['metadata']['container']['tags']:
if re.match('v[0-9]+\\\.[0-9]+\\\.[0-9]+', tag):
delete = False
continue
if re.match('pr-.*', tag):
delete = False
continue
if tag in branches:
delete = False
continue
if tag == "latest":
delete = False
if delete:
print("delete", i['id'])
delete_versions.write(str(i['id']))
delete_versions.write("\n")
print(i['metadata']['container']['tags'])
EOF
for id in $(cat delete_versions)
do
curl -L \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.PACKAGE_DELETER }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/user/packages/container/build-container-installer/versions/${id}
done