Skip to content

🙌 OpenHands: AI-Driven Development

fix(apps): accept GitHub tree URLs as app source (#17504)

Vasco Schiavob52a46483bb6
diff --git a/.pr/forgejo-git-items-host/index.html b/.pr/forgejo-git-items-host/index.html
deleted file mode 100644
index 198175ce8b..0000000000
--- a/.pr/forgejo-git-items-host/index.html
+++ /dev/null
@@ -1,12 +0,0 @@
-<!doctype html>
-<html lang="en">
-<head>
- <meta charset="utf-8" />
- <title>Forgejo PR/issue lists queried api.github.com - repro evidence</title>
-</head>
-<body>
- <h1>Reproduction evidence</h1>
- <p>Terminal runs of <code>__tests__/api/git-provider-items-service.test.ts</code>: failing twice on main (requests go to api.github.com), passing twice with the fix (requests go to codeberg.org/api/v1).</p>
- <img src="terminal.png" alt="vitest runs failing before the fix and passing after it" style="max-width:100%" />
-</body>
-</html>
diff --git a/.pr/forgejo-git-items-host/terminal.png b/.pr/forgejo-git-items-host/terminal.png
deleted file mode 100644
index c9d871bbe6..0000000000
Binary files a/.pr/forgejo-git-items-host/terminal.png and /dev/null differ
diff --git a/__tests__/utils/parse-git-tree-url.test.ts b/__tests__/utils/parse-git-tree-url.test.ts
new file mode 100644
index 0000000000..9bc0b915a3
--- /dev/null
+++ b/__tests__/utils/parse-git-tree-url.test.ts
@@ -0,0 +1,62 @@
+import { describe, expect, it } from "vitest";
+import { parseGitTreeUrl } from "#/utils/parse-git-tree-url";
+
+describe("parseGitTreeUrl", () => {
+ it.each([
+ [
+ "https://github.com/OpenHands/canvas-apps/tree/main/conversation-search-sidecar",
+ "https://github.com/OpenHands/canvas-apps",
+ "main",
+ "conversation-search-sidecar",
+ ],
+ [
+ "https://github.example.com/o/r.git/tree/v1.2/apps/demo/",
+ "https://github.example.com/o/r",
+ "v1.2",
+ "apps/demo",
+ ],
+ [
+ "https://gitlab.com/group/subgroup/repo/-/tree/main/apps/demo",
+ "https://gitlab.com/group/subgroup/repo",
+ "main",
+ "apps/demo",
+ ],
+ [
+ "https://git.example.com/o/r/-/tree/dev",
+ "https://git.example.com/o/r",
+ "dev",
+ null,
+ ],
+ [
+ "https://oauth2:[email protected]:8443/g/r/-/tree/main/x",
+ "https://oauth2:[email protected]:8443/g/r",
+ "main",
+ "x",
+ ],
+ [
+ "https://bitbucket.org/ws/repo/src/main/apps/demo/",
+ "https://bitbucket.org/ws/repo",
+ "main",
+ "apps/demo",
+ ],
+ [
+ "https://codeberg.org/o/r/src/branch/main/apps/demo",
+ "https://codeberg.org/o/r",
+ "main",
+ "apps/demo",
+ ],
+ ])("splits %s", (url, source, ref, repoPath) => {
+ expect(parseGitTreeUrl(url)).toEqual({ source, ref, repoPath });
+ });
+
+ it.each([
+ "https://github.com/o/r",
+ "https://gitlab.com/o/r.git",
+ "[email protected]:o/r.git",
+ "github:o/r",
+ "/local/path",
+ "https://github.com/o/r/blob/main/README.md",
+ ])("returns null for %s", (source) => {
+ expect(parseGitTreeUrl(source)).toBeNull();
+ });
+});
diff --git a/src/components/features/canvas-extensions/add-canvas-extension-modal.tsx b/src/components/features/canvas-extensions/add-canvas-extension-modal.tsx
index 49dee3f138..7e4de6cd51 100644
--- a/src/components/features/canvas-extensions/add-canvas-extension-modal.tsx
+++ b/src/components/features/canvas-extensions/add-canvas-extension-modal.tsx
@@ -6,6 +6,7 @@ import { ModalBackdrop } from "#/components/shared/modals/modal-backdrop";
import { ModalCloseButton } from "#/components/shared/modals/modal-close-button";
import { useInstallCanvasExtension } from "#/hooks/mutation/use-manage-canvas-extensions";
import { I18nKey } from "#/i18n/declaration";
+import { parseGitTreeUrl } from "#/utils/parse-git-tree-url";
import { cn } from "#/utils/utils";
import { modalTitleLgClassName } from "#/utils/modal-classes";
@@ -28,11 +29,12 @@ export function AddCanvasExtensionModal({
const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();
if (!canSubmit) return;
+ const treeUrl = parseGitTreeUrl(trimmedSource);
install.mutate(
{
- source: trimmedSource,
- ref: ref.trim() || null,
- repo_path: repoPath.trim() || null,
+ source: treeUrl?.source ?? trimmedSource,
+ ref: ref.trim() || treeUrl?.ref || null,
+ repo_path: repoPath.trim() || treeUrl?.repoPath || null,
},
{ onSuccess: onClose },
);
diff --git a/src/utils/parse-git-tree-url.ts b/src/utils/parse-git-tree-url.ts
new file mode 100644
index 0000000000..e0a332be4b
--- /dev/null
+++ b/src/utils/parse-git-tree-url.ts
@@ -0,0 +1,45 @@
+// Browser folder URL layouts, matched on the path so self-hosted instances
+// work too. Order matters: Gitea's `src/branch/<ref>` would otherwise read
+// `branch` as the ref.
+const TREE_URL_PATTERNS = [
+ // GitLab (incl. subgroups): <group…>/<repo>/-/tree/<ref>/<path>
+ /^(?<repo>.+?)\/-\/tree\/(?<ref>[^/]+)(?:\/(?<path>.+))?$/,
+ // Gitea / Forgejo: <o>/<r>/src/branch|tag|commit/<ref>/<path>
+ /^(?<repo>[^/]+\/[^/]+)\/src\/(?:branch|tag|commit)\/(?<ref>[^/]+)(?:\/(?<path>.+))?$/,
+ // GitHub: <o>/<r>/tree/<ref>/<path>; Bitbucket: <ws>/<r>/src/<ref>/<path>
+ /^(?<repo>[^/]+\/[^/]+)\/(?:tree|src)\/(?<ref>[^/]+)(?:\/(?<path>.+))?$/,
+];
+
+/**
+ * Split a git host's browser folder URL into a cloneable source, ref and repo
+ * path. A ref containing `/` is ambiguous in these URLs, so only its first
+ * segment is taken as the ref.
+ */
+export function parseGitTreeUrl(
+ url: string,
+): { source: string; ref: string; repoPath: string | null } | null {
+ let parsed: URL;
+ try {
+ parsed = new URL(url.trim());
+ } catch {
+ return null;
+ }
+ if (parsed.protocol !== "https:" && parsed.protocol !== "http:") return null;
+
+ const pathname = parsed.pathname.replace(/^\/+|\/+$/g, "");
+ // `origin` drops userinfo; keep it so token-authenticated URLs still clone.
+ const userinfo = parsed.username
+ ? `${parsed.username}${parsed.password ? `:${parsed.password}` : ""}@`
+ : "";
+ for (const pattern of TREE_URL_PATTERNS) {
+ const groups = pattern.exec(pathname)?.groups;
+ if (groups) {
+ return {
+ source: `${parsed.protocol}//${userinfo}${parsed.host}/${groups.repo.replace(/\.git$/, "")}`,
+ ref: decodeURIComponent(groups.ref),
+ repoPath: groups.path ? decodeURIComponent(groups.path) : null,
+ };
+ }
+ }
+ return null;
+}