Open History to see the last commit
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/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 sql = yield* SqlClient.SqlClient;
20 const rows = yield* sql<{
21 id: string;
22 projectId: string;
23 title: string;
24 archivedAt: string | null;
25 host: string;
26 repository: string;
27 number: number;
28 url: string;
29 }>`
30 SELECT t.thread_id AS id, t.project_id AS "projectId", t.title,
31 t.archived_at AS "archivedAt", json_extract(link.value, '$.host') AS host,
32 json_extract(link.value, '$.repository') AS repository,
33 json_extract(link.value, '$.number') AS number,
34 json_extract(link.value, '$.url') AS url,
35 t.updated_at AS "updatedAt"
36 FROM orchestration_v2_projection_threads AS t
37 JOIN json_each(t.payload_json, '$.pullRequests') AS link
38 WHERE json_extract(link.value, '$.number') = ${key.number}
39 AND json_extract(link.value, '$.source') != 'stack-dismissed'
40 AND t.deleted_at IS NULL
41 ORDER BY "updatedAt" DESC, id ASC
42 `;
43 const threads = rows
44 .filter((row) => threadPullRequestKeysEqual(row, key))
45 .map(({ id, projectId, title, archivedAt }) => ({ id, projectId, title, archivedAt }));
46 return yield* decodeLinkedThreads({ threads });
47 },
48 Effect.mapError(
49 (cause) =>
50 new PullRequestOperationError({
51 operation: "linkedThreads",
52 detail: "Could not load linked threads.",
53 cause,
54 }),
55 ),
56);