π OpenHands: AI-Driven Development
fix: show the connected organization name in the Manage Backends modal (#1530)
Hiep Le2acb3a39fc4c
diff --git a/__tests__/components/backends/manage-backends-modal.test.tsx b/__tests__/components/backends/manage-backends-modal.test.tsxindex 4457c0ed02..420ca4f264 100644--- a/__tests__/components/backends/manage-backends-modal.test.tsx+++ b/__tests__/components/backends/manage-backends-modal.test.tsx@@ -20,6 +20,11 @@ import { BackendVersion } from "#/components/features/backends/backend-version";import { BackendRow } from "#/components/features/backends/backend-row";import { type Backend } from "#/api/backend-registry/types";import { CLOUD_BACKEND_LOGGED_OUT_ERROR } from "#/hooks/query/use-backends-health";+import {+ getCloudOrganizations,+ getCloudOrganizationMe,+ getCurrentCloudApiKey,+} from "#/api/cloud/organization-service.api";const deviceFlowMocks = vi.hoisted(() => ({startDeviceFlow: vi.fn(),@@ -39,10 +44,9 @@ vi.mock("@openhands/typescript-client/clients", () => ({}));vi.mock("#/api/cloud/organization-service.api", () => ({- getCurrentCloudApiKey: vi.fn().mockResolvedValue({- orgId: null,- isLegacyKey: true,- }),+ getCloudOrganizations: vi.fn(),+ getCloudOrganizationMe: vi.fn(),+ getCurrentCloudApiKey: vi.fn(),}));vi.mock("#/api/device-flow-client", () => ({@@ -90,6 +94,21 @@ beforeEach(() => {getServerInfoMock.mockResolvedValue({ version: "1.28.0" });getSettingsMock.mockReset();getSettingsMock.mockResolvedValue({});+ vi.mocked(getCloudOrganizations).mockReset();+ vi.mocked(getCloudOrganizations).mockResolvedValue({+ items: [],+ currentOrgId: null,+ });+ vi.mocked(getCloudOrganizationMe).mockReset();+ vi.mocked(getCloudOrganizationMe).mockResolvedValue({+ orgId: "",+ userId: "",+ });+ vi.mocked(getCurrentCloudApiKey).mockReset();+ vi.mocked(getCurrentCloudApiKey).mockResolvedValue({+ orgId: null,+ isLegacyKey: true,+ });deviceFlowMocks.startDeviceFlow.mockReset();deviceFlowMocks.startDeviceFlow.mockResolvedValue({device_code: "device-code",@@ -340,6 +359,111 @@ describe("ManageBackendsModal", () => {});expect(screen.getByTestId("manage-backends-modal")).toBeInTheDocument();});++ it("shows each cloud backend's connected organization so same-named backends are distinguishable", async () => {+ // Two cloud backends share the same name and host; each API key is bound+ // to a different org, which is the only thing that tells them apart.+ vi.mocked(getCloudOrganizations).mockResolvedValue({+ items: [+ { id: "org-acme", name: "Acme Inc" },+ { id: "org-beta", name: "Beta Co" },+ ],+ currentOrgId: "org-acme",+ });+ vi.mocked(getCurrentCloudApiKey).mockImplementation(async (backend) => ({+ orgId: backend?.apiKey === "key-acme" ? "org-acme" : "org-beta",+ isLegacyKey: false,+ }));++ renderWithProviders(+ <TestSeed+ onMount={(ctx) => {+ ctx.addBackend({+ name: "Production",+ host: "https://app.all-hands.dev",+ apiKey: "key-acme",+ kind: "cloud",+ });+ ctx.addBackend({+ name: "Production",+ host: "https://app.all-hands.dev",+ apiKey: "key-beta",+ kind: "cloud",+ });+ }}+ >+ <ManageBackendsModal onClose={vi.fn()} />+ </TestSeed>,+ );++ // Each otherwise-identical row now surfaces its own bound organization.+ expect(await screen.findByText("Acme Inc")).toBeInTheDocument();+ expect(screen.getByText("Beta Co")).toBeInTheDocument();+ });++ it("labels a cloud backend's personal workspace instead of showing its raw org name", async () => {+ const personalOrgId = "0b93b5f2-5396-49f2-8d98-61f906184270";+ vi.mocked(getCloudOrganizations).mockResolvedValue({+ items: [{ id: personalOrgId, name: `user_${personalOrgId}_org` }],+ currentOrgId: personalOrgId,+ });+ vi.mocked(getCurrentCloudApiKey).mockResolvedValue({+ orgId: personalOrgId,+ isLegacyKey: false,+ });+ // /me reports user_id === org_id, so the bound org is the user's personal+ // workspace and must render the friendly label, not the backend-side name.+ vi.mocked(getCloudOrganizationMe).mockResolvedValue({+ orgId: personalOrgId,+ userId: personalOrgId,+ });++ renderWithProviders(+ <TestSeed+ onMount={(ctx) => {+ ctx.addBackend({+ name: "Production",+ host: "https://app.all-hands.dev",+ apiKey: "bearer-key",+ kind: "cloud",+ });+ }}+ >+ <ManageBackendsModal onClose={vi.fn()} />+ </TestSeed>,+ );++ expect(+ await screen.findByText("BACKEND$PERSONAL_WORKSPACE"),+ ).toBeInTheDocument();+ expect(+ screen.queryByText(`user_${personalOrgId}_org`),+ ).not.toBeInTheDocument();+ });++ it("does not render an organization line for a local backend", async () => {+ renderWithProviders(+ <TestSeed+ onMount={(ctx) => {+ ctx.addBackend({+ name: "Acme Local",+ host: "http://localhost:9000",+ apiKey: "k",+ kind: "local",+ });+ }}+ >+ <ManageBackendsModal onClose={vi.fn()} />+ </TestSeed>,+ );++ expect(+ await screen.findByTestId("manage-backends-row-Acme Local"),+ ).toBeInTheDocument();+ expect(+ screen.queryByTestId("manage-backends-org-Acme Local"),+ ).not.toBeInTheDocument();+ });});function renderInQueryClient(ui: React.ReactElement) {diff --git a/src/components/features/backends/backend-row.tsx b/src/components/features/backends/backend-row.tsxindex ba8fb3df33..e2be7ac3db 100644--- a/src/components/features/backends/backend-row.tsx+++ b/src/components/features/backends/backend-row.tsx@@ -20,6 +20,7 @@ const ROW_ACTION_BUTTON_CLASS =interface BackendRowProps {backend: Backend;health: BackendHealth | undefined;+ orgLabel?: string;onSelect: () => void;onEdit: () => void;onRemove: () => void;@@ -29,6 +30,7 @@ interface BackendRowProps {export function BackendRow({backend,health,+ orgLabel,onSelect,onEdit,onRemove,@@ -80,6 +82,14 @@ export function BackendRow({<span className="truncate text-sm text-white">{backend.name}</span><BackendVersion backend={backend} /></div>+ {orgLabel ? (+ <span+ data-testid={`manage-backends-org-${backend.name}`}+ className="truncate text-xs text-[var(--oh-text-secondary)]"+ >+ {orgLabel}+ </span>+ ) : null}<span className="truncate text-xs text-[var(--oh-muted)]">{backend.host}</span>diff --git a/src/components/features/backends/manage-backends-modal.tsx b/src/components/features/backends/manage-backends-modal.tsxindex 34965a418b..3de5fce168 100644--- a/src/components/features/backends/manage-backends-modal.tsx+++ b/src/components/features/backends/manage-backends-modal.tsx@@ -13,6 +13,8 @@ import {import { ModalCloseButton } from "#/components/shared/modals/modal-close-button";import { useActiveBackendContext } from "#/contexts/active-backend-context";import { useBackendsHealth } from "#/hooks/query/use-backends-health";+import { useAllCloudOrganizations } from "#/hooks/query/use-cloud-organizations";+import { useCloudCurrentUserId } from "#/hooks/query/use-cloud-current-user-id";import { I18nKey } from "#/i18n/declaration";import { cn } from "#/utils/utils";import { modalTitleLgClassName } from "#/utils/modal-classes";@@ -33,6 +35,29 @@ interface PendingRemoval {name: string;}+/**+ * Resolve the connected organization label for a backend row, mirroring the+ * per-org labelling used by the backend selector. Cloud API keys are bound to+ * a single org (legacy keys may expose several, joined here); local backends+ * and unresolved/errored lookups yield no label.+ */+function resolveBackendOrgLabel(+ backend: Backend,+ cloudOrgs: ReturnType<typeof useAllCloudOrganizations>,+ currentUserIds: ReturnType<typeof useCloudCurrentUserId>,+ personalWorkspaceLabel: string,+): string | undefined {+ if (backend.kind !== "cloud") return undefined;+ const entry = cloudOrgs[backend.id];+ if (!entry || entry.orgs.length === 0) return undefined;+ const userId = currentUserIds[backend.id]?.userId ?? null;+ return entry.orgs+ .map((org) =>+ !!userId && userId === org.id ? personalWorkspaceLabel : org.name,+ )+ .join(", ");+}+export function ManageBackendsModal({onClose,recoveryMode = false,@@ -43,6 +68,9 @@ export function ManageBackendsModal({const healthByBackendId = useBackendsHealth(backends, {probeDisabledOnce: true,});+ const cloudOrgs = useAllCloudOrganizations();+ const currentUserIds = useCloudCurrentUserId();+ const personalWorkspaceLabel = t(I18nKey.BACKEND$PERSONAL_WORKSPACE);const [pendingRemoval, setPendingRemoval] =React.useState<PendingRemoval | null>(null);const [editingBackend, setEditingBackend] = React.useState<Backend | null>(@@ -118,6 +146,12 @@ export function ManageBackendsModal({key={backend.id}backend={backend}health={healthByBackendId[backend.id]}+ orgLabel={resolveBackendOrgLabel(+ backend,+ cloudOrgs,+ currentUserIds,+ personalWorkspaceLabel,+ )}onSelect={() => handleSelectBackend(backend)}onEdit={() => setEditingBackend(backend)}onRemove={() =>