"""Collect credit information for recently merged PRs.
A full-history rebuild (removing the cutoff below) takes a long time (hours!)
due to GitHub rate limits, even with a personal GITHUB_TOKEN.
"""
import datetime
import json
import os
import re
from pathlib import Path
from github import Auth, Github
from tqdm import tqdm
auth = Auth.Token(os.environ["GITHUB_TOKEN"])
g = Github(auth=auth, per_page=100)
out_path = Path(__file__).parents[2] / "doc" / "sphinxext" / "prs"
out_path.mkdir(exist_ok=True)
json_kwargs = dict(indent=2, ensure_ascii=False, sort_keys=False)
repo = g.get_repo("mne-tools/mne-python")
co_re = re.compile("Co-authored-by: ([^<>]+) <([^()>]+)>")
cutoff = datetime.datetime.now(datetime.UTC) - datetime.timedelta(days=90)
pulls_iter = repo.get_pulls(state="closed", sort="updated", direction="desc")
iter_ = tqdm(pulls_iter, unit="pr", desc="Traversing")
n_added = 0
for pull in iter_:
fname_out = out_path / f"{pull.number}.json"
if pull.updated_at < cutoff:
iter_.close()
print(
f"After checking {iter_.n + 1} and adding {n_added} PR(s), reached "
f"PRs last updated before {cutoff.date()}, stopping"
)
break
if fname_out.is_file():
continue
if not pull.merged:
continue
out = dict()
out["merge_commit_sha"] = pull.merge_commit_sha
name, email, login = pull.user.name, pull.user.email, pull.user.login
if email is None:
email = f"{pull.user.id}+{login}@users.noreply.github.com"
if name is None:
name = pull.get_commits()[0].commit.author.name
out["authors"] = [dict(n=name, e=email, l=login)]
if out["merge_commit_sha"]:
try:
merge_commit = repo.get_commit(out["merge_commit_sha"])
except Exception:
pass
else:
msg = merge_commit.commit.message.replace("\r", "")
for n, e in co_re.findall(msg):
if n not in {a["n"] for a in out["authors"]}:
out["authors"].append(dict(n=n, e=e))
out["changes"] = dict()
for file in pull.get_files():
out["changes"][file.filename] = {
k[0]: getattr(file, k) for k in ("additions", "deletions")
}
n_added += 1
fname_out.write_text(json.dumps(out, **json_kwargs), encoding="utf-8")
g.close()