forked from mozilla/github-org-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_repos.py
executable file
·88 lines (73 loc) · 2.26 KB
/
old_repos.py
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
#!/usr/bin/env python
import json
import os
import re
from datetime import datetime, timedelta
import requests
from client import get_token
CACHEFILE = "cache/mozilla_all_repos.json"
def parse_timestamp(tst):
return datetime.strptime(tst, "%Y-%m-%dT%H:%M:%SZ")
if __name__ == "__main__":
headers = {
"Accept": "application/vnd.github.moondragon+json",
"Authorization": "token %s" % get_token(),
}
if os.path.exists(CACHEFILE):
repos = json.loads(open(CACHEFILE).read())
print(
"Found cached repository list. Delete %s if you want a new "
"one.\n" % CACHEFILE
)
else:
repos = []
repos_api = "https://api.github.com/orgs/%s/repos" % "mozilla"
while True:
resp = requests.get(repos_api, headers=headers)
repos += resp.json()
# Next page.
next_match = re.search(r'<([^>]+)>; rel="next"', resp.headers["Link"])
if not next_match:
break
else:
repos_api = next_match.group(1)
open(CACHEFILE, "w").write(json.dumps(repos))
# Find small/empty repos older than a month.
SMALL_MINAGE = 31
small_repos = [
r
for r in repos
if (
r["size"] < 50
and r["open_issues_count"] == 0
and parse_timestamp(r["updated_at"]) + timedelta(days=SMALL_MINAGE)
< datetime.now()
)
]
small_repos.sort(key=lambda r: r["name"])
print(
"## {} small/empty repositories older than {} days".format(
len(small_repos), SMALL_MINAGE
)
)
for repo in small_repos:
print(repo["name"], ":", repo["size"], "(%s)" % repo["updated_at"])
print("\n\n")
# Find recently untouched repos.
UNTOUCHED_MINAGE = 2 * 365
old_repos = [
r
for r in repos
if (
parse_timestamp(r["updated_at"]) + timedelta(days=(UNTOUCHED_MINAGE))
< datetime.now()
)
]
old_repos.sort(key=lambda r: r["name"])
print(
"## {} repos touched less recently than {} days ago.".format(
len(old_repos), UNTOUCHED_MINAGE
)
)
for repo in old_repos:
print(repo["name"], ":", repo["updated_at"])