ci: Rewrite relative readme URLs before release (#52)

This commit is contained in:
Mike A.
2024-08-01 20:41:28 +02:00
parent a25ca28f9f
commit 6dddb5f77e
3 changed files with 54 additions and 0 deletions

View File

@@ -25,6 +25,9 @@ jobs:
python -m pip install poetry python -m pip install poetry
poetry config virtualenvs.in-project true poetry config virtualenvs.in-project true
poetry install poetry install
- name: Prepare README
run: ./scripts/refactor_readme.py README.md
- name: Build package - name: Build package
run: poetry build run: poetry build

View File

@@ -47,6 +47,7 @@ ignore = [
"D105", # docstrings in magic methods "D105", # docstrings in magic methods
"S101", # assert statements "S101", # assert statements
"S603", # false-positive subprocess call (https://github.com/astral-sh/ruff/issues/4045)
"PLR2004", # "magic" values >.> "PLR2004", # "magic" values >.>
"FBT", # boolean "traps" "FBT", # boolean "traps"
@@ -61,6 +62,10 @@ line-length = 100
"D", # documentation "D", # documentation
"INP001", # namespacing "INP001", # namespacing
] ]
"scripts/*" = [
"T201", # use of "print"
"D", # documentation
]
[build-system] [build-system]
requires = ["poetry-core"] requires = ["poetry-core"]

46
scripts/refactor_readme.py Executable file
View File

@@ -0,0 +1,46 @@
#!/usr/bin/env python3
"""Script to resolve relative URLs in README prior to release."""
from __future__ import annotations
import re
import subprocess
import sys
from pathlib import Path
def main(args: list[str]) -> int:
if len(args) < 1:
print("No README path supplied.")
return 1
remote_url = (
subprocess.run(
["/usr/bin/env", "git", "remote", "get-url", "origin"],
check=True,
capture_output=True,
)
.stdout.decode("utf-8")
.strip()
)
# Convert SSH remote URLs to HTTPS
remote_url = re.sub(r"^ssh://git@", "https://", remote_url)
readme_path = Path(args[0])
readme_content = readme_path.read_text("utf-8")
new_content = re.sub(
r"(\[[^]]+]\()((?!https?:)[^)]+)(\))",
lambda m: m.group(1) + remote_url + "/blob/main/" + m.group(2) + m.group(3),
readme_content,
)
readme_path.write_text(new_content)
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))