From f552a0419aca9cf643706b1a1bda3432da5148c4 Mon Sep 17 00:00:00 2001 From: NotXia <35894453+NotXia@users.noreply.github.com> Date: Fri, 20 Dec 2024 20:45:40 +0100 Subject: [PATCH] Add contributors generation --- .github/update_readme.py | 63 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/.github/update_readme.py b/.github/update_readme.py index 4a7fc43..d233141 100644 --- a/.github/update_readme.py +++ b/.github/update_readme.py @@ -1,5 +1,50 @@ import argparse from read_metadata import readMetadata +import re +import subprocess + + + +def get_contributors(dir=".", filter_usernames=["NotXia"]): + contributors = {} + regex_gh_noreply1 = re.compile(r"\s*(?P\d+)\s+(?P.+) <(?P\d+\+(?P.+)@users\.noreply\.github\.com)>") + regex_gh_noreply2 = re.compile(r"\s*(?P\d+)\s+(?P.+) <(?P(?P.+)@users\.noreply\.github\.com)>") + regex_fallback = re.compile(r"\s*(?P\d+)\s+(?P.+) <(?P.+@.+\.\w+)>") + + p1 = subprocess.Popen(["git", "log"], stdout=subprocess.PIPE) + p2 = subprocess.Popen(["git", "shortlog", "-n", "-s", "-e"], stdin=p1.stdout, stdout=subprocess.PIPE) + result = p2.communicate()[0].decode("utf-8").strip() + + if len(result) == 0: return [] + + for l in result.split("\n"): + if ((res := regex_gh_noreply1.search(l)) != None) or ((res := regex_gh_noreply2.search(l)) != None): + email = res.group("email") + username = res.group("username") + fullname = res.group("fullname") + commits = int(res.group("commits")) + elif (res := regex_fallback.search(l)) != None: + email = res.group("email") + username = None + fullname = res.group("fullname") + commits = int(res.group("commits")) + + if username in filter_usernames: + continue + + if email not in contributors: + contributors[email] = { + "gh_username": None, + "fullnames": [], + "commits": 0, + } + contributors[email]["gh_username"] = username + contributors[email]["fullnames"].append(fullname) + contributors[email]["commits"] += commits + + contributors = list(contributors.values()) + contributors.sort(key=lambda x: x["commits"], reverse=True) + return contributors if __name__ == "__main__": @@ -9,9 +54,23 @@ if __name__ == "__main__": parser.add_argument("--gh-link", type=str, required=True, help="Link to the GitHub repo") args = parser.parse_args() - notes_metadata = readMetadata(args.src_path, args.gh_link) - # Appends links to README + # Adds contributors to README + contributors = get_contributors(args.src_path) + with open(args.readme_path, "a") as readme_f: + readme_f.write(f"\n\n## Contributors\n") + readme_f.write(f"Special thanks to: ") + contributors_strs = [] + for i in range(len(contributors)): + if contributors[i]["gh_username"] is not None: + contributors_strs.append(f"[{contributors[i]['gh_username']}](https://github.com/{contributors[i]['gh_username']})") + elif len(contributors[i]["fullnames"]) > 0: + contributors_strs.append(f"{contributors[i]['fullnames'][-1]}") + readme_f.write(", ".join(contributors_strs)) + + + # Adds ToC to README + notes_metadata = readMetadata(args.src_path, args.gh_link) with open(args.readme_path, "a") as readme_f: readme_f.write(f"\n\n## Table of contents\n") for year in sorted(notes_metadata.keys()):