-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathget_labels.py
128 lines (108 loc) · 4.12 KB
/
get_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
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
# Standard library
import logging
from pathlib import Path
# Third-party
import yaml
# First-party/Local
from ccos.norm.models import Group, Label
LOG = logging.root
def get_required_label_groups():
"""
Get the list of all the label_groups, at least one label of which is
required to be present on every triaged issue.
@return: the filtered list of label groups that that are required by
definition
"""
labels_dict = load_yaml_from_file("labels")
label_groups = []
for group_info in labels_dict["groups"]:
group = Group(**group_info)
label_names = group_info.pop("labels", [])
label_groups.append(group)
for label_info in label_names:
Label(**label_info, group=group)
LOG.info(f"Filtering {len(label_groups)} label_groups...")
required_label_groups = [
group for group in label_groups if group.is_required
]
LOG.success(f"done. Required {len(required_label_groups)} label groups.")
return required_label_groups
def get_standard_labels():
"""
Get the list of standard labels that apply to every repository.
@return: the list of standard labels
"""
labels_dict = load_yaml_from_file("labels")
standard_labels = []
for group_info in labels_dict["groups"]:
label_names = group_info.pop("labels", [])
group = Group(**group_info)
for label_info in label_names:
label = Label(**label_info, group=group)
standard_labels.append(label)
for label_info in labels_dict["standalone"]:
label = Label(**label_info)
standard_labels.append(label)
return standard_labels
def get_repo_specific_labels():
"""
Get the dict mapping each repository to a list of skill labels. For each
repository, skill levels will be added on top of the standard labels.
@return: the dict mapping repo names to skill labels
"""
skill_group = Group(color="5ff1f5", name="skill")
labels_dict = load_yaml_from_file("skills")
repo_specific_labels = {}
for repo_name, skill_names in labels_dict.items():
skill_labels = [
get_skill_label_from_name(skill_group, skill_name)
for skill_name in skill_names
]
repo_specific_labels[repo_name] = skill_labels
return repo_specific_labels
def get_skill_label_from_name(skill_group, skill_name):
"""
Generate the skill label purely from the name of the skill. The name of the
skill is plugged into the description and the lower-cased version is used
as the label name.
@param skill_group: the logical parent group of all skill labels
@param skill_name: the name of the skill to convert into a label
@return: an instance of Label derived from the skill name
"""
return Label(
name=skill_name.lower(),
description=f"Requires proficiency in '{skill_name}'",
emoji="💪",
group=skill_group,
)
def load_yaml_from_file(file_name):
"""
Load the YAML file into a Python list or dict. The extension '.yml' is
appended to the file name and only the current directory is scanned for the
matching file.
@param file_name: the name of the file to load
@return: the contents of the YAML file as a Python object
"""
file_path = get_datafile_path(file_name)
with open(file_path, "r") as file:
data = yaml.safe_load(file)
return data
def get_datafile_path(file_name):
"""
Get the path to the datafile by searching the current directory for a file
with the given name and the '.yml' extension.
@param file_name: the name of the file whose path is required
@return: the path to the file
"""
current_file = Path(__file__).resolve()
data_file = current_file.parent.joinpath(f"{file_name}.yml")
return data_file
def get_labels():
"""
Get the list of standard and repository-specific labels.
@return: the list of standard and repository-specific labels
"""
standard_labels = get_standard_labels()
repo_specific_labels = get_repo_specific_labels()
return standard_labels, repo_specific_labels
__all__ = ["get_labels", "get_required_label_groups"]