mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
fix(vcs): build Open In and commit links on the host behind an SSH alias (#4554)
Open In, Copy Path › <hosting> Link and the Git log's View in Browser read the raw remote.<name>.url, so a remote behind an ~/.ssh/config host alias (github.com-work → github.com) produced pages on the alias host and a guessed hosting. They now use the remote's resolvedUrl from vcs_remotes (the existing ~/.ssh/config resolver), falling back to the config value; the agent session's origin lookup for gh links does the same.
This commit is contained in:
@@ -229,7 +229,9 @@ describe("SessionAssets component", () => {
|
||||
vi.spyOn(ipc, "call").mockImplementation(async (promise) => {
|
||||
return promise;
|
||||
});
|
||||
vi.spyOn(ipc.commands, "vcsConfigGet").mockResolvedValue("git@github-personal:acme/monorepo.git" as never);
|
||||
vi.spyOn(ipc.commands, "vcsRemotes").mockResolvedValue([
|
||||
{ name: "origin", url: "git@github-personal:acme/monorepo.git" },
|
||||
] as never);
|
||||
vi.spyOn(ipc.commands, "ghPrList").mockResolvedValue([branchSummary] as never);
|
||||
vi.spyOn(ipc.commands, "ghApi").mockImplementation((_repo, req) => {
|
||||
if (req.endpoint === "repos/acme/monorepo/pulls/122518") {
|
||||
|
||||
@@ -204,13 +204,15 @@ export function SessionAssets({ task, entries, head, onFocusComposer }: SessionA
|
||||
const refsAnchor = useRef<HTMLButtonElement>(null);
|
||||
const artifactsAnchor = useRef<HTMLButtonElement>(null);
|
||||
|
||||
// Read `remote.origin.url` so `gh pr checks 123` / `gh issue view 456` resolve to `<owner>/<repo>`.
|
||||
// Read `origin` (with any `~/.ssh/config` host alias resolved) so `gh pr checks 123` /
|
||||
// `gh issue view 456` resolve to `<owner>/<repo>`.
|
||||
useEffect(() => {
|
||||
if (!isTauri()) return;
|
||||
let alive = true;
|
||||
call(commands.vcsConfigGet(repoRoot, "remote.origin.url"))
|
||||
.then((url) => {
|
||||
if (alive) setRemoteUrl(url || null);
|
||||
call(commands.vcsRemotes(repoRoot))
|
||||
.then((remotes) => {
|
||||
const origin = remotes.find((r) => r.name === "origin");
|
||||
if (alive) setRemoteUrl(origin ? (origin.resolvedUrl ?? origin.url) : null);
|
||||
})
|
||||
.catch(() => {
|
||||
if (alive) setRemoteUrl(null);
|
||||
|
||||
@@ -6,6 +6,7 @@ import type { MenuItem } from "@/ui";
|
||||
|
||||
const config = vi.hoisted(() => new Map<string, string>());
|
||||
const configReads = vi.hoisted(() => vi.fn());
|
||||
const remotes = vi.hoisted(() => new Map<string, { name: string; url: string; resolvedUrl?: string | null }[]>());
|
||||
|
||||
vi.mock("@/platform/ipc", async (importOriginal) => {
|
||||
const real = await importOriginal<typeof import("@/platform/ipc")>();
|
||||
@@ -14,6 +15,7 @@ vi.mock("@/platform/ipc", async (importOriginal) => {
|
||||
call: vi.fn(async (request: Promise<unknown>) => request),
|
||||
commands: {
|
||||
...real.commands,
|
||||
vcsRemotes: async (root: string) => remotes.get(root) ?? [],
|
||||
vcsConfigGet: async (root: string, key: string) => {
|
||||
configReads(root, key);
|
||||
return config.get(`${root} ${key}`) ?? null;
|
||||
@@ -42,6 +44,7 @@ async function start() {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
config.clear();
|
||||
remotes.clear();
|
||||
config.set("/p remote.origin.url", "git@gitlab.example.com:group/sub/app.git");
|
||||
vi.stubGlobal("navigator", { clipboard: { writeText }, userAgent: navigator.userAgent });
|
||||
provideNotificationService({ notify });
|
||||
@@ -113,6 +116,17 @@ describe("Copy Path/Reference… › <hosting> Link", () => {
|
||||
expect(items({ paths: ["/p/a.ts"] })).toEqual([]);
|
||||
});
|
||||
|
||||
it("builds the link on the web host behind an ~/.ssh/config host alias", async () => {
|
||||
remotes.set("/p", [
|
||||
{ name: "origin", url: "git@github.com-work:acme/app.git", resolvedUrl: "git@github.com:acme/app.git" },
|
||||
]);
|
||||
await start();
|
||||
expect(items({ paths: ["/p/src/main.rs"], line: 9 })[0]).toMatchObject({
|
||||
label: "GitHub Link",
|
||||
preview: "https://github.com/acme/app/blob/feature/x/src/main.rs#L10",
|
||||
});
|
||||
});
|
||||
|
||||
it("warns instead of copying when the file has no page on a remote", async () => {
|
||||
await executeAction("vcs.copyRemoteLink", { path: "/elsewhere/a.ts", line: 0 });
|
||||
await vi.waitFor(() =>
|
||||
|
||||
@@ -13,6 +13,7 @@ import { executeAction, registerActions, type Action } from "@/platform/actions"
|
||||
import { checkoutDetached, newBranch, rebaseOnto } from "./branchOps";
|
||||
import { openUrl } from "./issueLinks";
|
||||
import { commitWebUrl } from "./model";
|
||||
import { remoteWebSource } from "./remoteUrl";
|
||||
import {
|
||||
commitChangelist,
|
||||
deleteChangelist,
|
||||
@@ -156,7 +157,7 @@ function withCommits(fn: (cs: Commit[]) => unknown, allowReadOnly = false) {
|
||||
async function openCommitInBrowser(c: Commit) {
|
||||
const upstream = useVcs.getState().branch?.upstream;
|
||||
const remote = upstream ? upstream.slice(0, upstream.indexOf("/")) : "origin";
|
||||
const url = await call(commands.vcsConfigGet(currentRepo(), `remote.${remote}.url`)).catch(() => null);
|
||||
const url = await remoteWebSource(currentRepo(), remote);
|
||||
const page = url ? commitWebUrl(url, c.id) : null;
|
||||
if (page) await openUrl(page);
|
||||
else notifications().notify({ group: "vcs", level: "warning", title: `No web URL for remote '${remote}'` });
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
* of its repository's remote, at the current branch's upstream (else the HEAD commit).
|
||||
* Copy Path/Reference… › <hosting> Link copies that page's address, with the selected lines.
|
||||
*/
|
||||
import { call, commands } from "@/platform/ipc";
|
||||
import { executeAction, type Action } from "@/platform/actions";
|
||||
import { writeClipboardText } from "@/platform/clipboard";
|
||||
import { tx } from "@/platform/i18n";
|
||||
import { notifications, registerMenuGroup } from "@/workbench/contributions";
|
||||
import { openUrl } from "./issueLinks";
|
||||
import { fileWebUrl, remoteHosting, rootOf } from "./model";
|
||||
import { remoteWebSource } from "./remoteUrl";
|
||||
import { useVcs } from "./store";
|
||||
|
||||
interface Target {
|
||||
@@ -30,9 +30,9 @@ function remoteName(root: string): string {
|
||||
}
|
||||
|
||||
async function readRemoteUrl(root: string): Promise<string | null> {
|
||||
const url = await call(commands.vcsConfigGet(root, `remote.${remoteName(root)}.url`)).catch(() => null);
|
||||
remoteUrls.set(root, url || null);
|
||||
return url || null;
|
||||
const url = await remoteWebSource(root, remoteName(root));
|
||||
remoteUrls.set(root, url);
|
||||
return url;
|
||||
}
|
||||
|
||||
function revision(root: string): string | null {
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { call, commands } from "@/platform/ipc";
|
||||
|
||||
/**
|
||||
* The URL web pages of `remote` in `repo` are built from: its fetch URL with any `~/.ssh/config`
|
||||
* host alias resolved (`git@github.com-work:o/r` → `git@github.com:o/r`). Null without that remote.
|
||||
*/
|
||||
export async function remoteWebSource(repo: string | null, remote: string): Promise<string | null> {
|
||||
const remotes = await call(commands.vcsRemotes(repo)).catch(() => null);
|
||||
const found = remotes?.find((r) => r.name === remote);
|
||||
if (found) return found.resolvedUrl ?? found.url;
|
||||
return (await call(commands.vcsConfigGet(repo, `remote.${remote}.url`)).catch(() => null)) || null;
|
||||
}
|
||||
Reference in New Issue
Block a user