"""What a lane's upstream has actually taken: merged PRs, not pushes or opened PRs. Why (11-10). The math lane's results are PRs to a maintainer's repo. By 11-10 nothing had been merged there since 10-26, for anyone; 18 of the 86 open PRs were ours and only 6 of ours had ever merged, yet the lane's yield counted every refereed row as shipped and its reports said "live on Tao's table". A push or an opened PR is activity. So, for a lane with UPSTREAM.json: * lane-yield counts a result only when its link is a merged PR (or a commit) in that repo; * `rh lanes upstream` shows open-unmerged N and the oldest one's age; * tell-general refuses a report that calls an unmerged PR "live". lanes/LANE/UPSTREAM.json: {"repo": "owner/name", "author": "github-login"} The PR list is fetched with `gh api` (refresh) and cached beside it as .upstream-prs.json. Readers never touch the network (the publish hook may not); a cache older than MAX_AGE_H is a refusal for the gate, not a guess. """ import datetime as dt import json from pathlib import Path import re import shutil import subprocess from lane_paths import lane_dirs CONFIG = 'UPSTREAM.json' CACHE = '.upstream-prs.json' MAX_AGE_H = 6 UTC = dt.timezone.utc def now_utc(): return dt.datetime.now(UTC) def parse(stamp): return dt.datetime.fromisoformat(str(stamp).replace('Z', '+00:00')).astimezone(UTC) def config(lane): """The lane's upstream settings, or None when it has none.""" path = Path(lane) / CONFIG if not path.is_file(): return None cfg = json.loads(path.read_text()) if not re.fullmatch(r'[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+', str(cfg.get('repo', ''))): raise ValueError('{}: repo must be owner/name'.format(path)) if not re.fullmatch(r'[A-Za-z0-9-]+', str(cfg.get('author', ''))): raise ValueError('{}: author must be a GitHub login'.format(path)) return cfg def lanes_with_upstream(root): return [p for p in lane_dirs(root) if (p / CONFIG).is_file()] def gh_pulls(repo): """Every PR of the repo, as dicts, from the GitHub API through an authenticated gh.""" gh = shutil.which('gh') or '/opt/homebrew/bin/gh' out = subprocess.run([gh, 'api', '-X', 'GET', 'repos/{}/pulls'.format(repo), '-f', 'state=all', '-f', 'per_page=100', '--paginate', '--jq', '.[] | {number, state, merged_at, created_at, url: .html_url, ' 'author: .user.login, base: .base.ref}'], capture_output=True, text=True, timeout=120, check=True).stdout return [json.loads(line) for line in out.splitlines() if line.strip()] def refresh(lane, fetch=gh_pulls, now=None): cfg = config(lane) if cfg is None: raise ValueError('{} has no {}'.format(lane, CONFIG)) prs = fetch(cfg['repo']) data = dict(fetched=(now or now_utc()).isoformat(timespec='seconds'), repo=cfg['repo'], prs=prs) tmp = Path(lane) / (CACHE + '.tmp') tmp.write_text(json.dumps(data)) tmp.replace(Path(lane) / CACHE) return data def cached(lane): try: return json.loads((Path(lane) / CACHE).read_text()) except (OSError, ValueError): return None def age_hours(data, now=None): return ((now or now_utc()) - parse(data['fetched'])).total_seconds() / 3600 def summary(lane, now=None): """Our PRs there: open-unmerged count and the oldest one's age, merged count, last merge.""" cfg, data = config(lane), cached(lane) if cfg is None or data is None: return None now = now or now_utc() ours = [p for p in data['prs'] if p.get('author', '').lower() == cfg['author'].lower()] open_ = sorted((p for p in ours if p['state'] == 'open'), key=lambda p: p['created_at']) merged = [p for p in ours if p.get('merged_at')] anyone = [p['merged_at'] for p in data['prs'] if p.get('merged_at')] return dict(repo=cfg['repo'], open_unmerged=len(open_), oldest_open_days=round((now - parse(open_[0]['created_at'])).total_seconds() / 86400, 1) if open_ else None, oldest_open=open_[0]['url'] if open_ else None, merged=len(merged), last_merge_ours=max((p['merged_at'] for p in merged), default=None), last_merge_anyone=max(anyone, default=None), cache_age_h=round(age_hours(data, now), 1)) def line(s): return ('upstream {repo}: open-unmerged {n}{oldest}; merged {m} (last ours {lo}, last anyone {la}); ' 'list {age} h old').format( repo=s['repo'], n=s['open_unmerged'], oldest=', oldest {} d'.format(s['oldest_open_days']) if s['oldest_open_days'] is not None else '', m=s['merged'], lo=(s['last_merge_ours'] or 'never')[:10], la=(s['last_merge_anyone'] or 'never')[:10], age=s['cache_age_h']) def taken(lane, link): """True when link is a merged PR, or a commit, in the lane's upstream repo.""" cfg, data = config(lane), cached(lane) if cfg is None or data is None or not link: return False repo = re.escape(cfg['repo']) m = re.match(r'https://github\.com/' + repo + r'/pull/(\d+)', str(link), re.I) if m: return any(p['number'] == int(m.group(1)) and p.get('merged_at') for p in data['prs']) return bool(re.match(r'https://github\.com/' + repo + r'/commit/[0-9a-f]{7,40}', str(link), re.I)) def targets(lane, link): """True when link points into the lane's upstream repo (a PR, commit or file there). 11-15: math's Erdos #1183 values (refereed HOLDS, published at erdosproblems.com) never counted in lane-yield, because every math result had to be a merged PR in the optimization-table repo. The upstream rule is about results sent THERE; a result published elsewhere counts on its verdict.""" cfg = config(lane) if cfg is None or not link: return False return bool(re.match(r'https://github\.com/' + re.escape(cfg['repo']) + r'(/|$)', str(link), re.I)) PR_REF = re.compile(r'github\.com/([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+)/pull/(\d+)|(?