Skip to content

Commit

Permalink
Link Checker test enhancement (#1352)
Browse files Browse the repository at this point in the history
Check link in current branch if "main" link fails.
  • Loading branch information
elad-c authored Feb 3, 2025
1 parent 6104903 commit 3ade82d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
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

0 comments on commit 3ade82d

Please sign in to comment.