Skip to content

Commit

Permalink
feat: auto fetch github username
Browse files Browse the repository at this point in the history
  • Loading branch information
jnoortheen committed Mar 17, 2024
1 parent 3176d5c commit 7119c96
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion copier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ _jinja_extensions:
### question ###

full_name:
default: "{{ git_user_name }}"
type: str
help: Enter your full name. This will appear in the License generated and `pyproject.toml`'s authors field.

Expand All @@ -22,7 +23,7 @@ email:
help: Your E-mail address. This will appear in the License generated and `pyproject.toml`'s authors field.

github_username:
default: "{{ git_user_name }}"
default: "{{ github_username }}"
type: str
help: You Github username or organization name under which the projects lives.

Expand Down
19 changes: 17 additions & 2 deletions extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Thanks to the original author

import re
import shutil
import subprocess
import unicodedata
from datetime import date
Expand All @@ -17,6 +18,19 @@ def git_user_email() -> str:
return subprocess.getoutput("git config user.email").strip()


def github_username() -> str:
# run if gh is installed
if not shutil.which("gh"):
return ""
import json

data = subprocess.getoutput("gh api user").strip()
try:
return json.loads(data)["login"]
except json.JSONDecodeError:
return ""


def slugify(value, separator="-"):
value = (
unicodedata.normalize("NFKD", str(value))
Expand All @@ -30,8 +44,9 @@ def slugify(value, separator="-"):
class GitExtension(Extension):
def __init__(self, environment):
super().__init__(environment)
environment.globals["git_user_name"] = git_user_name
environment.globals["git_user_email"] = git_user_email
environment.globals["git_user_name"] = git_user_name()
environment.globals["git_user_email"] = git_user_email()
environment.globals["github_username"] = github_username()


class SlugifyExtension(Extension):
Expand Down

0 comments on commit 7119c96

Please sign in to comment.