Skip to content

Commit

Permalink
Refactor RepositoryManagerRemote class
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmoreton committed Jul 15, 2024
1 parent db8a54f commit 0fb982a
Showing 1 changed file with 36 additions and 112 deletions.
148 changes: 36 additions & 112 deletions src/managers/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,70 +60,35 @@ def parse_poetry_version(self):

self.poetry_version = poetry_version


class RepositoryManagerRemote(RepositoryManagerBase):
"""Class for managing remote repositories"""

def __init__(self, repo_url):
super().__init__()

self.repo_url = repo_url
self.repo_dir = tempfile.TemporaryDirectory(
# cloning to the root of the project so we can access the files if needed
dir=pathlib.Path(__file__).parent.parent.parent,
prefix="docker-run-",
)
subprocess.run(
["git", "clone", self.repo_url, self.repo_dir.name],
check=True,
capture_output=True,
)

@property
def get_repo_dir(self):
return pathlib.Path(self.repo_dir.name)
return pathlib.Path(self.repo_dir) if self.repo_url == "local" else pathlib.Path(self.repo_dir.name)

def find_docker_files(self):
return list(pathlib.Path(self.repo_dir.name).glob("**/Dockerfile*"))
return (
list(pathlib.Path(self.repo_dir).glob("**/Dockerfile*"))
if self.repo_url == "local"
else list(pathlib.Path(self.repo_dir.name).glob("**/Dockerfile*"))
)

def get_branch(self):
cwd = self.repo_dir if self.repo_url == "local" else self.repo_dir.name
cmd = ["git", "branch", "--show-current"]
return subprocess.run(cmd, cwd=cwd, check=True, capture_output=True).stdout.decode("utf-8").strip()

def change_branch(self, branch_name):
cwd = self.repo_dir if self.repo_url == "local" else self.repo_dir.name
cmd = ["git", "checkout", branch_name]
try:
return (
subprocess.run(
["git", "checkout", branch_name],
cwd=self.repo_dir.name,
check=True,
capture_output=True,
)
.stdout.decode("utf-8")
.strip()
)
return subprocess.run(cmd, cwd=cwd, check=True, capture_output=True).stdout.decode("utf-8").strip()
except subprocess.CalledProcessError:
return f"Error: Branch {branch_name} does not exist in the repository. Please check the branch name and try again." # noqa

def get_branch(self):
return (
subprocess.run(
["git", "branch", "--show-current"],
cwd=self.repo_dir.name,
check=True,
capture_output=True,
)
.stdout.decode("utf-8")
.strip()
)

def get_branches(self):
cwd = self.repo_dir if self.repo_url == "local" else self.repo_dir.name
cmd = ["git", "branch"] if self.repo_url == "local" else ["git", "branch", "--remote"]
branches = (
subprocess.run(
["git", "branch", "--remote"],
cwd=self.repo_dir.name,
check=True,
capture_output=True,
)
.stdout.decode("utf-8")
.strip()
.split("\n")
subprocess.run(cmd, cwd=cwd, check=True, capture_output=True).stdout.decode("utf-8").strip().split("\n")
)
branches_cleaned = []

Expand All @@ -140,17 +105,32 @@ def get_branches(self):
return branch_index


class RepositoryManagerRemote(RepositoryManagerBase):
"""Class for managing remote repositories"""

def __init__(self, repo_url):
super().__init__()

self.repo_url = repo_url
self.repo_dir = tempfile.TemporaryDirectory(
# cloning to the root of the project so we can access the files if needed
dir=pathlib.Path(__file__).parent.parent.parent,
prefix="docker-run-",
)
subprocess.run(
["git", "clone", self.repo_url, self.repo_dir.name],
check=True,
capture_output=True,
)


class RepositoryManagerLocal(RepositoryManagerBase):
def __init__(self, path):
super().__init__()

self.repo_url = "local"
self.repo_dir = self.get_absolute_path(path)

@property
def get_repo_dir(self):
return pathlib.Path(self.repo_dir)

def get_absolute_path(self, path):
path_parts = path.strip("/").split("/")
parent_dirs_count = 0
Expand All @@ -167,59 +147,3 @@ def get_absolute_path(self, path):
cwd = cwd.parent

return cwd.joinpath(*dir_parts)

def get_branch(self):
return (
subprocess.run(
["git", "branch", "--show-current"],
cwd=self.repo_dir,
check=True,
capture_output=True,
)
.stdout.decode("utf-8")
.strip()
)

def get_branches(self):
branches = (
subprocess.run(
["git", "branch"],
cwd=self.repo_dir,
check=True,
capture_output=True,
)
.stdout.decode("utf-8")
.strip()
.split("\n")
)
branches_cleaned = []

for branch in branches:
branches_cleaned.append(branch.replace("origin/", "").strip())

branch_index = {}
for i, branch in enumerate(sorted(branches_cleaned), 1):
branch_name = branch
if branch == self.get_branch():
branch_name = f"{branch} ( DEFAULT )"
branch_index[i] = branch_name

return branch_index

def change_branch(self, branch_name):
try:
return (
subprocess.run(
["git", "checkout", branch_name],
cwd=self.repo_dir,
check=True,
capture_output=True,
)
.stdout.decode("utf-8")
.strip()
)
except subprocess.CalledProcessError:
return f"Error: Branch {branch_name} does not exist in the repository. Please check the branch name and try again." # noqa

def find_docker_files(self):
return list(pathlib.Path(self.repo_dir).glob("**/Dockerfile*"))

0 comments on commit 0fb982a

Please sign in to comment.