diff --git a/ui/src/components/ProjectGitAuthorization.test.tsx b/ui/src/components/ProjectGitAuthorization.test.tsx index ef833504266..419d214477e 100644 --- a/ui/src/components/ProjectGitAuthorization.test.tsx +++ b/ui/src/components/ProjectGitAuthorization.test.tsx @@ -6,17 +6,71 @@ import type { Project } from "@paperclipai/shared"; import { describe, expect, it, vi } from "vitest"; import { projectsApi } from "@/api/projects"; import { ProjectGitAuthorization } from "./ProjectGitAuthorization"; +import { repositoryOptionsKey } from "./ProjectRepositoryInput"; import { replayLeaks } from "@/lib/analytics/replay-recording.test-utils"; vi.mock("@/api/projects", () => ({ projectsApi: { gitAuthorization: vi.fn(), + repositoryOptions: vi.fn(async () => ({ repositories: [], connectionCount: 0, failedConnectionCount: 0 })), setGitAuthorization: vi.fn(), }, })); (globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; describe("ProjectGitAuthorization", () => { + const repoUrl = "https://github.com/team/source"; + type SavedWorkspace = Pick; + const privateWorkspace: SavedWorkspace = { repoUrl, metadata: { githubRepositoryUrl: repoUrl, githubRepositoryPrivate: true } }; + const project = { id: "project-a", companyId: "company-a", workspaces: [privateWorkspace] } as Project; + + it("shows and loads authorization only while a saved repository is known to be private", async () => { + const client = new QueryClient(); + const container = document.createElement("div"); + const root = createRoot(container); + const catalogUrl = "https://github.com/team/older"; + const publicUrl = "https://github.com/team/public"; + const catalogSize = 500; + let catalogUrlReads = 0; + const otherRepositories = Array.from({ length: catalogSize }, (_, index) => ({ + id: String(index), fullName: `team/repo-${index}`, private: true, connections: [], + get url() { + if (++catalogUrlReads > catalogSize) throw new Error("Repeated catalog scan"); + return `https://github.com/team/repo-${index}`; + }, + })); + client.setQueryData(repositoryOptionsKey(project.companyId), { + connectionCount: 1, failedConnectionCount: 0, + repositories: [ + ...otherRepositories, + { id: "older", fullName: "team/older", url: catalogUrl, private: true, connections: [] }, + { id: "public", fullName: "team/public", url: publicUrl, private: false, connections: [] }, + ], + }); + vi.mocked(projectsApi.gitAuthorization).mockClear().mockResolvedValue({ grantId: null, authorizedByUserId: null, options: [] }); + const render = async (workspaces: SavedWorkspace[]) => { + await act(async () => root.render( + + )); + return container.querySelector('section[aria-label="Background GitHub authorization"]'); + }; + try { + expect(await render([])).toBeNull(); + const publicWorkspace = { repoUrl: publicUrl, metadata: { githubRepositoryUrl: publicUrl, githubRepositoryPrivate: true } }; + expect(await render([publicWorkspace, { repoUrl, metadata: null }])).toBeNull(); + expect(projectsApi.gitAuthorization).not.toHaveBeenCalled(); + expect(await render([publicWorkspace, { repoUrl: catalogUrl + ".git", metadata: null }])).not.toBeNull(); + expect(projectsApi.gitAuthorization).toHaveBeenCalledWith(project.id); + expect(await render([{ ...privateWorkspace, repoUrl: repoUrl + ".git/" }])).not.toBeNull(); + expect(await render([{ ...privateWorkspace, repoUrl: "https://github.com/team/another" }])).toBeNull(); + expect(await render([{ ...privateWorkspace, repoUrl: null }, publicWorkspace])).toBeNull(); + expect(catalogUrlReads).toBe(catalogSize); + } finally { + await act(async () => root.unmount()); + client.clear(); + } + }); + it("explains standing credential consent and revokes without retaining task context", async () => { const saved = { grantId: "grant-a", @@ -46,7 +100,7 @@ describe("ProjectGitAuthorization", () => { await act(async () => root.render( - + , ), ); @@ -96,7 +150,7 @@ describe("ProjectGitAuthorization", () => { await act(async () => root.render( - + , ), ); diff --git a/ui/src/components/ProjectGitAuthorization.tsx b/ui/src/components/ProjectGitAuthorization.tsx index 4930a41a51c..076b8d31473 100644 --- a/ui/src/components/ProjectGitAuthorization.tsx +++ b/ui/src/components/ProjectGitAuthorization.tsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useMemo, useState } from "react"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import type { Project } from "@paperclipai/shared"; import { projectsApi } from "@/api/projects"; @@ -6,8 +6,31 @@ import { Button } from "./ui/button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./ui/select"; import { ANALYTICS_MASK_CLASS } from "@/lib/analytics/privacy"; import { cn } from "@/lib/utils"; +import { useProjectRepositoryOptions } from "./ProjectRepositoryInput"; + +function repositoryUrlKey(url: string) { + return url.toLowerCase().replace(/\/$/, "").replace(/\.git$/, ""); +} export function ProjectGitAuthorization({ project }: { project: Project }) { + const repositories = useProjectRepositoryOptions(project.companyId).data?.repositories; + const visibilityByUrl = useMemo( + () => new Map(repositories?.map((repo) => [repositoryUrlKey(repo.url), repo.private])), + [repositories], + ); + const hasPrivateRepo = project.workspaces.some(({ repoUrl, metadata }) => { + if (!repoUrl) return false; + const url = repositoryUrlKey(repoUrl); + return visibilityByUrl.get(url) ?? ( + metadata?.githubRepositoryPrivate === true && + typeof metadata.githubRepositoryUrl === "string" && + repositoryUrlKey(metadata.githubRepositoryUrl) === url + ); + }); + return hasPrivateRepo ? : null; +} + +function GitAuthorizationForm({ project }: { project: Project }) { const client = useQueryClient(); const key = ["project-git-authorization", project.companyId, project.id]; const authorization = useQuery({ diff --git a/ui/src/components/ProjectRepositoryInput.tsx b/ui/src/components/ProjectRepositoryInput.tsx index 93cd4864512..b24bd4cf2fd 100644 --- a/ui/src/components/ProjectRepositoryInput.tsx +++ b/ui/src/components/ProjectRepositoryInput.tsx @@ -7,6 +7,10 @@ import { useQueryView } from "./QueryView"; export const repositoryOptionsKey = (companyId: string) => ["project-repositories", companyId] as const; +export function useProjectRepositoryOptions(companyId: string) { + return useQuery({ queryKey: repositoryOptionsKey(companyId), queryFn: () => projectsApi.repositoryOptions(companyId), staleTime: 30_000 }); +} + export function ProjectRepositoryInput({ companyId, selected, onChange, onConnect, disabled }: { companyId: string; selected: ProjectRepository[]; @@ -14,7 +18,7 @@ export function ProjectRepositoryInput({ companyId, selected, onChange, onConnec onConnect: () => void; disabled?: boolean; }) { - const query = useQuery({ queryKey: repositoryOptionsKey(companyId), queryFn: () => projectsApi.repositoryOptions(companyId), staleTime: 30_000 }); + const query = useProjectRepositoryOptions(companyId); // Read through the shared view so cached options stay visible while a transient // refetch fails; only a no-data failure lands in the error state with Retry. const view = useQueryView(query);