Skip to content

An open-source, faster Devin alternative

TypeScript

fix: attach project GitHub installs directly (#836)

Mohamed Habib484d9ddb6d1f
diff --git a/web/src/managed-agents/GitHub.test.tsx b/web/src/managed-agents/GitHub.test.tsx
new file mode 100644
index 00000000..a663b1d3
--- /dev/null
+++ b/web/src/managed-agents/GitHub.test.tsx
@@ -0,0 +1,121 @@
+// @vitest-environment happy-dom
+
+import { act } from 'react'
+import { createRoot, type Root } from 'react-dom/client'
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+const api = vi.hoisted(() => ({
+ attachManagedGitHub: vi.fn(),
+ connectManagedGitHub: vi.fn(),
+ disconnectManagedGitHub: vi.fn(),
+ getManagedGitHubStatus: vi.fn(),
+}))
+const authorization = vi.hoisted(() => ({
+ launchAuthorizationWindow: vi.fn(),
+}))
+
+vi.mock('./api', () => api)
+vi.mock('./authorization-window', () => authorization)
+
+const { ManagedProjectGitHub } = await import('./GitHub')
+const { githubConnectionLabel } = await import('./github-connection')
+
+async function settle(until: () => boolean, label: string) {
+ for (let attempt = 0; attempt < 50; attempt += 1) {
+ if (until()) return
+ await act(async () => {
+ await new Promise((resolve) => setTimeout(resolve, 0))
+ })
+ }
+ throw new Error(`Timed out waiting for ${label}`)
+}
+
+describe('ManagedProjectGitHub', () => {
+ let container: HTMLDivElement
+ let root: Root
+ let client: QueryClient
+
+ beforeEach(() => {
+ ;(
+ globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }
+ ).IS_REACT_ACT_ENVIRONMENT = true
+ container = document.createElement('div')
+ document.body.append(container)
+ root = createRoot(container)
+ client = new QueryClient({
+ defaultOptions: {
+ queries: { retry: false },
+ mutations: { retry: false },
+ },
+ })
+ api.getManagedGitHubStatus.mockReset().mockResolvedValue({
+ environments: [
+ { environment: 'development', state: 'not_connected' },
+ { environment: 'production', state: 'not_connected' },
+ ],
+ connections: [],
+ app: { slug: 'opencomputer-app' },
+ })
+ api.connectManagedGitHub.mockReset().mockResolvedValue({
+ installUrl: 'https://github.com/apps/opencomputer-app/installations/new',
+ authorizeUrl: 'https://github.com/login/oauth/authorize',
+ })
+ authorization.launchAuthorizationWindow
+ .mockReset()
+ .mockImplementation(async (authorizationUrl: () => Promise<string>) => {
+ await authorizationUrl()
+ })
+ })
+
+ afterEach(() => {
+ act(() => root.unmount())
+ client.clear()
+ container.remove()
+ document.body.replaceChildren()
+ })
+
+ it('starts a project-scoped installation for the current environment', async () => {
+ act(() => {
+ root.render(
+ <QueryClientProvider client={client}>
+ <ManagedProjectGitHub
+ projectId="prj_rufus"
+ environment="development"
+ />
+ </QueryClientProvider>,
+ )
+ })
+ await settle(
+ () => container.textContent?.includes('Add GitHub connection') === true,
+ 'GitHub connection action',
+ )
+ const button = [...container.querySelectorAll('button')].find((candidate) =>
+ candidate.textContent?.includes('Add GitHub connection'),
+ )
+ if (!(button instanceof HTMLButtonElement)) {
+ throw new Error('Add GitHub connection button not found')
+ }
+
+ act(() => button.click())
+ await settle(
+ () => api.connectManagedGitHub.mock.calls.length === 1,
+ 'project-scoped GitHub connect request',
+ )
+
+ expect(api.connectManagedGitHub).toHaveBeenCalledWith({
+ projectId: 'prj_rufus',
+ environments: ['development'],
+ })
+ })
+
+ it('distinguishes installations for the same GitHub account', () => {
+ expect(
+ githubConnectionLabel({
+ accountLogin: 'diggerhq',
+ githubInstallationId: 164620985,
+ repositorySelection: 'selected',
+ }),
+ ).toBe('diggerhq · installation 164620985 · selected repositories')
+ })
+})
diff --git a/web/src/managed-agents/GitHub.tsx b/web/src/managed-agents/GitHub.tsx
index 4a788919..ca54b76f 100644
--- a/web/src/managed-agents/GitHub.tsx
+++ b/web/src/managed-agents/GitHub.tsx
@@ -14,12 +14,13 @@ import { StatusBadge } from '@/components/status-badge'
import { Button } from '@/components/ui/button'
import { notifyError, notifySuccess } from '@/lib/errors'
import {
- addManagedGitHubConnection,
attachManagedGitHub,
+ connectManagedGitHub,
disconnectManagedGitHub,
getManagedGitHubStatus,
} from './api'
import { launchAuthorizationWindow } from './authorization-window'
+import { githubConnectionLabel } from './github-connection'
export function ManagedProjectGitHub({
projectId,
@@ -155,13 +156,13 @@ export function ManagedProjectGitHub({
>
{availableConnections.map((connection) => (
<option key={connection.id} value={connection.id}>
- {connection.accountLogin}
+ {githubConnectionLabel(connection)}
</option>
))}
</select>
) : (
<span className="text-sm font-medium">
- {availableConnections[0].accountLogin}
+ {githubConnectionLabel(availableConnections[0])}
</span>
)}
<Button
@@ -182,7 +183,10 @@ export function ManagedProjectGitHub({
onClick={() => {
setAddingConnection(true)
void launchAuthorizationWindow(() =>
- addManagedGitHubConnection('install'),
+ connectManagedGitHub({
+ projectId,
+ environments: [environment],
+ }).then((connection) => connection.installUrl),
)
.catch((error: unknown) =>
notifyError(
diff --git a/web/src/managed-agents/github-connection.ts b/web/src/managed-agents/github-connection.ts
new file mode 100644
index 00000000..e07283fe
--- /dev/null
+++ b/web/src/managed-agents/github-connection.ts
@@ -0,0 +1,11 @@
+export function githubConnectionLabel(connection: {
+ accountLogin: string
+ githubInstallationId: number
+ repositorySelection: 'all' | 'selected'
+}) {
+ const repositories =
+ connection.repositorySelection === 'all'
+ ? 'all repositories'
+ : 'selected repositories'
+ return `${connection.accountLogin} · installation ${connection.githubInstallationId} · ${repositories}`
+}