diff --git a/biscd/biscd/models/project.py b/biscd/biscd/models/project.py index 0aeefdd..9f8392b 100644 --- a/biscd/biscd/models/project.py +++ b/biscd/biscd/models/project.py @@ -1,9 +1,10 @@ from pathlib import Path +import re from shutil import rmtree import string import random from werkzeug.utils import secure_filename -from git import Repo, InvalidGitRepositoryError, GitCommandError +from git import Repo, InvalidGitRepositoryError, GitCommandError, cmd as git_cmd from flask import current_app as app from biscd import config @@ -56,36 +57,53 @@ class Project(YamlSerializable): repo = Repo(path) app.logger.info('Already under git control') except InvalidGitRepositoryError: - app.logger.info(f"Cloning repo from {self.git_repo}") - repo = Repo.clone_from(self.git_repo, path) - app.logger.info('Done!') + repo = self._clone_repo(path) + # If repo is not type Repo it must be an error and thus returned directly + if not isinstance(repo, Repo): + return repo remotes = repo.remotes if not any(remotes): app.logger.error(f"Repo {path} doesn't have any remotes.") return ["Repo doesn't have any remotes. please fix manually", 'error'] response = remotes[0].pull() for item in response: + # TODO: is this nessecary? app.logger.debug(item) + response = self._checkout_branch(repo, self.branch) + return response + + def _clone_repo(self, path): + app.logger.info("Cloning repo from %s", self.git_repo) + try: + repo = Repo.clone_from(self.git_repo, path) + except GitCommandError as gce: + app.logger.error("Project %s's git repo: %s is invalid: %s", + self.name, self.git_repo, gce.stderr.strip('\n ')) + return ["Project does not have a valid git repository url", "error"] + app.logger.info('Cloning repo (%s) succeeded!', self.git_repo) + return repo + + def _checkout_branch(self, repo, branch): try: - response = repo.git.checkout(self.branch) + repo.git.checkout(branch) except AttributeError: return [f"Branch is not set for {self.name}", 'error'] except GitCommandError: return [ - f"Repo '{self.git_repo}' does not have an existing branch called {self.branch}", + f"Repo '{self.git_repo}' does not have an existing branch called {branch}", 'error'] return None def delete_files(self): - if self._production_path_exists(): + if self.production_path_exists(): path = self.absulute_path app.logger.info(f"Deleting {path}") rmtree(path) self.relative_production_path = None self.save() - def _production_path_exists(self): + def production_path_exists(self): """Return True if project's the production directory path exists""" prod_path_dir = Path(config['production_path'].get()) if hasattr(self, 'relative_production_path') and self.relative_production_path: diff --git a/biscd/biscd/templates/project.html b/biscd/biscd/templates/project.html index 8b4eeba..4876937 100644 --- a/biscd/biscd/templates/project.html +++ b/biscd/biscd/templates/project.html @@ -8,12 +8,16 @@ + {% if project.git_repo %} + {% endif %} + {% if project.production_path_exists() %} + {% endif %}

{% endif %}