From f5e0d7aec0f3318c44d64ec6c4b5cf42af19e05c Mon Sep 17 00:00:00 2001 From: fullsend-code <278716306+fullsend-ai-coder[bot]@users.noreply.github.com> Date: Thu, 3 Dec 2026 14:54:46 +0000 Subject: [PATCH] fix(#71): build cross-fork compare commit URLs from their repository A cross-fork compare reads the fork's objects in place, without copying them into the base repository. Its files' URLs already named the fork, but the commit objects (commits, base_commit, merge_base_commit) built every URL from the base repository, so following url, commit.url, commit.tree.url or a parent URL of a fork-only commit answered 404. _build_compare now takes the base side's fork as well as the head side's, and builds commits' URLs from the head side's repository and base_commit's and merge_base_commit's from the base side's (the merge base is an ancestor of the base commit). Comparisons with no fork side are unchanged. This is option 1 from the issue, as the maintainer chose. The new test compares with the fork on the head side and then on the base side, and follows each commit's URLs as the comparing user. The bug is recorded in docs/bugs/fixed and indexed in PLAN.md. Note: pre-commit could not fetch its hook repositories in the sandbox (TLS verification failure). ruff-check v0.17.3 was run directly and passed; whitespace, end-of-file and secret scans also passed. In the full suite, two unrelated tests fail in the sandbox: test_runner_shutdown (fails on main too) and test_security_headers (no built frontend/dist). Closes #71 Co-Authored-By: Claude Opus 5.5 --- PLAN.md | 1 + ...re-commit-urls-name-the-base-repository.md | 46 ++++++++++++++++ src/app/api/commits.py | 26 +++++---- tests/test_compare_and_label_edges.py | 54 +++++++++++++++++++ 4 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 docs/bugs/fixed/cross-fork-compare-commit-urls-name-the-base-repository.md diff --git a/PLAN.md b/PLAN.md index 0f10112a..cdefa0a8 100644 --- a/PLAN.md +++ b/PLAN.md @@ -193,6 +193,7 @@ Actions visibility and runner evaluation. - `docs/bugs/fixed/runner-end-report-of-a-deleted-job-answers-500.md` - `docs/bugs/fixed/ci-stack-tls-ca-rejected-by-python-3-13.md` - `docs/bugs/fixed/ci-stack-start-gives-up-after-30-seconds.md` +- `docs/bugs/fixed/cross-fork-compare-commit-urls-name-the-base-repository.md` ## Decisions diff --git a/docs/bugs/fixed/cross-fork-compare-commit-urls-name-the-base-repository.md b/docs/bugs/fixed/cross-fork-compare-commit-urls-name-the-base-repository.md new file mode 100644 index 00000000..4998b229 --- /dev/null +++ b/docs/bugs/fixed/cross-fork-compare-commit-urls-name-the-base-repository.md @@ -0,0 +1,46 @@ +# Bug: cross-fork compare commit URLs name the base repository + +Status: fixed 2026-12-03. Issue #71, part of #39, found in the review of #32. + +## Summary + +A cross-fork compare (`compare/main...owner:branch`) runs in a temporary view +that borrows the base's and the fork's objects +(`cross_repo_pulls.object_view`) and copies nothing into the base. Its +`files` URLs already named the fork, which holds the head commit. + +The commit objects in the same response (`commits`, `base_commit`, +`merge_base_commit`) still built every URL from the base repository: `url`, +`html_url`, `comments_url`, `commit.url`, `commit.tree.url` and the parent +URLs. A commit that exists only in the fork is not in the base, so following +one of those URLs answered 404. + +On GitHub the fork network shares one object store, so the base-repository +URL shape resolves there. The emulator keeps each repository's objects +apart. + +## Fix + +`_build_compare` in `src/app/api/commits.py` builds each commit's URLs from +the repository that holds it, as `files` already did: + +- `commits` (and `files`) use the head side's repository: the head fork when + the head side is one, otherwise the base repository. +- `base_commit` uses the base side's repository: the base fork when the base + side is one. `merge_base_commit` is an ancestor of the base commit, so it + uses the same repository. + +A comparison with no fork side is unchanged. + +Making `get_commit` and the git commit and tree routes resolve a fork-only +commit through the network, which would keep GitHub's base-repository URL +shape, is left as a possible follow-up. + +## Tests + +- `tests/test_compare_and_label_edges.py` + (`test_cross_fork_commit_urls_lead_to_the_repository_that_holds_them`): + with the fork on the head side and then on the base side, each commit's + URLs name the repository that holds it, and following `url`, `commit.url`, + `commit.tree.url` and the parent URLs of `commits`, `base_commit` and + `merge_base_commit` answers 200 for the comparing user. diff --git a/src/app/api/commits.py b/src/app/api/commits.py index 5eb87c28..350db4ee 100644 --- a/src/app/api/commits.py +++ b/src/app/api/commits.py @@ -293,7 +293,10 @@ async def compare_commits( if not base_sha or not head_sha: raise HTTPException(status_code=404, detail="Not Found") - args = (repository, owner, repo, basehead, base_sha, head_sha, head_fork, page, per_page, request) + args = ( + repository, owner, repo, basehead, base_sha, head_sha, base_fork, head_fork, + page, per_page, request, + ) forks = [fork for fork in (base_fork, head_fork) if fork is not None] if not forks: return await _build_compare(repository.disk_path, *args) @@ -306,14 +309,17 @@ async def compare_commits( async def _build_compare( git_dir: str, repository, owner: str, repo: str, basehead: str, base_sha: str, head_sha: str, - head_fork, page: int | None, per_page: int | None, request: Request, + base_fork, head_fork, page: int | None, per_page: int | None, request: Request, ): """The compare response, with every git read made in ``git_dir``. - The files' URLs point at the repository that holds the head commit: the - head fork when the head side is one, since its commits are not in the - base repository. + Each commit's and file's URLs point at the repository that holds it, since + a fork's commits are not in the base repository: ``commits`` and ``files`` + at the head side's repository (the head fork when the head side is one), + and ``base_commit`` and ``merge_base_commit`` at the base side's. """ + base_owner, base_repo = base_fork.full_name.split("/", 1) if base_fork is not None else (owner, repo) + head_owner, head_repo = head_fork.full_name.split("/", 1) if head_fork is not None else (owner, repo) merge_base = await get_merge_base(git_dir, base_sha, head_sha) if not merge_base: @@ -332,10 +338,11 @@ async def _build_compare( status = "identical" verified = bool(getattr(request.state, "is_installation_token", False)) - base_commit = (await _commit_objects(git_dir, owner, repo, [base_sha], verified))[0] + base_commit = (await _commit_objects(git_dir, base_owner, base_repo, [base_sha], verified))[0] + # The merge base is an ancestor of the base commit, so it is in the same repository. merge_base_commit = ( base_commit if merge_base == base_sha - else (await _commit_objects(git_dir, owner, repo, [merge_base], verified))[0] + else (await _commit_objects(git_dir, base_owner, base_repo, [merge_base], verified))[0] ) commit_shas = ( @@ -353,13 +360,12 @@ async def _build_compare( link = pagination_links(request, page or 1, size, len(commit_shas)) commit_shas = commit_shas[start:start + size] first_page = (page or 1) == 1 - commits = await _commit_objects(git_dir, owner, repo, commit_shas, verified) + commits = await _commit_objects(git_dir, head_owner, head_repo, commit_shas, verified) files = [] if first_page: - file_owner, file_repo = head_fork.full_name.split("/", 1) if head_fork is not None else (owner, repo) files = _with_file_urls( - file_owner, file_repo, head_sha, + head_owner, head_repo, head_sha, await get_compare_files(git_dir, base_sha, head_sha), ) diff --git a/tests/test_compare_and_label_edges.py b/tests/test_compare_and_label_edges.py index 19a5d1ab..d2db4e80 100644 --- a/tests/test_compare_and_label_edges.py +++ b/tests/test_compare_and_label_edges.py @@ -374,6 +374,60 @@ async def test_cross_fork_file_urls_lead_to_the_head_commit(client, db_session, assert base64.b64decode(followed.json()["content"]) == b"from the fork\n" +def _commit_urls(commit): + """The API URLs on a compare commit object that the emulator serves + (it has no commit comments route, so ``comments_url`` is checked apart).""" + return [ + commit["url"], commit["commit"]["url"], commit["commit"]["tree"]["url"], + *(parent["url"] for parent in commit["parents"]), + ] + + +async def test_cross_fork_commit_urls_lead_to_the_repository_that_holds_them( + client, db_session, test_token, admin_token, +): + """A fork-only commit is not in the base repository, so each commit's URLs + point at the repository that holds it, and following them works for the + caller, whichever side the fork is on.""" + admin = auth_headers(admin_token) + await client.post(f"{API}/user/repos", json={"name": "commit-links", "auto_init": True}, headers=auth_headers(test_token)) + assert (await client.post(f"{API}/repos/testuser/commit-links/forks", json={}, headers=admin)).status_code == 202 + main = await _main_sha(client, admin_token, "admin/commit-links") + await _branch(client, admin_token, "admin/commit-links", "work", main) + head = await _put(client, admin_token, "admin/commit-links", "notes.txt", "from the fork\n", "work") + assert (await client.patch(f"{API}/repos/admin/commit-links", json={"private": True}, headers=admin)).status_code == 200 + + async def follow(urls): + for url in urls: + followed = await client.get(f"{API}{url.split('/api/v3', 1)[1]}", headers=admin) + assert followed.status_code == 200, (url, followed.text) + + # The fork is the head side: its commits are in the fork. + resp = await client.get(f"{API}/repos/testuser/commit-links/compare/main...admin:work", headers=admin) + + assert resp.status_code == 200, resp.text + body = resp.json() + (commit,) = body["commits"] + assert commit["sha"] == head + assert all("/repos/admin/commit-links/" in url for url in _commit_urls(commit)) + assert "/admin/commit-links/commit/" in commit["html_url"] + assert "/repos/admin/commit-links/" in commit["comments_url"] + assert all("/repos/testuser/commit-links/" in url for url in _commit_urls(body["base_commit"])) + await follow(_commit_urls(commit)) + await follow(_commit_urls(body["base_commit"]) + _commit_urls(body["merge_base_commit"])) + + # The fork is the base side: the base and merge base commits are in the fork. + resp = await client.get(f"{API}/repos/testuser/commit-links/compare/admin:work...main", headers=admin) + + assert resp.status_code == 200, resp.text + body = resp.json() + assert body["base_commit"]["sha"] == head + assert all("/repos/admin/commit-links/" in url for url in _commit_urls(body["base_commit"])) + assert "/admin/commit-links/commit/" in body["base_commit"]["html_url"] + assert "/repos/admin/commit-links/" in body["base_commit"]["comments_url"] + await follow(_commit_urls(body["base_commit"]) + _commit_urls(body["merge_base_commit"])) + + async def test_an_object_view_leaves_out_a_path_with_a_line_break(tmp_path): from app.services import cross_repo_pulls