mirror of
https://github.com/sartography/cr-connect-workflow.git
synced 2025-02-23 21:28:32 +00:00
clone repo if it doesn't exist
This commit is contained in:
parent
070c3cfd5d
commit
8a8c3de8c4
@ -1,4 +1,7 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
from crc import app
|
from crc import app
|
||||||
|
from crc.api.common import ApiError
|
||||||
from crc.models.git_models import GitRepo
|
from crc.models.git_models import GitRepo
|
||||||
from git import Repo, InvalidGitRepositoryError, NoSuchPathError
|
from git import Repo, InvalidGitRepositoryError, NoSuchPathError
|
||||||
|
|
||||||
@ -9,21 +12,32 @@ class GitService(object):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __get_repo():
|
def __get_repo():
|
||||||
|
remote = app.config['GIT_REMOTE']
|
||||||
|
git_branch = app.config['GIT_BRANCH']
|
||||||
directory = app.config['SYNC_FILE_ROOT']
|
directory = app.config['SYNC_FILE_ROOT']
|
||||||
repo = None
|
# repo = None
|
||||||
try:
|
try:
|
||||||
repo = Repo(directory)
|
repo = Repo(directory)
|
||||||
except InvalidGitRepositoryError as ge:
|
except InvalidGitRepositoryError:
|
||||||
# Thrown if the given repository appears to have an invalid format.
|
# Thrown if the given repository appears to have an invalid format.
|
||||||
# TODO: I believe this means there is no .git directory
|
# I believe this means there is no .git directory
|
||||||
# So, git init?
|
if os.listdir(directory):
|
||||||
print(ge)
|
# If the directory is not empty, we let them decide how to fix it
|
||||||
repo = Repo.init_repo(directory)
|
raise ApiError(code='invalid_git_repo',
|
||||||
except NoSuchPathError as pe:
|
message=f'The directory {directory} is not empty, and is not a valid git repository. Please fix this before continuing.')
|
||||||
# TODO: clone from remote?
|
else:
|
||||||
print(pe)
|
# If the directory is empty, we clone
|
||||||
|
repo = Repo.clone_from(remote, directory)
|
||||||
|
repo.git.checkout(git_branch)
|
||||||
|
except NoSuchPathError:
|
||||||
|
# The directory does not exist, so clone
|
||||||
|
repo = Repo.clone_from(remote, directory)
|
||||||
|
repo.git.checkout(git_branch)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
raise ApiError(code='unknown_exception',
|
||||||
|
message=f'There was an unknown exception. Original message is: {e}')
|
||||||
print(e)
|
print(e)
|
||||||
|
app.logger.error(e)
|
||||||
return repo
|
return repo
|
||||||
|
|
||||||
def _get_repo(self):
|
def _get_repo(self):
|
||||||
@ -34,7 +48,7 @@ class GitService(object):
|
|||||||
repo_model = GitRepo().from_repo(repo)
|
repo_model = GitRepo().from_repo(repo)
|
||||||
return repo_model
|
return repo_model
|
||||||
|
|
||||||
def push_to_remote(self, comment):
|
def push_to_remote(self, comment=None):
|
||||||
if comment is None:
|
if comment is None:
|
||||||
comment = f"Git commit: {datetime.now()}"
|
comment = f"Git commit: {datetime.now()}"
|
||||||
repo = self.__get_repo()
|
repo = self.__get_repo()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user