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.

g1t.sh
170 linesCodeBlameRaw
1import type { ServiceBinding } from "./clients";
2import type { User } from "./identity";
3import type { Result } from "./result";
4
5/**
6 * GitHub: signing in with a GitHub account, and bringing repositories
7 * across through g1t's GitHub App. Mirrors `crates/contracts/src/github.rs`.
8 *
9 * The identity service keeps linked accounts and their user tokens; the
10 * integrations service keeps installations, the repositories imported
11 * through them and the app's webhook. With no app configured, `enabled`/`configured` are false
12 * and the site shows no GitHub buttons.
13 */
14
15export type GithubPurpose = "sign_in" | "link";
16
17/** How a return from GitHub ended. */
18export type GithubFinished =
19 | {
20 kind: "signed_in";
21 /** `twoFactorChallenge` is set, and `sessionToken` empty, while the account's two-factor code is still to come. */
22 signedIn: { user: User; sessionToken: string; twoFactorChallenge?: string | null };
23 created: boolean;
24 next: string;
25 }
26 | { kind: "linked"; login: string; next: string }
27 /** An account has one of its verified emails: sign in to it, then claim. */
28 | { kind: "needs_link"; pending: string; login: string; next: string }
29 /** A new account waiting on a username, or on an invite code. */
30 | { kind: "needs_username"; pending: string; login: string; suggestion: string; next: string; inviteRequired: boolean };
31
32export type GithubPending = {
33 login: string;
34 kind: "link" | "username";
35 suggestion: string | null;
36 next: string;
37 inviteRequired: boolean;
38};
39
40export type GithubAccount = {
41 githubId: number;
42 login: string;
43 linkedAt: string;
44 /** Whether g1t holds a working user token for it. */
45 authorized: boolean;
46};
47
48export type GithubAccountView = {
49 enabled: boolean;
50 account: GithubAccount | null;
51 hasPassword: boolean;
52};
53
54export type GithubInstallation = {
55 id: number;
56 workspace: string;
57 account: string;
58 accountType: string;
59 repositorySelection: "all" | "selected" | string;
60 suspended: boolean;
61 settingsUrl: string;
62 createdAt: string;
63};
64
65/**
66 * An installation of the app the person's own GitHub account can see,
67 * from `github_visible_installations`: how one installed on GitHub
68 * directly is found and added to a workspace.
69 */
70export type GithubVisibleInstallation = {
71 id: number;
72 account: string;
73 accountType: string;
74 repositorySelection: "all" | "selected" | string;
75 suspended: boolean;
76 settingsUrl: string;
77 /** The person's workspaces it is recorded in already. */
78 recordedIn: string[];
79};
80
81export type GithubAppStatus = {
82 configured: boolean;
83 installUrl: string | null;
84 linked: boolean;
85 installations: GithubInstallation[];
86};
87
88export type GithubRepository = {
89 id: number;
90 fullName: string;
91 name: string;
92 private: boolean;
93 description: string | null;
94 defaultBranch: string;
95 /** The g1t repository already imported from it, as `workspace/name`. */
96 linkedTo: string | null;
97};
98
99export type GithubRepositories = {
100 repositories: GithubRepository[];
101 total: number;
102 page: number;
103 perPage: number;
104};
105
106/** Where a g1t repository was imported from on GitHub. The import is copied once: the g1t repository is then its own. */
107export type GithubRepoLink = {
108 repoId: string;
109 repo: string;
110 installationId: number;
111 githubRepoId: number;
112 fullName: string;
113 /** RFC 3339: when it was imported. */
114 syncedAt: string | null;
115 lastError: string | null;
116 issuesImported: number;
117};
118
119export type GithubImportInput = {
120 installationId: number;
121 githubRepoId: number;
122 name?: string;
123 private?: boolean;
124 issues: boolean;
125};
126
127async function rpc<T>(service: ServiceBinding, method: string, args: object): Promise<T> {
128 const response = await service.fetch(`https://service/rpc/${method}`, {
129 method: "POST",
130 headers: { "content-type": "application/json" },
131 body: JSON.stringify(args),
132 });
133 if (!response.ok) throw new Error(`${method} failed with status ${response.status}`);
134 return (await response.json()) as T;
135}
136
137/** Signing in with GitHub: methods of the identity service. */
138export function githubSignInClient(identity: ServiceBinding) {
139 const call = <T>(method: string, args: object) => rpc<T>(identity, method, args);
140 return {
141 enabled: () => call<boolean>("github_enabled", {}),
142 start: (input: { purpose: GithubPurpose; user?: User | null; redirectUri: string; next: string; inviteCode?: string | null }) =>
143 call<Result<{ authorizeUrl: string; state: string }>>("github_start", input),
144 finish: (state: string, code: string) => call<Result<GithubFinished>>("github_finish", { state, code }),
145 pending: (pending: string) => call<Result<GithubPending>>("github_pending", { pending }),
146 signUp: (pending: string, username: string, inviteCode?: string | null) =>
147 call<Result<{ user: User; sessionToken: string }>>("github_sign_up", { pending, username, inviteCode: inviteCode ?? null }),
148 claim: (pending: string, user: User) => call<Result<GithubAccount>>("github_claim", { pending, user }),
149 account: (user: User) => call<GithubAccountView>("github_account", { user }),
150 unlink: (user: User) => call<Result<boolean>>("github_unlink", { user }),
151 };
152}
153
154/** g1t's GitHub App: methods of the integrations service. */
155export function githubAppClient(integrations: ServiceBinding) {
156 const call = <T>(method: string, args: object) => rpc<T>(integrations, method, args);
157 return {
158 status: (viewer: User, workspace: string) => call<Result<GithubAppStatus>>("github_status", { viewer, workspace }),
159 addInstallation: (actor: User, workspace: string, installationId: number) =>
160 call<Result<GithubInstallation>>("github_add_installation", { actor, workspace, installationId }),
161 visibleInstallations: (actor: User) => call<Result<GithubVisibleInstallation[]>>("github_visible_installations", { actor }),
162 removeInstallation: (actor: User, workspace: string, installationId: number) =>
163 call<Result<boolean>>("github_remove_installation", { actor, workspace, installationId }),
164 repositories: (actor: User, workspace: string, installationId: number, page?: number) =>
165 call<Result<GithubRepositories>>("github_repositories", { actor, workspace, installationId, page: page ?? null }),
166 import: (actor: User, workspace: string, input: GithubImportInput) =>
167 call<Result<GithubRepoLink>>("github_import", { actor, workspace, ...input }),
168 link: (repoId: string) => call<GithubRepoLink | null>("github_link", { repoId }),
169 };
170}