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
174 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, linked repositories and the
11 * 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. */
30 | { kind: "needs_username"; pending: string; login: string; suggestion: string; next: string };
31
32export type GithubPending = {
33 login: string;
34 kind: "link" | "username";
35 suggestion: string | null;
36 next: string;
37};
38
39export type GithubAccount = {
40 githubId: number;
41 login: string;
42 linkedAt: string;
43 /** Whether g1t holds a working user token for it. */
44 authorized: boolean;
45};
46
47export type GithubAccountView = {
48 enabled: boolean;
49 account: GithubAccount | null;
50 hasPassword: boolean;
51};
52
53/** How a GitHub repository comes to g1t. */
54export type GithubMode = "import" | "mirror" | "push";
55
56export type GithubInstallation = {
57 id: number;
58 workspace: string;
59 account: string;
60 accountType: string;
61 repositorySelection: "all" | "selected" | string;
62 suspended: boolean;
63 settingsUrl: string;
64 createdAt: string;
65};
66
67/**
68 * An installation of the app the person's own GitHub account can see,
69 * from `github_visible_installations`: how one installed on GitHub
70 * directly is found and added to a workspace.
71 */
72export type GithubVisibleInstallation = {
73 id: number;
74 account: string;
75 accountType: string;
76 repositorySelection: "all" | "selected" | string;
77 suspended: boolean;
78 settingsUrl: string;
79 /** The person's workspaces it is recorded in already. */
80 recordedIn: string[];
81};
82
83export type GithubAppStatus = {
84 configured: boolean;
85 installUrl: string | null;
86 linked: boolean;
87 installations: GithubInstallation[];
88};
89
90export type GithubRepository = {
91 id: number;
92 fullName: string;
93 name: string;
94 private: boolean;
95 description: string | null;
96 defaultBranch: string;
97 /** The g1t repository already linked to it, as `workspace/name`. */
98 linkedTo: string | null;
99};
100
101export type GithubRepositories = {
102 repositories: GithubRepository[];
103 total: number;
104 page: number;
105 perPage: number;
106};
107
108export type GithubRepoLink = {
109 repoId: string;
110 repo: string;
111 installationId: number;
112 githubRepoId: number;
113 fullName: string;
114 mode: GithubMode;
115 syncedAt: string | null;
116 lastError: string | null;
117 issuesImported: number;
118};
119
120export type GithubImportInput = {
121 installationId: number;
122 githubRepoId: number;
123 name?: string;
124 mode: GithubMode;
125 private?: boolean;
126 issues: boolean;
127};
128
129async function rpc<T>(service: ServiceBinding, method: string, args: object): Promise<T> {
130 const response = await service.fetch(`https://service/rpc/${method}`, {
131 method: "POST",
132 headers: { "content-type": "application/json" },
133 body: JSON.stringify(args),
134 });
135 if (!response.ok) throw new Error(`${method} failed with status ${response.status}`);
136 return (await response.json()) as T;
137}
138
139/** Signing in with GitHub: methods of the identity service. */
140export function githubSignInClient(identity: ServiceBinding) {
141 const call = <T>(method: string, args: object) => rpc<T>(identity, method, args);
142 return {
143 enabled: () => call<boolean>("github_enabled", {}),
144 start: (input: { purpose: GithubPurpose; user?: User | null; redirectUri: string; next: string; inviteCode?: string | null }) =>
145 call<Result<{ authorizeUrl: string; state: string }>>("github_start", input),
146 finish: (state: string, code: string) => call<Result<GithubFinished>>("github_finish", { state, code }),
147 pending: (pending: string) => call<Result<GithubPending>>("github_pending", { pending }),
148 signUp: (pending: string, username: string) =>
149 call<Result<{ user: User; sessionToken: string }>>("github_sign_up", { pending, username }),
150 claim: (pending: string, user: User) => call<Result<GithubAccount>>("github_claim", { pending, user }),
151 account: (user: User) => call<GithubAccountView>("github_account", { user }),
152 unlink: (user: User) => call<Result<boolean>>("github_unlink", { user }),
153 };
154}
155
156/** g1t's GitHub App: methods of the integrations service. */
157export function githubAppClient(integrations: ServiceBinding) {
158 const call = <T>(method: string, args: object) => rpc<T>(integrations, method, args);
159 return {
160 status: (viewer: User, workspace: string) => call<Result<GithubAppStatus>>("github_status", { viewer, workspace }),
161 addInstallation: (actor: User, workspace: string, installationId: number) =>
162 call<Result<GithubInstallation>>("github_add_installation", { actor, workspace, installationId }),
163 visibleInstallations: (actor: User) => call<Result<GithubVisibleInstallation[]>>("github_visible_installations", { actor }),
164 removeInstallation: (actor: User, workspace: string, installationId: number) =>
165 call<Result<boolean>>("github_remove_installation", { actor, workspace, installationId }),
166 repositories: (actor: User, workspace: string, installationId: number, page?: number) =>
167 call<Result<GithubRepositories>>("github_repositories", { actor, workspace, installationId, page: page ?? null }),
168 import: (actor: User, workspace: string, input: GithubImportInput) =>
169 call<Result<GithubRepoLink>>("github_import", { actor, workspace, ...input }),
170 link: (repoId: string) => call<GithubRepoLink | null>("github_link", { repoId }),
171 unlinkRepo: (actor: User, repoId: string) => call<Result<boolean>>("github_unlink_repo", { actor, repoId }),
172 sync: (actor: User, repoId: string) => call<Result<GithubRepoLink>>("github_sync", { actor, repoId }),
173 };
174}