Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

linkedThreads.ts

39 lines1.4 KB

1import { normalizeThreadPullRequestKey } from "@t3tools/shared/threadPullRequests";
2import {
3 PullRequestLinkedThreadsResult,
4 PullRequestOperationError,
5 type ThreadPullRequestKey,
6} from "@t3tools/contracts";
7import * as Effect from "effect/Effect";
8import * as Schema from "effect/Schema";
9import * as SqlClient from "effect/unstable/sql/SqlClient";
10​
11const decodeLinkedThreads = Schema.decodeUnknownEffect(PullRequestLinkedThreadsResult);
12​
13export const listLinkedPullRequestThreads = Effect.fn("listLinkedPullRequestThreads")(
14 function* (input: ThreadPullRequestKey) {
15 const key = normalizeThreadPullRequestKey(input);
16 const sql = yield* SqlClient.SqlClient;
17 const threads = yield* sql`
18 SELECT t.thread_id AS id, t.project_id AS "projectId", t.title,
19 t.archived_at AS "archivedAt"
20 FROM projection_thread_pull_requests AS link
21 JOIN projection_threads AS t ON t.thread_id = link.thread_id
22 WHERE link.host = ${key.host.toLowerCase()}
23 AND link.repository = ${key.repository.toLowerCase()}
24 AND link.number = ${key.number}
25 AND link.source != 'stack-dismissed'
26 AND t.deleted_at IS NULL
27 ORDER BY t.updated_at DESC, t.thread_id ASC
28 `;
29 return yield* decodeLinkedThreads({ threads });
30 },
31 Effect.mapError(
32 (cause) =>
33 new PullRequestOperationError({
34 operation: "linkedThreads",
35 detail: "Could not load linked threads.",
36 cause,
37 }),
38 ),
39);