Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link Checker test enhancement #1352

Merged
merged 8 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/link_checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ name: Check Markdown and doc links

on:
workflow_dispatch: # Allow manual triggers
schedule:
- cron: 0 0 * * *
pull_request:
branches:
- main
push:
branches:
- main

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down
23 changes: 17 additions & 6 deletions tests/doc_tests/test_docs_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import unittest
import subprocess
from shutil import rmtree
from os import walk, getcwd
from os import walk, getcwd, getenv
from os.path import join, isdir, isfile
import requests
import re
Expand All @@ -28,18 +28,29 @@ class TestDocsLinks(unittest.TestCase):
"""

@staticmethod
def check_link(_url):
def check_link(_url, branch_name):
try:
response = requests.get(_url)
if response.status_code == 200:
return True
except Exception as e:
print(f"Error checking link '{_url}': {e}")
return False

try:
_url = _url.replace('/main/', f'/{branch_name}/')
response = requests.get(_url)
if response.status_code == 200:
return True
except Exception as e:
print(f"Error checking link '{_url}': {e}")

return False

def test_readme_and_rst_files(self):
mct_folder = getcwd()
print("MCT folder:", mct_folder)
branch_name = getenv("GITHUB_HEAD_REF")
print("Branch name:", branch_name)
are_links_ok = True
for filepath, _, filenames in walk(mct_folder):
for filename in filenames:
Expand All @@ -61,9 +72,9 @@ def test_readme_and_rst_files(self):
pass
elif _link.startswith('data:image/'):
# A link starting with 'data:image/' is a base64-encoded image --> ignore
print("Inline image, skipping:", _link)
print("Inline image, skipping.")
elif 'http://' in _link or 'https://' in _link:
if self.check_link(_link):
if self.check_link(_link, branch_name):
print("Link ok:", _link)
else:
are_links_ok = False
Expand All @@ -90,7 +101,7 @@ def test_readme_and_rst_files(self):
# This link is checked when generating the docs
pass
elif 'http://' in _link or 'https://' in _link:
if self.check_link(_link):
if self.check_link(_link, branch_name):
print("Link ok:", _link)
else:
are_links_ok = False
Expand Down