Skip to content

Commit

Permalink
Ensure a user is configured in Git config. Required for Github Action…
Browse files Browse the repository at this point in the history
…s, but may also affect normal usage.
  • Loading branch information
MatthiasZepper committed Apr 28, 2024
1 parent 72d56fa commit 014962f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
18 changes: 13 additions & 5 deletions nf_core/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions nf_core/synced_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
31 changes: 20 additions & 11 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@


class DownloadTest(unittest.TestCase):

@pytest.fixture(autouse=True)
def use_caplog(self, caplog):
self._caplog = caplog
Expand All @@ -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)

Expand Down Expand Up @@ -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(
Expand All @@ -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.
Expand Down Expand Up @@ -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?'",
}
)

0 comments on commit 014962f

Please sign in to comment.