-
Notifications
You must be signed in to change notification settings - Fork 1
/
dump_github_labels.py
executable file
·50 lines (41 loc) · 1.68 KB
/
dump_github_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
#!/usr/bin/python3
# Python helper to dump labels from a Github repo
# 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"
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 user/org_name repo_name")
sys.exit(1)
labels_file = sys.argv[1]
user = sys.argv[2]
token = os.environ['GITHUB_API_TOKEN']
repo = user + "/" + sys.argv[3]
def dump_all_labels(repo):
# Retrieve all labels in a repo
response = requests.get(baseurl + "/repos/" + repo + "/labels",
params={'per_page' : 100},
headers=headers,
auth=(user, token))
if response.status_code != 200:
# An error occured
print(COLINFO + "Error getting labels : " + str(response.status_code) + " " + response.text
+ COLRESET)
# Convert labels to YAML
json_labels = json.loads(response.text)
f = open(labels_file, 'w')
for label in json_labels:
f.write ('---\n')
f.write (f'name: "{label["name"]}"\n')
f.write (f'description: "{label["description"]}"\n')
f.write (f'color: "{label["color"]}"\n')
f.close
dump_all_labels(repo)