Repository: nf-core/tools

Issue:
auth problems with private modules repositories
<!--
# nf-core/tools bug report

Hi there!

Thanks for telling us about a problem with the nf-core/tools package.
Please delete this text and anything that's not relevant from the template below:
-->

## Description of the bug

<!-- A clear and concise description of what the bug is. -->
Dear developers,

I'm not sure if you plan to fully support this feature, but I found that using *nf-core tools* with private repositories (or even with public branches) has some issues when accessing modules outside from `nf-core/modules`.

## Steps to reproduce

Each of the following command lines have issues with other repositories than `nf-core/modules`:

```bash
nf-core modules --github-repository <a repository> --branch <a branch> list remote
nf-core modules --github-repository <a repository> --branch <a branch> --dir . <a module>
nf-core modules list local # after installing private modules
nf-core modules --github-repository <a repository> --branch <a branch> update <a module>
```

## Expected behaviour

<!-- A clear and concise description of what you expected to happen. -->

*nf-core tool* lets to specify different repositories and branches with ``--github-repository`` and ``--branch`` options, and the ``modules.json`` has keys to model different repositories and *SHA* than the standard ``nf-core/modules``. Some requests methods to the github API already provide authentication with `auth=nf_core.utils.github_api_auto_auth()`, so the required paches are not so complex as we could expect.

## System

- Hardware: <!-- [e.g. HPC, Desktop, Cloud...] --> Desktop
- Executor: <!-- [e.g. slurm, local, awsbatch...] --> Local
- OS: <!-- [e.g. CentOS Linux, macOS, Linux Mint...] --> Debian Buster
- Version of nf-core/tools: <!-- [e.g. 1.1, 1.5, 1.8.2...] --> 2.2.dev0
- Python version: <!-- [e.g. 3.7, 3.8...] --> 3.9.7

## Nextflow Installation

- Version: <!-- [e.g. 19.10.0] --> 21.04.3 build 5560

## Additional context

<!-- Add any other context about the problem here. -->
I can open a PR in which I had fixed such issues, however this context is difficult to test: should I prove that those patches works in your CI system? Should I need to mock up the API requests (and reponses)?


Discussion:

Resolving patch:
diff --git a/nf_core/modules/list.py b/nf_core/modules/list.py
--- a/nf_core/modules/list.py
+++ b/nf_core/modules/list.py
@@ -103,7 +103,8 @@ def pattern_msg(keywords):
                     if module_entry:
                         version_sha = module_entry["git_sha"]
                         try:
-                            message, date = nf_core.modules.module_utils.get_commit_info(version_sha)
+                            # pass repo_name to get info on modules even outside nf-core/modules
+                            message, date = nf_core.modules.module_utils.get_commit_info(version_sha, repo_name)
                         except LookupError as e:
                             log.warning(e)
                             date = "[red]Not Available"
diff --git a/nf_core/modules/module_utils.py b/nf_core/modules/module_utils.py
--- a/nf_core/modules/module_utils.py
+++ b/nf_core/modules/module_utils.py
@@ -46,6 +46,7 @@ def get_module_git_log(module_name, modules_repo=None, per_page=30, page_nbr=1,
     update breaking backwards compatibility.
     Args:
         module_name (str): Name of module
+        modules_repo (ModulesRepo): A ModulesRepo object configured for the repository in question
         per_page (int): Number of commits per page returned by API
         page_nbr (int): Page number of the retrieved commits
         since (str): Only show commits later than this timestamp.
@@ -57,7 +58,7 @@ def get_module_git_log(module_name, modules_repo=None, per_page=30, page_nbr=1,
     if modules_repo is None:
         modules_repo = ModulesRepo()
     api_url = f"https://api.github.com/repos/{modules_repo.name}/commits"
-    api_url += f"?sha{modules_repo.branch}"
+    api_url += f"?sha={modules_repo.branch}"
     if module_name is not None:
         api_url += f"&path=modules/{module_name}"
     api_url += f"&page={page_nbr}"
@@ -84,19 +85,21 @@ def get_module_git_log(module_name, modules_repo=None, per_page=30, page_nbr=1,
         )
 
 
-def get_commit_info(commit_sha):
+def get_commit_info(commit_sha, repo_name="nf-core/modules"):
     """
     Fetches metadata about the commit (dates, message, etc.)
     Args:
-        module_name (str): Name of module
         commit_sha (str): The SHA of the requested commit
+        repo_name (str): module repos name (def. {0})
     Returns:
         message (str): The commit message for the requested commit
         date (str): The commit date for the requested commit
     Raises:
         LookupError: If the call to the API fails.
-    """
-    api_url = f"https://api.github.com/repos/nf-core/modules/commits/{commit_sha}?stats=false"
+    """.format(
+        repo_name
+    )
+    api_url = f"https://api.github.com/repos/{repo_name}/commits/{commit_sha}?stats=false"
     log.debug(f"Fetching commit metadata for commit at {commit_sha}")
     response = requests.get(api_url, auth=nf_core.utils.github_api_auto_auth())
     if response.status_code == 200:
@@ -259,7 +262,7 @@ def local_module_equal_to_commit(local_files, module_name, modules_repo, commit_
     for i, file in enumerate(files_to_check):
         # Download remote copy and compare
         api_url = f"{module_base_url}/{file}"
-        r = requests.get(url=api_url)
+        r = requests.get(url=api_url, auth=nf_core.utils.github_api_auto_auth())
         if r.status_code != 200:
             log.debug(f"Could not download remote copy of file module {module_name}/{file}")
             log.debug(api_url)
@@ -367,7 +370,7 @@ def verify_pipeline_dir(dir):
         modules_is_software = False
         for repo_name in repo_names:
             api_url = f"https://api.github.com/repos/{repo_name}/contents"
-            response = requests.get(api_url)
+            response = requests.get(api_url, auth=nf_core.utils.github_api_auto_auth())
             if response.status_code == 404:
                 missing_remote.append(repo_name)
                 if repo_name == "nf-core/software":
diff --git a/nf_core/modules/modules_repo.py b/nf_core/modules/modules_repo.py
--- a/nf_core/modules/modules_repo.py
+++ b/nf_core/modules/modules_repo.py
@@ -39,7 +39,7 @@ def verify_modules_repo(self):
 
         # Check if repository exist
         api_url = f"https://api.github.com/repos/{self.name}/branches"
-        response = requests.get(api_url)
+        response = requests.get(api_url, auth=nf_core.utils.github_api_auto_auth())
         if response.status_code == 200:
             branches = [branch["name"] for branch in response.json()]
             if self.branch not in branches:
@@ -48,7 +48,7 @@ def verify_modules_repo(self):
             raise LookupError(f"Repository '{self.name}' is not available on GitHub")
 
         api_url = f"https://api.github.com/repos/{self.name}/contents?ref={self.branch}"
-        response = requests.get(api_url)
+        response = requests.get(api_url, auth=nf_core.utils.github_api_auto_auth())
         if response.status_code == 200:
             dir_names = [entry["name"] for entry in response.json() if entry["type"] == "dir"]
             if "modules" not in dir_names:


Test patch: