Skip to content

g1t/packages/contracts/src/github.ts

158 lines6,057 bytesCodeBlame

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look1import 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 =
Merge main (membership, two-factor, GitHub repo roles) into tokens19 | {
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 }
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look26 | { 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
54/** How a GitHub repository comes to g1t. */
55export type GithubMode = "import" | "mirror" | "push";
56
57export type GithubInstallation = {
58 id: number;
59 workspace: string;
60 account: string;
61 accountType: string;
62 repositorySelection: "all" | "selected" | string;
63 suspended: boolean;
64 settingsUrl: string;
65 createdAt: string;
66};
67
68export type GithubAppStatus = {
69 configured: boolean;
70 installUrl: string | null;
71 linked: boolean;
72 installations: GithubInstallation[];
73};
74
75export type GithubRepository = {
76 id: number;
77 fullName: string;
78 name: string;
79 private: boolean;
80 description: string | null;
81 defaultBranch: string;
82 /** The g1t repository already linked to it, as `workspace/name`. */
83 linkedTo: string | null;
84};
85
86export type GithubRepositories = {
87 repositories: GithubRepository[];
88 total: number;
89 page: number;
90 perPage: number;
91};
92
93export type GithubRepoLink = {
94 repoId: string;
95 repo: string;
96 installationId: number;
97 githubRepoId: number;
98 fullName: string;
99 mode: GithubMode;
100 syncedAt: string | null;
101 lastError: string | null;
102 issuesImported: number;
103};
104
105export type GithubImportInput = {
106 installationId: number;
107 githubRepoId: number;
108 name?: string;
109 mode: GithubMode;
110 private?: boolean;
111 issues: boolean;
112};
113
114async function rpc<T>(service: ServiceBinding, method: string, args: object): Promise<T> {
115 const response = await service.fetch(`https://service/rpc/${method}`, {
116 method: "POST",
117 headers: { "content-type": "application/json" },
118 body: JSON.stringify(args),
119 });
120 if (!response.ok) throw new Error(`${method} failed with status ${response.status}`);
121 return (await response.json()) as T;
122}
123
124/** Signing in with GitHub: methods of the identity service. */
125export function githubSignInClient(identity: ServiceBinding) {
126 const call = <T>(method: string, args: object) => rpc<T>(identity, method, args);
127 return {
128 enabled: () => call<boolean>("github_enabled", {}),
129 start: (input: { purpose: GithubPurpose; user?: User | null; redirectUri: string; next: string; inviteCode?: string | null }) =>
130 call<Result<{ authorizeUrl: string; state: string }>>("github_start", input),
131 finish: (state: string, code: string) => call<Result<GithubFinished>>("github_finish", { state, code }),
132 pending: (pending: string) => call<Result<GithubPending>>("github_pending", { pending }),
133 signUp: (pending: string, username: string, inviteCode?: string | null) =>
134 call<Result<{ user: User; sessionToken: string }>>("github_sign_up", { pending, username, inviteCode: inviteCode ?? null }),
135 claim: (pending: string, user: User) => call<Result<GithubAccount>>("github_claim", { pending, user }),
136 account: (user: User) => call<GithubAccountView>("github_account", { user }),
137 unlink: (user: User) => call<Result<boolean>>("github_unlink", { user }),
138 };
139}
140
141/** g1t's GitHub App: methods of the integrations service. */
142export function githubAppClient(integrations: ServiceBinding) {
143 const call = <T>(method: string, args: object) => rpc<T>(integrations, method, args);
144 return {
145 status: (viewer: User, workspace: string) => call<Result<GithubAppStatus>>("github_status", { viewer, workspace }),
146 addInstallation: (actor: User, workspace: string, installationId: number) =>
147 call<Result<GithubInstallation>>("github_add_installation", { actor, workspace, installationId }),
148 removeInstallation: (actor: User, workspace: string, installationId: number) =>
149 call<Result<boolean>>("github_remove_installation", { actor, workspace, installationId }),
150 repositories: (actor: User, workspace: string, installationId: number, page?: number) =>
151 call<Result<GithubRepositories>>("github_repositories", { actor, workspace, installationId, page: page ?? null }),
152 import: (actor: User, workspace: string, input: GithubImportInput) =>
153 call<Result<GithubRepoLink>>("github_import", { actor, workspace, ...input }),
154 link: (repoId: string) => call<GithubRepoLink | null>("github_link", { repoId }),
155 unlinkRepo: (actor: User, repoId: string) => call<Result<boolean>>("github_unlink_repo", { actor, repoId }),
156 sync: (actor: User, repoId: string) => call<Result<GithubRepoLink>>("github_sync", { actor, repoId }),
157 };
158}

This file's history is long; its oldest lines are credited to the oldest commit read.