Skip to content

Commit 1b951bf

Browse files
committed
Only run linkcheck for changed source files in PRs
1 parent a89d857 commit 1b951bf

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

.github/workflows/sphinx.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,28 @@ jobs:
1111
steps:
1212
- name: Clone repository
1313
uses: actions/checkout@v6
14+
with:
15+
fetch-depth: ${{ github.event_name == 'pull_request' && 0 || 1 }}
1416
- name: Set up Python
1517
uses: actions/setup-python@v6
1618
with:
1719
python-version: '3.13'
20+
- name: Get changed source files
21+
id: changed
22+
if: github.event_name == 'pull_request'
23+
env:
24+
GITHUB_EVENT_NAME: ${{ github.event_name }}
25+
PR_BASE_SHA: ${{ github.event.pull_request.base.sha }}
26+
PUSH_BEFORE_SHA: ${{ github.event.before }}
27+
run: python tools/list_changed_source_files.py
1828
- name: Install Python dependencies
29+
if: github.event_name == 'push' || steps.changed.outputs.files != ''
1930
run: |
2031
python -m pip install --upgrade pip
2132
python -m pip install -r requirements.txt
2233
- name: Check for broken links
23-
run: sphinx-build -q --color -b linkcheck source build
34+
if: github.event_name == 'push' || steps.changed.outputs.files != ''
35+
run: sphinx-build -q --color -b linkcheck source build ${{ steps.changed.outputs.files }}
2436

2537
build:
2638
name: Build Check

tools/list_changed_source_files.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
import subprocess
3+
import sys
4+
5+
SOURCE_SUFFIXES = {".rst", ".md"}
6+
7+
8+
def get_changed_source_files() -> list[str]:
9+
event_name = os.environ.get("GITHUB_EVENT_NAME", "")
10+
if event_name != "pull_request":
11+
return []
12+
13+
baseCommitSHA = os.environ.get("PR_BASE_SHA", "")
14+
if not baseCommitSHA or baseCommitSHA == "0" * 40:
15+
return []
16+
17+
filesDifferFromBaseCommit = subprocess.run(
18+
# "source/" matches all subdirectories recursively
19+
["git", "diff", "--name-only", baseCommitSHA, "HEAD", "--", "source/"],
20+
capture_output=True,
21+
text=True,
22+
check=True,
23+
)
24+
25+
return [
26+
line.removeprefix("source/")
27+
for line in filesDifferFromBaseCommit.stdout.splitlines()
28+
if any(line.endswith(suffix) for suffix in SOURCE_SUFFIXES)
29+
]
30+
31+
32+
def main() -> None:
33+
files = get_changed_source_files()
34+
github_output = os.environ.get("GITHUB_OUTPUT")
35+
36+
if github_output:
37+
with open(github_output, "a", encoding="utf-8") as f:
38+
f.write(f"files={' '.join(files)}\n")
39+
40+
if files:
41+
print(f"Changed source files: {files}")
42+
else:
43+
print("No source files changed.")
44+
45+
sys.exit(0)
46+
47+
48+
if __name__ == "__main__":
49+
main()

0 commit comments

Comments
 (0)