Skip to content

Commit bd157ed

Browse files
committed
Check new links against linkcheck baseline
1 parent a9b1f5e commit bd157ed

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,55 @@ jobs:
5454
run: |
5555
python -m pip install --upgrade nox virtualenv
5656
57+
- name: Restore linkcheck baseline
58+
id: restore-linkcheck-baseline
59+
if: matrix.noxenv == 'linkcheck'
60+
uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4
61+
with:
62+
path: .linkcheck-cache/output.json
63+
key: linkcheck-output-${{ github.event.repository.default_branch }}-${{ github.sha }}
64+
restore-keys: |
65+
linkcheck-output-${{ github.event.repository.default_branch }}-
66+
5767
- name: Nox ${{ matrix.noxenv }}
5868
env:
5969
# Authenticate github.com requests during linkcheck to avoid rate limits.
6070
GITHUB_TOKEN: ${{ matrix.noxenv == 'linkcheck' && github.token || '' }}
71+
LINKCHECK_BASELINE_JSON: >-
72+
${{
73+
matrix.noxenv == 'linkcheck' &&
74+
github.event_name == 'pull_request' &&
75+
'.linkcheck-cache/output.json' ||
76+
''
77+
}}
6178
run: |
6279
python -m nox -s ${{ matrix.noxenv }}
6380
81+
- name: Store linkcheck baseline
82+
if: >-
83+
${{
84+
matrix.noxenv == 'linkcheck' &&
85+
github.event_name != 'pull_request' &&
86+
github.ref_name == github.event.repository.default_branch &&
87+
hashFiles('build/output.json') != ''
88+
}}
89+
run: |
90+
mkdir -p .linkcheck-cache
91+
cp build/output.json .linkcheck-cache/output.json
92+
93+
- name: Save linkcheck baseline
94+
if: >-
95+
${{
96+
matrix.noxenv == 'linkcheck' &&
97+
github.event_name != 'pull_request' &&
98+
github.ref_name == github.event.repository.default_branch &&
99+
hashFiles('.linkcheck-cache/output.json') != ''
100+
}}
101+
uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4
102+
with:
103+
path: .linkcheck-cache/output.json
104+
key: linkcheck-output-${{ github.event.repository.default_branch }}-${{ github.sha }}
105+
64106

65107
check:
66108
# This job does nothing and is only used for the branch protection

‎source/conf.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# -- Project information ---------------------------------------------------------------
22
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
33

4+
import json
45
import os
56
import pathlib
7+
import re
68
import sys
79

810
_ROOT = pathlib.Path(__file__).resolve().parent.parent
@@ -163,6 +165,30 @@
163165
# Temporarily ignored due to expired TLS cert.
164166
r"https://kivy.org/.*",
165167
]
168+
169+
170+
def _load_linkcheck_baseline(path):
171+
baseline_path = pathlib.Path(path)
172+
if not baseline_path.is_file():
173+
return []
174+
175+
patterns = set()
176+
for line in baseline_path.read_text(encoding="utf-8").splitlines():
177+
try:
178+
output = json.loads(line)
179+
except json.JSONDecodeError:
180+
continue
181+
182+
uri = output.get("uri")
183+
if isinstance(uri, str) and uri:
184+
patterns.add(rf"^{re.escape(uri)}$")
185+
186+
return sorted(patterns)
187+
188+
189+
if _baseline := os.getenv("LINKCHECK_BASELINE_JSON"):
190+
linkcheck_ignore.extend(_load_linkcheck_baseline(_baseline))
191+
166192
linkcheck_retries = 2
167193
linkcheck_timeout = 30
168194
# Ignore anchors for common targets when we know they likely won't be found

0 commit comments

Comments
 (0)