-
Notifications
You must be signed in to change notification settings - Fork 1
/
unify_labels.py
executable file
·90 lines (78 loc) · 3.31 KB
/
unify_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
#!/usr/bin/python3
# Python script to unify labels across repos in an org
# Inspired by github-labels.py from DXC DevOps Dojo
# https://github.com/dxc-technology/online-devops-dojo/blob/master/online-devops-dojo/welcome/assets/github-labels.py
# 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 https://mozilla.org/MPL/2.0/.
import os, sys, json, requests, yaml
# Color constants
# Reference: https://gist.github.com/chrisopedia/8754917
COLINFO="\033[0;35m"
COLRESET="\033[m"
COLUPDATE="\033[0;33m"
COLWARN="\033[0;31m"
baseurl = 'https://api.github.com'
headers = {"Content-Type": "application/json", "Accept": "application/vnd.github.v3+json"}
if len(sys.argv) != 4:
print(" Usage: " + sys.argv[0] + " github-labels.yaml myorg-repos.yaml org_name")
sys.exit(1)
labels_file = sys.argv[1]
repos_file = sys.argv[2]
user = sys.argv[3]
token = os.environ['GITHUB_API_TOKEN']
def push_labels(repo):
# read the labels
try:
labels = yaml.load_all(open(labels_file, 'r'), Loader=yaml.FullLoader)
except yaml.YAMLError as exc:
print(COLINFO + exc + COLRESET)
# Populate the labels
for label in labels:
payload = json.dumps({
"name" : label['name'],
"description": label['description'],
"color" : label['color']
})
there_already = requests.get(baseurl + "/repos/" + repo + "/labels/" + label['name'],
headers=headers,
auth=(user, token))
if there_already.status_code == 404:
response = requests.post(baseurl + "/repos/" + repo + "/labels",
data=payload,
headers=headers,
auth=(user, token))
if response.status_code != 201:
# An error occured
print(COLWARN + "Error adding label " + label['name'] + ": " + str(response.status_code) + " " + response.text + COLRESET)
else:
sys.stdout.write(COLINFO + "." + COLRESET)
sys.stdout.flush()
else:
# Check descriptions match
if there_already.json()['description'] == label['description']:
sys.stdout.write(".")
sys.stdout.flush()
else:
payload = json.dumps({
"new_name" : label['name'],
"description": label['description'],
"color" : label['color']
})
response = requests.patch(baseurl + "/repos/" + repo + "/labels/" + label['name'],
data=payload,
headers=headers,
auth=(user, token))
sys.stdout.write(COLUPDATE + "." + COLRESET)
sys.stdout.flush()
if response.status_code != 200:
print(COLWARN + "Error updating label " + label['name'] + ": " + str(response.status_code) + " " + response.text + COLRESET)
print(COLRESET)
try:
repos = yaml.load_all(open(repos_file, 'r'), Loader=yaml.FullLoader)
except yaml.YAMLError as exc:
print(COLWARN + exc + COLRESET)
for repo in repos:
full_repo_name = user + "/" + repo['name']
print(f'Applying labels to {full_repo_name}')
push_labels(full_repo_name)