From 8ec170d063cff4ba079f6bf84d83e02a1bc69966 Mon Sep 17 00:00:00 2001 From: mjh316 <61671361+mjh316@users.noreply.github.com> Date: Thu, 30 Dec 2021 20:12:21 -0800 Subject: [PATCH 1/8] footer keeps track of main contributor --- source/_ext/gitstamp.py | 51 ++++++++++++++++++++------- source/_templates/include/footer.html | 28 +++++++++++---- 2 files changed, 60 insertions(+), 19 deletions(-) diff --git a/source/_ext/gitstamp.py b/source/_ext/gitstamp.py index ea13f6b65..b7fb75512 100644 --- a/source/_ext/gitstamp.py +++ b/source/_ext/gitstamp.py @@ -28,6 +28,7 @@ class GHCachedUser: def __init__(self, name): self.name = name self.login = None + self.commits = None self.avatar_url = "/_static/images/user-placeholder.png" def page_context_handler(app, pagename, templatename, context, doctree): @@ -44,6 +45,11 @@ def page_context_handler(app, pagename, templatename, context, doctree): try: commits = g.iter_commits('--all', max_count=1, paths="%s.rst" % fullpagename) + # Splits on newline to get the first (highest commits) contributor, then handles whitespace, then gets info + most_commits, _most_user = g.git.shortlog('-sne', '--', ("%s.rst" % fullpagename)).split('\n')[0].strip().split('\t') + # Parses all of 'first_name last_name ', 'first_name middle_name last_name ', and 'name ' correctly + most_user_name, most_user_email = ' '.join(_most_user.split(' ')[:-1]), _most_user.split(' ')[-1][1:-1] + if not commits: # Don't datestamp generated rst's (e.g. imapd.conf.rst) # Ideally want to check their source - lib/imapoptions, etc, but @@ -52,32 +58,51 @@ def page_context_handler(app, pagename, templatename, context, doctree): commit = next(iter(commits)) context['gitstamp'] = datetime.datetime.fromtimestamp(commit.authored_date).strftime("%Y‑%m‑%d") + #raise Exception(f'{commit.author.email} \n {most_user_name, most_user_email}') - user = GHCachedUser(commit.author.name) + last_user = GHCachedUser(commit.author.name) + main_user = GHCachedUser(most_user_name) + main_user.commits = most_commits if 'gh' in globals(): # Look in cache first to avoid spamming GitHub if commit.author.email in gh_user_cache: - user = gh_user_cache[commit.author.email] + last_user = gh_user_cache[commit.author.email] else: # Search GitHub and retrieve first user with matching email (if any) gh_users = gh.search_users(commit.author.email) if gh_users.totalCount: gh_user = next(iter(gh_users)) - user.name = gh_user.name - user.login = gh_user.login - user.avatar_url = gh_user.avatar_url + last_user.name = gh_user.name + last_user.login = gh_user.login + last_user.avatar_url = gh_user.avatar_url else: # Try searching the commit hash instead gh_commit = gh_repo.get_commit(commit.hexsha) if gh_commit: - user.name = gh_commit.author.name - user.login = gh_commit.author.login - user.avatar_url = gh_commit.author.avatar_url - - gh_user_cache[commit.author.email] = user - context['gitauthor'] = user.name - context['gitlogin'] = user.login - context['gitavatar'] = user.avatar_url + last_user.name = gh_commit.author.name + last_user.login = gh_commit.author.login + last_user.avatar_url = gh_commit.author.avatar_url + + gh_user_cache[commit.author.email] = last_user + # Repeat above for most user + if most_user_email in gh_user_cache: + main_user = gh_user_cache[most_user_email] + else: + gh_users = gh.search_users(most_user_email) + if gh_users.totalCount: + gh_user = next(iter(gh_users)) + main_user.name = gh_user.name + main_user.login = gh_user.login + main_user.avatar_url = gh_user.avatar_url + + context['gitLastAuthor'] = last_user.name + context['gitLastLogin'] = last_user.login + context['gitLastAvatar'] = last_user.avatar_url + context['gitMainAuthor'] = main_user.name + context['gitMainLogin'] = main_user.login + context['gitMainAvatar'] = main_user.avatar_url + context['gitMainCommits'] = main_user.commits + except git.exc.GitCommandError: # File doesn't exist or something else went wrong. diff --git a/source/_templates/include/footer.html b/source/_templates/include/footer.html index ae9db03d4..aa37365ba 100644 --- a/source/_templates/include/footer.html +++ b/source/_templates/include/footer.html @@ -1,14 +1,24 @@