-
Notifications
You must be signed in to change notification settings - Fork 7
/
cleanup-old-gists
executable file
·53 lines (39 loc) · 1.31 KB
/
cleanup-old-gists
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
#!/usr/bin/env python
"""
Delete uploaded logfiles from Gist that are older than 30 days.
"""
import os
import sys
import requests
import dateutil.parser
from datetime import datetime
from socket import setdefaulttimeout
setdefaulttimeout(120)
# https://docs.github.com/en/rest/reference/gists
headers = {
"Accept": "application/vnd.github.v3+json",
"Authorization": "token %s" % os.environ["GITHUBTOKEN"],
}
r = requests.get("https://api.github.com/gists", headers=headers)
response_json = r.json()
# only delete this many gists per-run of this script
gist_delete_limit = 5
gists_deleted = 0
for gist in response_json:
if gists_deleted > gist_delete_limit:
print("gist_delete_limit reached - exit for now.")
sys.exit(0)
if "id" not in gist:
print(response_json)
print("Couldn't find an id field. Assume the API request failed - exit.")
sys.exit(1)
time_created = dateutil.parser.isoparse(gist["created_at"])
id = gist["id"]
age_days = (
datetime.now(time_created.tzinfo) - time_created
).total_seconds() / 86400.0
if age_days > 30:
print(f"Gist {id} is {age_days} days old - will delete.")
r = requests.delete(f"https://api.github.com/gists/{id}", headers=headers)
print(f"Deleted {id}.")
gists_deleted += 1