-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathset_labels.py
79 lines (69 loc) · 2.88 KB
/
set_labels.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
# Standard library
import logging
LOG = logging.root
def map_repo_to_labels(repo, final_labels, non_destructive=True):
"""
Map the given list of labels to GitHub. Any labels that do not already
exist on the repository will be created and if chosen to, any additional
lables on the repository will be removed.
@param repo: the repo on which the labels are being synced
@param final_labels: the list of labels that should be present on the repo
@param non_destructive: whether to trim extra labels or preserve them
"""
LOG.info("Fetching initial labels...")
initial_labels = {
label.name.casefold(): label for label in repo.get_labels()
}
LOG.success(f"done. Found {len(initial_labels)} labels.")
LOG.info("Parsing final labels...")
final_labels = {
label.qualified_name.casefold(): label for label in final_labels
}
LOG.success(f"done. Found {len(final_labels)} labels.")
if not non_destructive:
LOG.info("Syncing initial labels...")
LOG.change_indent(+1)
for initial_label_name, initial_label in initial_labels.items():
LOG.info(f"Syncing '{initial_label_name}'...")
LOG.change_indent(+1)
if initial_label_name not in final_labels:
LOG.info("Does not exist, deleting...")
initial_label.delete()
LOG.success("done.")
LOG.change_indent(-1)
LOG.success("done.")
LOG.change_indent(-1)
LOG.success("done.")
LOG.info("Syncing final labels...")
LOG.change_indent(+1)
for final_label_name, final_label in final_labels.items():
LOG.info(f"Syncing '{final_label_name}'...")
LOG.change_indent(+1)
if final_label_name not in initial_labels:
LOG.info("Did not exist, creating...")
repo.create_label(**final_label.api_arguments)
LOG.success("done.")
elif final_label != initial_labels[final_label_name]:
LOG.info("Differences found, updating...")
initial_label = initial_labels[final_label_name]
initial_label.edit(**final_label.api_arguments)
LOG.success("done.")
else:
LOG.info("Match found, moving on.")
LOG.change_indent(-1)
LOG.success("done.")
LOG.change_indent(-1)
LOG.success("done.")
def set_labels(repos, standard_labels, repo_specific_labels):
"""
Set labels on all repos for the organisation. This is the main entrypoint
of the module.
"""
for repo in list(repos):
LOG.info(f"Getting labels for repo '{repo.name}'...")
labels = standard_labels + repo_specific_labels.get(repo.name, [])
LOG.success(f"done. Found {len(labels)} labels.")
LOG.info(f"Syncing labels for repo '{repo.name}'...")
map_repo_to_labels(repo, labels)
LOG.success("done.")
__all__ = ["set_labels"]