-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_update_member.py
115 lines (85 loc) · 3.1 KB
/
add_update_member.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
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
import os
from pathlib import Path
from ruamel.yaml import YAML
from . import save_url_image, parse_issue_body, remove_keys, remove_items_with_values
def format_site_label(name):
if name == "github":
return "GitHub"
elif name in ["twitter", "scholar", "website"]:
return name.title()
else:
return name
def format_parsed_content(parsed):
"""
Format the parsed content into a string.
"""
parsed["alumni"] = parsed["status"] == "Alumni"
parsed["links"] = [
{"label": format_site_label(key), "url": parsed[key]}
for key in ["website", "twitter", "github", "scholar"]
if parsed[key] != "_No response_"
]
parsed = remove_keys(
parsed,
keys_to_remove=["status", "website", "twitter", "github", "scholar", "action"],
)
parsed = remove_items_with_values(parsed, "_No response_")
return parsed
def merge_links(old_links, new_links):
new_labels = {link["label"] for link in new_links}
out_links = [link for link in old_links if link["label"] not in new_labels]
out_links.extend(new_links)
return out_links
def sort_by_lastname(authors):
lastname_to_user = {
desc["name"].split()[-1] + user: user for user, desc in authors.items()
}
for key in sorted(lastname_to_user, reverse=True):
user = lastname_to_user[key]
desc = authors.pop(user)
authors.insert(0, user, desc)
def main(parsed, site_data_dir="_data/", image_dir="assets/images/bio"):
site_data_dir = Path(site_data_dir)
profile = format_parsed_content(parsed)
yaml = YAML()
yaml.preserve_quotes = True
with open(site_data_dir / "authors.yml") as f:
authors = yaml.load(f)
name_to_username = {authors[username]["name"]: username for username in authors}
if parsed["action"] == "Add member":
if profile["name"] not in authors:
username = profile["name"]
else:
n = 2
while (k := f'{profile["name"]} {n}') in authors:
n += 1
username = k
authors[username] = profile
else:
if profile["name"] not in name_to_username:
raise ValueError(f'{profile["name"]} not in authors')
username = name_to_username[profile["name"]]
profile["links"] = merge_links(
authors[username].get("links", []), profile.get("links", [])
)
authors[username].update(profile)
img_path = save_url_image(
fname=username,
profile=authors[username],
key="avatar",
image_dir=image_dir,
crop_center=True,
size=(300, 300),
)
if img_path is not None:
# This could either mean that a new image was saved or that the image was
# updated. In either case, we need to update the path to the image.
authors[username]["avatar"] = img_path
sort_by_lastname(authors)
with open(site_data_dir / "authors.yml", "w") as f:
yaml.dump(authors, f)
return authors
if __name__ == "__main__":
issue_body = os.environ["ISSUE_BODY"]
parsed = parse_issue_body(issue_body)
main(parsed)