Last active
May 12, 2017 21:54
-
-
Save bilelmoussaoui/a9b9084816a9c21f3a5676fd615b5d6a to your computer and use it in GitHub Desktop.
Manage your labels
This file contains 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
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