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

linkedThreads.ts

55 lines1.9 KB

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