Файли
openidea/src/features/github/clone/GitHubCloneSource.tsx
T
Deng Liuтаdengliu cea9d863f9 feat(github): install the GitHub CLI as a managed tool (#6271)
Registers the GitHub CLI as a managed tool (cli/cli release archives for
macOS / Linux / Windows on arm64 and x64, verified against the release's
checksums file), and offers Install GitHub CLI next to Download manually in
Pull Requests and Clone Repository > GitHub when gh is missing. An existing gh
(OPENIDEA_GH, PATH, Homebrew, Program Files, Snap) counts as installed, so no
duplicate is offered. gh is now looked up on each use instead of once per
process, so a copy installed while the IDE runs is picked up without a restart;
ide-github drops its `which` dependency for the shared tool_path lookup.

Co-authored-by: dengliu <[email protected]>
2026-11-27 06:21:21 +08:00

203 рядки
6.5 KiB
TypeScript
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
/** Clone Repository › GitHub: the repositories of the account gh is signed in to. */
import { useEffect, useMemo, useState, type KeyboardEvent } from "react";
import type { GhAccountRepo, GhAccountRepos } from "@/bindings";
import { GH_MANUAL_INSTALL_URL, installGhTool } from "@/platform/ghTool";
import { tx } from "@/platform/i18n";
import { call, commands, isCancelled } from "@/platform/ipc";
import {
Button,
EmptyState,
HighlightedText,
Icon,
List,
SearchField,
Spinner,
T,
fuzzyMatch,
listNavigate,
} from "@/ui";
import type { CloneSourceProps } from "@/workbench/contributions";
import { openInBrowser } from "../data/local";
import styles from "./GitHubCloneSource.module.css";
import { useRowHeight } from "@/ui/density";
type Row = { repo: GhAccountRepo; indices: number[] | null };
/** Repositories matching `query` (fuzzy, on `owner/name`), in the account's order. */
export function matchRepos(repos: readonly GhAccountRepo[], query: string): Row[] {
const q = query.trim();
if (!q) return repos.map((repo) => ({ repo, indices: null }));
return repos.flatMap((repo) => {
const indices = fuzzyMatch(repo.nameWithOwner, q);
return indices ? [{ repo, indices }] : [];
});
}
export function GitHubCloneSource({ url, onUrl, onClone, disabled }: CloneSourceProps) {
const rowHeight = useRowHeight(26);
const [state, setState] = useState<GhAccountRepos | null>(null);
const [query, setQuery] = useState("");
const [attempt, setAttempt] = useState(0);
const [installingGh, setInstallingGh] = useState(false);
const [installError, setInstallError] = useState<string | null>(null);
useEffect(() => {
let alive = true;
call(commands.ghAccountRepos())
.then((s) => alive && setState(s))
.catch((e) => alive && setState({ kind: "error", message: e instanceof Error ? e.message : String(e) }));
return () => {
alive = false;
};
}, [attempt]);
const rows = useMemo(() => (state?.kind === "ready" ? matchRepos(state.repos, query) : []), [state, query]);
const active = rows.findIndex((r) => r.repo.cloneUrl === url);
const reload = () => {
setInstallError(null);
setState(null);
setAttempt((n) => n + 1);
};
const installGh = () => {
if (installingGh) return;
setInstallingGh(true);
setInstallError(null);
void installGhTool()
.promise.then((installed) => {
if (installed) reload();
})
.catch((e: unknown) => {
if (!isCancelled(e)) setInstallError(e instanceof Error ? e.message : String(e));
})
.finally(() => setInstallingGh(false));
};
const retry = (
<Button size="small" onClick={reload}>
<T>Retry</T>
</Button>
);
if (!state)
return (
<div className={styles.center}>
<Spinner />
<span className={styles.muted}>
<T>Loading repositories…</T>
</span>
</div>
);
if (state.kind === "notInstalled")
return (
<EmptyState
title="GitHub CLI is not installed"
actions={
<>
<Button size="small" variant="primary" disabled={installingGh} onClick={installGh}>
{installingGh ? (
<>
<Spinner size={12} /> <T>Installing…</T>
</>
) : (
<T>Install GitHub CLI</T>
)}
</Button>
<Button size="small" onClick={() => void openInBrowser(GH_MANUAL_INSTALL_URL)}>
<T>Download manually…</T>
</Button>
{retry}
</>
}
>
<T>GitHub repositories are listed with the</T> <code>gh</code> <T>command line tool. Install it, then run</T>{" "}
<code>gh auth login</code>.{installError && <div className={styles.muted}>{installError}</div>}
</EmptyState>
);
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>
);
if (state.kind === "error")
return (
<EmptyState title="Could not reach GitHub" actions={retry}>
{state.message}
</EmptyState>
);
const pick = (i: number) => {
const row = rows[i];
if (row) onUrl(row.repo.cloneUrl);
};
const onKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter" && rows[active]) {
e.preventDefault();
onClone(rows[active].repo.cloneUrl);
return;
}
const next = listNavigate(e.key, { items: rows, active: Math.max(active, 0) });
if (next !== null) {
e.preventDefault();
pick(next);
}
};
return (
<div className={styles.pane}>
<div className={styles.header}>
<Icon name="github" size={16} />
<span className={styles.login}>{state.login}</span>
<SearchField
className={styles.search}
aria-label="Search repositories"
placeholder={tx("Search repositories")}
value={query}
onChange={setQuery}
onKeyDown={onKeyDown}
disabled={disabled}
data-autofocus
/>
</div>
<div className={styles.list}>
<List<Row>
aria-label="Repositories"
items={rows}
getKey={(r) => r.repo.nameWithOwner}
activeIndex={active}
onActiveChange={pick}
focusable={false}
rowHeight={rowHeight}
empty={
<span className={styles.muted}>
<T>{state.repos.length ? "No matching repositories" : "No repositories"}</T>
</span>
}
renderItem={(r) => (
<span
className={styles.row}
title={r.repo.description ?? undefined}
onDoubleClick={() => onClone(r.repo.cloneUrl)}
>
<Icon name={r.repo.private ? "lock" : "github"} size={14} className={styles.rowIcon} />
<span className={styles.name}>
{r.indices ? <HighlightedText text={r.repo.nameWithOwner} indices={r.indices} /> : r.repo.nameWithOwner}
</span>
{r.repo.fork && (
<span className={styles.tag}>
<T>fork</T>
</span>
)}
{r.repo.archived && (
<span className={styles.tag}>
<T>archived</T>
</span>
)}
{r.repo.description && <span className={styles.description}>{r.repo.description}</span>}
</span>
)}
/>
</div>
</div>
);
}