@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 233 lines 7.46 KB
Raw
1 import * as Issues from "@distilled.cloud/github/issues";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import { dedent } from "alchemy/Util/dedent";
6 import * as Effect from "effect/Effect";
7 import { gitHubBaseUrlChanged, withBaseUrl } from "./BaseUrl.ts";
8 import * as GitHub from "./Providers.ts";
9
10 export interface CommentProps {
11 /**
12 * Repository owner (user or organization).
13 */
14 owner: string;
15
16 /**
17 * Repository name.
18 */
19 repository: string;
20
21 /**
22 * Issue or Pull Request number to comment on.
23 */
24 issueNumber: number;
25
26 /**
27 * Comment body (supports GitHub Markdown).
28 *
29 * The body is automatically dedented, so you can use indented template
30 * literals without worrying about leading whitespace. Accepts
31 * `Output<string>` at the call site via `Output.interpolate` to embed
32 * resource attributes that are not yet resolved.
33 */
34 body: string;
35
36 /**
37 * Whether to allow deletion of the comment when the resource is destroyed.
38 * By default, comments are never deleted to preserve discussion history.
39 * @default false
40 */
41 allowDelete?: boolean;
42
43 /**
44 * Override the GitHub host or API base URL for this resource only (e.g.
45 * `github.example.com` for GitHub Enterprise). Falls back to
46 * `GitHub.providers({ baseUrl })`, then to the host resolved by the auth
47 * provider. Changing it replaces the resource — the same name on a
48 * different GitHub instance is a different physical resource.
49 */
50 baseUrl?: string;
51 }
52
53 export interface Comment extends Resource<
54 "GitHub.Comment",
55 CommentProps,
56 {
57 /**
58 * The numeric ID of the comment in GitHub.
59 */
60 commentId: number;
61
62 /**
63 * URL to view the comment in a browser.
64 */
65 htmlUrl: string;
66
67 /**
68 * ISO-8601 timestamp of the last update.
69 */
70 updatedAt: string;
71 },
72 never,
73 GitHub.Providers
74 > {}
75
76 /**
77 * A GitHub issue or pull request comment.
78 *
79 * `Comment` manages the lifecycle of a single comment on an issue or pull
80 * request. Comments are created on the first deploy and updated in place on
81 * subsequent deploys when the `body` changes. By default, comments are never
82 * deleted to preserve discussion history — set `allowDelete: true` to opt in.
83 *
84 * Authentication is resolved in order: explicit `token` prop,
85 * `GITHUB_ACCESS_TOKEN` env var, `GITHUB_TOKEN` env var. The token needs
86 * `repo` scope for private repositories or `public_repo` for public ones.
87 * ### Creating Comments
88 * **Example:** Comment on an Issue
89 * ```typescript
90 * const comment = yield* GitHub.Comment("issue-comment", {
91 * owner: "my-org",
92 * repository: "my-repo",
93 * issueNumber: 123,
94 * body: "This is a comment created by Alchemy!",
95 * });
96 * ```
97 *
98 * **Example:** Comment on a Pull Request
99 * ```typescript
100 * const prComment = yield* GitHub.Comment("pr-comment", {
101 * owner: "my-org",
102 * repository: "my-repo",
103 * issueNumber: 456,
104 * body: "## Deployment Status\n\nSuccessfully deployed to staging!",
105 * });
106 * ```
107 *
108 * ### Updating Comments
109 * Deploy with the same logical ID and a different `body` to update the
110 * existing comment in place rather than creating a new one.
111 *
112 * **Example:** Update Comment Content
113 * ```typescript
114 * const comment = yield* GitHub.Comment("status-comment", {
115 * owner: "my-org",
116 * repository: "my-repo",
117 * issueNumber: 789,
118 * body: "Deployment completed successfully!",
119 * });
120 * ```
121 *
122 * ### Deleting Comments
123 * **Example:** Allow Comment Deletion
124 * ```typescript
125 * const comment = yield* GitHub.Comment("temp-comment", {
126 * owner: "my-org",
127 * repository: "my-repo",
128 * issueNumber: 123,
129 * body: "This comment can be deleted",
130 * allowDelete: true,
131 * });
132 * ```
133 *
134 * ### CI Preview Comments
135 * A common pattern is posting a preview-deployment URL on every pull request.
136 * The comment auto-updates on each push because the logical ID stays the same.
137 *
138 * **Example:** PR Preview Comment
139 * ```typescript
140 * if (process.env.PULL_REQUEST) {
141 * yield* GitHub.Comment("preview-comment", {
142 * owner: "my-org",
143 * repository: "my-repo",
144 * issueNumber: Number(process.env.PULL_REQUEST),
145 * body: Output.interpolate`
146 * ## Preview Deployed
147 *
148 * **URL:** ${website.url}
149 * `,
150 * });
151 * }
152 * ```
153 *
154 * @resource
155 * @product Issue
156 */
157 export const Comment = Resource<Comment>("GitHub.Comment");
158
159 export const CommentProvider = () =>
160 Provider.succeed(Comment, {
161 stables: ["commentId"],
162 // Non-listable: a Comment is identified entirely by its parent
163 // {owner, repository, issueNumber} plus the server-assigned commentId.
164 // GitHub only exposes comment enumeration *within* a specific issue or PR
165 // (`issues.listComments`); there is no account- or repo-wide API to
166 // enumerate every comment without first knowing the issue/PR. With no
167 // ambient scope to enumerate from, this collapses to the empty list.
168 list: () => Effect.succeed([]),
169
170 // A comment belongs to (host, owner, repository, issueNumber) — its
171 // server-assigned id is meaningless anywhere else, so moving it replaces
172 // the resource: a fresh comment is posted on the new issue, and the old
173 // one is deleted only when `allowDelete` is set (the provider's `delete`
174 // no-ops otherwise, preserving discussion history — the default).
175 diff: Effect.fn(function* ({ news, olds }) {
176 if (!isResolved(news)) return;
177 if (olds === undefined) return;
178 if (
179 news.owner !== olds.owner ||
180 news.repository !== olds.repository ||
181 news.issueNumber !== olds.issueNumber ||
182 (yield* gitHubBaseUrlChanged(olds, news))
183 ) {
184 return { action: "replace" };
185 }
186 }),
187
188 reconcile: Effect.fn(
189 function* ({ news, output }) {
190 const body = dedent(news.body);
191 const scope = { owner: news.owner, repo: news.repository };
192
193 // Observe — GitHub assigns `comment_id` server-side. Probe for live
194 // state via the cached id; a 404 (deleted out-of-band, or never
195 // created) collapses to "no observed comment" so we converge by
196 // posting a fresh one.
197 const observed = output?.commentId
198 ? yield* Issues.getComment({ ...scope, comment_id: output.commentId }).pipe(
199 Effect.catchTag("NotFound", () => Effect.undefined),
200 )
201 : undefined;
202
203 // Ensure — when no live comment exists, POST creates one. Sync — PATCH
204 // the existing comment with the desired body. GitHub's updateComment is
205 // idempotent for identical bodies (returns same updatedAt), so we
206 // always issue the call rather than diffing.
207 const data =
208 observed === undefined
209 ? yield* Issues.createComment({ ...scope, issue_number: news.issueNumber, body })
210 : yield* Issues.updateComment({ ...scope, comment_id: observed.id, body });
211 return {
212 commentId: data.id,
213 htmlUrl: data.html_url,
214 updatedAt: data.updated_at,
215 };
216 },
217 (effect, { news }) => effect.pipe(withBaseUrl(news.baseUrl)),
218 ),
219
220 delete: Effect.fn(
221 function* ({ olds, output }) {
222 if (!olds.allowDelete) {
223 return;
224 }
225 yield* Issues.deleteComment({
226 owner: olds.owner,
227 repo: olds.repository,
228 comment_id: output.commentId,
229 }).pipe(Effect.catchTag("NotFound", () => Effect.void));
230 },
231 (effect, { olds }) => effect.pipe(withBaseUrl(olds.baseUrl)),
232 ),
233 });