Skip to content
56 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 the Marketplace both 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 { githubApp } from "./github.server";
13import { integrationsSection } from "./integration-sections";
14import { integrations, webhooks } from "./services.server";
15
16/** What the workspace `slug` has connected, as `viewer` may see it. Each source that does not answer counts as nothing connected. */
17export async function connectedStates(slug: string, viewer: User): Promise<Record<string, ConnectedState>> {
18 const [connections, github, hooks] = await Promise.all([
19 integrations.list(slug, viewer).catch(() => null),
20 githubApp.status(viewer, slug).catch(() => null),
21 webhooks.list(viewer, { workspace: slug }).catch(() => null),
22 ]);
23
24 const connected: Record<string, ConnectedState> = {};
25 if (connections?.ok) {
26 for (const connector of CONNECTORS) {
27 if (!connector.provider) continue;
28 const mine = connections.value.filter((connection) => connection.provider === connector.provider);
29 if (mine.length === 0) continue;
30 const broken = mine.find((connection) => connection.lastError);
31 connected[connector.id] = {
32 detail: mine.length === 1 ? mine[0]!.name : `${mine.length} connections: ${mine.map((c) => c.name).join(", ")}`,
33 problem: broken ? `${broken.name}: ${broken.lastError}` : null,
34 manage: `/${slug}/-/integrations/${integrationsSection(PROVIDERS[connector.provider].kind)}`,
35 };
36 }
37 }
38 if (github?.ok && github.value.installations.length > 0) {
39 const suspended = github.value.installations.find((installation) => installation.suspended);
40 connected.github = {
41 detail: `On ${github.value.installations.map((installation) => installation.account).join(", ")}`,
42 problem: suspended ? `The installation on ${suspended.account} is suspended on GitHub.` : null,
43 manage: null,
44 };
45 }
46 const ours = hooks?.ok ? hooks.value.filter((hook) => hook.scope === "workspace") : [];
47 if (ours.length > 0) {
48 const failing = ours.filter((hook) => hook.lastStatus === "failed");
49 connected.webhooks = {
50 detail: ours.length === 1 ? ours[0]!.url : `${ours.length} webhooks`,
51 problem: failing.length > 0 ? `The last delivery to ${failing[0]!.url} failed.` : null,
52 manage: null,
53 };
54 }
55 return connected;
56}