Created
November 6, 2025 09:13
-
-
Save romac/3f6de57ca8c663fb7fbc10b7f70c8bc3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import re | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| # --- CONFIG --- | |
| PR_URL_PATTERN = re.compile( | |
| r'(https://github\.com/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+/pull/(\d+))' | |
| ) | |
| def get_pr_title(pr_url: str) -> str: | |
| """Fetch the PR title from GitHub CLI for a given PR URL.""" | |
| try: | |
| result = subprocess.run( | |
| ["gh", "pr", "view", pr_url, "--json", "title", "--jq", ".title"], | |
| capture_output=True, | |
| text=True, | |
| check=True, | |
| ) | |
| return result.stdout.strip() | |
| except subprocess.CalledProcessError as e: | |
| print(f"⚠️ Failed to fetch title for {pr_url}: {e.stderr.strip()}", file=sys.stderr) | |
| return None | |
| def replace_urls_with_titles(markdown_text: str) -> str: | |
| """Replace each GitHub PR URL with: TITLE ([#PRNUM](URL))""" | |
| urls = dict(PR_URL_PATTERN.findall(markdown_text)) | |
| print(f"Found {len(urls)} PR links...") | |
| for url, pr_num in urls.items(): | |
| title = get_pr_title(url) | |
| if not title: | |
| continue | |
| replacement = f"{title} ([#{pr_num}]({url}))" | |
| markdown_text = markdown_text.replace(url, replacement) | |
| print(f"✅ Replaced {url} → {replacement}") | |
| return markdown_text | |
| def main(): | |
| if len(sys.argv) < 2: | |
| print("Usage: python update_pr_links.py <input.md> [output.md]") | |
| sys.exit(1) | |
| input_path = Path(sys.argv[1]) | |
| output_path = Path(sys.argv[2]) if len(sys.argv) > 2 else input_path | |
| text = input_path.read_text(encoding="utf-8") | |
| updated = replace_urls_with_titles(text) | |
| output_path.write_text(updated, encoding="utf-8") | |
| print(f"\n🎉 Done! Updated file saved to: {output_path}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment