The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Actions from "@distilled.cloud/github/actions"; |
| 2 | import * as Repos from "@distilled.cloud/github/repos"; |
| 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 Environment, resolveEnvironmentName } from "./Environment.ts"; |
| 10 | import type * as GitHub from "./Providers.ts"; |
| 11 | |
| 12 | export interface VariableProps { |
| 13 | /** |
| 14 | * Repository owner (user or organization). |
| 15 | */ |
| 16 | owner: string; |
| 17 | |
| 18 | /** |
| 19 | * Repository name. |
| 20 | */ |
| 21 | repository: string; |
| 22 | |
| 23 | /** |
| 24 | * Variable name (e.g. `AWS_ROLE_ARN`). |
| 25 | */ |
| 26 | name: string; |
| 27 | |
| 28 | /** |
| 29 | * Variable value. |
| 30 | */ |
| 31 | value: string; |
| 32 | |
| 33 | /** |
| 34 | * Optional environment. When set the variable is scoped to that GitHub |
| 35 | * Actions environment instead of the whole repository. Accepts an |
| 36 | * environment name or a `GitHub.Environment` resource. |
| 37 | */ |
| 38 | environment?: string | Environment; |
| 39 | |
| 40 | /** |
| 41 | * Override the GitHub host or API base URL for this resource only (e.g. |
| 42 | * `github.example.com` for GitHub Enterprise). Falls back to |
| 43 | * `GitHub.providers({ baseUrl })`, then to the host resolved by the auth |
| 44 | * provider. Changing it replaces the resource — the same name on a |
| 45 | * different GitHub instance is a different physical resource. |
| 46 | */ |
| 47 | baseUrl?: string; |
| 48 | } |
| 49 | |
| 50 | export interface Variable extends Resource< |
| 51 | "GitHub.Variable", |
| 52 | VariableProps, |
| 53 | { |
| 54 | /** |
| 55 | * ISO-8601 timestamp of the last update. |
| 56 | */ |
| 57 | updatedAt: string; |
| 58 | }, |
| 59 | never, |
| 60 | GitHub.Providers |
| 61 | > {} |
| 62 | |
| 63 | /** |
| 64 | * A GitHub Actions repository variable. |
| 65 | * |
| 66 | * `Variable` manages the lifecycle of a plain-text configuration variable |
| 67 | * in GitHub Actions. Variables are visible in workflow logs and are |
| 68 | * suitable for non-sensitive configuration like region names, environment |
| 69 | * labels, or feature flags. For sensitive values, use `GitHub.Secret` |
| 70 | * instead. |
| 71 | * |
| 72 | * Authentication is resolved via the GitHub `Credentials` service supplied |
| 73 | * by `GitHub.providers()` (which uses the Alchemy AuthProvider — env, |
| 74 | * stored PAT, `gh` CLI, or OAuth). The token needs `repo` scope for |
| 75 | * private repositories or `public_repo` for public ones. |
| 76 | * ### Repository Variables |
| 77 | * Store variables accessible to all GitHub Actions workflows in the |
| 78 | * repository. |
| 79 | * |
| 80 | * **Example:** Create a Repository Variable |
| 81 | * ```typescript |
| 82 | * yield* GitHub.Variable("aws-region", { |
| 83 | * owner: "my-org", |
| 84 | * repository: "my-repo", |
| 85 | * name: "AWS_REGION", |
| 86 | * value: "us-east-1", |
| 87 | * }); |
| 88 | * ``` |
| 89 | * |
| 90 | * ### Environment Variables |
| 91 | * Scope a variable to a specific GitHub Actions environment (e.g. |
| 92 | * `production`, `staging`). Use `GitHub.Environment` to manage the |
| 93 | * environment itself. |
| 94 | * |
| 95 | * **Example:** Create an Environment Variable |
| 96 | * ```typescript |
| 97 | * yield* GitHub.Variable("region", { |
| 98 | * owner: "my-org", |
| 99 | * repository: "my-repo", |
| 100 | * environment: "production", |
| 101 | * name: "AWS_REGION", |
| 102 | * value: "us-east-1", |
| 103 | * }); |
| 104 | * ``` |
| 105 | * |
| 106 | * ### Wiring with Other Resources |
| 107 | * Pass output attributes from other resources into GitHub variables so |
| 108 | * that CI workflows can reference them. |
| 109 | * |
| 110 | * **Example:** Store a Worker URL for CI |
| 111 | * ```typescript |
| 112 | * const worker = yield* Cloudflare.Worker("Api", { ... }); |
| 113 | * |
| 114 | * yield* GitHub.Variable("api-url", { |
| 115 | * owner: "my-org", |
| 116 | * repository: "my-repo", |
| 117 | * name: "API_URL", |
| 118 | * value: worker.url!, |
| 119 | * }); |
| 120 | * ``` |
| 121 | * |
| 122 | * **Example:** Multiple Variables |
| 123 | * ```typescript |
| 124 | * yield* GitHub.Variable("region", { |
| 125 | * owner: "my-org", |
| 126 | * repository: "my-repo", |
| 127 | * name: "AWS_REGION", |
| 128 | * value: "us-east-1", |
| 129 | * }); |
| 130 | * |
| 131 | * yield* GitHub.Variable("stage", { |
| 132 | * owner: "my-org", |
| 133 | * repository: "my-repo", |
| 134 | * name: "DEPLOY_STAGE", |
| 135 | * value: "production", |
| 136 | * }); |
| 137 | * ``` |
| 138 | * |
| 139 | * @resource |
| 140 | * @product Actions |
| 141 | */ |
| 142 | export const Variable = Resource<Variable>("GitHub.Variable"); |
| 143 | |
| 144 | export const VariableProvider = () => |
| 145 | Provider.succeed(Variable, { |
| 146 | // A variable's entire identity is (host, owner, repository, environment, |
| 147 | // name) — GitHub has no rename/move API for variables, so changing any |
| 148 | // of them replaces the resource: the engine creates the new variable |
| 149 | // first, then `delete` removes the old one from its old location. |
| 150 | // Replacement is safe here — a variable is declarative config that is |
| 151 | // fully re-creatable from props. |
| 152 | diff: Effect.fn(function* ({ news, olds }) { |
| 153 | if (!isResolved(news)) return; |
| 154 | if (olds === undefined) return; |
| 155 | if ( |
| 156 | news.owner !== olds.owner || |
| 157 | news.repository !== olds.repository || |
| 158 | news.name !== olds.name || |
| 159 | resolveEnvironmentName(news.environment) !== resolveEnvironmentName(olds.environment) || |
| 160 | (yield* gitHubBaseUrlChanged(olds, news)) |
| 161 | ) { |
| 162 | return { action: "replace" }; |
| 163 | } |
| 164 | }), |
| 165 | |
| 166 | reconcile: Effect.fn(function* ({ news, olds }) { |
| 167 | // A variable is keyed by its location (repo vs. environment). A |
| 168 | // location change normally arrives as a replacement (see `diff`); this |
| 169 | // guard is the safety net for the plan-time path where `news` contained |
| 170 | // unresolved outputs and diff could not compare — delete the orphaned |
| 171 | // variable from its old location before converging the new one. |
| 172 | if ( |
| 173 | olds !== undefined && |
| 174 | resolveEnvironmentName(olds.environment) !== resolveEnvironmentName(news.environment) |
| 175 | ) { |
| 176 | yield* deleteVariable(olds); |
| 177 | } |
| 178 | |
| 179 | // Observe — `name` is the path identifier; ask GitHub directly for the |
| 180 | // live row. A 404 means it doesn't exist (deleted out-of-band, or never |
| 181 | // created), so we converge by creating it; otherwise we PATCH the value. |
| 182 | const observed = yield* getVariable(news); |
| 183 | |
| 184 | // Ensure — POST creates the variable. |
| 185 | if (observed === undefined) { |
| 186 | yield* createVariable(news); |
| 187 | return { updatedAt: new Date().toISOString() }; |
| 188 | } |
| 189 | |
| 190 | // Sync — PATCH the value if it drifted; skip the call when the |
| 191 | // observed value already matches to keep the API quiet. |
| 192 | if (observed.value !== news.value) { |
| 193 | yield* updateVariable(news); |
| 194 | } |
| 195 | return { updatedAt: new Date().toISOString() }; |
| 196 | }), |
| 197 | |
| 198 | // Enumerate every Actions variable visible to the authenticated token. |
| 199 | // GitHub variables are keyed by {owner, repository, name} and there is no |
| 200 | // account-wide "list all variables" endpoint, so the ambient scope is the |
| 201 | // authenticated account: list every repository the token can see, then |
| 202 | // exhaustively paginate each repo's variables and hydrate into the same |
| 203 | // `Attributes` shape `reconcile` returns. Variable values are readable |
| 204 | // (unlike secrets), but the resource's `Attributes` only exposes |
| 205 | // `updatedAt`, so that's all we surface here. |
| 206 | list: Effect.fn(function* () { |
| 207 | const repos = yield* Stream.runCollect( |
| 208 | Repos.listForAuthenticatedUser.items({ per_page: 100 }), |
| 209 | ); |
| 210 | const perRepo = yield* Effect.forEach( |
| 211 | repos, |
| 212 | (repo) => |
| 213 | Stream.runCollect( |
| 214 | Actions.listRepoVariables.items({ |
| 215 | owner: repo.owner.login, |
| 216 | repo: repo.name, |
| 217 | per_page: 100, |
| 218 | }), |
| 219 | ).pipe( |
| 220 | Effect.map((variables) => variables.map((v) => ({ updatedAt: v.updated_at }))), |
| 221 | // Repos with Actions disabled, or where the token lacks the |
| 222 | // `repo`/`actions` scope, reject the variables endpoint with |
| 223 | // 403/404 — skip them per the per-item not-found rule rather |
| 224 | // than failing the whole enumeration. |
| 225 | Effect.catchTag(["Forbidden", "NotFound"], () => Effect.succeed([])), |
| 226 | ), |
| 227 | { concurrency: 10 }, |
| 228 | ); |
| 229 | return perRepo.flat(); |
| 230 | }), |
| 231 | |
| 232 | delete: Effect.fn(function* ({ olds }) { |
| 233 | yield* deleteVariable(olds); |
| 234 | }), |
| 235 | }); |
| 236 | |
| 237 | /** The variable's location: the repository, or one of its environments. */ |
| 238 | const locate = (props: VariableProps) => ({ |
| 239 | scope: { owner: props.owner, repo: props.repository, name: props.name }, |
| 240 | environment: resolveEnvironmentName(props.environment), |
| 241 | }); |
| 242 | |
| 243 | const getVariable = Effect.fn( |
| 244 | function* (props: VariableProps) { |
| 245 | const { scope, environment } = locate(props); |
| 246 | return yield* ( |
| 247 | environment !== undefined |
| 248 | ? Actions.getEnvironmentVariable({ ...scope, environment_name: environment }) |
| 249 | : Actions.getRepoVariable(scope) |
| 250 | ).pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 251 | }, |
| 252 | (effect, props) => effect.pipe(withBaseUrl(props.baseUrl)), |
| 253 | ); |
| 254 | |
| 255 | const createVariable = Effect.fn( |
| 256 | function* (props: VariableProps) { |
| 257 | const { scope, environment } = locate(props); |
| 258 | yield* environment !== undefined |
| 259 | ? Actions.createEnvironmentVariable({ |
| 260 | ...scope, |
| 261 | environment_name: environment, |
| 262 | value: props.value, |
| 263 | }) |
| 264 | : Actions.createRepoVariable({ ...scope, value: props.value }); |
| 265 | }, |
| 266 | (effect, props) => effect.pipe(withBaseUrl(props.baseUrl)), |
| 267 | ); |
| 268 | |
| 269 | const updateVariable = Effect.fn( |
| 270 | function* (props: VariableProps) { |
| 271 | const { scope, environment } = locate(props); |
| 272 | yield* environment !== undefined |
| 273 | ? Actions.updateEnvironmentVariable({ |
| 274 | ...scope, |
| 275 | environment_name: environment, |
| 276 | value: props.value, |
| 277 | }) |
| 278 | : Actions.updateRepoVariable({ ...scope, value: props.value }); |
| 279 | }, |
| 280 | (effect, props) => effect.pipe(withBaseUrl(props.baseUrl)), |
| 281 | ); |
| 282 | |
| 283 | const deleteVariable = Effect.fn( |
| 284 | function* (props: VariableProps) { |
| 285 | const { scope, environment } = locate(props); |
| 286 | yield* ( |
| 287 | environment !== undefined |
| 288 | ? Actions.deleteEnvironmentVariable({ ...scope, environment_name: environment }) |
| 289 | : Actions.deleteRepoVariable(scope) |
| 290 | ).pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 291 | }, |
| 292 | (effect, props) => effect.pipe(withBaseUrl(props.baseUrl)), |
| 293 | ); |