import os import json import requests import time from github import Github, GithubIntegration import gradio as gr app_id = '307916' owner='duerrsimon' repo_name='bioicons-test' # with open('bot_key.pem','r') as cert_file: # app_key = cert_file.read() app_key = os.environ.get('GITHUB_TOKEN') git_integration = GithubIntegration( app_id, app_key, ) api = Github( login_or_token=git_integration.get_access_token( git_integration.get_repo_installation(owner, repo_name).id ).token ) #api = Github(token) site = api.get_repo('duerrsimon/bioicons-test') licenses = ['mit', 'cc-by-3.0', 'cc-0', 'cc-by-4.0', 'bsd', 'cc-by-sa-3.0', 'cc-by-sa-4.0'] categories = ["All_icons", "Machine_Learning", "Genetics", "Chemistry", "Scientific_graphs", "Cell_membrane", "Microbiology", "Oncology", "Molecular_modelling", "Intracellular_components", "Cell_types", "Cell_lines", "Plants_Algae", "Chemo-_and_Bioinformatics", "Cell_culture", "Tissues", "Computer_hardware", "Nucleic_acids", "General_items", "Safety_symbols", "Receptors_channels", "Human_physiology", "Animals", "Lab_apparatus", "Blood_Immunology", "Viruses", "Parasites"] def create_branch(branch_name): return site.create_git_ref( 'refs/heads/{branch_name}'.format(**locals()), site.get_branch('main').commit.sha ) def create_pull_request(branch_name, github_user): if github_user!="": message = f"submitted by: @{github_user}." else: message ="No github account provided." return site.create_pull( title="New icons submitted via web", body= message, base="main", head=branch_name ) def remove_special_chars(author): author = author.replace(" ", "-") author = author.replace(".", "-") author = author.replace("(", "-") author = author.replace(")", "-") author = author.replace(":", "-") author = author.replace(";", "-") author = author.replace(",", "-") author = author.replace("!", "-") author = author.replace("?", "-") author = author.replace("=", "-") author = author.replace("+", "-") author = author.replace("/", "-") return author def process_and_create_pull(icon_files,category, new_cateogry_check, new_category, license, creator_name, creator_url, copyright, github_user): if copyright: if new_cateogry_check: category = remove_special_chars(new_category) hash = '{0:010x}'.format(int(time.time() * 256))[:8] branch_name = f"new-icons-{hash}" create_branch(branch_name) authors = requests.get("https://bioicons.com/icons/authors.json").json() #remove whitespace and other special characters author = remove_special_chars(creator_name) if author not in authors.keys(): index_file = site.get_contents('static/icons/authors.json') authors[author]=creator_url updated_content = json.dumps(authors, indent=4) site.update_file( path='static/icons/authors.json', message='Insert author', content=updated_content, sha=index_file.sha, branch=branch_name ) for file in icon_files: #get file content from filesystem with open(file.name, 'r') as fp: file_content = fp.read() site.create_file( path=f'static/icons/{license}/{category}/{author}/{os.path.basename(file.name)}', message=f'create {os.path.basename(file.name)}', content=file_content, branch=branch_name ) pull = create_pull_request(branch_name, github_user) return f"""