fix(github): keep a new pull request's head on the base repository's host (#6746)

Follow-up to #6742. The new pull request form's head repository picker keyed its options by owner/name, so same-named repositories on an Enterprise server and on github.com could not both be picked, and it offered remotes on another host. It now lists only remotes on the base repository's host, keyed by host/owner/name, and the default head ignores a tracked remote on another host. A selection saved before #6742 (no host) now resolves to the origin remote's repository when that is one of the matches, instead of depending on remote order.

Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
LU XIANGRUIтаwildcatDownstairs authored and GitHub committed 2026-11-28 23:04:41 +08:00
1 джерело 836a4405d6
коміт 9aa0718cf9
6 files changed
+79 -11

No files matched your search

+10 -7
Переглянути файл
@@ -7,11 +7,12 @@
import { useMemo, useState, type KeyboardEvent } from "react";
import { HighlightedText, Icon, List, Popup, SearchField, Select, T, fuzzyMatch, listNavigate } from "@/ui";
import { useTx } from "@/platform/i18n";
import { NO_REMOTES, nameWithOwner, sameRepository, useGhRepository, type GHRemote } from "../data/repository";
import { NO_REMOTES, nameWithOwner, repositoryKey, useGhRepository, type GHRemote } from "../data/repository";
import { ChooserPopup } from "../ui/ChooserPopup";
import {
baseText,
headBranchName,
headRepositories,
headText,
localBranchesOf,
remoteBranchesOf,
@@ -78,6 +79,7 @@ export function DirectionSelector() {
{open?.which === "head" && (
<HeadPopup
anchor={open.anchor}
base={direction.baseRepo}
initialRepo={direction.headRepo ?? direction.baseRepo}
onClose={() => setOpen(null)}
/>
@@ -89,9 +91,10 @@ export function DirectionSelector() {
type HeadRow = { section: string } | { branch: HeadBranch; text: string; match: number[] | null };
/** The head: the repository (several GitHub remotes only), then its branches, local first. */
function HeadPopup(props: { anchor: HTMLElement; initialRepo: GHRemote; onClose: () => void }) {
function HeadPopup(props: { anchor: HTMLElement; base: GHRemote; initialRepo: GHRemote; onClose: () => void }) {
const tx = useTx();
const remotes = useGhRepository((s) => s.connection?.remotes ?? NO_REMOTES);
const connected = useGhRepository((s) => s.connection?.remotes ?? NO_REMOTES);
const remotes = useMemo(() => headRepositories(connected, props.base), [connected, props.base]);
const branches = useCreatePr((s) => s.branches);
const current = useCreatePr((s) => s.direction?.headBranch ?? null);
const [repo, setRepo] = useState(props.initialRepo);
@@ -144,11 +147,11 @@ function HeadPopup(props: { anchor: HTMLElement; initialRepo: GHRemote; onClose:
<T>Repository:</T>
</span>
<Select
value={nameWithOwner(repo)}
options={remotes.map((r) => ({ value: nameWithOwner(r), label: `${nameWithOwner(r)} (${r.remote})` }))}
value={repositoryKey(repo)}
options={remotes.map((r) => ({ value: repositoryKey(r), label: `${nameWithOwner(r)} (${r.remote})` }))}
onChange={(v) => {
const next = remotes.find((r) => nameWithOwner(r) === v);
if (next && !sameRepository(next, repo)) setRepo(next);
const next = remotes.find((r) => repositoryKey(r) === v);
if (next && repositoryKey(next) !== repositoryKey(repo)) setRepo(next);
}}
aria-label="Head repository"
/>
+18
Переглянути файл
@@ -10,6 +10,7 @@ import {
defaultBaseBranch,
defaultDirection,
generateTitle,
headRepositories,
headRefName,
headText,
needsPush,
@@ -48,6 +49,23 @@ const branches = [
];
describe("merge direction", () => {
it("offers heads only on the base repository's host", () => {
const ghe: GHRemote = { owner: "acme", name: "app", host: "ghe.example.com", remote: "origin", url: "" };
const gheFork: GHRemote = { owner: "al", name: "app", host: "ghe.example.com", remote: "fork", url: "" };
const dotcom: GHRemote = { owner: "acme", name: "app", host: "github.com", remote: "public", url: "" };
expect(headRepositories([ghe, dotcom, gheFork], ghe)).toEqual([ghe, gheFork]);
// The current branch tracks the github.com remote: the head stays on the Enterprise base.
const tracking = [branch("feature", { current: true, upstream: "public/feature" }), remote("public/feature")];
const d = defaultDirection({
baseRepo: ghe,
remotes: [ghe, dotcom, gheFork],
branches: tracking,
currentBranch: "feature",
defaultBranch: "main",
});
expect(d.headRepo).toBe(ghe);
});
it("splits remote branches by the longest remote name", () => {
expect(splitRemoteBranch("origin/feat/x", ["origin"])).toEqual({ remote: "origin", name: "feat/x" });
expect(splitRemoteBranch("my/fork/x", ["my", "my/fork"])).toEqual({ remote: "my/fork", name: "x" });
+8 -1
Переглянути файл
@@ -100,6 +100,12 @@ export function trackedHead(
return head.kind === "remote" ? head : upstreamOf(branches, head.name, remotes);
}
/** The repositories a head can be in: the remotes on the base repository's host (forks there included). */
export function headRepositories(remotes: readonly GHRemote[], base: GHRemote): GHRemote[] {
const host = (r: GHRemote) => (r.host ?? "github.com").toLowerCase();
return remotes.filter((r) => host(r) === host(base));
}
/**
* The default direction: base the default branch of the connected repository's remote,
* head the current branch in the repository of its tracked remote (else the base one).
@@ -114,7 +120,8 @@ export function defaultDirection(opts: {
const { baseRepo, remotes, branches, currentBranch } = opts;
const names = remotes.map((r) => r.remote);
const tracked = currentBranch ? upstreamOf(branches, currentBranch, names) : null;
const headRepo = (tracked && remotes.find((r) => r.remote === tracked.remote)) || baseRepo;
const heads = headRepositories(remotes, baseRepo);
const headRepo = (tracked && heads.find((r) => r.remote === tracked.remote)) || baseRepo;
return {
baseRepo,
baseBranch: defaultBaseBranch(branches, baseRepo.remote, opts.defaultBranch),
+7
Переглянути файл
@@ -434,6 +434,13 @@ describe("pull request helpers", () => {
])
expect(parseGitHubRemote(url, hosts), url).toEqual({ owner: "acme", name: "app", host: "ghe.example.com" });
expect(parseGitHubRemote("https://ghe.example.com/acme/app.git")).toBeNull();
// gh names a server on another port `host:port`; its remotes match whatever port they use.
for (const url of ["https://ghe.example.com:8443/acme/app.git", "[email protected]:acme/app.git"])
expect(parseGitHubRemote(url, ["ghe.example.com:8443"]), url).toEqual({
owner: "acme",
name: "app",
host: "ghe.example.com:8443",
});
expect(parseGitHubRemote("https://gheXexample.com/acme/app.git", hosts)).toBeNull();
expect(
githubRemotes(
+31 -2
Переглянути файл
@@ -27,7 +27,15 @@ vi.mock("@/platform/ipc", () => ({
import { useProject } from "@/platform/project";
import { rest } from "../api/client";
import { connectedRoot, connectedRemotes, connect, disconnect, refreshConnection, useGhRepository } from "./repository";
import {
connectedRoot,
connectedRemotes,
connect,
disconnect,
refreshConnection,
resetConnection,
useGhRepository,
} from "./repository";
afterEach(() => {
ghApi.mockReset();
@@ -35,6 +43,7 @@ afterEach(() => {
localStorage.clear();
layout = FOLDER;
useProject.setState({ project: null });
resetConnection();
});
describe("a folder holding several git repositories", () => {
@@ -158,16 +167,36 @@ describe("GitHub Enterprise Server hosts beside github.com", () => {
expect(ghApi).toHaveBeenLastCalledWith("/w/app", expect.objectContaining({ host: "ghe.example.com" }));
});
it("reads a choice saved without a host as the origin remote's repository", async () => {
layout = {
"/w/app": [
{ name: "public", url: "https://github.com/acme/app.git" },
{ name: "origin", url: "[email protected]:acme/app.git" },
],
};
useProject.setState({ project: { root: "/w" } as never });
localStorage.setItem("openidea.github.repository.v1:/w", "acme/app");
ghAvailability.mockReturnValue(ready("https://ghe.example.com/acme/app"));
ghApi.mockImplementation(answer);
await refreshConnection();
expect(useGhRepository.getState().connection?.coordinates).toMatchObject({ host: "ghe.example.com" });
});
it("recognises SSH remotes of a server whose web address has a port", async () => {
layout = {
"/w/app": [
{ name: "origin", url: "[email protected]:acme/app.git" },
{ name: "fork", url: "ssh://[email protected]/al/app.git" },
{ name: "mirror", url: "https://ghe.example.com:8443/acme/app-mirror.git" },
],
};
ghAvailability.mockReturnValue(ready("https://ghe.example.com:8443/acme/app"));
ghApi.mockImplementation(answer);
await refreshConnection();
expect(remotesOf()).toEqual(["origin=ghe.example.com:8443/acme/app", "fork=ghe.example.com:8443/al/app"]);
expect(remotesOf()).toEqual([
"origin=ghe.example.com:8443/acme/app",
"fork=ghe.example.com:8443/al/app",
"mirror=ghe.example.com:8443/acme/app-mirror",
]);
});
});
+5 -1
Переглянути файл
@@ -233,10 +233,14 @@ export async function refreshConnection(): Promise<void> {
disconnect({ forget: false });
return;
}
// A choice saved without its host (before hosts were recorded): the origin remote's, when it is one of them.
const chosen = saved?.host
? saved
: (remotes.find((r) => r.remote === "origin" && sameRepository(r, saved)) ?? saved);
const target =
remotes.length === 1
? remotes[0]!
: (remotes.find((r) => sameRepository(r, current)) ?? remotes.find((r) => sameRepository(r, saved)) ?? null);
: (remotes.find((r) => sameRepository(r, current)) ?? remotes.find((r) => sameRepository(r, chosen)) ?? null);
// Only a choice among several repositories is remembered.
if (target) await connect(target, { remember: remotes.length > 1 });
else disconnect({ forget: false });