diff --git a/src/renderer/src/components/orca-cloud/CloudSetupNotices.test.tsx b/src/renderer/src/components/orca-cloud/CloudSetupNotices.test.tsx index 1ec240795ca..6e6166a2273 100644 --- a/src/renderer/src/components/orca-cloud/CloudSetupNotices.test.tsx +++ b/src/renderer/src/components/orca-cloud/CloudSetupNotices.test.tsx @@ -12,8 +12,11 @@ const { pushAccess, copyRequest } = vi.hoisted(() => ({ copyRequest: vi.fn(async () => true) })) vi.mock('@/lib/orca-cloud/cloud-push-access', () => ({ - fetchCloudPushAccess: pushAccess, - copyOwnerPushRequest: copyRequest + fetchCloudPushAccess: pushAccess +})) +// The shared request (owner-request.ts) reads the install link from repo access, then copies. +vi.mock('@/lib/orca-cloud/use-orca-cloud-repo-access', () => ({ + fetchOrcaCloudRepoAccess: async () => null })) vi.mock('@/store', () => ({ useAppStore: (select: (state: unknown) => unknown) => @@ -116,12 +119,19 @@ describe('CloudSetupNoticesDock (DS-o, DS-q)', () => { repoUrl: 'https://github.com/acme/web', notices: [PUSH] }) + Object.defineProperty(window, 'api', { + configurable: true, + value: { ui: { writeClipboardText: copyRequest } } + }) render() await act(async () => {}) const [, shown] = stack.show.mock.calls.at(-1) ?? [] expect(shown.lines[0].action).toMatchObject({ label: 'Copy request for an owner' }) await act(async () => shown.lines[0].action.onClick()) - expect(copyRequest).toHaveBeenCalledWith('https://github.com/acme/web') + expect(copyRequest).toHaveBeenCalledWith( + expect.stringContaining('push from workspaces on this repository') + ) + expect(copyRequest).toHaveBeenCalledWith(expect.stringContaining('acme/web')) const [, copied] = stack.show.mock.calls.at(-1) ?? [] expect(copied.lines[0].action).toMatchObject({ label: 'Copied' }) }) diff --git a/src/renderer/src/components/orca-cloud/cloud-push-notice-line.tsx b/src/renderer/src/components/orca-cloud/cloud-push-notice-line.tsx index 394877a1ae6..472c3c39faa 100644 --- a/src/renderer/src/components/orca-cloud/cloud-push-notice-line.tsx +++ b/src/renderer/src/components/orca-cloud/cloud-push-notice-line.tsx @@ -5,6 +5,7 @@ import { Check, Copy, GitPullRequestArrow, Loader2, RefreshCw } from 'lucide-rea import type { Translate } from '@/i18n/use-translate' import { repoOwner, repoSlug } from '@/lib/orca-cloud/cloud-setup-notice-actions' import type { CloudPushAccess } from '@/lib/orca-cloud/cloud-push-access' +import { ownerRequestLabel } from '@/lib/orca-cloud/owner-request' import type { CloudSetupNoticeLine } from './CloudSetupNotices' export type CloudPushLineHandlers = { @@ -17,15 +18,13 @@ export type CloudPushLineHandlers = { rechecking?: boolean } -/** Copy a request for an owner (pushRequestMessage), reading "Copied" for a moment after. */ +/** Copy a request for an owner (owner-request.ts), reading "Copied" for a moment after. */ function copyRequestAction( h: CloudPushLineHandlers, t: Translate ): NonNullable { return { - label: h.copied - ? t('orcaCloud.setupNotices.copied', 'Copied') - : t('orcaCloud.setupNotices.copyRequest', 'Copy request for an owner'), + label: ownerRequestLabel(h.copied === true, t), icon: h.copied ? : , run: h.copyRequest } diff --git a/src/renderer/src/components/orca-cloud/use-cloud-push-notice.ts b/src/renderer/src/components/orca-cloud/use-cloud-push-notice.ts index b75e767164a..a6e411c445e 100644 --- a/src/renderer/src/components/orca-cloud/use-cloud-push-notice.ts +++ b/src/renderer/src/components/orca-cloud/use-cloud-push-notice.ts @@ -1,24 +1,19 @@ // The "can't push" notice's state: what installing can do for this person (cloud-push-access), the // re-checks after Install, Copy request's brief "Copied", and Check again where the App already // reaches the repository. +import { useOwnerRequestCopy } from '@/lib/orca-cloud/owner-request' import { useEffect, useRef, useState } from 'react' import { openOrcaGitHubAppInstall, recheckCloudPushAccess } from '@/lib/orca-cloud/cloud-setup-notice-actions' -import { - copyOwnerPushRequest, - fetchCloudPushAccess, - type CloudPushAccess -} from '@/lib/orca-cloud/cloud-push-access' +import { fetchCloudPushAccess, type CloudPushAccess } from '@/lib/orca-cloud/cloud-push-access' // After Install: GitHub's webhook usually lands within seconds, then the owner may take a while. const FAST_POLL_MS = 2_000 const FAST_POLL_WINDOW_MS = 3 * 60_000 const SLOW_POLL_MS = 10_000 const GIVE_UP_MS = 30 * 60_000 -/** How long "Copied" stays on the request button. */ -export const COPIED_MS = 1_500 export type CloudPushNoticeState = { access: CloudPushAccess @@ -137,9 +132,7 @@ export function useCloudPushNotice( ): CloudPushNoticeState { const access = usePushAccess(repoUrl, hasPush) const push = usePushRecheck(environmentId, hasPush) - const [copied, setCopied] = useState(false) - const copiedTimer = useRef | undefined>(undefined) - useEffect(() => () => clearTimeout(copiedTimer.current), []) + const request = useOwnerRequestCopy(repoUrl, 'push') const [rechecking, setRechecking] = useState(false) const recheckBusy = useRef(false) return { @@ -150,19 +143,8 @@ export function useCloudPushNotice( push.startInstall(repoUrl) } }, - copyRequest: () => { - if (!repoUrl) { - return - } - void copyOwnerPushRequest(repoUrl).then((ok) => { - if (ok) { - setCopied(true) - clearTimeout(copiedTimer.current) - copiedTimer.current = setTimeout(() => setCopied(false), COPIED_MS) - } - }) - }, - copied, + copyRequest: request.copy, + copied: request.copied, recheck: () => { if (!environmentId || recheckBusy.current) { return diff --git a/src/renderer/src/components/worktree-creation/clone-access-actions.tsx b/src/renderer/src/components/worktree-creation/clone-access-actions.tsx index 86e82dc152b..a4fc4a59d98 100644 --- a/src/renderer/src/components/worktree-creation/clone-access-actions.tsx +++ b/src/renderer/src/components/worktree-creation/clone-access-actions.tsx @@ -3,6 +3,7 @@ import { Check, Copy, FolderSearch, Github, Loader2 } from 'lucide-react' import type { Translate } from '@/i18n/use-translate' import type { CloneAccessPhase } from './use-clone-access-handoff' +import { ownerRequestLabel } from '@/lib/orca-cloud/owner-request' import { repoOwner } from './vm-build-clone-failure-copy' export type CloneAccessHandlers = { @@ -84,9 +85,7 @@ export function cloneAccessActions( return [ { key: 'copyRequest', - label: access.copied - ? t('worktreeCreation.cloneAccess.copied', 'Copied') - : t('worktreeCreation.cloneAccess.copyRequest', 'Copy request for an owner'), + label: ownerRequestLabel(access.copied === true, t), icon: access.copied ? : , onClick: access.copyRequest }, diff --git a/src/renderer/src/components/worktree-creation/use-clone-access-handoff.test.tsx b/src/renderer/src/components/worktree-creation/use-clone-access-handoff.test.tsx index 694b114effc..924f1898b4f 100644 --- a/src/renderer/src/components/worktree-creation/use-clone-access-handoff.test.tsx +++ b/src/renderer/src/components/worktree-creation/use-clone-access-handoff.test.tsx @@ -18,7 +18,6 @@ vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } })) import { CLONE_ACCESS_WAIT_MS, - copyOwnerRequest, githubRepoUrlFor, useCloneAccessHandoff } from './use-clone-access-handoff' @@ -121,17 +120,6 @@ describe('CLONE-ACCESS-ERROR: a clone GitHub refused', () => { await waitFor(() => expect(hook.result.current.phase).toBe('missing')) }) - it('the request for an owner carries the install link that preselects the repository', async () => { - mocks.access.mockResolvedValue({ - ...answer('requested'), - installUrl: 'https://github.com/apps/orca/install' - }) - await copyOwnerRequest(REPO, githubRepoUrlFor(REPO)) - const [text] = mocks.clipboard.mock.calls[0] ?? [] - expect(text).toContain(REPO) - expect(text).toContain('https://github.com/apps/orca/install') - }) - it('names only owner/repo as a GitHub repository', () => { expect(githubRepoUrlFor('stablyai/orca')).toBe('https://github.com/stablyai/orca') expect(githubRepoUrlFor('My project')).toBeNull() diff --git a/src/renderer/src/components/worktree-creation/use-clone-access-handoff.ts b/src/renderer/src/components/worktree-creation/use-clone-access-handoff.ts index fa58a208f1a..eb829c86f28 100644 --- a/src/renderer/src/components/worktree-creation/use-clone-access-handoff.ts +++ b/src/renderer/src/components/worktree-creation/use-clone-access-handoff.ts @@ -3,10 +3,9 @@ // (repository preselected), and once access shows up starts the create again on its own. Someone // who can't install (GitHub turns their install into a request for an owner) is told who to ask. import { useCallback, useEffect, useRef, useState } from 'react' -import { toast } from 'sonner' -import { translate } from '@/i18n/i18n' import { openOrcaGitHubAppInstall } from '@/lib/orca-cloud/cloud-setup-notice-actions' import { fetchOrcaCloudRepoAccess } from '@/lib/orca-cloud/use-orca-cloud-repo-access' +import { useOwnerRequestCopy } from '@/lib/orca-cloud/owner-request' /** * `checking`: asking GitHub. `missing`: the App doesn't reach it (Give Orca access). `waiting`: @@ -17,8 +16,6 @@ export type CloneAccessPhase = 'checking' | 'missing' | 'waiting' | 'requested' /** Re-check delays while focused after Give Orca access (as REPO-ACCESS-REFRESH backs off). */ export const CLONE_ACCESS_POLL_DELAYS_MS = [4_000, 4_000, 8_000, 15_000, 30_000] as const -/** How long "Copied" stays on the request button. */ -export const COPIED_MS = 1_500 /** Stop waiting after this long: the button comes back. */ export const CLONE_ACCESS_WAIT_MS = 3 * 60_000 @@ -150,36 +147,7 @@ export function useCloneAccessHandoff({ }) }, [repoUrl]) - const [copied, setCopied] = useState(false) - const copiedTimer = useRef(undefined) - useEffect(() => () => window.clearTimeout(copiedTimer.current), []) - const copyRequest = useCallback(() => { - void copyOwnerRequest(repoName, repoUrl).then((ok) => { - if (ok) { - setCopied(true) - window.clearTimeout(copiedTimer.current) - copiedTimer.current = window.setTimeout(() => setCopied(false), COPIED_MS) - } - }) - }, [repoName, repoUrl]) + const { copy: copyRequest, copied } = useOwnerRequestCopy(repoUrl, 'start') return { phase, giveAccess, copyRequest, copied } } - -/** A message for an owner of the repository's org, with the install link that preselects it. */ -export async function copyOwnerRequest(repoName: string, repoUrl: string | null): Promise { - // The repo-access answer carries GitHub's install page with this repository preselected. - const link = repoUrl ? ((await fetchOrcaCloudRepoAccess(repoUrl))?.installUrl ?? null) : null - const message = translate( - 'worktreeCreation.cloneAccess.requestMessage', - 'Could you give the Orca GitHub App access to {{repo}}? Orca Cloud needs it to start workspaces from this repository.', - { repo: repoName } - ) - try { - await window.api.ui.writeClipboardText(link ? `${message}\n${link}` : message) - return true - } catch { - toast.error(translate('worktreeCreation.cloneAccess.requestCopyFailed', 'Couldn’t copy it.')) - return false - } -} diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 07edd30aea1..974c8862c7b 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -28603,16 +28603,19 @@ "diskVeryLowMeaning": "Installs and builds may fail. Delete caches or build output you don’t need.", "diskLowMeaning": "Large dependency installs may run out of space.", "pushRequested": "Orca Cloud can read {{repo}} but can’t push yet. An owner of {{owner}} has been asked to approve the Orca GitHub App.", - "copied": "Copied", - "copyRequest": "Copy request for an owner", "pushCantInstall": "Orca Cloud can read {{repo}} but can’t push to it.", "pushCantInstallMeaning": "Pushing needs the Orca GitHub App on {{owner}}. Ask an owner of {{owner}} to install it.", - "pushNotYet": "Orca Cloud can’t push to {{repo}} yet.", - "pushRequestMessage": "Could you give the Orca GitHub App access to {{repo}}? Orca Cloud needs it to push from workspaces on this repository.", - "pushRequestCopyFailed": "Couldn’t copy it." + "pushNotYet": "Orca Cloud can’t push to {{repo}} yet." }, "toast": { "dismiss": "Dismiss" + }, + "ownerRequest": { + "messagePush": "Could you give the Orca GitHub App access to {{repo}}? Orca Cloud needs it to push from workspaces on this repository.", + "messageStart": "Could you give the Orca GitHub App access to {{repo}}? Orca Cloud needs it to start workspaces from this repository.", + "copyFailed": "Couldn’t copy it.", + "copied": "Copied", + "copy": "Copy request for an owner" } }, "runActions": { @@ -29622,12 +29625,8 @@ "askOwnerBody": "Ask an owner of {{owner}} to give Orca access. Orca tries again once they do. The sandbox was deleted.", "body": "The Orca GitHub App on {{owner}} doesn’t include this repository. Give Orca access, and Orca tries again. The sandbox was deleted.", "chooseAnother": "Choose another repository", - "copyRequest": "Copy request for an owner", "waiting": "Waiting for GitHub…", - "giveAccess": "Give Orca access", - "requestMessage": "Could you give the Orca GitHub App access to {{repo}}? Orca Cloud needs it to start workspaces from this repository.", - "requestCopyFailed": "Couldn’t copy it.", - "copied": "Copied" + "giveAccess": "Give Orca access" } }, "renderer": { diff --git a/src/renderer/src/lib/orca-cloud/cloud-push-access.ts b/src/renderer/src/lib/orca-cloud/cloud-push-access.ts index 9b8cf994696..ef5d855436b 100644 --- a/src/renderer/src/lib/orca-cloud/cloud-push-access.ts +++ b/src/renderer/src/lib/orca-cloud/cloud-push-access.ts @@ -2,13 +2,10 @@ // the Orca GitHub App's access (repo-access) and what the person's own gh login says about installing // it. Install only shows where installing can work; a request already sent offers a copy for an owner; // a public repository someone else owns (octocat/Hello-World) gets a request to copy, no Install. -import { toast } from 'sonner' -import { translate } from '@/i18n/i18n' import type { HostedGithubRepoAccessResponse } from '../../../../shared/hosted-sandbox/hosted-github-wire' import type { GitHubInstallRights } from '../../../../shared/hosted-sandbox/cloud-workspace-api' import { cloudWorkspaceApi } from './orca-cloud-workspace-store' import { fetchOrcaCloudRepoAccess } from './use-orca-cloud-repo-access' -import { repoSlug } from './cloud-setup-notice-actions' /** * `install`: Install on {owner}. `requested`: an owner was asked; copy a request for them. @@ -64,21 +61,3 @@ export async function fetchCloudPushAccess( .catch(() => null) return cloudPushAccess(access, rights ?? null) } - -/** A message for an owner of the repository's org, with the install page that preselects it. */ -export async function copyOwnerPushRequest(repoUrl: string): Promise { - // The install page the control plane minted for this repository (preselects it), if any. - const link = (await fetchOrcaCloudRepoAccess(repoUrl).catch(() => null))?.installUrl ?? null - const message = translate( - 'orcaCloud.setupNotices.pushRequestMessage', - 'Could you give the Orca GitHub App access to {{repo}}? Orca Cloud needs it to push from workspaces on this repository.', - { repo: repoSlug(repoUrl) } - ) - try { - await window.api.ui.writeClipboardText(link ? `${message}\n${link}` : message) - return true - } catch { - toast.error(translate('orcaCloud.setupNotices.pushRequestCopyFailed', 'Couldn’t copy it.')) - return false - } -} diff --git a/src/renderer/src/lib/orca-cloud/owner-request.test.ts b/src/renderer/src/lib/orca-cloud/owner-request.test.ts new file mode 100644 index 00000000000..00adc81b9aa --- /dev/null +++ b/src/renderer/src/lib/orca-cloud/owner-request.test.ts @@ -0,0 +1,51 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +const mocks = vi.hoisted(() => ({ access: vi.fn(), clipboard: vi.fn() })) +vi.mock('./use-orca-cloud-repo-access', () => ({ fetchOrcaCloudRepoAccess: mocks.access })) +vi.mock('sonner', () => ({ toast: { error: vi.fn() } })) + +import { translate } from '@/i18n/i18n' +import { copyOwnerRequest, ownerRequestLabel } from './owner-request' + +const REPO_URL = 'https://github.com/stablyai/orca-plugins' + +describe('Copy request for an owner (REQUEST-COPY-SHARE)', () => { + beforeEach(() => { + Object.defineProperty(globalThis, 'window', { + configurable: true, + value: { api: { ui: { writeClipboardText: mocks.clipboard } } } + }) + }) + afterEach(() => { + mocks.access.mockReset() + mocks.clipboard.mockReset() + }) + + it('names the repository and what the access is for, with the install link that preselects it', async () => { + mocks.access.mockResolvedValue({ + access: 'requested', + installUrl: 'https://github.com/apps/orca/install' + }) + await expect(copyOwnerRequest(REPO_URL, 'start')).resolves.toBe(true) + const [start] = mocks.clipboard.mock.calls[0] ?? [] + expect(start).toContain('stablyai/orca-plugins') + expect(start).toContain('start workspaces from this repository') + expect(start).toContain('https://github.com/apps/orca/install') + await copyOwnerRequest(REPO_URL, 'push') + const [push] = mocks.clipboard.mock.calls[1] ?? [] + expect(push).toContain('push from workspaces on this repository') + }) + + it('without an install link the message still goes; a clipboard failure says so', async () => { + mocks.access.mockResolvedValue(null) + await copyOwnerRequest(REPO_URL, 'push') + expect(mocks.clipboard.mock.calls[0]?.[0]).not.toContain('\n') + mocks.clipboard.mockRejectedValueOnce(new Error('no clipboard')) + await expect(copyOwnerRequest(REPO_URL, 'push')).resolves.toBe(false) + }) + + it('one label for both surfaces, Copied for a moment after', () => { + expect(ownerRequestLabel(false, translate)).toBe('Copy request for an owner') + expect(ownerRequestLabel(true, translate)).toBe('Copied') + }) +}) diff --git a/src/renderer/src/lib/orca-cloud/owner-request.ts b/src/renderer/src/lib/orca-cloud/owner-request.ts new file mode 100644 index 00000000000..71d4757a6d9 --- /dev/null +++ b/src/renderer/src/lib/orca-cloud/owner-request.ts @@ -0,0 +1,78 @@ +// "Copy request for an owner" (REQUEST-COPY-SHARE): someone who can't install the Orca GitHub App +// copies a message for an owner of the repository's org, with the install page that preselects +// the repository. Shared by the refused-clone card (CLONE-ACCESS-ERROR) and the push notice. +import { useCallback, useEffect, useRef, useState } from 'react' +import { toast } from 'sonner' +import { translate } from '@/i18n/i18n' +import type { Translate } from '@/i18n/use-translate' +import { fetchOrcaCloudRepoAccess } from './use-orca-cloud-repo-access' + +/** What the access is for: starting workspaces from the repository, or pushing from them. */ +export type OwnerRequestPurpose = 'start' | 'push' + +/** How long "Copied" stays on the button. */ +export const OWNER_REQUEST_COPIED_MS = 1_500 + +function repoSlugOf(repoUrl: string): string { + return repoUrl.replace(/^https:\/\/github\.com\//, '').replace(/\.git$/, '') +} + +function requestMessage(repo: string, purpose: OwnerRequestPurpose): string { + return purpose === 'push' + ? translate( + 'orcaCloud.ownerRequest.messagePush', + 'Could you give the Orca GitHub App access to {{repo}}? Orca Cloud needs it to push from workspaces on this repository.', + { repo } + ) + : translate( + 'orcaCloud.ownerRequest.messageStart', + 'Could you give the Orca GitHub App access to {{repo}}? Orca Cloud needs it to start workspaces from this repository.', + { repo } + ) +} + +/** Copies the request with the install link from the repo-access answer, when it has one. */ +export async function copyOwnerRequest( + repoUrl: string, + purpose: OwnerRequestPurpose +): Promise { + const link = (await fetchOrcaCloudRepoAccess(repoUrl).catch(() => null))?.installUrl ?? null + const message = requestMessage(repoSlugOf(repoUrl), purpose) + try { + await window.api.ui.writeClipboardText(link ? `${message}\n${link}` : message) + return true + } catch { + toast.error(translate('orcaCloud.ownerRequest.copyFailed', 'Couldn’t copy it.')) + return false + } +} + +/** The button's label: "Copied" for a moment after a copy. */ +export function ownerRequestLabel(copied: boolean, t: Translate): string { + return copied + ? t('orcaCloud.ownerRequest.copied', 'Copied') + : t('orcaCloud.ownerRequest.copy', 'Copy request for an owner') +} + +/** Copy the request, and say "Copied" on the button for OWNER_REQUEST_COPIED_MS. */ +export function useOwnerRequestCopy( + repoUrl: string | null, + purpose: OwnerRequestPurpose +): { copy: () => void; copied: boolean } { + const [copied, setCopied] = useState(false) + const timer = useRef | undefined>(undefined) + useEffect(() => () => clearTimeout(timer.current), []) + const copy = useCallback(() => { + if (!repoUrl) { + return + } + void copyOwnerRequest(repoUrl, purpose).then((ok) => { + if (ok) { + setCopied(true) + clearTimeout(timer.current) + timer.current = setTimeout(() => setCopied(false), OWNER_REQUEST_COPIED_MS) + } + }) + }, [purpose, repoUrl]) + return { copy, copied } +}