diff --git a/backend/apimanager/publish_methods_github.py b/backend/apimanager/publish_methods_github.py index c4f576a..9e41719 100644 --- a/backend/apimanager/publish_methods_github.py +++ b/backend/apimanager/publish_methods_github.py @@ -5,13 +5,15 @@ import subprocess import urllib.parse from .utilities import ( - copy_content + copy_content, + run_git_command ) from .dialogue_box import ( draw_dialogue_box ) + def github_init(deploy_location, git_commands): user_details_defined = git_check_user_details(deploy_location, git_commands) if not user_details_defined: @@ -20,13 +22,12 @@ def github_init(deploy_location, git_commands): username, password = git_get_username_password() remote_url = f'https://{username}:{password}@github.com/{username}/{username}.github.io.git' - subprocess.run(git_commands["git_init"], cwd=deploy_location, check=True, text=True, capture_output=True) - subprocess.run(git_commands["git_add"], cwd=deploy_location, check=True, text=True, capture_output=True) - subprocess.run(git_commands["git_commit"], cwd=deploy_location, check=True, text=True, capture_output=True) - subprocess.run(git_commands["git_branch"], cwd=deploy_location, check=True, text=True, capture_output=True) - subprocess.run((git_commands["git_add_url"]).append(remote_url), cwd=deploy_location, check=True, text=True, - capture_output=True) - subprocess.run(git_commands["git_push"], cwd=deploy_location, check=True, text=True, capture_output=True) + run_git_command('git_init', deploy_location) + run_git_command('git_add', deploy_location) + run_git_command('git_commit', deploy_location) + run_git_command('git_branch', deploy_location) + run_git_command('git_add_url', deploy_location, [remote_url]) + run_git_command('git_push', deploy_location) def git_set_user_details(deploy_location, git_commands): @@ -40,10 +41,9 @@ def git_set_user_details(deploy_location, git_commands): ) if input_confirmation: break - subprocess.run(git_commands["git_config_email"] + [email], cwd=deploy_location, check=True, text=True, - capture_output=True) - subprocess.run(git_commands["git_config_name"] + [name], cwd=deploy_location, check=True, - text=True, capture_output=True) + + run_git_command('git_config_email', deploy_location, [email]) + run_git_command('git_config_name', deploy_location, [name]) def git_existing_repo_setup(deploy_location, git_commands): @@ -61,18 +61,16 @@ def git_existing_repo_setup(deploy_location, git_commands): repo_url = repo_url + '.git' dist_folder_name = ((repo_url.split('/')).pop()).removesuffix('.git') - subprocess.run(git_commands["git_clone"] + [repo_url], cwd=settings.DEPLOY_CONFIG["DEPLOY_LOCATION"], check=True, - text=True, capture_output=True) + run_git_command('git_clone', settings.DEPLOY_CONFIG["DEPLOY_LOCATION"], [repo_url]) git_update_viewable_ui(deploy_location, dist_folder_name) def git_check_user_details(deploy_location, git_commands): - try: - subprocess.run(git_commands["git_config_name"], cwd=deploy_location, check=True, text=True, capture_output=True) - subprocess.run(git_commands["git_config_email"], cwd=deploy_location, check=True, text=True, capture_output=True) - return True - except: + config_email = run_git_command('git_config_name', deploy_location) + config_name = run_git_command('git_config_email', deploy_location) + if not config_email or not config_name: return False + return True def git_update_viewable_ui(deploy_location, dist_folder_name, build_frontend=False): @@ -137,22 +135,33 @@ def github_pages_deploy(deploy_location, git_commands): user_details_defined = git_check_user_details(deploy_location, git_commands) if not user_details_defined: git_set_user_details(deploy_location, git_commands) - subprocess.run(git_commands["git_pull"], cwd=deploy_location, check=True, text=True, capture_output=True) - print("completed git pull") - origin_url_subprocess = subprocess.run(git_commands["git_get_origin_url"], cwd=deploy_location, check=True, - text=True, capture_output=True) - origin_url = origin_url_subprocess.stdout.strip() - print("Got origin as "+str(origin_url)) - parsed_url = urllib.parse.urlparse(origin_url) - print(parsed_url) - if not '@' in parsed_url.netloc: - username = draw_dialogue_box('Github Deploy', 'Enter your username', 'textbox') - password = draw_dialogue_box('Github Deploy', 'Enter your github token', 'password') - netloc = f"{username}:{password}@{parsed_url.hostname}" - new_url = urllib.parse.urlunparse(parsed_url._replace(netloc=netloc)) - subprocess.run(git_commands["git_set_origin_url"] + [new_url], cwd=deploy_location, check=True, text=True, - capture_output=True) - print("Origin URL changed") - subprocess.run(git_commands["git_add"], cwd=deploy_location, check=True, text=True, capture_output=True) - subprocess.run(git_commands["git_commit"], cwd=deploy_location, check=True, text=True, capture_output=True) - subprocess.run(git_commands["git_push"], cwd=deploy_location, check=True, text=True, capture_output=True) + + user_email = run_git_command("git_config_email", deploy_location) + user_name = run_git_command("git_config_name", deploy_location) + repo_name = run_git_command("git_get_origin_url", deploy_location).split("/").pop() + + deploy_confirmation = draw_dialogue_box( + 'Github Deploy', + f'Deploying as {user_name} with email {user_email} to {repo_name}', + 'confirmation' + ) + + if deploy_confirmation: + run_git_command('git_pull', deploy_location) + print("completed git pull") + origin_url = run_git_command('git_get_origin_url', deploy_location) + print("Got origin as "+str(origin_url)) + parsed_url = urllib.parse.urlparse(origin_url) + print(parsed_url) + if not '@' in parsed_url.netloc: + username = draw_dialogue_box('Github Deploy', 'Enter your username', 'textbox') + password = draw_dialogue_box('Github Deploy', 'Enter your github token', 'password') + netloc = f"{username}:{password}@{parsed_url.hostname}" + new_url = urllib.parse.urlunparse(parsed_url._replace(netloc=netloc)) + run_git_command('git_set_origin_url', deploy_location, [new_url]) + print("Origin URL changed") + run_git_command('git_add', deploy_location) + run_git_command('git_commit', deploy_location) + run_git_command('git_push', deploy_location) + else: + raise subprocess.CalledProcessError(1, 'failed', 'Deployment Cancelled') diff --git a/backend/apimanager/utilities.py b/backend/apimanager/utilities.py index 99ea8fb..8c11e6b 100644 --- a/backend/apimanager/utilities.py +++ b/backend/apimanager/utilities.py @@ -1,5 +1,6 @@ import os import shutil +import subprocess def copy_content(source, destination, content_type, copy_type='merge'): @@ -17,4 +18,30 @@ def copy_content(source, destination, content_type, copy_type='merge'): shutil.copy(source, destination) print(f'{content_type} copied successfully from {source} to {destination}') except Exception as e: - print(f'Error occurred: {e}') \ No newline at end of file + print(f'Error occurred: {e}') + + +def run_git_command(operation, deploy_location, parameter=None): + if not parameter: + parameter=[] + git_commands = { + "git_init": ['git', 'init'], + "git_add": ['git', 'add', '.'], + "git_pull": ['git', 'pull'], + "git_config_email": ['git', 'config', '--local', 'user.email'], + "git_config_name": ['git', 'config', '--local', 'user.name'], + "git_commit": ['git', 'commit', '-m', 'Update website'], + "git_branch": ['git', 'branch', '-m', 'main'], + "git_get_origin_url": ['git', 'remote', 'get-url', 'origin'], + "git_set_origin_url": ['git', 'remote', 'set-url', 'origin'], + "git_add_url": ['git', 'remote', 'add', 'origin'], + "git_push": ['git', 'push', '-u', 'origin', 'main'], + "git_clone": ['git', 'clone'] + } + try: + subprocess_operation = subprocess.run(git_commands[operation] + parameter, cwd=deploy_location, check=True, text=True, capture_output=True) + subprocess_output = subprocess_operation.stdout.strip() + except subprocess.CalledProcessError as e: + subprocess_output = None + + return subprocess_output