fix(website): preserve cached GitHub refs in link audits (#12427)

Co-authored-by: ShuaixinHuang <ShuaixinHuang@users.noreply.github.com>
This commit is contained in:
ShuaixinHuangandShuaixinHuang authored and GitHub committed 2026-11-19 12:17:05 -05:00
1 parent 5e8a451e1c
commit 0c191d8b2c
2 files changed
+21 -2

No files matched your search

+3 -2
View File
@@ -41,8 +41,9 @@ def github_target(url):
ref = parts[3]
# GitHub uses the longest matching branch name before the source path.
if not re.fullmatch(r"[0-9a-f]{40}", ref):
references = github_api(f"{repo}/git/matching-refs/heads/{quote(ref, safe='')}")
references += github_api(f"{repo}/git/matching-refs/tags/{quote(ref, safe='')}")
heads = github_api(f"{repo}/git/matching-refs/heads/{quote(ref, safe='')}")
tags = github_api(f"{repo}/git/matching-refs/tags/{quote(ref, safe='')}")
references = (*heads, *tags)
suffix = "/".join(parts[3:])
matches = [item["ref"].split("/", 2)[2] for item in references]
matches = [name for name in matches if suffix == name or suffix.startswith(name + "/")]
+18
View File
@@ -121,6 +121,24 @@ class WebsiteLinkAuditChecks(unittest.TestCase):
self.assertEqual(report["status"], "ok")
self.assertEqual(report["endpoint"], endpoint)
def test_repeated_link_checks_do_not_mutate_cached_github_refs(self):
heads = [{"ref": "refs/heads/topic"}]
tags = [{"ref": "refs/tags/topic/nested"}]
ref_endpoint = "repos/example/project/git/matching-refs/"
responses = {
ref_endpoint + "heads/topic": heads,
ref_endpoint + "tags/topic": tags,
}
with patch.object(AUDIT, "github_api", side_effect=responses.__getitem__):
for file in ("README.md", "guide.md", "examples/a.md"):
url = urlsplit(f"https://github.com/example/project/blob/topic/nested/{file}")
self.assertEqual(
AUDIT.github_target(url),
f"repos/example/project/contents/{file}?ref=topic%2Fnested",
)
self.assertEqual(heads, [{"ref": "refs/heads/topic"}])
self.assertEqual(tags, [{"ref": "refs/tags/topic/nested"}])
def test_plain_refs_and_pinned_commits_keep_their_contents_target(self):
for ref in ("main", "a" * 40):
responses = {