forked from webcompat/webcompat.com
-
Notifications
You must be signed in to change notification settings - Fork 1
/
labels.py
74 lines (62 loc) · 2.61 KB
/
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import requests
import json
import sys
from webcompat import app
API_URI = 'https://api.github.com/repos/'
ISSUES_URI = app.config['ISSUES_REPO_URI'].rpartition('/issues')[0]
USER_LABELS_URI = API_URI + ISSUES_URI + "/labels"
WEBCOMPAT_LABELS_URI = API_URI + 'webcompat/webcompat-tests/labels'
AUTH_HEADERS = {'Authorization': 'token {0}'.format(app.config['OAUTH_TOKEN']),
'User-Agent': 'webcompat/webcompat-bot'}
def get_issue_labels(labels_uri):
'''Get the issue labels from the given repo.'''
response = requests.get(labels_uri)
if not response.status_code == 200:
response.raise_for_status()
return response.json()
def create_label(labels):
'''Create a label in the user repo'''
response = requests.post(
USER_LABELS_URI,
json.dumps(labels),
headers=AUTH_HEADERS)
if not response.status_code == 201:
response.raise_for_status()
def delete_label(name):
'''Delete a label in the user repo'''
delete_labels_uri = USER_LABELS_URI + "/" + name
response = requests.delete(delete_labels_uri, headers=AUTH_HEADERS)
if not response.status_code == 204:
response.raise_for_status()
def main():
'''Duplicate webcompat test repo labels in user test repo'''
# check if the user repo is a webcompat repo
if (ISSUES_URI == "webcompat/webcompat-tests" or
ISSUES_URI == "webcompat/webcompat.com"):
sys.exit("Error: Attempting to change a webcompat repo")
try:
# get all existing labels from the user issues repo
user_labels = get_issue_labels(USER_LABELS_URI)
# delete all existing labels from the user issues repo
for label in user_labels:
delete_label(label.get('name'))
# get all labels from the webcompat tests repo
webcompat_labels = get_issue_labels(WEBCOMPAT_LABELS_URI)
# re-create labels in user issues repo
for label in webcompat_labels:
data = {"name": label.get('name'), "color": label.get('color')}
create_label(data)
sys.exit(0)
except requests.exceptions.HTTPError as e:
# handles exceptions occuring due to undesired response status code
sys.exit("Error: {}".format(e))
except requests.exceptions.RequestException as e:
# handles all other exceptions
sys.exit("Error: {}".format(e))
if __name__ == '__main__':
main()