-
Notifications
You must be signed in to change notification settings - Fork 1
/
github.py
64 lines (54 loc) · 1.76 KB
/
github.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
import json
import requests
import yaml
token = raw_input('Paste your GitHub token? (see https://github.com/settings/tokens): ')
repo = raw_input('Which repo do you want to setup? (e.g. Wiredcraft/test): ')
with open('labels.yml', 'r') as f:
default_labels = yaml.load(f)
url = 'https://api.github.com/repos/' + repo + '/labels'
headers = {'Authorization': 'token %s' % token}
def get_labels():
try:
response = requests.get(url, headers=headers)
response.raise_for_status() # optional but good practice in case the call fails!
return [label['name'] for label in response.json()]
except Exception as e:
return e
def set_labels(labels):
for label in default_labels:
try:
# Label was in the list, we update GH
labels.remove(label['name'])
update_label(label)
except ValueError:
# Label wasn't in the array, we add it to GH
create_label(label)
# We now remove the labels left
for label in labels:
delete_label(label)
def update_label(label):
print 'Update label (%s) on GH' % label['name']
try:
response = requests.patch(url + '/' + label['name'], data=json.dumps(label), headers=headers)
response.raise_for_status()
return response.json()
except Exception as e:
return e
def create_label(label):
print 'Create label (%s) on GH' % label['name']
try:
response = requests.post(url, data=json.dumps(label), headers=headers)
response.raise_for_status()
return response.json()
except Exception as e:
return e
def delete_label(label):
print 'Delete label (%s) from GH' % label
try:
response = requests.delete(url + '/' + label, headers=headers)
response.raise_for_status()
return response.json()
except Exception as e:
return e
labels = get_labels()
set_labels(labels)