Skip to content

Instantly share code, notes, and snippets.

@lewisgoddard
Forked from bilelmoussaoui/github.py
Created May 12, 2017 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lewisgoddard/a2eb09a1964e4a4cc80219519fc97bd5 to your computer and use it in GitHub Desktop.
Save lewisgoddard/a2eb09a1964e4a4cc80219519fc97bd5 to your computer and use it in GitHub Desktop.
Manage your labels
import requests
from urllib.parse import urlencode
import logging
ORGANIZATION = "elementary"
logging.basicConfig(format='%(asctime)-15s %(message)s')
logger = logging.getLogger('github')
logger.setLevel(logging.INFO)
def get_all_label(repository):
labels = []
r = requests.get("https://api.github.com/repos/{0}/issues".format(repository))
logger.info("Getting all labels from {0}".format(repository))
if r.status_code == 200:
i = 0
data = r.json()
while i < len(data):
labels.extend(data[i]["labels"])
i += 1
else:
logging.warning("No repository was found")
logging.error(r.json()["message"])
return labels
def get_all_repos(organization):
orgs = []
pages = 5 # Number of pages on your org pages on github
page = 1
logger.info("Getting list of repositroies of {0}".format(organization))
while page <= pages:
uri_data = {"page" : page}
org_uri = "https://api.github.com/orgs/{0}/repos?".format(organization)
r = requests.get(org_uri + urlencode(uri_data))
if r.status_code == 200:
data = r.json()
i = 0
while i < len(data):
full_name = data[i]["full_name"]
if full_name not in orgs:
orgs.append(full_name)
i += 1
page += 1
return orgs
def handle_labels(labels_by_repo):
labels = {}
for repo in labels_by_repo:
repo_labels = labels_by_repo[repo]
for label in repo_labels:
name = label["name"]
color = label["color"].lower()
if labels.get(name):
if color != labels[name].lower():
print("Current label \"{0}\" has already a predefined color".format(name))
print("1 ) - #" + labels[name])
print("2 ) - #" + color)
has_chosen = False
while not has_chosen:
try:
selected_color = int(input("Please select the new one"))
except ValueError:
selected_color = 0
if selected_color in [1, 2]:
has_chosen = True
if selected_color == 2:
labels[name] = color
else:
labels[name] = color
return labels
repos = get_all_repos(ORGANIZATION)
labels_by_repo = {}
for repo in repos:
labels_by_repo[repo] = get_all_label(repo)
new_labels = handle_labels(labels_by_repo)
for label in new_labels:
print(label["name"] + " - #" + label["color"])
print(new_labels)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment