-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgh-info
executable file
·128 lines (96 loc) · 3.2 KB
/
gh-info
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">3.11"
# dependencies = [
# "cachier",
# "pendulum",
# "PyGithub",
# "rich",
# "typer",
# ]
# ///
# Author: github.com/danielhoherd
# License: MIT
"""Show a table of info about Github users or orgs.
"export GITHUB_TOKEN=foo" to authenticate for higher API rate limit.
https://github.com/settings/tokens
"""
import datetime
from os import getenv
from sys import exit
import github
import pendulum
import typer
from cachier import cachier
from rich import box
from rich.console import Console
from rich.table import Table
console = Console()
TZ = pendulum.now().tz
gh = github.Github(login_or_token=getenv("GITHUB_TOKEN"))
gh.per_page = 100
app = typer.Typer(
help=__doc__,
no_args_is_help=True,
pretty_exceptions_enable=False,
)
def hash_func(args, kwargs):
return f"{args} {kwargs}"
@cachier(stale_after=datetime.timedelta(days=1), hash_func=hash_func)
def get_all_repos_for_org(org: github.Organization.Organization) -> list:
return sorted(org.get_repos(), key=lambda x: x.pushed_at)
@cachier(stale_after=datetime.timedelta(days=1))
def get_all_repos_for_user(user: github.NamedUser.NamedUser) -> list:
return sorted(user.get_repos(), key=lambda x: x.pushed_at)
@cachier(stale_after=datetime.timedelta(days=1))
def get_all_starred_repos_for_user(user: github.NamedUser.NamedUser) -> list:
return sorted(user.get_starred(), key=lambda x: x.pushed_at)
@cachier(stale_after=datetime.timedelta(days=7), hash_func=hash_func)
def get_license_for_repo(repo):
try:
license = repo.get_license()
return license.license.name
except github.GithubException:
pass
return ""
def print_repo_table(title: str, repos: list) -> bool:
table = Table(title=title, show_lines=True, style="grey19", box=box.MINIMAL)
table.add_column("Index", style="grey42")
table.add_column("Last push", style="grey42")
table.add_column("Name", style="light_goldenrod1")
table.add_column("URL", style="blue3")
table.add_column("Homepage", style="blue3")
table.add_column("Description", style="grey93")
table.add_column("License", style="light_goldenrod1")
for index, repo in enumerate(repos):
table.add_row(
str(index + 1),
pendulum.instance(repo.pushed_at, tz=TZ).strftime("%F"),
repo.name,
repo.html_url,
repo.homepage,
repo.description,
get_license_for_repo(repo),
)
console.print(table)
@app.command()
def get_org_repos(org: str):
gh_org = gh.get_organization(org)
repos = get_all_repos_for_org(gh_org)
print_repo_table(title=f"Repositories for {org}", repos=repos)
@app.command()
def get_user_repos(user: str):
gh_user = gh.get_user(user)
repos = get_all_repos_for_user(gh_user)
print_repo_table(title=f"Repositories for {user}", repos=repos)
@app.command()
def get_user_stars(user: str):
try:
gh_user = gh.get_user(user)
except github.GithubException as e:
print(e)
exit(1)
stars = get_all_starred_repos_for_user(gh_user)
print_repo_table(title=f"Starred repositories for {user}", repos=stars)
if __name__ == "__main__":
app()