Skip to content

Commit 8934e74

Browse files
committed
ci: validate repository links before merge
1 parent 1f41281 commit 8934e74

6 files changed

Lines changed: 248 additions & 0 deletions

File tree

‎.github/actions/check-doc-links/action.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ runs:
5151
echo "prefix=${prefix}" >> "${GITHUB_OUTPUT}"
5252
echo "key=${prefix}${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}" >> "${GITHUB_OUTPUT}"
5353
echo "version=${LYCHEE_VERSION}" >> "${GITHUB_OUTPUT}"
54+
# Validate main-branch source links against the actual checkout. Keep
55+
# GitHub's file/directory views so rendered checks retain UI fragments.
56+
checked_commit="$(git rev-parse HEAD)"
57+
for view in blob tree; do
58+
LYCHEE_ARGS+=" --remap \"^https://github[.]com/NVIDIA/cuda-python/${view}/main/ ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/${view}/${checked_commit}/\""
59+
done
60+
echo "Checking repository source links at ${checked_commit}"
5461
echo "args=${LYCHEE_ARGS}" >> "${GITHUB_OUTPUT}"
5562
5663
- name: Restore successful checks from the nightly baseline

‎.pre-commit-config.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ repos:
115115
- id: lychee
116116
args:
117117
- LYCHEE_VERSION=0.24.2 # Must match the pinned rev above
118+
- --preprocess=./ci/tools/preprocess_repo_links.py
118119
- --cache
119120
- --max-concurrency=4
120121
- --max-retries=3

‎ci/README.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ The pinned checkout action's
2424
[`README`](https://github.com/actions/checkout/blob/3d3c42e5aac5ba805825da76410c181273ba90b1/README.md#fetch-all-history-for-all-tags-and-branches)
2525
documents `fetch-depth: 0` for fetching all history, branches, and tags.
2626

27+
## Repository Links Before Merge
28+
29+
Keep links to the latest repository sources on `main`, even when their targets
30+
are added or moved by the same pull request. Lychee remaps canonical
31+
`https://github.com/NVIDIA/cuda-python/blob/main/` and `tree/main/` links
32+
for validation only; source files and published documentation keep their URLs.
33+
Links to tags, commit SHAs, other branches, and other repositories are checked
34+
as written.
35+
36+
CI remaps these links to the CI repository at the actual checked-out commit
37+
using `--remap`, retaining GitHub's file/directory views and fragment behavior.
38+
The initial check and all retries use the same remaps. Missing targets in that
39+
commit still fail.
40+
41+
Local pre-commit uses `ci/tools/preprocess_repo_links.py` through `--preprocess`
42+
to check these paths in the working tree, including uncommitted and unpushed
43+
targets. Local checks retain their existing policy of not checking fragments.
44+
2745
## Temporary Pixi Lockfile Maintenance Pause
2846

2947
Pixi lockfile freshness enforcement and automated refreshes are temporarily

‎ci/tools/preprocess_repo_links.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
"""Check this repository's main-branch source links against the local checkout."""
6+
7+
from __future__ import annotations
8+
9+
import argparse
10+
import sys
11+
from pathlib import Path
12+
13+
ROOT = Path(__file__).resolve().parents[2]
14+
15+
16+
def preprocess_links(content: str, root: Path = ROOT) -> str:
17+
"""Keep published URLs in source files while checking paths before merge."""
18+
local_prefix = root.resolve().as_uri() + "/"
19+
for view in ("blob", "tree"):
20+
content = content.replace(f"https://github.com/NVIDIA/cuda-python/{view}/main/", local_prefix)
21+
return content
22+
23+
24+
def main() -> None:
25+
parser = argparse.ArgumentParser(description=__doc__)
26+
parser.add_argument("input", type=Path)
27+
args = parser.parse_args()
28+
# Lychee consumes stdout without changing the input file or its relative-link base.
29+
sys.stdout.write(preprocess_links(args.input.read_text(encoding="utf-8")))
30+
31+
32+
if __name__ == "__main__":
33+
main()
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from __future__ import annotations
5+
6+
import shutil
7+
import subprocess
8+
import sys
9+
from pathlib import Path
10+
11+
import pytest
12+
13+
sys.path.insert(0, str(Path(__file__).parent.parent))
14+
from preprocess_repo_links import preprocess_links
15+
16+
SCRIPT = Path(__file__).resolve().parents[1] / "preprocess_repo_links.py"
17+
18+
19+
@pytest.mark.parametrize(
20+
"template",
21+
[
22+
"[Directory]({directory})\n[File]({file})\n[Relative](guide.rst)\n",
23+
"`Directory <{directory}>`_\n`File <{file}>`_\n`Relative <guide.rst>`_\n",
24+
'<a href="{directory}">Directory</a><a href="{file}">File</a><a href="guide.rst">Relative</a>\n',
25+
],
26+
)
27+
@pytest.mark.agent_authored(model="gpt-6.1-sol")
28+
def test_preprocess_links_preserves_format_relative_links_and_url_encoding(tmp_path, template):
29+
root = tmp_path / "checkout space$#"
30+
content = template.format(
31+
directory="https://github.com/NVIDIA/cuda-python/tree/main/new_directory/",
32+
file="https://github.com/NVIDIA/cuda-python/blob/main/docs/a%20guide.md?plain=1#some-heading",
33+
)
34+
35+
assert preprocess_links(content, root) == template.format(
36+
directory=f"{root.as_uri()}/new_directory/",
37+
file=f"{root.as_uri()}/docs/a%20guide.md?plain=1#some-heading",
38+
)
39+
40+
41+
@pytest.mark.parametrize(
42+
"url",
43+
[
44+
"https://github.com/NVIDIA/cuda-python/blob/12.9.x/README.md",
45+
"https://github.com/NVIDIA/cuda-python/tree/v13.0.0/cuda_bindings",
46+
"https://github.com/NVIDIA/cuda-python/blob/0123456789abcdef/README.md",
47+
"https://github.com/NVIDIA/cuda-python/blob/mainly/README.md",
48+
"https://github.com/NVIDIA/cuda-python/tree/main",
49+
"https://github.com/rwgk/cuda-python/blob/main/README.md",
50+
"https://github.com/NVIDIA/other-repository/tree/main/docs",
51+
"http://github.com/NVIDIA/cuda-python/blob/main/README.md",
52+
"https://raw.githubusercontent.com/NVIDIA/cuda-python/main/README.md",
53+
"https://nvidia.github.io/cuda-python/latest/",
54+
],
55+
)
56+
@pytest.mark.agent_authored(model="gpt-6.1-sol")
57+
def test_preprocess_links_leaves_other_targets_unchanged(tmp_path, url):
58+
assert preprocess_links(f"[Link]({url})\n", tmp_path) == f"[Link]({url})\n"
59+
60+
61+
@pytest.mark.agent_authored(model="gpt-6.1-sol")
62+
def test_cli_uses_its_checkout_root_and_leaves_input_unchanged(tmp_path):
63+
checkout = tmp_path / "checkout space$#"
64+
script = checkout / "ci" / "tools" / SCRIPT.name
65+
script.parent.mkdir(parents=True)
66+
shutil.copyfile(SCRIPT, script)
67+
source = tmp_path / "source guide.rst"
68+
content = b"`Repository <https://github.com/NVIDIA/cuda-python/tree/main/new_directory>`_\r\n"
69+
source.write_bytes(content)
70+
71+
result = subprocess.run( # noqa: S603
72+
[sys.executable, str(script), str(source)],
73+
cwd=tmp_path,
74+
check=True,
75+
capture_output=True,
76+
text=True,
77+
)
78+
79+
assert result.stdout == f"`Repository <{checkout.as_uri()}/new_directory>`_\n"
80+
assert result.stderr == ""
81+
assert source.read_bytes() == content
82+
83+
84+
@pytest.mark.agent_authored(model="gpt-6.1-sol")
85+
def test_cli_missing_input_fails_without_success_output(tmp_path):
86+
source = tmp_path / "missing.md"
87+
88+
result = subprocess.run([sys.executable, str(SCRIPT), str(source)], capture_output=True, text=True) # noqa: S603
89+
90+
assert result.returncode != 0
91+
assert result.stdout == ""
92+
assert "FileNotFoundError" in result.stderr
93+
assert not source.exists()
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from __future__ import annotations
5+
6+
import os
7+
import re
8+
import shlex
9+
import shutil
10+
import subprocess
11+
import sys
12+
from pathlib import Path
13+
14+
import pytest
15+
import yaml
16+
17+
ROOT = Path(__file__).resolve().parents[3]
18+
19+
20+
@pytest.mark.agent_authored(model="gpt-6.1-sol")
21+
def test_action_prepares_remaps_for_actual_checkout_commit(tmp_path):
22+
action = yaml.safe_load((ROOT / ".github/actions/check-doc-links/action.yml").read_text(encoding="utf-8"))
23+
prepare = next(step for step in action["runs"]["steps"] if step.get("id") == "inputs")
24+
checkout = tmp_path / "checkout with spaces"
25+
helper = checkout / "ci/tools/prepare_lychee_inputs.py"
26+
helper.parent.mkdir(parents=True)
27+
shutil.copyfile(ROOT / "ci/tools/prepare_lychee_inputs.py", helper)
28+
readme = checkout / "README.md"
29+
readme.write_text("# Smoke test\n", encoding="utf-8")
30+
for arguments in (
31+
["init", "--quiet"],
32+
["add", "README.md"],
33+
[
34+
"-c",
35+
"user.name=Test",
36+
"-c",
37+
38+
"-c",
39+
"core.hooksPath=/dev/null",
40+
"commit",
41+
"--quiet",
42+
"--no-gpg-sign",
43+
"-m",
44+
"Smoke test",
45+
],
46+
):
47+
subprocess.run(["git", *arguments], cwd=checkout, check=True) # noqa: S603, S607
48+
head = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=checkout, text=True).strip() # noqa: S607
49+
output = tmp_path / "action-output"
50+
input_list = checkout / "lychee-authored-files.txt"
51+
env = dict(
52+
os.environ,
53+
PATH=f"{Path(sys.executable).parent}{os.pathsep}{os.environ['PATH']}",
54+
KIND="authored",
55+
GITHUB_WORKSPACE=str(checkout),
56+
RUNNER_TEMP=str(tmp_path / "runner temp"),
57+
LYCHEE_VERSION="v0.24.2",
58+
POLICY_HASH="test-policy",
59+
GITHUB_OUTPUT=str(output),
60+
LYCHEE_ARGS=f'--files-from "{input_list}" --cache',
61+
GITHUB_RUN_ID="123",
62+
GITHUB_RUN_ATTEMPT="1",
63+
GITHUB_SHA="0" * 40,
64+
GITHUB_SERVER_URL="https://github.com",
65+
GITHUB_REPOSITORY="example-org/cuda-python-mirror",
66+
)
67+
68+
subprocess.run( # noqa: S603
69+
["bash", "--noprofile", "--norc", "-euo", "pipefail", "-c", prepare["run"]], # noqa: S607
70+
cwd=checkout,
71+
env=env,
72+
check=True,
73+
capture_output=True,
74+
text=True,
75+
)
76+
77+
outputs = dict(line.split("=", 1) for line in output.read_text(encoding="utf-8").splitlines())
78+
args = shlex.split(outputs["args"])
79+
assert args[:3] == ["--files-from", str(input_list), "--cache"]
80+
remaps = [args[index + 1] for index, argument in enumerate(args) if argument == "--remap"]
81+
assert remaps == [
82+
f"^https://github[.]com/NVIDIA/cuda-python/{view}/main/ "
83+
f"https://github.com/example-org/cuda-python-mirror/{view}/{head}/"
84+
for view in ("blob", "tree")
85+
]
86+
assert input_list.read_text(encoding="utf-8") == f"{readme}\n"
87+
for remap in remaps:
88+
pattern, replacement = remap.split()
89+
view = "blob" if "/blob/" in pattern else "tree"
90+
url = f"https://github.com/NVIDIA/cuda-python/{view}/main/new-directory"
91+
assert re.sub(pattern, replacement, url) == (
92+
f"https://github.com/example-org/cuda-python-mirror/{view}/{head}/new-directory"
93+
)
94+
assert re.search(pattern, url.replace("github.com", "githubXcom")) is None
95+
assert re.search(pattern, url.replace("/main/", "/mainly/")) is None
96+
assert re.search(pattern, "https://example.com/" + url) is None

0 commit comments

Comments
 (0)