Open History to see the last commit
1import {
2 normalizeThreadPullRequestKey,
3 threadPullRequestKeysEqual,
4} from "../sourceControl/threadPullRequests.ts";
5import {
6 PullRequestLinkedThreadsResult,
7 PullRequestOperationError,
8 type ThreadPullRequestKey,
9} from "@t3tools/contracts";
10import * as Effect from "effect/Effect";
11import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
12import * as Schema from "effect/Schema";
13import * as SqlClient from "effect/sql/SqlClient";
14
15const decodeLinkedThreads = Schema.decodeUnknownEffect(PullRequestLinkedThreadsResult);
16
17export const listLinkedPullRequestThreads = Effect.fn("listLinkedPullRequestThreads")(
18 function* (input: ThreadPullRequestKey) {
19 const key = normalizeThreadPullRequestKey(input);
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", json_extract(link.value, '$.host') AS host,
33 json_extract(link.value, '$.repository') AS repository,
34 json_extract(link.value, '$.number') AS number,
35 json_extract(link.value, '$.url') AS url,
36 t.updated_at AS "updatedAt"
37 FROM orchestration_v2_projection_threads AS t
38 JOIN json_each(t.payload_json, '$.pullRequests') AS link
39 WHERE json_extract(link.value, '$.number') = ${key.number}
40 AND json_extract(link.value, '$.source') != 'stack-dismissed'
41 AND t.deleted_at IS NULL
42 ORDER BY "updatedAt" DESC, id ASC
43 `.pipe(NodeSqliteClient.readOnly);
44 const threads = rows
45 .filter((row) => threadPullRequestKeysEqual(row, key))
46 .map(({ id, projectId, title, archivedAt }) => ({ id, projectId, title, archivedAt }));
47 return yield* decodeLinkedThreads({ threads });
48 },
49 Effect.mapError(
50 (cause) =>
51 new PullRequestOperationError({
52 operation: "linkedThreads",
53 detail: "Could not load linked threads.",
54 cause,
55 }),
56 ),
57);