Skip to content

An open-source, faster Devin alternative

TypeScript

Allow the GitHub repositories route at the API edge and document it

Igor Zalutski7ce906fa5e78
diff --git a/cloudflare-workers/api-edge/src/managed_agents.test.ts b/cloudflare-workers/api-edge/src/managed_agents.test.ts
index 7b8630d0..0d144825 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.test.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.test.ts
@@ -92,6 +92,107 @@ describe("managed agents proxy", () => {
);
});
+ it("forwards the GitHub repository listing with its query and the page it returns", async () => {
+ const fetchMock = vi.fn().mockResolvedValue(
+ Response.json({
+ repositories: [
+ {
+ id: 1,
+ fullName: "octo-org/service",
+ private: true,
+ defaultBranch: "main",
+ archived: false,
+ },
+ ],
+ nextCursor: "2",
+ }),
+ );
+ vi.stubGlobal("fetch", fetchMock);
+ const response = await proxyManagedAgents(
+ new Request(
+ "https://mo-oc-dev.com/api/managed-agents/projects/prj_test/github/repositories?environment=development&limit=100&cursor=2",
+ ),
+ {
+ MANAGED_AGENTS_API_URL: "https://manage-agents.mo-oc-dev.com",
+ OC_MANAGED_AGENTS_SECRET: "test-secret",
+ },
+ { orgID: "org_test", userID: "user_test" },
+ "/api/managed-agents",
+ );
+
+ expect(response.status).toBe(200);
+ await expect(response.json()).resolves.toEqual({
+ repositories: [
+ {
+ id: 1,
+ fullName: "octo-org/service",
+ private: true,
+ defaultBranch: "main",
+ archived: false,
+ },
+ ],
+ nextCursor: "2",
+ });
+ expect(fetchMock.mock.calls[0]?.[0].toString()).toBe(
+ "https://manage-agents.mo-oc-dev.com/v1/projects/prj_test/github/repositories?environment=development&limit=100&cursor=2",
+ );
+ });
+
+ it("passes the repository listing's failure codes through", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue(
+ Response.json(
+ {
+ error: {
+ code: "github_connection_not_found",
+ message: "GitHub is not connected to the production environment",
+ },
+ },
+ { status: 404 },
+ ),
+ ),
+ );
+ const response = await proxyManagedAgents(
+ new Request(
+ "https://mo-oc-dev.com/api/managed-agents/projects/prj_test/github/repositories?environment=production",
+ ),
+ {
+ MANAGED_AGENTS_API_URL: "https://manage-agents.mo-oc-dev.com",
+ OC_MANAGED_AGENTS_SECRET: "test-secret",
+ },
+ { orgID: "org_test", userID: "user_test" },
+ "/api/managed-agents",
+ );
+ expect(response.status).toBe(404);
+ // The code is what a client branches on; the message is the edge's public copy.
+ await expect(response.json()).resolves.toEqual({
+ error: {
+ code: "github_connection_not_found",
+ message: "The requested agent resource was not found.",
+ },
+ });
+ });
+
+ it("only reads the repository listing", async () => {
+ const fetchSpy = vi.fn();
+ vi.stubGlobal("fetch", fetchSpy);
+ const response = await proxyManagedAgents(
+ new Request(
+ "https://mo-oc-dev.com/api/managed-agents/projects/prj_test/github/repositories?environment=development",
+ { method: "POST", body: "{}" },
+ ),
+ {
+ MANAGED_AGENTS_API_URL: "https://manage-agents.mo-oc-dev.com",
+ OC_MANAGED_AGENTS_SECRET: "test-secret",
+ },
+ { orgID: "org_test", userID: "user_test" },
+ "/api/managed-agents",
+ );
+ expect(response.status).toBe(404);
+ expect(fetchSpy).not.toHaveBeenCalled();
+ });
+
it("forwards the unauthenticated GitHub setup callback as HTML", async () => {
const fetchMock = vi.fn().mockResolvedValue(
new Response("<html>connected</html>", {
diff --git a/cloudflare-workers/api-edge/src/managed_agents.ts b/cloudflare-workers/api-edge/src/managed_agents.ts
index ec43ce53..4c261729 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.ts
@@ -1106,7 +1106,7 @@ function publicSuccessBody(
}
if (
/^\/github(?:\/connect)?$/.test(suffix) ||
- /^\/projects\/[^/]+\/github(?:\/(?:connect|attach))?$/.test(suffix)
+ /^\/projects\/[^/]+\/github(?:\/(?:connect|attach|repositories))?$/.test(suffix)
) {
return stripPrivateValues(body);
}
@@ -1650,6 +1650,9 @@ function isAllowedManagedAgentsRoute(method: string, suffix: string): boolean {
) {
return true;
}
+ if (method === "GET" && /^\/projects\/[^/]+\/github\/repositories$/.test(suffix)) {
+ return true;
+ }
if (
method === "POST" &&
/^\/projects\/[^/]+\/github\/(connect|attach)$/.test(suffix)
diff --git a/docs/agents/api.mdx b/docs/agents/api.mdx
index 30f95856..d5cc6eb9 100644
--- a/docs/agents/api.mdx
+++ b/docs/agents/api.mdx
@@ -307,6 +307,7 @@ treat that text as data about another agent's work, not as instructions.
| List agents | `GET /agents` | `200` with `{ agents }`: `id`, `name`, `activeAlias`, `activeDeploymentId`, `deploymentCount` |
| List deployments | `GET /deployments?agentId=<agent-id>` | `200` with `{ deployments }` |
| Get deployment | `GET /deployments/<id>` | `200` with `id`, `agentId`, `alias`, `memory` declarations, `createdAt` |
+| List GitHub repositories | `GET /projects/<p>/github/repositories?environment=<env>` | `200` with `{ repositories, nextCursor }`; see [GitHub App connections](/agents/github#list-the-repositories-an-environment-can-reach) |
A project is `{ id, slug, name, environments, agents, createdAt, updatedAt }`;
`agents` carries each agent's `id` and `name`. See
diff --git a/docs/agents/github.mdx b/docs/agents/github.mdx
index faad1dcb..b2e7cc17 100644
--- a/docs/agents/github.mdx
+++ b/docs/agents/github.mdx
@@ -64,7 +64,52 @@ installation; if specific repositories are selected, it can access only those
repositories. Change the selection in GitHub's installation settings.
Connections are attached independently to the project's development and
-production environments.
+production environments. A session uses the installation attached to the
+environment it was created in: a session started as `worker@production` gets
+the production installation's credentials even when the same deployment is
+also the development alias. Promote one artifact to both environments with
+different installations and each environment's sessions reach only their own
+installation's repositories.
+
+## List the repositories an environment can reach
+
+An application that lets a person pick a repository reads the installation's
+current selection from the connection rather than holding a GitHub credential
+of its own:
+
+```text
+GET /projects/<p>/github/repositories?environment=<env>&cursor=&limit=
+```
+
+`environment` is `development` or `production` and is required. `limit` is at
+most 100, the default; `cursor` is the `nextCursor` of the previous page.
+
+```json
+{
+ "repositories": [
+ {
+ "id": 1296269,
+ "fullName": "acme/service",
+ "private": true,
+ "defaultBranch": "main",
+ "archived": false
+ }
+ ],
+ "nextCursor": null
+}
+```
+
+The list is read live from GitHub each time, with a token minted for that read
+that can see repository metadata and nothing else. OpenComputer keeps no copy:
+change the selection in GitHub's installation settings and the next read shows
+it. It is available to the organization's API key, not to project tokens.
+
+| Status | Code | Meaning |
+| --- | --- | --- |
+| `400` | `invalid_environment`, `invalid_limit`, `invalid_cursor` | The query is malformed |
+| `404` | `github_connection_not_found` | The environment has no GitHub installation attached |
+| `409` | `installation_suspended` | GitHub reports the installation suspended |
+| `502` | `github_unavailable` | GitHub could not be read; retry |
## Use Git and the API
@@ -81,7 +126,14 @@ gh api repos/acme/service/pulls
OpenComputer mints a GitHub installation token whose permissions are limited
to those declared by the deployed agent. GitHub installation tokens expire
-after about one hour.
+after about one hour; the agent does not manage that. Before a command is
+dispatched, the token is renewed if it would expire before the command's
+allowed duration is up, so a `git push` or `gh pr create` late in a long
+session, or on a computer that has been running for most of an hour, uses a
+token that outlives the command. Git and `gh` both pick up the renewed token
+without any change to how the agent calls them. Renewal never widens what the
+token can do: it carries the same declared permissions against the
+installation's current repository selection.
### Understand GitHub permission responses