Skip to content

An open-source, faster Devin alternative

TypeScript

Work web: read the repositories the installations grant, with a preview mock

Igor Zalutski via Computer8fe9b734938d
diff --git a/web/src/api/mock-work.ts b/web/src/api/mock-work.ts
index 8e1248b2..63481232 100644
--- a/web/src/api/mock-work.ts
+++ b/web/src/api/mock-work.ts
@@ -36,6 +36,57 @@ const repo = {
}
let repository: typeof repo | null = repo
+// What the preview's GitHub installations grant (the computer mock's acme and
+// acme-labs), most recently pushed first as the server sends them.
+const granted = (
+ owner: string,
+ list: [name: string, isPrivate: boolean, pushed: number | null][],
+) =>
+ list.map(([name, isPrivate, pushed], i) => ({
+ id: (owner === 'acme' ? 1000 : 2000) + i,
+ owner,
+ name,
+ private: isPrivate,
+ defaultBranch: name === 'legacy-site' ? 'master' : 'main',
+ pushedAt: pushed === null ? null : ago(pushed),
+ }))
+const granting = [
+ {
+ id: '1',
+ accountLogin: 'acme',
+ accountType: 'Organization',
+ settingsUrl:
+ 'https://github.com/organizations/acme/settings/installations/41000001',
+ repositories: granted('acme', [
+ ['app', true, 12],
+ ['knowledge', true, 3 * H],
+ ['infra', true, 26 * H],
+ ['docs', false, 3 * D],
+ ['design-system', false, 9 * D],
+ ['mobile', true, 21 * D],
+ ['legacy-site', false, 140 * D],
+ ['scratch', true, null],
+ ]),
+ truncated: false,
+ problem: null,
+ problemMessage: null,
+ },
+ {
+ id: '2',
+ accountLogin: 'acme-labs',
+ accountType: 'Organization',
+ settingsUrl:
+ 'https://github.com/organizations/acme-labs/settings/installations/41000002',
+ repositories: granted('acme-labs', [
+ ['experiments', true, 2 * D],
+ ['notes', false, 30 * D],
+ ]),
+ truncated: false,
+ problem: null,
+ problemMessage: null,
+ },
+]
+
const pr = (n: number, state: string | null, title: string) => ({
url: `https://github.com/acme/app/pull/${n}`,
state,
@@ -394,6 +445,9 @@ export function workMock(
}
return { repository: current }
}
+ if (rest === '/repositories' && method === 'GET') {
+ return { installations: granting }
+ }
const mineEmpty = s === 'mine-empty'
const visible = (list: Json[]) =>
s === 'empty' || s === 'building' || !current
diff --git a/web/src/managed-agents/work/api.test.ts b/web/src/managed-agents/work/api.test.ts
index c3e00b5d..b614db18 100644
--- a/web/src/managed-agents/work/api.test.ts
+++ b/web/src/managed-agents/work/api.test.ts
@@ -1,6 +1,7 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import {
getOpenItems,
+ getRepositories,
keepOpenItems,
putRepository,
type OpenItemsPoll,
@@ -115,3 +116,40 @@ describe('repository setting', () => {
).toBeTruthy()
})
})
+
+describe('repositories the installations grant', () => {
+ it('reads them for the picker', async () => {
+ const installations = [
+ {
+ id: '1',
+ accountLogin: 'acme',
+ accountType: 'Organization',
+ settingsUrl:
+ 'https://github.com/organizations/acme/settings/installations/41',
+ repositories: [
+ {
+ id: 7,
+ owner: 'acme',
+ name: 'knowledge',
+ private: true,
+ defaultBranch: 'main',
+ pushedAt: null,
+ },
+ ],
+ truncated: false,
+ problem: null,
+ problemMessage: null,
+ },
+ ]
+ const fetch = vi
+ .fn()
+ .mockResolvedValue(
+ new Response(JSON.stringify({ installations }), { status: 200 }),
+ )
+ vi.stubGlobal('fetch', fetch)
+ expect(await getRepositories()).toEqual(installations)
+ expect(fetch.mock.calls[0][0]).toBe(
+ '/api/managed-agents/computer/work/repositories',
+ )
+ })
+})
diff --git a/web/src/managed-agents/work/api.ts b/web/src/managed-agents/work/api.ts
index 5fcbdf3f..ccad0550 100644
--- a/web/src/managed-agents/work/api.ts
+++ b/web/src/managed-agents/work/api.ts
@@ -139,6 +139,36 @@ const RepositoryResultSchema = z.looseObject({
repository: RepositorySchema.nullable(),
})
+const GrantedRepositorySchema = z.looseObject({
+ id: z.number(),
+ owner: z.string(),
+ name: z.string(),
+ private: z.boolean(),
+ defaultBranch: z.string(),
+ // Null for a repository never pushed to.
+ pushedAt: z.string().nullable(),
+})
+export type GrantedRepository = z.infer<typeof GrantedRepositorySchema>
+
+// One of the organization's GitHub installations and the repositories it
+// grants, most recently pushed first (the server's order). `settingsUrl` is
+// the installation's page on GitHub, where a person grants it more.
+const GrantingInstallationSchema = z.looseObject({
+ id: z.string(),
+ accountLogin: z.string(),
+ accountType: z.string(),
+ settingsUrl: z.string(),
+ repositories: z.array(GrantedRepositorySchema),
+ truncated: z.boolean(),
+ problem: z.string().nullable(),
+ problemMessage: z.string().nullable(),
+})
+export type GrantingInstallation = z.infer<typeof GrantingInstallationSchema>
+
+const RepositoriesSchema = z.looseObject({
+ installations: z.array(GrantingInstallationSchema),
+})
+
export type RepositoryChoice = {
installationId: string | number
owner: string
@@ -168,6 +198,16 @@ export function putRepository(choice: RepositoryChoice) {
).then((r) => r.repository)
}
+/** What each of the organization's GitHub installations grants, for the picker. */
+export function getRepositories() {
+ return apiFetch(
+ `${PREFIX}/repositories`,
+ undefined,
+ RepositoriesSchema,
+ '/api',
+ ).then((r) => r.installations)
+}
+
/** What the open-items poll keeps: the page and the ETag it came with. */
export type OpenItemsPoll = { etag: string | null; page: OpenItems }
@@ -251,6 +291,7 @@ export function getHistory(number: number, cursor: string | undefined) {
export const workKeys = {
all: ['work'] as const,
repository: (org: string) => ['work', org, 'repository'] as const,
+ repositories: (org: string) => ['work', org, 'repositories'] as const,
open: (org: string) => ['work', org, 'items', 'open'] as const,
closed: (org: string) => ['work', org, 'items', 'closed'] as const,
item: (org: string, n: number) => ['work', org, 'item', n] as const,