Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

65 linesCodeBlameRaw
1/**
2 * What a workspace has connected, by connector id (@g1t/contracts/connectors):
3 * its provider connections from the integrations service, the GitHub App's
4 * installations and its own webhooks, each with how it is doing. The
5 * Integrations directory and Apps read it.
6 */
7import type { User } from "@g1t/contracts";
8import { PROVIDERS } from "@g1t/contracts";
9import { CONNECTORS } from "@g1t/contracts/connectors";
10
11import type { ConnectedState } from "./connectors";
12import { githubConnected } from "./github";
13import { githubApp } from "./github.server";
14import { providerPage } from "./integration-sections";
15import { integrations, webhooks } from "./services.server";
16
17/** What the workspace `slug` has connected, as `viewer` may see it. Each source that does not answer counts as nothing connected. */
18export async function connectedStates(slug: string, viewer: User): Promise<Record<string, ConnectedState>> {
19 return (await workspaceConnections(slug, viewer)).connected;
20}
21
22/**
23 * What the workspace `slug` has connected, and the connectors this g1t
24 * can't connect at all, by id, each with why: GitHub when no GitHub App is
25 * set up (as on a self-hosted g1t that hasn't added one).
26 */
27export async function workspaceConnections(
28 slug: string,
29 viewer: User,
30): Promise<{ connected: Record<string, ConnectedState>; unavailable: Record<string, string> }> {
31 const [connections, github, hooks] = await Promise.all([
32 integrations.list(slug, viewer).catch(() => null),
33 githubApp.status(viewer, slug).catch(() => null),
34 webhooks.list(viewer, { workspace: slug }).catch(() => null),
35 ]);
36
37 const connected: Record<string, ConnectedState> = {};
38 if (connections?.ok) {
39 for (const connector of CONNECTORS) {
40 if (!connector.provider) continue;
41 const mine = connections.value.filter((connection) => connection.provider === connector.provider);
42 if (mine.length === 0) continue;
43 const broken = mine.find((connection) => connection.lastError);
44 connected[connector.id] = {
45 detail: mine.length === 1 ? mine[0]!.name : `${mine.length} connections: ${mine.map((c) => c.name).join(", ")}`,
46 problem: broken ? `${broken.name}: ${broken.lastError}` : null,
47 manage: `/${slug}/-/${providerPage(PROVIDERS[connector.provider].kind)}`,
48 };
49 }
50 }
51 const fromGithub = github?.ok ? githubConnected(github.value.installations) : null;
52 if (fromGithub) connected.github = fromGithub;
53 const ours = hooks?.ok ? hooks.value.filter((hook) => hook.scope === "workspace") : [];
54 if (ours.length > 0) {
55 const failing = ours.filter((hook) => hook.lastStatus === "failed");
56 connected.webhooks = {
57 detail: ours.length === 1 ? ours[0]!.url : `${ours.length} webhooks`,
58 problem: failing.length > 0 ? `The last delivery to ${failing[0]!.url} failed.` : null,
59 manage: null,
60 };
61 }
62 const unavailable: Record<string, string> = {};
63 if (github?.ok && !github.value.configured) unavailable.github = "This g1t has no GitHub App set up. Whoever runs it adds one first.";
64 return { connected, unavailable };
65}