diff --git a/scripts/audit-website-links.py b/scripts/audit-website-links.py index 389887090c..9c2b0c1700 100644 --- a/scripts/audit-website-links.py +++ b/scripts/audit-website-links.py @@ -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 + "/")] diff --git a/scripts/test-website.py b/scripts/test-website.py index 096da0ab20..0babffcb6a 100644 --- a/scripts/test-website.py +++ b/scripts/test-website.py @@ -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 = {