The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect" |
| 2 | import type * as HttpClient from "effect/http/HttpClient" |
| 3 | import * as HttpClientRequest from "effect/http/HttpClientRequest" |
| 4 | import * as HttpClientResponse from "effect/http/HttpClientResponse" |
| 5 | import type * as Redacted from "effect/Redacted" |
| 6 | import * as Schema from "effect/Schema" |
| 7 | import { type GitHubRepository, makeClient } from "./GitHub.ts" |
| 8 | import { NpmPromoteError } from "./NpmPromoteError.ts" |
| 9 | |
| 10 | /** |
| 11 | * The GitHub deployment environment that records website deploys requested |
| 12 | * for a revision of this repository. |
| 13 | */ |
| 14 | const environment = "website-v4" |
| 15 | |
| 16 | const Deployment = Schema.Struct({ id: Schema.Number }) |
| 17 | const DeploymentStatus = Schema.Struct({ state: Schema.String }) |
| 18 | |
| 19 | /** |
| 20 | * Whether a website deploy for `revision` was recorded. A recorded deployment |
| 21 | * whose latest status is `success`, or `inactive` after a newer deploy |
| 22 | * superseded it, counts. |
| 23 | */ |
| 24 | export const isDeployed = ( |
| 25 | github: GitHubRepository, |
| 26 | revision: string |
| 27 | ): Effect.Effect<boolean, NpmPromoteError, HttpClient.HttpClient> => |
| 28 | Effect.gen(function*() { |
| 29 | const client = yield* makeClient(github.repository, github.token) |
| 30 | const deployments = yield* client.get("/deployments", { |
| 31 | urlParams: { sha: revision, environment, per_page: "100" } |
| 32 | }).pipe(Effect.flatMap(HttpClientResponse.schemaBodyJson(Schema.Array(Deployment)))) |
| 33 | for (const deployment of deployments) { |
| 34 | const statuses = yield* client.get(`/deployments/${deployment.id}/statuses`, { |
| 35 | urlParams: { per_page: "1" } |
| 36 | }).pipe(Effect.flatMap(HttpClientResponse.schemaBodyJson(Schema.Array(DeploymentStatus)))) |
| 37 | if (statuses[0]?.state === "success" || statuses[0]?.state === "inactive") { |
| 38 | return true |
| 39 | } |
| 40 | } |
| 41 | return false |
| 42 | }).pipe( |
| 43 | Effect.mapError((cause) => |
| 44 | new NpmPromoteError({ message: `Could not read the website deployments of ${revision}`, cause }) |
| 45 | ) |
| 46 | ) |
| 47 | |
| 48 | /** |
| 49 | * Asks Effect-TS/website to publish the API reference for `revision`, the same |
| 50 | * dispatch the release workflow used to send. |
| 51 | */ |
| 52 | const dispatch = (dispatchToken: Redacted.Redacted<string>, revision: string) => |
| 53 | Effect.flatMap(makeClient("Effect-TS/website", dispatchToken), (client) => |
| 54 | client.execute( |
| 55 | HttpClientRequest.post("/dispatches").pipe( |
| 56 | HttpClientRequest.bodyJsonUnsafe({ |
| 57 | event_type: "publish-api-reference", |
| 58 | client_payload: { repository: "Effect-TS/effect", channel: "v4", revision } |
| 59 | }) |
| 60 | ) |
| 61 | )) |
| 62 | |
| 63 | /** |
| 64 | * Records a deployment for `revision`, sends the website dispatch and marks the |
| 65 | * deployment `success`, or `failure` when the dispatch failed. |
| 66 | * |
| 67 | * Delivery is at least once: if the run stops between the dispatch and the |
| 68 | * status update, the next run dispatches again. The website reuses the |
| 69 | * published API reference snapshot for a revision it already has, so a repeat |
| 70 | * only redeploys the site. |
| 71 | */ |
| 72 | export const deploy = ( |
| 73 | github: GitHubRepository, |
| 74 | dispatchToken: Redacted.Redacted<string>, |
| 75 | revision: string |
| 76 | ): Effect.Effect<void, NpmPromoteError, HttpClient.HttpClient> => |
| 77 | Effect.gen(function*() { |
| 78 | if (!/^[a-f0-9]{40}$/.test(revision)) { |
| 79 | return yield* new NpmPromoteError({ message: `Website deploys need a full commit SHA, got ${revision}` }) |
| 80 | } |
| 81 | const client = yield* makeClient(github.repository, github.token) |
| 82 | const deployment = yield* client.execute( |
| 83 | HttpClientRequest.post("/deployments").pipe(HttpClientRequest.bodyJsonUnsafe({ |
| 84 | ref: revision, |
| 85 | environment, |
| 86 | auto_merge: false, |
| 87 | required_contexts: [], |
| 88 | description: "API reference publication requested from Effect-TS/website" |
| 89 | })) |
| 90 | ).pipe(Effect.flatMap(HttpClientResponse.schemaBodyJson(Deployment))) |
| 91 | const setStatus = (state: "success" | "failure") => |
| 92 | client.execute( |
| 93 | HttpClientRequest.post(`/deployments/${deployment.id}/statuses`).pipe( |
| 94 | HttpClientRequest.bodyJsonUnsafe({ state }) |
| 95 | ) |
| 96 | ) |
| 97 | yield* dispatch(dispatchToken, revision).pipe( |
| 98 | Effect.tapError(() => Effect.ignore(setStatus("failure"))) |
| 99 | ) |
| 100 | yield* setStatus("success") |
| 101 | }).pipe( |
| 102 | Effect.mapError((cause) => |
| 103 | Schema.is(NpmPromoteError)(cause) |
| 104 | ? cause |
| 105 | : new NpmPromoteError({ message: `Could not deploy the website for ${revision}`, cause }) |
| 106 | ) |
| 107 | ) |