|
| 1 | +import type { OrchestratorMcpGitHubWaitCondition } from "@t3tools/contracts"; |
| 2 | +import * as Context from "effect/Context"; |
| 3 | +import * as DateTime from "effect/DateTime"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | +import * as Layer from "effect/Layer"; |
| 6 | +import * as Schema from "effect/Schema"; |
| 7 | + |
| 8 | +import * as GitHubCli from "../sourceControl/GitHubCli.ts"; |
| 9 | + |
| 10 | +const WATCH_FIELDS = "headRefOid,state,mergedAt,updatedAt,statusCheckRollup,comments,reviews,url"; |
| 11 | + |
| 12 | +const RawCheck = Schema.Struct({ |
| 13 | + __typename: Schema.optional(Schema.String), |
| 14 | + name: Schema.optional(Schema.String), |
| 15 | + context: Schema.optional(Schema.String), |
| 16 | + status: Schema.optional(Schema.String), |
| 17 | + state: Schema.optional(Schema.String), |
| 18 | + conclusion: Schema.optional(Schema.NullOr(Schema.String)), |
| 19 | +}); |
| 20 | +const RawReviewActivity = Schema.Struct({ |
| 21 | + id: Schema.String, |
| 22 | + createdAt: Schema.optional(Schema.String), |
| 23 | + submittedAt: Schema.optional(Schema.String), |
| 24 | +}); |
| 25 | +const RawSnapshot = Schema.Struct({ |
| 26 | + comments: Schema.Array(RawReviewActivity), |
| 27 | + headRefOid: Schema.String, |
| 28 | + mergedAt: Schema.NullOr(Schema.String), |
| 29 | + reviews: Schema.Array(RawReviewActivity), |
| 30 | + state: Schema.String, |
| 31 | + statusCheckRollup: Schema.Array(RawCheck), |
| 32 | + updatedAt: Schema.String, |
| 33 | + url: Schema.String, |
| 34 | +}); |
| 35 | +const decodeRawSnapshot = Schema.decodeUnknownEffect(Schema.fromJsonString(RawSnapshot)); |
| 36 | + |
| 37 | +export const GitHubPullRequestSnapshot = Schema.Struct({ |
| 38 | + url: Schema.String, |
| 39 | + state: Schema.Literals(["open", "closed", "merged"]), |
| 40 | + headSha: Schema.String, |
| 41 | + mergedAt: Schema.NullOr(Schema.String), |
| 42 | + updatedAt: Schema.String, |
| 43 | + checks: Schema.Array( |
| 44 | + Schema.Struct({ |
| 45 | + name: Schema.String, |
| 46 | + status: Schema.Literals(["pending", "completed"]), |
| 47 | + conclusion: Schema.NullOr(Schema.String), |
| 48 | + }), |
| 49 | + ), |
| 50 | + reviewActivity: Schema.Array( |
| 51 | + Schema.Struct({ |
| 52 | + id: Schema.String, |
| 53 | + occurredAt: Schema.String, |
| 54 | + }), |
| 55 | + ), |
| 56 | +}); |
| 57 | +export type GitHubPullRequestSnapshot = typeof GitHubPullRequestSnapshot.Type; |
| 58 | + |
| 59 | +export class GitHubPullRequestProbeDecodeError extends Schema.TaggedErrorClass<GitHubPullRequestProbeDecodeError>()( |
| 60 | + "GitHubPullRequestProbeDecodeError", |
| 61 | + { |
| 62 | + repository: Schema.String, |
| 63 | + pullRequestNumber: Schema.Int, |
| 64 | + cause: Schema.Defect(), |
| 65 | + }, |
| 66 | +) { |
| 67 | + override get message(): string { |
| 68 | + return `GitHub returned an invalid watch snapshot for ${this.repository}#${this.pullRequestNumber}.`; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export type GitHubPullRequestProbeError = |
| 73 | + | GitHubCli.GitHubCliError |
| 74 | + | GitHubPullRequestProbeDecodeError; |
| 75 | + |
| 76 | +export interface GitHubPullRequestProbeInput { |
| 77 | + readonly cwd: string; |
| 78 | + readonly repository: string; |
| 79 | + readonly pullRequestNumber: number; |
| 80 | +} |
| 81 | + |
| 82 | +export class GitHubPullRequestProbe extends Context.Service< |
| 83 | + GitHubPullRequestProbe, |
| 84 | + { |
| 85 | + readonly get: ( |
| 86 | + input: GitHubPullRequestProbeInput, |
| 87 | + ) => Effect.Effect<GitHubPullRequestSnapshot, GitHubPullRequestProbeError>; |
| 88 | + } |
| 89 | +>()("t3/github/GitHubPullRequestProbe") {} |
| 90 | + |
| 91 | +function iso(value: string): string { |
| 92 | + return DateTime.formatIso(DateTime.makeUnsafe(value)); |
| 93 | +} |
| 94 | + |
| 95 | +function normalizeState( |
| 96 | + state: string, |
| 97 | + mergedAt: string | null, |
| 98 | +): GitHubPullRequestSnapshot["state"] { |
| 99 | + if (mergedAt !== null || state.toUpperCase() === "MERGED") return "merged"; |
| 100 | + return state.toUpperCase() === "OPEN" ? "open" : "closed"; |
| 101 | +} |
| 102 | + |
| 103 | +function normalizeCheck(check: typeof RawCheck.Type): GitHubPullRequestSnapshot["checks"][number] { |
| 104 | + const rawStatus = (check.status ?? check.state ?? "PENDING").toUpperCase(); |
| 105 | + const completed = |
| 106 | + rawStatus === "COMPLETED" || |
| 107 | + rawStatus === "SUCCESS" || |
| 108 | + rawStatus === "FAILURE" || |
| 109 | + rawStatus === "ERROR"; |
| 110 | + const conclusion = |
| 111 | + check.conclusion ?? (completed && rawStatus !== "COMPLETED" ? rawStatus : null); |
| 112 | + return { |
| 113 | + name: check.name ?? check.context ?? "GitHub check", |
| 114 | + status: completed ? "completed" : "pending", |
| 115 | + conclusion: conclusion?.toLowerCase() ?? null, |
| 116 | + }; |
| 117 | +} |
| 118 | + |
| 119 | +function normalizeActivity( |
| 120 | + activity: typeof RawReviewActivity.Type, |
| 121 | +): GitHubPullRequestSnapshot["reviewActivity"][number] | undefined { |
| 122 | + const occurredAt = activity.createdAt ?? activity.submittedAt; |
| 123 | + return occurredAt === undefined ? undefined : { id: activity.id, occurredAt: iso(occurredAt) }; |
| 124 | +} |
| 125 | + |
| 126 | +function normalizeSnapshot(raw: typeof RawSnapshot.Type): GitHubPullRequestSnapshot { |
| 127 | + const mergedAt = raw.mergedAt === null ? null : iso(raw.mergedAt); |
| 128 | + const reviewActivity = [...raw.comments, ...raw.reviews] |
| 129 | + .map(normalizeActivity) |
| 130 | + .filter((activity) => activity !== undefined) |
| 131 | + .sort( |
| 132 | + (left, right) => |
| 133 | + left.occurredAt.localeCompare(right.occurredAt) || left.id.localeCompare(right.id), |
| 134 | + ); |
| 135 | + return { |
| 136 | + url: raw.url, |
| 137 | + state: normalizeState(raw.state, mergedAt), |
| 138 | + headSha: raw.headRefOid, |
| 139 | + mergedAt, |
| 140 | + updatedAt: iso(raw.updatedAt), |
| 141 | + checks: raw.statusCheckRollup.map(normalizeCheck), |
| 142 | + reviewActivity, |
| 143 | + }; |
| 144 | +} |
| 145 | + |
| 146 | +export interface GitHubWaitpointEvaluation { |
| 147 | + readonly satisfied: boolean; |
| 148 | + readonly summary: string; |
| 149 | +} |
| 150 | + |
| 151 | +export function evaluateGitHubWaitpoint( |
| 152 | + condition: OrchestratorMcpGitHubWaitCondition, |
| 153 | + baseline: GitHubPullRequestSnapshot, |
| 154 | + current: GitHubPullRequestSnapshot, |
| 155 | +): GitHubWaitpointEvaluation { |
| 156 | + switch (condition) { |
| 157 | + case "checks_settled": { |
| 158 | + const settled = |
| 159 | + current.checks.length > 0 && current.checks.every((check) => check.status === "completed"); |
| 160 | + const failed = current.checks.filter( |
| 161 | + (check) => |
| 162 | + check.conclusion !== null && |
| 163 | + !["success", "skipped", "neutral"].includes(check.conclusion), |
| 164 | + ).length; |
| 165 | + return { |
| 166 | + satisfied: settled, |
| 167 | + summary: settled |
| 168 | + ? `${current.checks.length} checks settled (${failed} unsuccessful).` |
| 169 | + : `${current.checks.filter((check) => check.status === "pending").length} checks remain pending.`, |
| 170 | + }; |
| 171 | + } |
| 172 | + case "new_review_activity": { |
| 173 | + const baselineIds = new Set(baseline.reviewActivity.map((activity) => activity.id)); |
| 174 | + const added = current.reviewActivity.filter((activity) => !baselineIds.has(activity.id)); |
| 175 | + return { |
| 176 | + satisfied: added.length > 0, |
| 177 | + summary: |
| 178 | + added.length > 0 |
| 179 | + ? `${added.length} new review or comment event${added.length === 1 ? "" : "s"}.` |
| 180 | + : "No new review or comment activity.", |
| 181 | + }; |
| 182 | + } |
| 183 | + case "pull_request_closed": |
| 184 | + return { |
| 185 | + satisfied: current.state !== "open", |
| 186 | + summary: |
| 187 | + current.state === "merged" |
| 188 | + ? "Pull request merged." |
| 189 | + : current.state === "closed" |
| 190 | + ? "Pull request closed without merging." |
| 191 | + : "Pull request remains open.", |
| 192 | + }; |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +export const layer = Layer.effect( |
| 197 | + GitHubPullRequestProbe, |
| 198 | + Effect.gen(function* () { |
| 199 | + const gitHubCli = yield* GitHubCli.GitHubCli; |
| 200 | + return GitHubPullRequestProbe.of({ |
| 201 | + get: Effect.fn("GitHubPullRequestProbe.get")(function* (input) { |
| 202 | + const output = yield* gitHubCli.execute({ |
| 203 | + cwd: input.cwd, |
| 204 | + args: [ |
| 205 | + "pr", |
| 206 | + "view", |
| 207 | + String(input.pullRequestNumber), |
| 208 | + "--repo", |
| 209 | + input.repository, |
| 210 | + "--json", |
| 211 | + WATCH_FIELDS, |
| 212 | + ], |
| 213 | + }); |
| 214 | + const raw = yield* decodeRawSnapshot(output.stdout).pipe( |
| 215 | + Effect.mapError( |
| 216 | + (cause) => |
| 217 | + new GitHubPullRequestProbeDecodeError({ |
| 218 | + repository: input.repository, |
| 219 | + pullRequestNumber: input.pullRequestNumber, |
| 220 | + cause, |
| 221 | + }), |
| 222 | + ), |
| 223 | + ); |
| 224 | + return normalizeSnapshot(raw); |
| 225 | + }), |
| 226 | + }); |
| 227 | + }), |
| 228 | +); |
0 commit comments