diff --git a/src/features/github/create/create.test.ts b/src/features/github/create/create.test.ts index 4f12932fc..2ee06e35a 100644 --- a/src/features/github/create/create.test.ts +++ b/src/features/github/create/create.test.ts @@ -64,6 +64,14 @@ describe("merge direction", () => { defaultBranch: "main", }); expect(d.headRepo).toBe(ghe); + // Its github.com upstream still names the branch to look for an open pull request with, but it is + // no push target for the Enterprise head: creating asks for the branch name and pushes to origin. + const names = ["origin", "fork", "public"]; + expect(trackedHead({ kind: "local", name: "feature" }, tracking, names)).toEqual({ + remote: "public", + name: "feature", + }); + expect(pushTarget(tracking, "feature", ghe.remote, names)).toBeNull(); }); it("splits remote branches by the longest remote name", () => { diff --git a/src/features/github/data/repository.test.ts b/src/features/github/data/repository.test.ts index e970dc4ed..ce66f1bfe 100644 --- a/src/features/github/data/repository.test.ts +++ b/src/features/github/data/repository.test.ts @@ -167,18 +167,52 @@ 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: "git@ghe.example.com:acme/app.git" }, - ], - }; + // A choice saved without a host comes from a release that knew only github.com. + const legacy = async (remotes: Array<{ name: string; url: string }>) => { + layout = { "/w/app": remotes }; 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(); + return useGhRepository.getState().connection?.coordinates; + }; + + it("reads a choice saved without a host as github.com's repository", async () => { + const connected = await legacy([ + { name: "origin", url: "git@ghe.example.com:acme/app.git" }, + { name: "public", url: "https://github.com/acme/app.git" }, + ]); + expect(connected).toMatchObject({ owner: "acme", name: "app", host: "github.com" }); + }); + + it("reads a choice saved without a host as the Enterprise repository when github.com has none", async () => { + const connected = await legacy([ + { name: "origin", url: "git@ghe.example.com:acme/app.git" }, + { name: "public", url: "https://github.com/acme/tools.git" }, + ]); + expect(connected).toMatchObject({ owner: "acme", name: "app", host: "ghe.example.com" }); + }); + + it("asks gh in the repository holding the Enterprise choice when github.com's remotes don't have it", async () => { + layout = { + "/w/tools": [{ name: "origin", url: "https://github.com/acme/tools.git" }], + "/w/app": [{ name: "origin", url: "git@ghe.example.com:acme/app.git" }], + }; + useProject.setState({ project: { root: "/w" } as never }); + localStorage.setItem("openidea.github.repository.v1:/w", "acme/app"); + ghAvailability.mockImplementation((root: string | null) => + root === "/w/app" + ? ready("https://ghe.example.com/acme/app") + : ok({ + kind: "ready", + repo: { nameWithOwner: "acme/tools", url: "https://github.com/acme/tools" }, + login: "al", + }), + ); + ghApi.mockImplementation(answer); + await refreshConnection(); + expect(ghAvailability).toHaveBeenCalledWith("/w/app"); expect(useGhRepository.getState().connection?.coordinates).toMatchObject({ host: "ghe.example.com" }); }); diff --git a/src/features/github/data/repository.ts b/src/features/github/data/repository.ts index 864d35856..946c842de 100644 --- a/src/features/github/data/repository.ts +++ b/src/features/github/data/repository.ts @@ -194,14 +194,23 @@ export async function refreshConnection(): Promise { const found = await projectGitHubRemotes(); if (seq !== checkSeq) return; const current = useGhRepository.getState().connection?.coordinates ?? null; - const saved = parseNameWithOwner(loadSelection(projectRoot())); - // gh resolves its repository in a local one; ask in the one likely to be connected. - const probe = - ( - found.remotes.find((r) => sameRepository(r, current)) ?? - found.remotes.find((r) => sameRepository(r, saved)) ?? - found.remotes[0] - )?.root ?? null; + const stored = parseNameWithOwner(loadSelection(projectRoot())); + // A choice saved without its host comes from a release that knew only github.com: that one first, + // else the repository of that name on the Enterprise host. + const legacy = stored && !stored.host ? { ...stored, host: "github.com" } : null; + const savedIn = (list: readonly GHRemote[]) => + legacy && !list.some((r) => sameRepository(r, legacy)) ? stored : (legacy ?? stored); + // gh resolves its repository in a local one; ask in the one likely to be connected. Before gh + // names the Enterprise host only github.com remotes are known: find an Enterprise repository by + // its remote's path, as `remoteOfRepository` does for SSH host aliases. + const rootWith = (repo: GHRepositoryCoordinates | null) => + repo ? [...found.all].find(([, list]) => remoteOfRepository(list, repo))?.[0] : undefined; + const known = + found.remotes.find((r) => sameRepository(r, current)) ?? + found.remotes.find((r) => sameRepository(r, savedIn(found.remotes))); + const probe = known + ? (known.root ?? null) + : (rootWith(current) ?? rootWith(stored) ?? found.remotes[0]?.root ?? null); try { availability = await call(commands.ghAvailability(probe)); } catch (e) { @@ -233,14 +242,12 @@ export async function refreshConnection(): Promise { 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, chosen)) ?? null); + : (remotes.find((r) => sameRepository(r, current)) ?? + remotes.find((r) => sameRepository(r, savedIn(localRemotes))) ?? + null); // Only a choice among several repositories is remembered. if (target) await connect(target, { remember: remotes.length > 1 }); else disconnect({ forget: false });