diff --git a/crates/ide-host/src/menu.rs b/crates/ide-host/src/menu.rs index fbb0079a8..f93af29fd 100644 --- a/crates/ide-host/src/menu.rs +++ b/crates/ide-host/src/menu.rs @@ -759,6 +759,8 @@ pub fn spec(os: Os) -> Vec { act("github.createPullRequest", "Create Pull Request…"), act("github.checkoutPullRequest", "Checkout Pull Request…"), act("github.openInBrowser", "Open on GitHub"), + SEP, + act("github.createGist", "Create Gist…"), ], ), act("vcs.manageRemotes", "Manage Remotes…"), diff --git a/src/features/github/api/client.ts b/src/features/github/api/client.ts index 2a09f99d4..f7ea80f7c 100644 --- a/src/features/github/api/client.ts +++ b/src/features/github/api/client.ts @@ -109,6 +109,8 @@ export function setCurrentRepository( repo: (GHRepositoryCoordinates & { id: string | null; root?: string | null }) | null, ) { current = repo; + // Disconnected: requests go to gh's default host again, not the last repository's. + if (!repo) host = null; } /** The connected repository: `owner`, `name` and its GraphQL node `id` (once loaded). */ diff --git a/src/features/github/data/repository.test.ts b/src/features/github/data/repository.test.ts index ce66f1bfe..a1e0aa318 100644 --- a/src/features/github/data/repository.test.ts +++ b/src/features/github/data/repository.test.ts @@ -110,6 +110,25 @@ describe("a GitHub Enterprise Server repository", () => { expect(await connect(s.remotes[1]!)).toBe(true); expect(ghApi).toHaveBeenLastCalledWith("/w/app", expect.objectContaining({ host: null })); }); + + it("stops sending requests to the server once disconnected (another project)", async () => { + layout = { "/w/app": [{ name: "origin", url: "git@ghe.example.com:acme/app.git" }] }; + ghAvailability.mockReturnValue( + ok({ kind: "ready", repo: { nameWithOwner: "acme/app", url: "https://ghe.example.com/acme/app" }, login: "al" }), + ); + ghApi.mockImplementation((_repo: string | null, req: { endpoint: string }) => + ok( + req.endpoint === "user" + ? JSON.stringify({ id: 1, node_id: "U1", login: "al", avatar_url: "", html_url: "" }) + : JSON.stringify({ data: { repository: { id: "R1", owner: { login: "acme" }, name: "app" } } }), + ), + ); + await refreshConnection(); + expect(useGhRepository.getState().connection?.coordinates.host).toBe("ghe.example.com"); + resetConnection(); + await rest("POST", "gists", {}); + expect(ghApi).toHaveBeenLastCalledWith(null, expect.objectContaining({ endpoint: "gists", host: null })); + }); }); describe("GitHub Enterprise Server hosts beside github.com", () => { diff --git a/src/features/github/gist/createGist.tsx b/src/features/github/gist/createGist.tsx new file mode 100644 index 000000000..e291b6c56 --- /dev/null +++ b/src/features/github/gist/createGist.tsx @@ -0,0 +1,136 @@ +/** + * Create Gist…: posts the editor selection (else the whole file) or the files the menu + * targets as a gist through `gh api`, then opens / copies its URL as the dialog chose. + */ +import { useState } from "react"; +import { activeEditor, editorsFor } from "@/editor/api"; +import { writeClipboardText } from "@/platform/clipboard"; +import { tx } from "@/platform/i18n"; +import { call, commands } from "@/platform/ipc"; +import { openExternalUrl } from "@/platform/openUrl"; +import { basename } from "@/platform/project"; +import { Checkbox, Dialog, Input, T } from "@/ui"; +import { notifications } from "@/workbench/contributions"; +import { errorMessage, rest } from "../api/client"; +import { refreshConnection, useGhRepository } from "../data/repository"; +import { openDialog } from "../ui/dialogs"; +import styles from "../ui/ui.module.css"; +import { gistBody, loadGistOptions, saveGistOptions, type GistFile, type GistOptions } from "./model"; + +/** The text of `path`: its open editor's (unsaved edits included), else the file on disk; null for folders and unreadable files. */ +async function fileText(path: string): Promise { + const open = editorsFor(path)[0]; + if (open) return open.view.state.doc.toString(); + try { + return await call(commands.fsReadText(path)); + } catch { + return null; + } +} + +/** The menu's paths (`arg`), else the active editor's selection or whole document. */ +export async function gistFiles(arg: unknown): Promise { + const paths = typeof arg === "string" ? [arg] : Array.isArray(arg) ? arg.filter((p) => typeof p === "string") : []; + if (!paths.length) { + const ed = activeEditor(); + if (!ed) return []; + const { state } = ed.view; + const sel = state.selection.main; + return [ + { name: basename(ed.ctx.path), content: sel.empty ? state.doc.toString() : state.sliceDoc(sel.from, sel.to) }, + ]; + } + const texts = await Promise.all(paths.map(fileText)); + return paths.flatMap((p, i) => (texts[i] === null ? [] : [{ name: basename(p), content: texts[i]! }])); +} + +interface GistDialogResult extends GistOptions { + fileName: string; + description: string; +} + +function GistDialog(props: { fileName: string | null; onDone: (v: GistDialogResult | null) => void }) { + const [fileName, setFileName] = useState(props.fileName ?? ""); + const [description, setDescription] = useState(""); + const [opts, setOpts] = useState(loadGistOptions); + const flag = (k: keyof GistOptions) => (v: boolean) => setOpts((o) => ({ ...o, [k]: v })); + return ( + props.onDone(null)} + width={460} + buttons={[ + { + label: "Create Gist", + role: "default", + disabled: props.fileName !== null && !fileName.trim(), + onClick: () => props.onDone({ ...opts, fileName, description }), + }, + { label: "Cancel", role: "cancel" }, + ]} + > +
+ {props.fileName !== null && ( + <> + + setFileName(e.target.value)} /> + + )} + +