diff --git a/PLAN.md b/PLAN.md index 0f10112..cdefa0a 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 0000000..4998b22 --- /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 5eb87c2..350db4e 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 19a5d1a..d2db4e8 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