Skip to content

Commit 11c7540

Browse files
author
btraven00
committed
factor out repo cleanup
1 parent 65bef6e commit 11c7540

3 files changed

Lines changed: 76 additions & 4 deletions

File tree

source/_templates/plugin_base.rst.j2

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ Snakemake {% block type %}{% endblock %} plugin: {{ plugin_name }}
1212
:target: #
1313
{% endif -%}
1414
{% if repository_type == "github" and repository is not none -%}
15-
{% set repo = repository.replace("https://github.com/", "").replace(".git", "").rstrip("/") -%}
16-
.. image:: https://img.shields.io/github/last-commit/{{ repo }}
15+
.. image:: https://img.shields.io/github/last-commit/{{ repo_shortname }}
1716
:alt: GitHub - Last commit
1817
:target: {{ repository }}
1918
{% elif repository_type == "gitlab" and repository is not none -%}
20-
{% set repo = repository.replace("https://gitlab.com/", "").replace(".git", "").rstrip("/") -%}
21-
.. image:: https://img.shields.io/gitlab/last-commit/{{ repo }}
19+
.. image:: https://img.shields.io/gitlab/last-commit/{{ repo_shortname }}
2220
:alt: GitLab - Last commit
2321
:target: {{ repository }}
2422
{% elif commit_info is not none -%}

source/collect_plugins.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ def get_setting_meta(setting, key, default="", verb=False):
237237
repository = project_urls.get("Repository") or project_urls.get(
238238
"repository"
239239
)
240+
# Clean up repository URL early - remove .git suffix and trailing slashes
241+
if repository:
242+
repository = repository.replace(".git", "").rstrip("/")
243+
240244
repository_type = None
241245
if repository is None:
242246
docs_warning = (
@@ -310,11 +314,15 @@ def get_setting_meta(setting, key, default="", verb=False):
310314
else:
311315
error += "\n\nPlease contact the plugin authors."
312316

317+
# Get repository shortname for shields.io badges
318+
repo_shortname = get_repo_shortname(repository) if repository else None
319+
313320
rendered = templates.get_template(f"{plugin_type}_plugin.rst.j2").render(
314321
plugin_name=plugin_name,
315322
package_name=package,
316323
authors=authors,
317324
repository=repository,
325+
repo_shortname=repo_shortname,
318326
repository_type=repository_type,
319327
commit_info=commit_info,
320328
commit_url=commit_url,
@@ -400,6 +408,32 @@ def _commit_url(
400408
return repository
401409

402410

411+
def get_repo_shortname(repository: Optional[str]) -> str:
412+
"""Extract the shortname from repository URL for shields.io badges.
413+
414+
Removes protocol and known forge prefix (https://github.com/ or https://gitlab.com/),
415+
returning just the user/repo path.
416+
417+
Args:
418+
repository: Full repository URL (e.g., https://github.com/user/repo)
419+
420+
Returns:
421+
Repository shortname (e.g., user/repo)
422+
"""
423+
if not repository:
424+
return ""
425+
426+
# Remove protocol and domain prefixes
427+
cleaned = (
428+
repository.replace("https://github.com/", "")
429+
.replace("https://gitlab.com/", "")
430+
.replace("http://github.com/", "")
431+
.replace("http://gitlab.com/", "")
432+
)
433+
434+
return cleaned
435+
436+
403437
def _commit_age_color(date_str: str) -> str:
404438
"""Return a shields.io color hex based on how old the commit date is."""
405439
dt = datetime.fromisoformat(date_str.replace("Z", "+00:00"))

source/test_collect_plugins.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
_convert_markdown_to_rst,
77
_plugin_min_snakemake,
88
_commit_url,
9+
get_repo_shortname,
910
)
1011

1112

@@ -253,3 +254,42 @@ def test_commit_url_unknown_type_non_none():
253254
"""Test unknown repository type string returns base URL."""
254255
url = _commit_url("https://bitbucket.org/user/repo", "bitbucket", "abc1234")
255256
assert url == "https://bitbucket.org/user/repo"
257+
258+
259+
# Repository shortname extraction tests
260+
261+
262+
def test_get_repo_shortname_github_https():
263+
"""Test extracting shortname from GitHub HTTPS URL."""
264+
shortname = get_repo_shortname("https://github.com/user/repo")
265+
assert shortname == "user/repo"
266+
267+
268+
def test_get_repo_shortname_gitlab_https():
269+
"""Test extracting shortname from GitLab HTTPS URL."""
270+
shortname = get_repo_shortname("https://gitlab.com/user/repo")
271+
assert shortname == "user/repo"
272+
273+
274+
def test_get_repo_shortname_http_protocol():
275+
"""Test extracting shortname from HTTP protocol URL."""
276+
shortname = get_repo_shortname("http://github.com/user/repo")
277+
assert shortname == "user/repo"
278+
279+
280+
def test_get_repo_shortname_empty_string():
281+
"""Test extracting shortname from empty string."""
282+
shortname = get_repo_shortname("")
283+
assert shortname == ""
284+
285+
286+
def test_get_repo_shortname_none():
287+
"""Test extracting shortname from None value."""
288+
shortname = get_repo_shortname(None)
289+
assert shortname == ""
290+
291+
292+
def test_get_repo_shortname_other_domain():
293+
"""Test URL from other domain remains unchanged."""
294+
shortname = get_repo_shortname("https://bitbucket.org/user/repo")
295+
assert shortname == "https://bitbucket.org/user/repo"

0 commit comments

Comments
 (0)