-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate_repository_labels.py
90 lines (81 loc) · 3.7 KB
/
update_repository_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
import os
import json
import logging
from ghorgs.managers import GithubOrganizationManager
logging.basicConfig(format='%(asctime) [%(level)s]: %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def update_repository_labels(organization, token, repositories, labels_definition_filepath, delete=False):
"""
Update a repository's labels based on a given definition file.
NOTE:
if delete == True, undefined labels will be removed/deleted
:param organization:
:param token:
:param repositories:
:param labels_definition_filepath:
:param delete:
:return: created_labels, deleted_labels
"""
created_labels = []
deleted_labels = []
assert os.path.exists(labels_definition_filepath)
manager = GithubOrganizationManager(organization,
token)
# load file
label_definitions = None
with open(labels_definition_filepath, 'r', encoding='utf8') as labels_json:
label_definitions = json.load(labels_json)
assert label_definitions
defined_label_names = [label['name'] for label in label_definitions]
repositories = tuple(repositories)
for repository in manager.repositories(names=repositories):
existing_label_names = [label['name']for label in repository.labels]
for label_definition in label_definitions:
repository.create_label(label_definition['name'],
label_definition['description'],
label_definition['color'])
created_labels.append(label_definition)
if delete:
undefined_label_names = set(existing_label_names) - set(defined_label_names)
for label_name in undefined_label_names:
repository.delete_label(label_name)
deleted_labels.append(label_name)
return created_labels, deleted_labels
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-o', '--organization',
default='abeja-inc',
help='Github Organization Name')
parser.add_argument('-t', '--token',
default=os.environ.get('GITHUB_OAUTH_TOKEN', None),
help='GITHUB OAUTH Token')
parser.add_argument('-r', '--repositories',
default=None,
nargs='+',
help='Repository Name(s) to apply labels to [DEFAULT=None]')
parser.add_argument('-l', '--labels-filepath',
dest='labels_filepath',
required=True,
default=None,
help='Specify the file that contains the labels to define for the given repositories.')
parser.add_argument('--delete',
action='store_true',
default=False,
help='DELETE existing labels not specified in given definitions file',
)
parser.add_argument('--verbose',
action='store_true',
default=False,
help='If given, DEBUG info will be displayed')
args = parser.parse_args()
if args.verbose:
logger.setLevel(logging.DEBUG)
created, deleted = update_repository_labels(args.organization,
args.token,
args.repositories,
args.labels_filepath,
args.delete)
print('Created: {}'.format(created))
print('Deleted: {}'.format(deleted))