From d846a6ed621fe86c53cb2b918ed6b6ff0a17c314 Mon Sep 17 00:00:00 2001 From: Matt Bernier Date: Fri, 14 Feb 2025 16:09:41 -0700 Subject: [PATCH 1/2] Create markdown-link-check.yaml Added a yaml to manage a forthcoming markdown link checker script --- .github/workflows/markdown-link-check.yaml | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .github/workflows/markdown-link-check.yaml diff --git a/.github/workflows/markdown-link-check.yaml b/.github/workflows/markdown-link-check.yaml new file mode 100644 index 0000000..e6b1e0d --- /dev/null +++ b/.github/workflows/markdown-link-check.yaml @@ -0,0 +1,27 @@ +name: Markdown Link Check + +on: + pull_request: + branches: + - main + - master # Adjust based on your default branch + +jobs: + link-check: + name: Check Markdown Links + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Set Up Python + uses: actions/setup-python@v4 + with: + python-version: "3.9" + + - name: Install Dependencies + run: pip install requests + + - name: Run Markdown Link Checker + run: python .github/hooks/pre-commit From a18e0b335de94b205d8256dc25ca515270f192d4 Mon Sep 17 00:00:00 2001 From: Matt Bernier Date: Fri, 14 Feb 2025 16:11:54 -0700 Subject: [PATCH 2/2] Create pre-commit Checks links on commit and PR merge --- .github/hooks/pre-commit | 55 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 .github/hooks/pre-commit diff --git a/.github/hooks/pre-commit b/.github/hooks/pre-commit new file mode 100644 index 0000000..deb7f5d --- /dev/null +++ b/.github/hooks/pre-commit @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +import re +import requests +import subprocess +import sys +import os + +def check_links(file_path): + with open(file_path, 'r') as file: + content = file.read() + + # Regex to find Markdown links + markdown_link_regex = r'\[([^\]]+)\]\((http[s]?://[^)]+)\)' + links = re.findall(markdown_link_regex, content) + + broken_links = [] + for label, url in links: + try: + response = requests.head(url, allow_redirects=True, timeout=10) + if response.status_code >= 400: + broken_links.append((label, url)) + except requests.RequestException: + broken_links.append((label, url)) + + return broken_links + +def get_markdown_files(): + """Returns a list of Markdown files to check in both local and CI/CD environments.""" + if os.getenv("GITHUB_ACTIONS"): # Running in GitHub Actions + result = subprocess.run( + ["git", "ls-files", "*.md"], capture_output=True, text=True + ) + return result.stdout.split() + else: # Running as a pre-commit hook locally + result = subprocess.run( + ["git", "diff", "--cached", "--name-only"], capture_output=True, text=True + ) + return [f for f in result.stdout.split() if f.endswith('.md')] + +def main(): + markdown_files = get_markdown_files() + + exit_code = 0 + for file_path in markdown_files: + broken_links = check_links(file_path) + if broken_links: + print(f"❌ Error: Broken links found in {file_path}:") + for label, url in broken_links: + print(f" - [{label}]({url})") + exit_code = 1 + + sys.exit(exit_code) + +if __name__ == "__main__": + main()