Skip to content
This repository was archived by the owner on Nov 5, 2025. It is now read-only.

Commit ab8707e

Browse files
committed
github actions workers join the fray
1 parent 91e64df commit ab8707e

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

main.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import argparse
4+
import io
45
import json
56
import os
67
import queue
@@ -10,6 +11,10 @@
1011
import sys
1112
import tempfile
1213
import threading
14+
import time
15+
import urllib.request
16+
import uuid
17+
import zipfile
1318
from typing import NamedTuple
1419

1520
SRC = os.path.abspath('../sentry')
@@ -177,10 +182,77 @@ def _ssh_worker(q: queue.Queue[str], ssh: SSH) -> None:
177182
)
178183

179184

185+
def _gha_worker(q: queue.Queue[str]) -> None:
186+
with open(os.path.expanduser('~/.github-auth.json')) as f:
187+
token = json.load(f)['token']
188+
189+
while True:
190+
items = []
191+
for _ in range(16):
192+
try:
193+
items.append(q.get(block=False))
194+
except queue.Empty:
195+
break
196+
if not items:
197+
return
198+
199+
aid = str(uuid.uuid4())
200+
data = {
201+
'ref': 'main',
202+
'inputs': {'artifact': aid, 'shas': ' '.join(items)},
203+
}
204+
headers = {'Authorization': f'Bearer {token}'}
205+
206+
req = urllib.request.Request(
207+
'https://api.github.com/repos/asottile/sentry-mypy-stats/actions/workflows/run.yml/dispatches', # noqa: E501
208+
method='POST',
209+
data=json.dumps(data).encode(),
210+
headers=headers,
211+
)
212+
urllib.request.urlopen(req).close()
213+
214+
time.sleep(120)
215+
216+
while True:
217+
req = urllib.request.Request(
218+
f'https://api.github.com/repos/asottile/sentry-mypy-stats/actions/artifacts?name={aid}', # noqa: E501
219+
headers=headers,
220+
)
221+
artifacts_resp = json.load(urllib.request.urlopen(req))
222+
if artifacts_resp['artifacts']:
223+
break
224+
else:
225+
time.sleep(2)
226+
227+
artifact, = artifacts_resp['artifacts']
228+
req = urllib.request.Request(artifact['archive_download_url'])
229+
for k, v in headers.items():
230+
req.add_unredirected_header(k, v)
231+
contents = urllib.request.urlopen(req).read()
232+
233+
with tempfile.TemporaryDirectory(dir=DATA) as tmpdir:
234+
zipf = zipfile.ZipFile(io.BytesIO(contents))
235+
zipf.extractall(tmpdir)
236+
237+
for name in os.listdir(tmpdir):
238+
os.rename(
239+
os.path.join(tmpdir, name),
240+
os.path.join(DATA, name),
241+
)
242+
243+
req = urllib.request.Request(
244+
artifact['url'],
245+
method='DELETE',
246+
headers=headers,
247+
)
248+
urllib.request.urlopen(req).close()
249+
250+
180251
def main() -> int:
181252
parser = argparse.ArgumentParser()
182253
parser.add_argument('--jobs', type=int, default=os.cpu_count() or 8)
183254
parser.add_argument('--ssh', type=SSH.parse, action='append', default=[])
255+
parser.add_argument('--gha-jobs', type=int, default=0)
184256
parser.add_argument('cid', nargs='*', default=[])
185257
args = parser.parse_args()
186258

@@ -224,6 +296,11 @@ def _clear_queue(*a: object) -> None:
224296
threads.append(t)
225297
t.start()
226298

299+
for _ in range(args.gha_jobs):
300+
t = threading.Thread(target=_gha_worker, args=(q,))
301+
threads.append(t)
302+
t.start()
303+
227304
for t in threads:
228305
t.join()
229306

0 commit comments

Comments
 (0)