From cb3a639e1005b9e3b7d92aae1ae951fd9449f1ca Mon Sep 17 00:00:00 2001 From: Marco Enrico Piras Date: Mon, 22 Jan 2024 18:15:09 +0100 Subject: [PATCH 1/7] fix(util): absence of git remotes should not block --- lifemonitor/api/models/repositories/local.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lifemonitor/api/models/repositories/local.py b/lifemonitor/api/models/repositories/local.py index 6a8e9d2a5..1f208b646 100644 --- a/lifemonitor/api/models/repositories/local.py +++ b/lifemonitor/api/models/repositories/local.py @@ -301,7 +301,8 @@ def __init__(self, self._remote_repo_info = None try: self._remote_repo_info = RemoteGitRepoInfo.parse(self._git_repo.remotes.origin.url) - except git.exc.GitCommandError as e: + except (git.exc.GitCommandError, AttributeError) as e: + logger.warning("Unable to parse remote repository info: %s", e) if logger.isEnabledFor(logging.DEBUG): logger.exception(e) From 5b85dc256cbb6a5a1328d31f896f5e94272dd0b6 Mon Sep 17 00:00:00 2001 From: Marco Enrico Piras Date: Tue, 23 Jan 2024 09:01:31 +0100 Subject: [PATCH 2/7] feat(model): check git workflow for the presence of at least one Git branch --- lifemonitor/api/models/issues/general/lm.py | 1 + .../api/models/issues/general/repo_layout.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lifemonitor/api/models/issues/general/lm.py b/lifemonitor/api/models/issues/general/lm.py index 3e219d286..10912bfe3 100644 --- a/lifemonitor/api/models/issues/general/lm.py +++ b/lifemonitor/api/models/issues/general/lm.py @@ -36,6 +36,7 @@ class MissingLMConfigFile(WorkflowRepositoryIssue): description = "No lifemonitor.yaml configuration file found on this repository.
"\ "The lifemonitor.yaml should be placed on the root of this repository." labels = ['lifemonitor'] + depends_on = ["GitRepositoryWithoutMainBranch"] def check(self, repo: WorkflowRepository) -> bool: if repo.config is None: diff --git a/lifemonitor/api/models/issues/general/repo_layout.py b/lifemonitor/api/models/issues/general/repo_layout.py index 98624d045..951861f5d 100644 --- a/lifemonitor/api/models/issues/general/repo_layout.py +++ b/lifemonitor/api/models/issues/general/repo_layout.py @@ -29,10 +29,25 @@ logger = logging.getLogger(__name__) +class GitRepositoryWithoutMainBranch(WorkflowRepositoryIssue): + name = "Repository without main branch" + description = "This repository does not have a main branch." + labels = ['best-practices'] + + def check(self, repo: WorkflowRepository) -> bool: + """ + If the repository is a Git repository, check if it has a main branch. + """ + if not repo.is_git_repo(repo.local_path): + return False + return repo.main_branch is None + + class RepositoryNotInitialised(WorkflowRepositoryIssue): name = "Repository not intialised" description = "No workflow and crate metadata found on this repository." labels = ['best-practices'] + depends_on = [GitRepositoryWithoutMainBranch] def check(self, repo: WorkflowRepository) -> bool: return repo.find_workflow() is None and repo.metadata is None From 4b9632da8e91e307259b15f289d61021234b7f40 Mon Sep 17 00:00:00 2001 From: Marco Enrico Piras Date: Tue, 23 Jan 2024 10:44:21 +0100 Subject: [PATCH 3/7] fix: check property existence --- lifemonitor/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lifemonitor/utils.py b/lifemonitor/utils.py index 830f6a557..d6b9187f2 100644 --- a/lifemonitor/utils.py +++ b/lifemonitor/utils.py @@ -807,7 +807,8 @@ class RemoteGitRepoInfo(giturlparse.result.GitUrlParsed): def __init__(self, parsed_info): # fix for giturlparse: protocols are not parsed correctly - del parsed_info['protocols'] + if 'protocols' in parsed_info: + del parsed_info['protocols'] super().__init__(parsed_info) @property From a2517561eeef794cbaeca637457a5bb510425d77 Mon Sep 17 00:00:00 2001 From: Marco Enrico Piras Date: Tue, 23 Jan 2024 13:13:32 +0100 Subject: [PATCH 4/7] feat(model): more properties to inspect git workflow repos --- lifemonitor/api/models/repositories/local.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lifemonitor/api/models/repositories/local.py b/lifemonitor/api/models/repositories/local.py index 1f208b646..b594868ab 100644 --- a/lifemonitor/api/models/repositories/local.py +++ b/lifemonitor/api/models/repositories/local.py @@ -310,6 +310,14 @@ def __init__(self, def main_branch(self) -> str: return self._git_repo.active_branch.name + @property + def remotes(self) -> List[str]: + return [r.name for r in self._git_repo.remotes] + + @property + def heads(self) -> List[str]: + return [h.name for h in self._git_repo.heads] + @property def owner(self) -> str: return super().owner or \ From 0c48c44be856e9d45d0bfd84932fe41b484b7315 Mon Sep 17 00:00:00 2001 From: Marco Enrico Piras Date: Tue, 23 Jan 2024 13:14:09 +0100 Subject: [PATCH 5/7] fix(model): check remote branches --- lifemonitor/api/models/issues/general/repo_layout.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lifemonitor/api/models/issues/general/repo_layout.py b/lifemonitor/api/models/issues/general/repo_layout.py index 951861f5d..ab4bfab30 100644 --- a/lifemonitor/api/models/issues/general/repo_layout.py +++ b/lifemonitor/api/models/issues/general/repo_layout.py @@ -24,6 +24,8 @@ from lifemonitor.api.models.issues import WorkflowRepositoryIssue from lifemonitor.api.models.repositories import WorkflowRepository +from lifemonitor.api.models.repositories.local import \ + LocalGitWorkflowRepository # set module level logger logger = logging.getLogger(__name__) @@ -40,7 +42,9 @@ def check(self, repo: WorkflowRepository) -> bool: """ if not repo.is_git_repo(repo.local_path): return False - return repo.main_branch is None + git_repo = LocalGitWorkflowRepository(repo.local_path) + logger.debug("Local Git repository: %r - branches: %r", git_repo, git_repo.heads) + return git_repo.heads is None or len(git_repo.heads) == 0 class RepositoryNotInitialised(WorkflowRepositoryIssue): From bf050b33edd3ec630994abf28f9c8bc7ab762543 Mon Sep 17 00:00:00 2001 From: Marco Enrico Piras Date: Tue, 23 Jan 2024 15:36:43 +0100 Subject: [PATCH 6/7] fix(model): replace check name with the corresponding class type --- lifemonitor/api/models/issues/general/lm.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lifemonitor/api/models/issues/general/lm.py b/lifemonitor/api/models/issues/general/lm.py index 10912bfe3..1d029b789 100644 --- a/lifemonitor/api/models/issues/general/lm.py +++ b/lifemonitor/api/models/issues/general/lm.py @@ -22,10 +22,12 @@ import logging -from lifemonitor.utils import get_validation_schema_url from lifemonitor.api.models.issues import IssueMessage, WorkflowRepositoryIssue +from lifemonitor.api.models.issues.general.repo_layout import \ + GitRepositoryWithoutMainBranch from lifemonitor.api.models.repositories import WorkflowRepository from lifemonitor.schemas.validators import ValidationError, ValidationResult +from lifemonitor.utils import get_validation_schema_url # set module level logger logger = logging.getLogger(__name__) @@ -36,7 +38,7 @@ class MissingLMConfigFile(WorkflowRepositoryIssue): description = "No lifemonitor.yaml configuration file found on this repository.
"\ "The lifemonitor.yaml should be placed on the root of this repository." labels = ['lifemonitor'] - depends_on = ["GitRepositoryWithoutMainBranch"] + depends_on = [GitRepositoryWithoutMainBranch] def check(self, repo: WorkflowRepository) -> bool: if repo.config is None: From c23b0d6c032f74a76e212def8746d725b8e1c325 Mon Sep 17 00:00:00 2001 From: Marco Enrico Piras Date: Thu, 25 Jan 2024 12:26:51 +0100 Subject: [PATCH 7/7] fix: reference static method using class reference --- lifemonitor/api/models/issues/general/repo_layout.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lifemonitor/api/models/issues/general/repo_layout.py b/lifemonitor/api/models/issues/general/repo_layout.py index ab4bfab30..556ffbb64 100644 --- a/lifemonitor/api/models/issues/general/repo_layout.py +++ b/lifemonitor/api/models/issues/general/repo_layout.py @@ -40,7 +40,7 @@ def check(self, repo: WorkflowRepository) -> bool: """ If the repository is a Git repository, check if it has a main branch. """ - if not repo.is_git_repo(repo.local_path): + if not LocalGitWorkflowRepository.is_git_repo(repo.local_path): return False git_repo = LocalGitWorkflowRepository(repo.local_path) logger.debug("Local Git repository: %r - branches: %r", git_repo, git_repo.heads)