The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 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 * as Schema from "effect/Schema" |
| 6 | import { type GitHubRepository, makeClient } from "./GitHub.ts" |
| 7 | import type { Diagnosis } from "./NpmPromote.ts" |
| 8 | import { NpmPromoteError } from "./NpmPromoteError.ts" |
| 9 | import type { VersionBump } from "./Workspace.ts" |
| 10 | |
| 11 | /** |
| 12 | * Identifies the tracking issue among open issues opened by the workflow. |
| 13 | */ |
| 14 | const marker = "<!-- npm-promote:tracking-issue -->" |
| 15 | |
| 16 | /** |
| 17 | * The login of the identity behind the workflow's `GITHUB_TOKEN`. |
| 18 | */ |
| 19 | const creator = "github-actions[bot]" |
| 20 | |
| 21 | const Issue = Schema.Struct({ |
| 22 | number: Schema.Number, |
| 23 | title: Schema.String, |
| 24 | body: Schema.optional(Schema.NullOr(Schema.String)), |
| 25 | pull_request: Schema.optional(Schema.Unknown) |
| 26 | }) |
| 27 | |
| 28 | /** |
| 29 | * Lists the open tracking issues, oldest first. The listing can miss an open |
| 30 | * issue while GitHub is degraded, so a run may open a duplicate; the oldest |
| 31 | * issue is the tracking issue and later ones are duplicates. |
| 32 | */ |
| 33 | const findOpen = (client: HttpClient.HttpClient) => |
| 34 | client.get("/issues", { urlParams: { state: "open", creator, per_page: "100" } }).pipe( |
| 35 | Effect.flatMap(HttpClientResponse.schemaBodyJson(Schema.Array(Issue))), |
| 36 | Effect.map((issues) => |
| 37 | issues |
| 38 | .filter((issue) => issue.pull_request === undefined && issue.body?.includes(marker) === true) |
| 39 | .sort((a, b) => a.number - b.number) |
| 40 | ) |
| 41 | ) |
| 42 | |
| 43 | const closeIssue = ( |
| 44 | client: HttpClient.HttpClient, |
| 45 | number: number, |
| 46 | options: { readonly comment: string; readonly reason: "completed" | "duplicate" } |
| 47 | ) => |
| 48 | client.execute( |
| 49 | HttpClientRequest.post(`/issues/${number}/comments`).pipe( |
| 50 | HttpClientRequest.bodyJsonUnsafe({ body: options.comment }) |
| 51 | ) |
| 52 | ).pipe( |
| 53 | Effect.andThen(client.execute( |
| 54 | HttpClientRequest.patch(`/issues/${number}`).pipe( |
| 55 | HttpClientRequest.bodyJsonUnsafe({ state: "closed", state_reason: options.reason }) |
| 56 | ) |
| 57 | )) |
| 58 | ) |
| 59 | |
| 60 | const githubError = (message: string) => (cause: unknown) => new NpmPromoteError({ message, cause }) |
| 61 | |
| 62 | export interface OverdueRelease { |
| 63 | readonly version: string |
| 64 | readonly bump: VersionBump |
| 65 | readonly overdueAfterHours: number |
| 66 | readonly diagnoses: ReadonlyArray<Diagnosis> |
| 67 | } |
| 68 | |
| 69 | export const render = (overdue: OverdueRelease): { readonly title: string; readonly body: string } => { |
| 70 | const list = (tag: Diagnosis["_tag"]) => |
| 71 | overdue.diagnoses.filter((diagnosis) => diagnosis._tag === tag).map((diagnosis) => |
| 72 | diagnosis._tag === "Unknown" || diagnosis._tag === "PromotionFailed" |
| 73 | ? `- \`${diagnosis.pkg.name}@${diagnosis.pkg.version}\`: ${diagnosis.reason}` |
| 74 | : `- \`${diagnosis.pkg.name}@${diagnosis.pkg.version}\`` |
| 75 | ) |
| 76 | const sections = [ |
| 77 | { |
| 78 | heading: "Promotion failed", |
| 79 | note: |
| 80 | "The version is visible, but `npm dist-tag add` failed. Check each package's trusted publisher configuration for `npm-promote.yml`, the workflow and environment match, expiry, and the \"Allow npm dist-tag\" permission.", |
| 81 | items: list("PromotionFailed") |
| 82 | }, |
| 83 | { |
| 84 | heading: "Missing from npm", |
| 85 | note: "The version document returns 404. `changeset publish` most likely failed for these packages.", |
| 86 | items: list("Missing") |
| 87 | }, |
| 88 | { |
| 89 | heading: "Published but not visible yet", |
| 90 | note: "The version document exists, but the abbreviated metadata used for promotion does not list it yet.", |
| 91 | items: list("Published") |
| 92 | }, |
| 93 | { |
| 94 | heading: "Could not check", |
| 95 | note: "The registry check failed, so these may be missing or only delayed.", |
| 96 | items: list("Unknown") |
| 97 | } |
| 98 | ].filter((section) => section.items.length > 0) |
| 99 | |
| 100 | return { |
| 101 | title: `npm promotion of ${overdue.version} is overdue`, |
| 102 | body: [ |
| 103 | marker, |
| 104 | `Version \`${overdue.version}\` landed on \`main\` in ${overdue.bump.sha} at ${overdue.bump.committedAt.toISOString()}, but \`latest\` has not moved to it after ${overdue.overdueAfterHours} hours.`, |
| 105 | ...sections.map((section) => `### ${section.heading}\n\n${section.note}\n\n${section.items.join("\n")}`), |
| 106 | "The promote workflow keeps retrying and closes this issue once promotion succeeds. Set the `NPM_PROMOTE_PAUSED` repository variable to `true` to stop promotion." |
| 107 | ].join("\n\n") |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Opens the tracking issue, or updates the open one when its content changed. |
| 113 | * Any other open tracking issues are closed as duplicates of it. |
| 114 | */ |
| 115 | export const upsert = ( |
| 116 | github: GitHubRepository, |
| 117 | content: { readonly title: string; readonly body: string } |
| 118 | ): Effect.Effect< |
| 119 | { readonly action: "created" | "updated" | "unchanged"; readonly number: number }, |
| 120 | NpmPromoteError, |
| 121 | HttpClient.HttpClient |
| 122 | > => |
| 123 | Effect.gen(function*() { |
| 124 | const client = yield* makeClient(github.repository, github.token) |
| 125 | const [issue, ...duplicates] = yield* findOpen(client) |
| 126 | if (issue === undefined) { |
| 127 | const created = yield* client.execute( |
| 128 | HttpClientRequest.post("/issues").pipe(HttpClientRequest.bodyJsonUnsafe(content)) |
| 129 | ).pipe(Effect.flatMap(HttpClientResponse.schemaBodyJson(Issue))) |
| 130 | return { action: "created", number: created.number } as const |
| 131 | } |
| 132 | for (const duplicate of duplicates) { |
| 133 | yield* closeIssue(client, duplicate.number, { comment: `Duplicate of #${issue.number}.`, reason: "duplicate" }) |
| 134 | } |
| 135 | if (issue.title === content.title && issue.body === content.body) { |
| 136 | return { action: "unchanged", number: issue.number } as const |
| 137 | } |
| 138 | yield* client.execute( |
| 139 | HttpClientRequest.patch(`/issues/${issue.number}`).pipe(HttpClientRequest.bodyJsonUnsafe(content)) |
| 140 | ) |
| 141 | return { action: "updated", number: issue.number } as const |
| 142 | }).pipe(Effect.mapError(githubError("Could not open or update the tracking issue"))) |
| 143 | |
| 144 | /** |
| 145 | * Comments on and closes every open tracking issue. Returns the closed issue |
| 146 | * numbers, oldest first. |
| 147 | */ |
| 148 | export const close = ( |
| 149 | github: GitHubRepository, |
| 150 | comment: string |
| 151 | ): Effect.Effect<ReadonlyArray<number>, NpmPromoteError, HttpClient.HttpClient> => |
| 152 | Effect.gen(function*() { |
| 153 | const client = yield* makeClient(github.repository, github.token) |
| 154 | const issues = yield* findOpen(client) |
| 155 | for (const issue of issues) { |
| 156 | yield* closeIssue(client, issue.number, { comment, reason: "completed" }) |
| 157 | } |
| 158 | return issues.map((issue) => issue.number) |
| 159 | }).pipe(Effect.mapError(githubError("Could not close the tracking issue"))) |