From 014962fbb1f65d1d18edd8668c6141ae04136e55 Mon Sep 17 00:00:00 2001 From: Matthias Zepper Date: Fri, 26 Apr 2024 21:42:01 +0200 Subject: [PATCH] Ensure a user is configured in Git config. Required for Github Actions, but may also affect normal usage. --- nf_core/download.py | 18 +++++++++++++----- nf_core/synced_repo.py | 16 ++++++++++++++++ tests/test_download.py | 31 ++++++++++++++++++++----------- 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/nf_core/download.py b/nf_core/download.py index 62fe08e7d5..89a2e39d71 100644 --- a/nf_core/download.py +++ b/nf_core/download.py @@ -1726,21 +1726,29 @@ def tidy_tags_and_branches(self): # "Private" method to add the additional custom tags to the repository. def __add_additional_tags(self) -> None: if self.additional_tags: + self.ensure_git_user_config(f"nf-core download v{nf_core.__version__}", "core@nf-co.re") + for additional_tag in self.additional_tags: # A valid git branch or tag name can contain alphanumeric characters, underscores, hyphens, and dots. # But it must not start with a dot, hyphen or underscore and also cannot contain two consecutive dots. - if re.match(r"^\w[\w_.-]+={1}\w[\w_.-]+$",additional_tag) and '..' not in additional_tag: + if re.match(r"^\w[\w_.-]+={1}\w[\w_.-]+$", additional_tag) and ".." not in additional_tag: anchor, tag = additional_tag.split("=") if self.repo.is_valid_object(anchor) and not self.repo.is_valid_object(tag): try: - self.repo.create_tag(tag,ref=anchor,message=f"Synonynmous tag to {anchor}; added by 'nf-core download'.") - self.repo.create_head(tag,anchor) # should heads be created as well? + self.repo.create_tag( + tag, ref=anchor, message=f"Synonynmous tag to {anchor}; added by 'nf-core download'." + ) + self.repo.create_head(tag, anchor) # should heads be created as well? except (GitCommandError, InvalidGitRepositoryError) as e: log.error(f"[red]Additional tag(s) could not be applied:[/]\n{e}\n") else: - log.error(f"[red]Adding the additional tag '{tag}' to '{anchor}' failed.[/]\n Mind that '{anchor}' must be a valid git reference that resolves to a commit, while '{tag}' must not exist hitherto.") + log.error( + f"[red]Adding the additional tag '{tag}' to '{anchor}' failed.[/]\n Mind that '{anchor}' must be a valid git reference that resolves to a commit, while '{tag}' must not exist hitherto." + ) else: - log.error(f"[red]Could not apply invalid '-a' / '--additional-tag' specification[/]: '{additional_tag}'") + log.error( + f"[red]Could not apply invalid '-a' / '--additional-tag' specification[/]: '{additional_tag}'" + ) def bare_clone(self, destination): if self.repo: diff --git a/nf_core/synced_repo.py b/nf_core/synced_repo.py index 5c31e96911..5e1587baef 100644 --- a/nf_core/synced_repo.py +++ b/nf_core/synced_repo.py @@ -116,6 +116,10 @@ def __init__(self, remote_url=None, branch=None, no_pull=False, hide_progress=Fa self.remote_url = remote_url + self.repo = None + # ToDo: SyncedRepo doesn't have this method and both the ModulesRepo and + # the WorkflowRepo define their own including custom init methods. This needs + # fixing, but is beyond the scope of this PR. self.setup_local_repo(remote_url, branch, hide_progress) config_fn, repo_config = load_tools_config(self.local_repo_dir) @@ -326,6 +330,18 @@ def component_files_identical(self, component_name, base_path, commit, component self.checkout_branch() return files_identical + def ensure_git_user_config(self, default_name: str, default_email: str) -> None: + with self.repo.config_reader() as git_config: + user_name = git_config.get_value("user", "name", default=None) + user_email = git_config.get_value("user", "email", default=None) + + if not user_name or not user_email: + with self.repo.config_writer() as git_config: + if not user_name: + git_config.set_value("user", "name", default_name) + if not user_email: + git_config.set_value("user", "email", default_email) + def get_component_git_log(self, component_name, component_type, depth=None): """ Fetches the commit history the of requested module/subworkflow since a given date. The default value is diff --git a/tests/test_download.py b/tests/test_download.py index 6928a9087a..5950004356 100644 --- a/tests/test_download.py +++ b/tests/test_download.py @@ -21,7 +21,6 @@ class DownloadTest(unittest.TestCase): - @pytest.fixture(autouse=True) def use_caplog(self, caplog): self._caplog = caplog @@ -36,7 +35,7 @@ def logged_messages(self) -> list[str]: def __contains__(self, item: str) -> bool: """Allows to check for log messages easily using the in operator inside a test: - assert 'my log message' in self + assert 'my log message' in self """ return any(record.message == item for record in self._caplog.records if self._caplog) @@ -644,15 +643,13 @@ def test_download_workflow_for_platform(self, tmp_dir, _): in download_obj.containers ) # indirect definition via $container variable. - # # Test adding custom tags to Seqera Platform download # @mock.patch("nf_core.download.DownloadWorkflow.get_singularity_images") @with_temporary_folder - def test_download_workflow_for_platform_with_custom_tags(self,_, tmp_dir): + def test_download_workflow_for_platform_with_custom_tags(self, _, tmp_dir): with self._caplog.at_level(logging.INFO): - from git.refs.head import Head from git.refs.tag import TagReference download_obj = DownloadWorkflow( @@ -661,7 +658,13 @@ def test_download_workflow_for_platform_with_custom_tags(self,_, tmp_dir): compress_type="none", platform=True, container_system=None, - additional_tags=("3.7=a.tad.outdated", "3.9=cool_revision", "3.9=invalid tag","3.14.0=not_included","What is this?"), + additional_tags=( + "3.7=a.tad.outdated", + "3.9=cool_revision", + "3.9=invalid tag", + "3.14.0=not_included", + "What is this?", + ), ) download_obj.include_configs = False # suppress prompt, because stderr.is_interactive doesn't. @@ -696,8 +699,14 @@ def test_download_workflow_for_platform_with_custom_tags(self,_, tmp_dir): workflow_repo_tags = {tag.name for tag in download_obj.workflow_repo.tags} assert len(workflow_repo_tags) == 4 # the invalid/malformed additional_tags should not have been added. - assert all(tag in workflow_repo_tags for tag in {'3.7', 'a.tad.outdated', 'cool_revision', '3.9'}) - assert not any(tag in workflow_repo_tags for tag in {'invalid tag','not_included','What is this?'}) - - assert all(log in self.logged_messages for log in {"[red]Could not apply invalid '-a' / '--additional-tag' specification[/]: '3.9=invalid tag'", "[red]Adding the additional tag 'not_included' to '3.14.0' failed.[/]\n Mind that '3.14.0' must be a valid git reference that resolves to a commit, while 'not_included' must not exist hitherto.", "[red]Could not apply invalid '-a' / '--additional-tag' specification[/]: 'What is this?'"}) - + assert all(tag in workflow_repo_tags for tag in {"3.7", "a.tad.outdated", "cool_revision", "3.9"}) + assert not any(tag in workflow_repo_tags for tag in {"invalid tag", "not_included", "What is this?"}) + + assert all( + log in self.logged_messages + for log in { + "[red]Could not apply invalid '-a' / '--additional-tag' specification[/]: '3.9=invalid tag'", + "[red]Adding the additional tag 'not_included' to '3.14.0' failed.[/]\n Mind that '3.14.0' must be a valid git reference that resolves to a commit, while 'not_included' must not exist hitherto.", + "[red]Could not apply invalid '-a' / '--additional-tag' specification[/]: 'What is this?'", + } + )