feat(github): add in-IDE GitHub account sign-in in Settings, Pull Requests, and Clone (#6642)

Settings › Version Control › GitHub only listed accounts already signed in with gh, Clone Repository › GitHub told signed-out users to run gh auth login in a terminal, and Pull Requests opened a raw terminal tab.

The PTY sign-in dialog and OAuth device-code parser move from features/agents to src/workbench/CliSignInDialog.tsx and src/platform/deviceCode.ts, so agents and GitHub share them without a cross-feature import. GitHub settings get Sign In… / Add account… (selecting a newly added second account for the project) and Install GitHub CLI when gh is missing; the Pull Requests tool window signs in in-IDE and reconnects, with Add account… in its error state, repository selector and account menu; Clone › GitHub gets Sign In… and Add account…. The merge with main carries the dialog's terminal-reply filtering and FIFO input over to the moved file.

Co-authored-by: dengliu <[email protected]>
Цей коміт міститься в:
Deng Liuтаdengliu authored and GitHub committed 2026-11-29 12:29:38 +08:00
1 джерело 90734775b9
коміт 4c5f4fa1e1
25 files changed
+784 -86

No files matched your search

+53 -2
Переглянути файл
@@ -1,19 +1,31 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
vi.mock("@tauri-apps/api/core", async (original) => ({
...(await original<typeof import("@tauri-apps/api/core")>()),
Channel: class {
onmessage?: (e: unknown) => void;
},
}));
const ghAccountRepos = vi.fn();
const toolsList = vi.fn();
const agentGhLoginSpawn = vi.fn();
vi.mock("@/platform/ipc", async (importOriginal) => ({
...(await importOriginal<typeof import("@/platform/ipc")>()),
isTauri: () => true,
commands: {
ghAccountRepos: () => ghAccountRepos(),
toolsList: () => toolsList(),
agentGhLoginSpawn: (cols: number, rows: number, ch: unknown) => agentGhLoginSpawn(cols, rows, ch),
termResize: () => Promise.resolve({ status: "ok", data: null }),
termKill: () => Promise.resolve({ status: "ok", data: null }),
},
}));
import type { GhAccountRepo } from "@/bindings";
import type { AgentLoginEvent, GhAccountRepo } from "@/bindings";
import { registerAction } from "@/platform/actions";
import { provideEmbeddedTerminal } from "@/workbench/contributions";
import { GitHubCloneSource, matchRepos } from "./GitHubCloneSource";
const repo = (nameWithOwner: string, extra: Partial<GhAccountRepo> = {}): GhAccountRepo => ({
@@ -29,8 +41,10 @@ const repos = [repo("me/app", { private: true, description: "The app" }), repo("
afterEach(() => {
cleanup();
provideEmbeddedTerminal(undefined);
ghAccountRepos.mockReset();
toolsList.mockReset();
agentGhLoginSpawn.mockReset();
});
describe("GitHub clone source", () => {
@@ -71,6 +85,43 @@ describe("GitHub clone source", () => {
await waitFor(() => expect(screen.getByRole("option", { name: /me\/app/ })).toBeInTheDocument());
});
it("signs in via the in-IDE GitHub CLI dialog from notLoggedIn and reloads repositories", async () => {
provideEmbeddedTerminal(async () => ({
write: () => {},
clear: () => {},
focus: () => {},
fit: () => {},
dispose: () => {},
}));
let sendEvent: ((e: AgentLoginEvent) => void) | null = null;
agentGhLoginSpawn.mockImplementation(async (_cols, _rows, ch: { onmessage?: (e: AgentLoginEvent) => void }) => {
sendEvent = (event) => ch.onmessage?.(event);
return { status: "ok", data: { id: 31, pid: 444, title: "gh" } };
});
ghAccountRepos.mockResolvedValueOnce({ status: "ok", data: { kind: "notLoggedIn", message: "x" } });
render(<GitHubCloneSource url="" onUrl={() => {}} onClone={() => {}} disabled={false} />);
fireEvent.click(await screen.findByRole("button", { name: "Sign In…" }));
expect(await screen.findByRole("dialog", { name: "Sign In to GitHub CLI" })).toBeInTheDocument();
await waitFor(() => expect(agentGhLoginSpawn).toHaveBeenCalledTimes(1));
act(() => {
sendEvent?.({
type: "output",
data: "! First copy your one-time code: 4FE2-9B7D\r\nOpen https://github.com/login/device\r\n",
});
});
expect(await screen.findByText("4FE2-9B7D")).toBeInTheDocument();
ghAccountRepos.mockResolvedValueOnce({ status: "ok", data: { kind: "ready", login: "me", repos } });
act(() => {
sendEvent?.({ type: "exited", code: 0 });
});
await waitFor(() => expect(screen.getByRole("option", { name: /me\/app/ })).toBeInTheDocument());
expect(screen.getByRole("button", { name: "Add account…" })).toBeInTheDocument();
});
it("installs GitHub CLI in-app when missing and reloads repositories", async () => {
const install = vi.fn().mockResolvedValue(undefined);
const dispose = registerAction({ id: "tools.install", title: "Install", run: install });
+25 -3
Переглянути файл
@@ -17,6 +17,7 @@ import {
listNavigate,
} from "@/ui";
import type { CloneSourceProps } from "@/workbench/contributions";
import { CliSignInDialog } from "@/workbench/CliSignInDialog";
import { openInBrowser } from "../data/local";
import styles from "./GitHubCloneSource.module.css";
import { useRowHeight } from "@/ui/density";
@@ -40,6 +41,7 @@ export function GitHubCloneSource({ url, onUrl, onClone, disabled }: CloneSource
const [attempt, setAttempt] = useState(0);
const [installingGh, setInstallingGh] = useState(false);
const [installError, setInstallError] = useState<string | null>(null);
const [signingIn, setSigningIn] = useState(false);
useEffect(() => {
let alive = true;
@@ -76,6 +78,9 @@ export function GitHubCloneSource({ url, onUrl, onClone, disabled }: CloneSource
<T>Retry</T>
</Button>
);
const signInDialog = signingIn ? (
<CliSignInDialog target={{ kind: "gh" }} onClose={() => setSigningIn(false)} onCompleted={reload} />
) : null;
if (!state)
return (
@@ -114,9 +119,22 @@ export function GitHubCloneSource({ url, onUrl, onClone, disabled }: CloneSource
);
if (state.kind === "notLoggedIn")
return (
<EmptyState title="Not logged in to GitHub" actions={retry}>
<T>Run</T> <code>gh auth login</code> <T>in a terminal, then retry.</T>
</EmptyState>
<>
<EmptyState
title="Not logged in to GitHub"
actions={
<>
<Button size="small" variant="primary" onClick={() => setSigningIn(true)}>
<T>Sign In…</T>
</Button>
{retry}
</>
}
>
<T>Sign in with GitHub CLI to list and clone your repositories.</T>
</EmptyState>
{signInDialog}
</>
);
if (state.kind === "error")
return (
@@ -157,6 +175,9 @@ export function GitHubCloneSource({ url, onUrl, onClone, disabled }: CloneSource
disabled={disabled}
data-autofocus
/>
<Button size="small" disabled={disabled} onClick={() => setSigningIn(true)}>
<T>Add account…</T>
</Button>
</div>
<div className={styles.list}>
<List<Row>
@@ -197,6 +218,7 @@ export function GitHubCloneSource({ url, onUrl, onClone, disabled }: CloneSource
)}
/>
</div>
{signInDialog}
</div>
);
}