-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscover-repos
More file actions
224 lines (175 loc) · 6.11 KB
/
discover-repos
File metadata and controls
224 lines (175 loc) · 6.11 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python
"""discover-repos: Find repos with recent activity.
For each owner (user + configured orgs), pages through repos sorted by
push date and stops when repos are older than the cutoff. For each
candidate, checks whether the user has activity and whether the default
branch has new commits.
Output (stdout):
user: <username>
<owner>/<repo> <flags>
Flags: user-active, has-commits
Progress messages go to stderr.
"""
import argparse
import json
import subprocess
import sys
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime, timedelta, timezone
DEBUG = False
def debug(msg):
if DEBUG:
print(msg, file=sys.stderr)
def gh_api(endpoint):
"""Call gh api and return parsed JSON, or None on failure."""
result = subprocess.run(
["gh", "api", endpoint],
capture_output=True, text=True, encoding="utf-8",
)
if result.returncode != 0:
return None
try:
return json.loads(result.stdout)
except json.JSONDecodeError:
return None
def get_username():
result = subprocess.run(
["gh", "api", "user", "--jq", ".login"],
capture_output=True, text=True, encoding="utf-8",
)
return result.stdout.strip()
def read_orgs(config_path="state/config.yaml"):
"""Parse orgs from config.yaml without a YAML library."""
orgs = []
try:
with open(config_path) as f:
for line in f:
stripped = line.strip()
if stripped.startswith("- "):
orgs.append(stripped[2:].strip())
except FileNotFoundError:
pass
return orgs
def list_repos_for_owner(owner, user, since_iso):
"""Page through repos for an owner, stop when pushed_at < since."""
candidates = []
page = 1
while True:
if owner == user:
endpoint = (
f"user/repos?sort=pushed&direction=desc"
f"&per_page=100&page={page}&type=owner"
)
else:
endpoint = (
f"orgs/{owner}/repos?sort=pushed&direction=desc"
f"&per_page=100&page={page}"
)
t0 = time.monotonic()
repos = gh_api(endpoint)
debug(f"[{int((time.monotonic() - t0) * 1000)}ms] list {owner} page {page}")
if not repos:
break
for repo in repos:
if repo.get("fork"):
continue
if repo["pushed_at"] >= since_iso:
candidates.append(f"{repo['owner']['login']}/{repo['name']}")
# Stop if oldest repo on page predates cutoff
oldest = repos[-1].get("pushed_at", "")
if not oldest or oldest < since_iso:
break
page += 1
return candidates
def check_user_events(repo, user, since_iso):
"""Check if user has events on this repo since cutoff."""
t0 = time.monotonic()
data = gh_api(f"repos/{repo}/events?per_page=100")
debug(f"[{int((time.monotonic() - t0) * 1000)}ms] events {repo}")
if not data:
return False
return any(
e.get("actor", {}).get("login") == user
and e.get("created_at", "") >= since_iso
for e in data
)
def check_commits(repo, since_iso):
"""Check if repo has commits on default branch since cutoff."""
t0 = time.monotonic()
data = gh_api(f"repos/{repo}/commits?since={since_iso}&per_page=1")
debug(f"[{int((time.monotonic() - t0) * 1000)}ms] commits {repo}")
if not data:
return False
return len(data) > 0
def main():
parser = argparse.ArgumentParser(description="Find repos with recent activity")
parser.add_argument("--since", help="Cutoff date (YYYY-MM-DD)")
parser.add_argument("--debug", action="store_true", help="Show timing info")
args = parser.parse_args()
global DEBUG
DEBUG = args.debug
if args.since:
since = args.since
else:
since = (datetime.now(timezone.utc) - timedelta(days=7)).strftime("%Y-%m-%d")
since_iso = f"{since}T00:00:00Z"
# Get username
t0 = time.monotonic()
user = get_username()
debug(f"[{int((time.monotonic() - t0) * 1000)}ms] gh api user")
print(f"user: {user}")
# Read orgs
orgs = read_orgs()
owners = [user] + orgs
# Scan all owners in parallel
t_scan = time.monotonic()
with ThreadPoolExecutor() as pool:
futures = {
pool.submit(list_repos_for_owner, owner, user, since_iso): owner
for owner in owners
}
owner_results = {}
for future in as_completed(futures):
owner = futures[future]
owner_results[owner] = future.result()
# Maintain owner order
candidates = []
for owner in owners:
candidates.extend(owner_results.get(owner, []))
debug(f"[{int((time.monotonic() - t_scan) * 1000)}ms] all scans done")
print(f"Checking {len(candidates)} candidates...", file=sys.stderr)
# Check all candidates for user activity and commits (all in parallel)
t_check = time.monotonic()
event_results = {}
commit_results = {}
with ThreadPoolExecutor() as pool:
event_futures = {
pool.submit(check_user_events, repo, user, since_iso): repo
for repo in candidates
}
commit_futures = {
pool.submit(check_commits, repo, since_iso): repo
for repo in candidates
}
all_futures = {}
all_futures.update(event_futures)
all_futures.update(commit_futures)
for future in as_completed(all_futures):
repo = all_futures[future]
if future in event_futures:
event_results[repo] = future.result()
else:
commit_results[repo] = future.result()
debug(f"[{int((time.monotonic() - t_check) * 1000)}ms] all checks done")
# Output results in candidate order
for repo in candidates:
flags = []
if event_results.get(repo):
flags.append("user-active")
if commit_results.get(repo):
flags.append("has-commits")
if flags:
print(f"{repo} {' '.join(flags)}")
if __name__ == "__main__":
main()