π OpenHands: AI-Driven Development
fix: list Azure DevOps repositories from every organization (#18479)
Alonaccdb08783cf8
diff --git a/__tests__/hooks/query/use-git-repositories-cloud-installations.test.tsx b/__tests__/hooks/query/use-git-repositories-cloud-installations.test.tsxnew file mode 100644index 0000000000..84ba8c45e1--- /dev/null+++ b/__tests__/hooks/query/use-git-repositories-cloud-installations.test.tsx@@ -0,0 +1,173 @@+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import { act, renderHook, waitFor } from "@testing-library/react";+import React from "react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import {+ __resetActiveStoreForTests,+ setActiveSelection,+ setRegisteredBackends,+} from "#/api/backend-registry/active-store";+import type { Backend } from "#/api/backend-registry/types";+import { callCloudProxy } from "#/api/cloud/proxy";+import SettingsService from "#/api/settings-service/settings-service.api";+import { ActiveBackendProvider } from "#/contexts/active-backend-context";+import { useGitRepositories } from "#/hooks/query/use-git-repositories";+import { DEFAULT_SETTINGS } from "#/services/settings";+import type { GitRepository } from "#/types/git";+import type { Provider } from "#/types/settings";++vi.mock("#/api/cloud/proxy", () => ({+ callCloudProxy: vi.fn(),+}));++const cloudBackend: Backend = {+ id: "cloud-ohe",+ name: "OpenHands Enterprise",+ host: "https://ohe.example.com",+ apiKey: "cloud-key",+ kind: "cloud",+};++const wrapper = ({ children }: { children: React.ReactNode }) => {+ const client = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });+ return (+ <QueryClientProvider client={client}>+ <ActiveBackendProvider>{children}</ActiveBackendProvider>+ </QueryClientProvider>+ );+};++const repo = (fullName: string, provider: Provider): GitRepository => ({+ id: fullName,+ full_name: fullName,+ git_provider: provider,+ is_public: false,+});++type RepoPages = Record<string, Record<string, GitRepository[]>>;++/**+ * Serves `/installations/search` and `/repositories/search` from fixed data.+ * `pages[installation][pageId]` holds one page; page ids chain in key order.+ */+function serveCloud(installations: string[], pages: RepoPages) {+ vi.mocked(callCloudProxy).mockImplementation((async (req: {+ path: string;+ }) => {+ const url = new URL(`https://ohe.example.com${req.path}`);+ if (url.pathname === "/api/v1/git/installations/search") {+ return { items: installations, next_page_id: null };+ }+ if (url.pathname === "/api/v1/git/repositories/search") {+ const byPage = pages[url.searchParams.get("installation_id") ?? ""];+ const pageIds = Object.keys(byPage ?? {});+ const pageId = url.searchParams.get("page_id") ?? pageIds[0];+ const next = pageIds[pageIds.indexOf(pageId) + 1] ?? null;+ return { items: byPage?.[pageId] ?? [], next_page_id: next };+ }+ throw new Error(`Unexpected cloud request ${req.path}`);+ }) as never);+}++const requestedPaths = () =>+ vi+ .mocked(callCloudProxy)+ .mock.calls.map((call) => (call[0] as { path: string }).path);++describe("useGitRepositories with cloud installations", () => {+ beforeEach(() => {+ window.localStorage.clear();+ __resetActiveStoreForTests();+ setRegisteredBackends([cloudBackend]);+ setActiveSelection({ backendId: cloudBackend.id, orgId: null });+ vi.mocked(callCloudProxy).mockReset();+ vi.spyOn(SettingsService, "getSettings").mockResolvedValue({+ ...DEFAULT_SETTINGS,+ provider_tokens_set: { azure_devops: null },+ });+ });++ afterEach(() => {+ vi.restoreAllMocks();+ __resetActiveStoreForTests();+ window.localStorage.clear();+ });++ it("lists Azure DevOps repositories from every organization, one page at a time", async () => {+ // Arrange: two organizations; org-a has two pages.+ serveCloud(["org-a", "org-b"], {+ "org-a": {+ first: [repo("org-a/proj/one", "azure_devops")],+ second: [repo("org-a/proj/two", "azure_devops")],+ },+ "org-b": { first: [repo("org-b/proj/three", "azure_devops")] },+ });++ // Act+ const { result } = renderHook(+ () => useGitRepositories({ provider: "azure_devops", pageSize: 1 }),+ { wrapper },+ );+ await waitFor(() => expect(result.current.data?.pages).toHaveLength(1));+ while (result.current.hasNextPage) {+ const loaded = result.current.data?.pages.length ?? 0;+ await act(() => result.current.fetchNextPage());+ await waitFor(() =>+ expect(result.current.data?.pages.length).toBeGreaterThan(loaded),+ );+ }++ // Assert+ const names = result.current.data?.pages.flatMap((page) =>+ page.items.map((item) => item.full_name),+ );+ expect(names).toEqual([+ "org-a/proj/one",+ "org-a/proj/two",+ "org-b/proj/three",+ ]);+ const paths = requestedPaths();+ expect(+ paths.filter((path) => path.includes("/installations/search")),+ ).toHaveLength(1);+ const repoQueries = paths+ .filter((path) => path.includes("/repositories/search"))+ .map((path) => new URL(`https://x${path}`).searchParams);+ expect(+ repoQueries.map((params) => [+ params.get("provider"),+ params.get("installation_id"),+ params.get("page_id"),+ ]),+ ).toEqual([+ ["azure_devops", "org-a", null],+ ["azure_devops", "org-a", "second"],+ ["azure_devops", "org-b", null],+ ]);+ });++ it("requests no repositories when the user has no Azure DevOps organizations", async () => {+ // Arrange+ serveCloud([], {});++ // Act+ const { result } = renderHook(+ () => useGitRepositories({ provider: "azure_devops" }),+ { wrapper },+ );+ await waitFor(() =>+ expect(requestedPaths().some((p) => p.includes("/installations"))).toBe(+ true,+ ),+ );++ // Assert: the picker falls back to its empty state.+ expect(result.current.data).toBeUndefined();+ expect(result.current.isLoading).toBe(false);+ expect(+ requestedPaths().some((path) => path.includes("/repositories/search")),+ ).toBe(false);+ });+});diff --git a/__tests__/utils/should-use-installation-repos.test.ts b/__tests__/utils/should-use-installation-repos.test.tsindex 2f8135a4fb..a51b0c2f4e 100644--- a/__tests__/utils/should-use-installation-repos.test.ts+++ b/__tests__/utils/should-use-installation-repos.test.ts@@ -8,12 +8,15 @@ describe("shouldUseInstallationRepos", () => {expect(shouldUseInstallationRepos(undefined, "local")).toBe(false);});- it("uses installations for github only when active backend is cloud", () => {- expect(shouldUseInstallationRepos("github", "cloud")).toBe(true);- expect(shouldUseInstallationRepos("github", "local")).toBe(false);- // Default (no app mode passed) preserves the old local behavior.- expect(shouldUseInstallationRepos("github")).toBe(false);- });+ it.each(["github", "azure_devops"] as const)(+ "uses installations for %s only when active backend is cloud",+ (provider) => {+ expect(shouldUseInstallationRepos(provider, "cloud")).toBe(true);+ expect(shouldUseInstallationRepos(provider, "local")).toBe(false);+ // Default (no app mode passed) preserves the old local behavior.+ expect(shouldUseInstallationRepos(provider)).toBe(false);+ },+ );it("always uses installations for bitbucket variants", () => {expect(shouldUseInstallationRepos("bitbucket", "local")).toBe(true);@@ -26,10 +29,9 @@ describe("shouldUseInstallationRepos", () => {);});- it("never uses installations for gitlab / azure / forgejo", () => {+ it("never uses installations for gitlab / forgejo", () => {expect(shouldUseInstallationRepos("gitlab", "cloud")).toBe(false);expect(shouldUseInstallationRepos("gitlab", "local")).toBe(false);- expect(shouldUseInstallationRepos("azure_devops", "cloud")).toBe(false);expect(shouldUseInstallationRepos("forgejo", "cloud")).toBe(false);});});diff --git a/src/utils/utils.ts b/src/utils/utils.tsindex b7e0a1353f..0a3a9dc533 100644--- a/src/utils/utils.ts+++ b/src/utils/utils.ts@@ -120,8 +120,9 @@ export const getFileExtension = (fileName: string): string => {** Mirrors OpenHands' cloud frontend (parameterized by `app_mode`):* - bitbucket / bitbucket_data_center β always installation-based- * - github β installation-based ONLY when the active backend is cloud- * - gitlab / azure_devops / forgejo β direct (search) flow+ * - github / azure_devops β installation-based ONLY when the active backend+ * is cloud (Azure DevOps organizations are its installations)+ * - gitlab / forgejo β direct (search) flow** `appMode` accepts the active backend `kind` ("local" | "cloud") so call* sites can hand it through directly.@@ -137,6 +138,7 @@ export const shouldUseInstallationRepos = (case "bitbucket_data_center":return true;case "github":+ case "azure_devops":return appMode === "cloud";default:return false;