The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Repos from "@distilled.cloud/github/repos"; |
| 2 | import * as Teams from "@distilled.cloud/github/teams"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | import { withBaseUrl, gitHubBaseUrlChanged } from "./BaseUrl.ts"; |
| 9 | import type * as GitHub from "./Providers.ts"; |
| 10 | |
| 11 | export interface TeamAccessProps { |
| 12 | /** |
| 13 | * Repository owner — must be an organization login. |
| 14 | */ |
| 15 | owner: string; |
| 16 | |
| 17 | /** |
| 18 | * Repository name. |
| 19 | */ |
| 20 | repository: string; |
| 21 | |
| 22 | /** |
| 23 | * Team slug within the organization (e.g. `platform`, `frontend`). |
| 24 | */ |
| 25 | teamSlug: string; |
| 26 | |
| 27 | /** |
| 28 | * Permission level to grant. |
| 29 | * - `pull` — read-only access |
| 30 | * - `push` — read and write access |
| 31 | * - `maintain` — read, write, and manage issues/PRs |
| 32 | * - `triage` — read and manage issues/PRs without write access |
| 33 | * - `admin` — full admin access |
| 34 | * |
| 35 | * @default "push" |
| 36 | */ |
| 37 | permission?: "pull" | "push" | "maintain" | "triage" | "admin"; |
| 38 | |
| 39 | /** |
| 40 | * Override the GitHub host or API base URL for this resource only (e.g. |
| 41 | * `github.example.com` for GitHub Enterprise). Falls back to |
| 42 | * `GitHub.providers({ baseUrl })`, then to the host resolved by the auth |
| 43 | * provider. Changing it replaces the resource — the same name on a |
| 44 | * different GitHub instance is a different physical resource. |
| 45 | */ |
| 46 | baseUrl?: string; |
| 47 | } |
| 48 | |
| 49 | export interface TeamAccess extends Resource< |
| 50 | "GitHub.TeamAccess", |
| 51 | TeamAccessProps, |
| 52 | { |
| 53 | /** |
| 54 | * Team slug. |
| 55 | */ |
| 56 | teamSlug: string; |
| 57 | |
| 58 | /** |
| 59 | * Permission level granted. |
| 60 | */ |
| 61 | permission: string; |
| 62 | }, |
| 63 | never, |
| 64 | GitHub.Providers |
| 65 | > {} |
| 66 | |
| 67 | /** |
| 68 | * A GitHub team repository access grant. |
| 69 | * |
| 70 | * `TeamAccess` grants a team access to a repository within an organization. |
| 71 | * Teams provide a scalable way to manage repository permissions — add users |
| 72 | * to teams instead of granting individual collaborator access. |
| 73 | * |
| 74 | * Team access grants default to **retain** on removal — destroying the stack |
| 75 | * does NOT remove the team's access, preventing accidental lockout. Opt in |
| 76 | * to actual removal by wrapping the resource in {@link destroy}() from |
| 77 | * `alchemy/RemovalPolicy`. |
| 78 | * |
| 79 | * Authentication is resolved via the GitHub `Credentials` service supplied by |
| 80 | * `GitHub.providers()` (env, stored PAT, `gh` CLI, or OAuth). The token needs |
| 81 | * `repo` scope and `admin:org` for managing team access. |
| 82 | * |
| 83 | * ### Granting Team Access |
| 84 | * **Example:** Grant Push Access to a Team |
| 85 | * ```typescript |
| 86 | * yield* GitHub.TeamAccess("platform-access", { |
| 87 | * owner: "my-org", |
| 88 | * repository: "my-repo", |
| 89 | * teamSlug: "platform", |
| 90 | * permission: "push", |
| 91 | * }) |
| 92 | * ``` |
| 93 | * |
| 94 | * **Example:** Grant Admin Access to a Team |
| 95 | * ```typescript |
| 96 | * yield* GitHub.TeamAccess("admin-access", { |
| 97 | * owner: "my-org", |
| 98 | * repository: "my-repo", |
| 99 | * teamSlug: "admins", |
| 100 | * permission: "admin", |
| 101 | * }) |
| 102 | * ``` |
| 103 | * |
| 104 | * **Example:** Grant Read-Only Access |
| 105 | * ```typescript |
| 106 | * yield* GitHub.TeamAccess("readonly", { |
| 107 | * owner: "my-org", |
| 108 | * repository: "my-repo", |
| 109 | * teamSlug: "contractors", |
| 110 | * permission: "pull", |
| 111 | * }) |
| 112 | * ``` |
| 113 | * |
| 114 | * ### Multiple Teams |
| 115 | * **Example:** Grant Different Permissions to Multiple Teams |
| 116 | * ```typescript |
| 117 | * yield* GitHub.TeamAccess("platform-write", { |
| 118 | * owner: "my-org", |
| 119 | * repository: "api", |
| 120 | * teamSlug: "platform", |
| 121 | * permission: "push", |
| 122 | * }) |
| 123 | * |
| 124 | * yield* GitHub.TeamAccess("security-read", { |
| 125 | * owner: "my-org", |
| 126 | * repository: "api", |
| 127 | * teamSlug: "security", |
| 128 | * permission: "pull", |
| 129 | * }) |
| 130 | * ``` |
| 131 | * |
| 132 | * ### Removing Team Access |
| 133 | * **Example:** Allow Removal on Destroy |
| 134 | * ```typescript |
| 135 | * import { destroy } from "alchemy/RemovalPolicy" |
| 136 | * |
| 137 | * yield* GitHub.TeamAccess("temp", { |
| 138 | * owner: "my-org", |
| 139 | * repository: "my-repo", |
| 140 | * teamSlug: "contractors", |
| 141 | * permission: "push", |
| 142 | * }).pipe(destroy()) |
| 143 | * ``` |
| 144 | * |
| 145 | * @resource |
| 146 | * @product Repository |
| 147 | */ |
| 148 | export const TeamAccess = Resource<TeamAccess>("GitHub.TeamAccess", { |
| 149 | defaultRemovalPolicy: "retain", |
| 150 | }); |
| 151 | |
| 152 | export const TeamAccessProvider = () => |
| 153 | Provider.succeed(TeamAccess, { |
| 154 | diff: Effect.fn(function* ({ news, olds }) { |
| 155 | if (!isResolved(news)) return; |
| 156 | if (olds === undefined) return; |
| 157 | if ( |
| 158 | news.owner !== olds.owner || |
| 159 | news.repository !== olds.repository || |
| 160 | news.teamSlug !== olds.teamSlug || |
| 161 | (yield* gitHubBaseUrlChanged(olds, news)) |
| 162 | ) { |
| 163 | return { action: "replace" }; |
| 164 | } |
| 165 | }), |
| 166 | |
| 167 | reconcile: Effect.fn( |
| 168 | function* ({ news }) { |
| 169 | // Ensure & Sync — PUT is idempotent; adds team or updates permission |
| 170 | yield* Teams.addOrUpdateRepoPermissionsInOrg({ |
| 171 | org: news.owner, |
| 172 | team_slug: news.teamSlug, |
| 173 | owner: news.owner, |
| 174 | repo: news.repository, |
| 175 | permission: news.permission ?? "push", |
| 176 | }); |
| 177 | |
| 178 | return { |
| 179 | teamSlug: news.teamSlug, |
| 180 | permission: news.permission ?? "push", |
| 181 | }; |
| 182 | }, |
| 183 | (effect, { news }) => effect.pipe(withBaseUrl(news.baseUrl)), |
| 184 | ), |
| 185 | |
| 186 | list: Effect.fn(function* () { |
| 187 | const repos = yield* Stream.runCollect( |
| 188 | Repos.listForAuthenticatedUser.items({ per_page: 100 }), |
| 189 | ); |
| 190 | const perRepo = yield* Effect.forEach( |
| 191 | repos, |
| 192 | (repo) => |
| 193 | Stream.runCollect( |
| 194 | Repos.listTeams.items({ |
| 195 | owner: repo.owner.login, |
| 196 | repo: repo.name, |
| 197 | per_page: 100, |
| 198 | }), |
| 199 | ).pipe( |
| 200 | Effect.map((teams) => |
| 201 | teams.map((team) => ({ |
| 202 | teamSlug: team.slug, |
| 203 | permission: (team.permission ?? "push") as NonNullable< |
| 204 | TeamAccessProps["permission"] |
| 205 | >, |
| 206 | })), |
| 207 | ), |
| 208 | Effect.catchTag(["Forbidden", "NotFound"], () => Effect.succeed([])), |
| 209 | ), |
| 210 | { concurrency: 10 }, |
| 211 | ); |
| 212 | return perRepo.flat(); |
| 213 | }), |
| 214 | |
| 215 | delete: Effect.fn( |
| 216 | function* ({ olds }) { |
| 217 | yield* Teams.removeRepoInOrg({ |
| 218 | org: olds.owner, |
| 219 | team_slug: olds.teamSlug, |
| 220 | owner: olds.owner, |
| 221 | repo: olds.repository, |
| 222 | }).pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 223 | }, |
| 224 | (effect, { olds }) => effect.pipe(withBaseUrl(olds.baseUrl)), |
| 225 | ), |
| 226 | }); |