-
Notifications
You must be signed in to change notification settings - Fork 24
/
team_query.py
83 lines (69 loc) · 1.79 KB
/
team_query.py
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
import os
import sys
import argparse
import string
import requests
team_query = string.Template(
"""
query {
organization(login: "$org") {
team(slug: "$team") {
name,
members(first: 100, orderBy: {field: LOGIN, direction: ASC}) {
nodes {
login
name
url
avatarUrl
}
}
}
}
}
"""
)
def api(query):
request = requests.post(
"https://api.github.com/graphql",
json={"query": query},
headers={"Authorization": f"bearer {token}"},
)
if request.status_code == 200:
return request.json()
else:
raise RuntimeError(f"Request received HTTP {request.status_code}: {query}")
parser = argparse.ArgumentParser(description="Generate team gallery from GitHub")
parser.add_argument("--org", required=True, help="GitHub organization name")
parser.add_argument("--team", required=True, help="Team name in the organization")
args = parser.parse_args()
org = args.org
team = args.team
token = os.environ.get("GH_TOKEN", None)
if token is None:
print(
"No token found. Please export a GH_TOKEN with permissions "
"to read team members.",
file=sys.stderr,
)
sys.exit(-1)
resp = api(team_query.substitute(org=org, team=team))
members = resp["data"]["organization"]["team"]["members"]["nodes"]
team_name = resp["data"]["organization"]["team"]["name"]
member_template = string.Template(
"""\
[[item]]
type = 'card'
classcard = 'text-center'
body = '''{{< image >}}
src = '${avatarUrl}"'
alt = 'Avatar of ${name}'
{{< /image >}}
${name}'''
link = '${url}'
"""
)
members_list = []
for m in members:
m["name"] = m["name"] or m["login"]
members_list.append(member_template.substitute(**m))
print("\n".join(members_list).rstrip())