Last active
April 12, 2022 13:59
-
-
Save darth-veitcher/ee3ea71e5b9d66d721af1d9784f9da7a to your computer and use it in GitHub Desktop.
setup github labels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# source: https://docs.github.com/en/rest/reference/issues#create-a-label | |
import os | |
import sys | |
import requests | |
TOKEN = os.environ.get("GITHUB_API_TOKEN") | |
if not TOKEN: | |
print("GITHUB_API_TOKEN not found. Please set as an environment variable.") | |
sys.exit(1) | |
REPO_URL = os.environ.get("REPO_URL") | |
if not REPO_URL: | |
print("REPO_URL not found. Please set as an environment variable.") | |
sys.exit(1) | |
REPO_URL = REPO_URL.replace('https://github.com/', 'https://api.github.com/repos/') | |
STATUS = [ | |
{'name': 'Abandoned', 'rgb': (255,255,255)}, | |
{'name': 'Accepted', 'rgb': (85,150,35)}, | |
{'name': 'Available', 'rgb': (203,229,191)}, | |
{'name': 'Blocked', 'rgb': (195,42,45)}, | |
{'name': 'Completed', 'rgb': (61,107,116)}, | |
{'name': 'In Progress', 'rgb': (203,203,203)}, | |
{'name': 'On Hold', 'rgb': (193,35,38)}, | |
{'name': 'Pending', 'rgb': (251,242,193)}, | |
{'name': 'Review Needed', 'rgb': (238,201,48)}, | |
{'name': 'Revision Needed', 'rgb': (193,35,38)}, | |
] | |
TYPE = [ | |
{'name': 'Bug', 'rgb': (193,35,38)}, | |
{'name': 'Enhancement', 'rgb': (148,181,232)}, | |
{'name': 'Maintenance', 'rgb': (238,201,48)}, | |
{'name': 'Question', 'rgb': (176,52,121)}, | |
{'name': 'Documentation', 'rgb': (203,203,203)}, | |
] | |
PRIORITY = [ | |
{'name': 'Critical', 'rgb': (193,35,38)}, | |
{'name': 'High', 'rgb': (207,100,42)}, | |
{'name': 'Medium', 'rgb': (85,150,35)}, | |
{'name': 'Low', 'rgb': (203,203,203)}, | |
] | |
categories = { | |
'Status': STATUS, | |
'Type': TYPE, | |
'Priority': PRIORITY | |
} | |
def rgb_to_hex(rgb): | |
return '%02x%02x%02x' % rgb | |
def create_label(name: str, colour_hex: str): | |
payload = { | |
'name': name, | |
'description': None, | |
'color': colour_hex | |
} | |
res = requests.post( | |
f'{REPO_URL}/labels', | |
headers={ | |
"Content-Type": "application/json", | |
"Authorization": f"Bearer {TOKEN}" | |
}, | |
json=payload | |
) | |
if res.status_code != 200: | |
print(f"Error: {res.status_code} | {res.text}") | |
def create_all_labels(): | |
for category in categories: | |
for label in categories[category]: | |
colour_hex = rgb_to_hex(label['rgb']) | |
create_label( | |
f'{category}: {label["name"]}', | |
colour_hex) | |
create_all_labels() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment