feat: add auth to git operations

This commit is contained in:
Sam Chau
2024-09-08 02:42:17 +09:30
parent 28851e0270
commit adc7fe836a
5 changed files with 125 additions and 29 deletions

View File

@@ -34,16 +34,7 @@ def delete_branch(repo_path, branch_name):
# Check if remote branch exists
remote_branch = f"origin/{branch_name}"
if remote_branch in repo.refs:
logger.debug(f"Attempting to delete remote branch: {branch_name}")
try:
repo.git.push('origin', '--delete', branch_name)
logger.debug(
f"Successfully deleted remote branch: {branch_name}")
except GitCommandError as e:
logger.error(
f"Failed to delete remote branch: {branch_name}. Error: {str(e)}"
)
return False, f"Failed to delete remote branch: {branch_name}. There might be permission issues."
pass
return True, {
"message": f"Deleted branch: {branch_name}",

View File

@@ -1,27 +1,75 @@
import git
import logging
import os
from git import RemoteProgress
from urllib.parse import urlparse, urlunparse
from ..auth.authenticate import get_github_token, check_dev_mode
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
class CredentialedRemoteProgress(RemoteProgress):
def __init__(self, github_token):
super().__init__()
self.github_token = github_token
def update(self, op_code, cur_count, max_count=None, message=''):
if op_code & RemoteProgress.AUTHENTICATING:
# This is where we would inject the GitHub token if needed
pass
def push_branch_to_remote(repo_path, branch_name):
try:
logger.debug(f"Attempting to push branch {branch_name} to remote")
# Check if we're in dev mode
if not check_dev_mode():
logger.warning("Not in dev mode. Push operation not allowed.")
return False, "Push operation not allowed in production mode."
# Get the GitHub token
github_token = get_github_token()
if not github_token:
logger.error("GitHub token not available")
return False, "GitHub token not available"
repo = git.Repo(repo_path)
# Check if the branch exists locally
if branch_name not in repo.heads:
return False, f"Branch '{branch_name}' does not exist locally."
# Push the branch to remote and set the upstream branch
# Get the remote URL and inject the GitHub token
origin = repo.remote(name='origin')
origin.push(refspec=f"{branch_name}:{branch_name}", set_upstream=True)
url = list(urlparse(next(origin.urls)))
url[1] = f"{github_token}@{url[1]}" # Inject GitHub token into the URL
auth_url = urlunparse(url)
# Set the new URL with the GitHub token
origin.set_url(auth_url)
# Push the branch to remote and set the upstream branch
progress = CredentialedRemoteProgress(github_token)
origin.push(refspec=f"{branch_name}:{branch_name}",
set_upstream=True,
progress=progress)
logger.debug(f"Successfully pushed branch to remote: {branch_name}")
return True, {"message": f"Pushed branch to remote: {branch_name}"}
except git.GitCommandError as e:
logger.error(f"Git command error pushing branch to remote: {str(e)}", exc_info=True)
logger.error(f"Git command error pushing branch to remote: {str(e)}",
exc_info=True)
return False, {"error": f"Error pushing branch to remote: {str(e)}"}
except Exception as e:
logger.error(f"Error pushing branch to remote: {str(e)}", exc_info=True)
logger.error(f"Error pushing branch to remote: {str(e)}",
exc_info=True)
return False, {"error": f"Error pushing branch to remote: {str(e)}"}
finally:
# Reset the URL to remove the GitHub token
origin.set_url(next(origin.urls))

View File

@@ -3,16 +3,49 @@
import git
import logging
from .commit import commit_changes
from ..auth.authenticate import check_dev_mode, get_github_token
logger = logging.getLogger(__name__)
def push_changes(repo_path, files, message):
try:
# Check if we're in dev mode
if not check_dev_mode():
logger.warning("Not in dev mode. Push operation not allowed.")
return False, "Push operation not allowed in production mode."
# Get the GitHub token
github_token = get_github_token()
if not github_token:
logger.error("GitHub token not available")
return False, "GitHub token not available"
repo = git.Repo(repo_path)
commit_changes(repo_path, files, message)
origin = repo.remote(name='origin')
origin.push()
# Commit changes
commit_success, commit_message = commit_changes(
repo_path, files, message)
if not commit_success:
return False, commit_message
# Authenticate and push changes
with repo.git.custom_environment(GIT_ASKPASS='echo',
GIT_USERNAME=github_token):
origin = repo.remote(name='origin')
push_info = origin.push()
# Check if the push was successful
if push_info[0].flags & push_info[0].ERROR:
raise git.GitCommandError("git push", push_info[0].summary)
return True, "Successfully pushed changes."
except git.GitCommandError as e:
logger.error(f"Git command error pushing changes: {str(e)}",
exc_info=True)
return False, f"Error pushing changes: {str(e)}"
except Exception as e:
logger.error(f"Error pushing changes: {str(e)}", exc_info=True)
return False, f"Error pushing changes: {str(e)}"
return False, f"Error pushing changes: {str(e)}"

View File

@@ -2,19 +2,43 @@
import git
import logging
from ..auth.authenticate import check_dev_mode, get_github_token
logger = logging.getLogger(__name__)
def stage_files(repo_path, files):
try:
# Check if we're in dev mode
if not check_dev_mode():
logger.warning("Not in dev mode. Staging operation not allowed.")
return False, "Staging operation not allowed in production mode."
# Get the GitHub token
github_token = get_github_token()
if not github_token:
logger.error("GitHub token not available")
return False, "GitHub token not available"
repo = git.Repo(repo_path)
if not files:
repo.git.add(A=True)
message = "All changes have been staged."
else:
repo.index.add(files)
message = "Specified files have been staged."
# Authenticate with GitHub token
with repo.git.custom_environment(GIT_ASKPASS='echo',
GIT_USERNAME=github_token):
if not files:
repo.git.add(A=True)
message = "All changes have been staged."
else:
repo.index.add(files)
message = "Specified files have been staged."
return True, message
except git.GitCommandError as e:
logger.error(f"Git command error staging files: {str(e)}",
exc_info=True)
return False, f"Error staging files: {str(e)}"
except Exception as e:
logger.error(f"Error staging files: {str(e)}", exc_info=True)
return False, f"Error staging files: {str(e)}"
return False, f"Error staging files: {str(e)}"

View File

@@ -346,7 +346,7 @@ const SettingsBranchModal = ({
</Tooltip>
)}
{(branch.isLocal ||
(branch.isRemote && isDevMode)) &&
(!branch.isRemote && isDevMode)) &&
branch.name !== currentBranch &&
branch.name.toLowerCase() !==
'stable' && (