π OpenHands: AI-Driven Development
fix: recognize Azure DevOps SSH remotes (#17375)
Matteo ATKINSONd149fb5a3f0d
diff --git a/__tests__/utils/azure-devops-remote-links.test.ts b/__tests__/utils/azure-devops-remote-links.test.tsnew file mode 100644index 0000000000..3e190fb2b6--- /dev/null+++ b/__tests__/utils/azure-devops-remote-links.test.ts@@ -0,0 +1,46 @@+import { describe, expect, it } from "vitest";+import { parseGitRemoteUrl } from "#/utils/parse-git-remote-url";+import { constructBranchUrl } from "#/utils/utils";++// Spans both modules on purpose: a remote is only useful once the parsed+// fields reach a link builder, and an Azure DevOps SSH remote used to parse+// into values `constructBranchUrl` could not build from.+function branchLinkFor(remoteUrl: string, branch: string): string {+ const parsed = parseGitRemoteUrl(remoteUrl);+ if (!parsed?.provider) return "";+ return constructBranchUrl(+ parsed.provider,+ parsed.repository ?? "",+ branch,+ parsed.host,+ );+}++describe("Azure DevOps remotes resolve a browsable branch link", () => {+ const expected =+ "https://dev.azure.com/myorg/myproject/_git/myrepo?version=GBmain";++ it("builds a branch link from an HTTPS remote", () => {+ expect(+ branchLinkFor(+ "https://dev.azure.com/myorg/myproject/_git/myrepo",+ "main",+ ),+ ).toBe(expected);+ });++ it("builds the same branch link from the SSH remote", () => {+ expect(+ branchLinkFor("[email protected]:v3/myorg/myproject/myrepo", "main"),+ ).toBe(expected);+ });++ it("builds the same branch link from a legacy visualstudio.com SSH remote", () => {+ expect(+ branchLinkFor(+ "[email protected]:v3/myorg/myproject/myrepo",+ "main",+ ),+ ).toBe(expected);+ });+});diff --git a/__tests__/utils/parse-git-remote-url.test.ts b/__tests__/utils/parse-git-remote-url.test.tsindex 8ab265cf28..adbf81c4f0 100644--- a/__tests__/utils/parse-git-remote-url.test.ts+++ b/__tests__/utils/parse-git-remote-url.test.ts@@ -110,6 +110,47 @@ describe("parseGitRemoteUrl", () => {expect(result?.repository).toBe("org/project/repo");});+ it("parses Azure DevOps SSH URLs, dropping the v3 prefix", () => {+ const result = parseGitRemoteUrl(+ "[email protected]:v3/org/project/repo",+ );+ expect(result?.provider).toBe("azure_devops");+ expect(result?.repository).toBe("org/project/repo");+ });++ it("parses Azure DevOps ssh:// URLs", () => {+ const result = parseGitRemoteUrl(+ "ssh://[email protected]/v3/org/project/repo",+ );+ expect(result?.host).toBe("dev.azure.com");+ expect(result?.provider).toBe("azure_devops");+ expect(result?.repository).toBe("org/project/repo");+ });++ it("parses Azure DevOps ssh:// URLs that carry an explicit port", () => {+ const result = parseGitRemoteUrl(+ "ssh://[email protected]:22/v3/org/project/repo",+ );+ expect(result?.host).toBe("dev.azure.com");+ expect(result?.provider).toBe("azure_devops");+ expect(result?.repository).toBe("org/project/repo");+ });++ it("parses legacy visualstudio.com SSH URLs", () => {+ const result = parseGitRemoteUrl(+ "[email protected]:v3/org/project/repo",+ );+ expect(result?.provider).toBe("azure_devops");+ expect(result?.repository).toBe("org/project/repo");+ });++ it("keeps an Azure DevOps organization actually named v3", () => {+ const result = parseGitRemoteUrl(+ "https://dev.azure.com/v3/project/_git/repo",+ );+ expect(result?.repository).toBe("v3/project/repo");+ });+it("preserves nested paths for unknown self-hosted hosts", () => {const result = parseGitRemoteUrl("https://git.example.com/group/subgroup/repo.git",@@ -129,4 +170,10 @@ describe("parseGitRemoteUrl", () => {it("returns null for unparseable strings", () => {expect(parseGitRemoteUrl("not a url")).toBeNull();});++ it("treats a host named after an Object.prototype member as a plain host", () => {+ const result = parseGitRemoteUrl("http://constructor/owner/repo.git");+ expect(result?.host).toBe("constructor");+ expect(result?.repository).toBe("owner/repo");+ });});diff --git a/src/utils/parse-git-remote-url.ts b/src/utils/parse-git-remote-url.tsindex 84fa41a856..b86d848a70 100644--- a/src/utils/parse-git-remote-url.ts+++ b/src/utils/parse-git-remote-url.ts@@ -3,7 +3,11 @@ import { Provider } from "#/types/settings";export interface ParsedGitRemoteUrl {/** Original URL, trimmed. */url: string;- /** Hostname of the remote (e.g. `github.com`, `git.example.com`). */+ /**+ * Browsable hostname of the remote (e.g. `github.com`, `git.example.com`).+ * Hosts that only serve SSH are mapped to their web equivalent, so callers+ * can build links from this directly. `url` keeps the original remote.+ */host: string | null;/** Path-style identifier, normalized to `owner/repo` (no leading slash, no `.git` suffix). */repository: string | null;@@ -18,6 +22,22 @@ const KNOWN_HOSTS: Record<string, Provider> = {"dev.azure.com": "azure_devops",};+// Hosts that serve git over SSH only, mapped to the host that serves the same+// repositories over the web. Azure DevOps hands out `ssh.dev.azure.com` (and+// `vs-ssh.visualstudio.com` for legacy organizations) in its SSH clone URLs,+// neither of which resolves a browsable page.+const SSH_WEB_HOSTS: Record<string, string> = {+ "ssh.dev.azure.com": "dev.azure.com",+ "vs-ssh.visualstudio.com": "dev.azure.com",+};++function toWebHost(host: string): string {+ const lower = host.toLowerCase();+ // hasOwn, so a host named after an Object.prototype member (`constructor`)+ // resolves to itself rather than to an inherited value.+ return Object.hasOwn(SSH_WEB_HOSTS, lower) ? SSH_WEB_HOSTS[lower] : host;+}+function stripGitSuffix(path: string): string {return path.replace(/\.git$/, "");}@@ -28,12 +48,17 @@ function detectProvider(host: string | null): Provider | null {}function normalizeAzureDevOpsPath(path: string): string {- // Azure paths look like `org/project/_git/repo` or `org/_git/repo`.- // Normalize to `org/project/repo` (or `org/repo`) so it lines up with- // constructBranchUrl's expectations.+ // Azure paths look like `org/project/_git/repo` or `org/_git/repo` over+ // HTTPS, and `v3/org/project/repo` over SSH. Normalize to `org/project/repo`+ // (or `org/repo`) so it lines up with constructBranchUrl's expectations.const segments = path.split("/").filter(Boolean);const gitIndex = segments.indexOf("_git");- if (gitIndex === -1) return segments.join("/");+ if (gitIndex === -1) {+ // Only the SSH form carries the `v3` prefix, and it never carries `_git`.+ // Checking that first leaves an organization actually named `v3` alone.+ if (segments[0] === "v3") segments.shift();+ return segments.join("/");+ }return [...segments.slice(0, gitIndex), ...segments.slice(gitIndex + 1)].join("/",);@@ -41,9 +66,10 @@ function normalizeAzureDevOpsPath(path: string): string {function buildParsedGitRemoteUrl(url: string,- host: string | null,+ rawHost: string | null,rawPath: string,): ParsedGitRemoteUrl {+ const host = rawHost ? toWebHost(rawHost) : rawHost;const path = stripGitSuffix(rawPath.replace(/^\/+|\/+$/g, ""));const provider = detectProvider(host);const repository =