Skip to content

Commit 70cd458

Browse files
author
btraven00
committed
factor out repo cleanup
1 parent 65bef6e commit 70cd458

3 files changed

Lines changed: 104 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/{{ repository_cleaned }}
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/{{ repository_cleaned }}
2220
:alt: GitLab - Last commit
2321
:target: {{ repository }}
2422
{% elif commit_info is not none -%}

source/collect_plugins.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,17 @@ def get_setting_meta(setting, key, default="", verb=False):
310310
else:
311311
error += "\n\nPlease contact the plugin authors."
312312

313+
# Clean up repository URL for shields.io badges
314+
repository_cleaned = (
315+
_cleanup_repository_url(repository) if repository else None
316+
)
317+
313318
rendered = templates.get_template(f"{plugin_type}_plugin.rst.j2").render(
314319
plugin_name=plugin_name,
315320
package_name=package,
316321
authors=authors,
317322
repository=repository,
323+
repository_cleaned=repository_cleaned,
318324
repository_type=repository_type,
319325
commit_info=commit_info,
320326
commit_url=commit_url,
@@ -400,6 +406,38 @@ def _commit_url(
400406
return repository
401407

402408

409+
def _cleanup_repository_url(repository: Optional[str]) -> str:
410+
"""Clean up repository URL for shields.io badges.
411+
412+
Removes protocol and known forge prefix (https://github.com/ or https://gitlab.com/),
413+
removes .git suffix, and strips trailing slashes.
414+
415+
Args:
416+
repository: Full repository URL (e.g., https://github.com/user/repo.git)
417+
418+
Returns:
419+
Cleaned repository path (e.g., user/repo)
420+
"""
421+
if not repository:
422+
return ""
423+
424+
# Remove protocol and domain prefixes
425+
cleaned = (
426+
repository.replace("https://github.com/", "")
427+
.replace("https://gitlab.com/", "")
428+
.replace("http://github.com/", "")
429+
.replace("http://gitlab.com/", "")
430+
)
431+
432+
# Remove .git suffix
433+
cleaned = cleaned.replace(".git", "")
434+
435+
# Strip trailing slashes
436+
cleaned = cleaned.rstrip("/")
437+
438+
return cleaned
439+
440+
403441
def _commit_age_color(date_str: str) -> str:
404442
"""Return a shields.io color hex based on how old the commit date is."""
405443
dt = datetime.fromisoformat(date_str.replace("Z", "+00:00"))

source/test_collect_plugins.py

Lines changed: 64 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+
_cleanup_repository_url,
910
)
1011

1112

@@ -253,3 +254,66 @@ 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 URL cleanup tests
260+
261+
262+
def test_cleanup_repository_url_github_https():
263+
"""Test cleaning up GitHub HTTPS URL."""
264+
cleaned = _cleanup_repository_url("https://github.com/user/repo")
265+
assert cleaned == "user/repo"
266+
267+
268+
def test_cleanup_repository_url_github_with_git_suffix():
269+
"""Test cleaning up GitHub URL with .git suffix."""
270+
cleaned = _cleanup_repository_url("https://github.com/user/repo.git")
271+
assert cleaned == "user/repo"
272+
273+
274+
def test_cleanup_repository_url_github_with_trailing_slash():
275+
"""Test cleaning up GitHub URL with trailing slash."""
276+
cleaned = _cleanup_repository_url("https://github.com/user/repo/")
277+
assert cleaned == "user/repo"
278+
279+
280+
def test_cleanup_repository_url_github_full():
281+
"""Test cleaning up GitHub URL with .git and trailing slash."""
282+
cleaned = _cleanup_repository_url("https://github.com/user/repo.git/")
283+
assert cleaned == "user/repo"
284+
285+
286+
def test_cleanup_repository_url_gitlab_https():
287+
"""Test cleaning up GitLab HTTPS URL."""
288+
cleaned = _cleanup_repository_url("https://gitlab.com/user/repo")
289+
assert cleaned == "user/repo"
290+
291+
292+
def test_cleanup_repository_url_gitlab_with_git_suffix():
293+
"""Test cleaning up GitLab URL with .git suffix."""
294+
cleaned = _cleanup_repository_url("https://gitlab.com/user/repo.git")
295+
assert cleaned == "user/repo"
296+
297+
298+
def test_cleanup_repository_url_http_protocol():
299+
"""Test cleaning up URL with HTTP protocol."""
300+
cleaned = _cleanup_repository_url("http://github.com/user/repo")
301+
assert cleaned == "user/repo"
302+
303+
304+
def test_cleanup_repository_url_empty_string():
305+
"""Test cleaning up empty string."""
306+
cleaned = _cleanup_repository_url("")
307+
assert cleaned == ""
308+
309+
310+
def test_cleanup_repository_url_none():
311+
"""Test cleaning up None value."""
312+
cleaned = _cleanup_repository_url(None)
313+
assert cleaned == ""
314+
315+
316+
def test_cleanup_repository_url_other_domain():
317+
"""Test cleaning up URL from other domain remains unchanged."""
318+
cleaned = _cleanup_repository_url("https://bitbucket.org/user/repo")
319+
assert cleaned == "https://bitbucket.org/user/repo"

0 commit comments

Comments
 (0)