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

linkedThreads.ts

68 lines2.4 KB

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 settled: number;
27 host: string;
28 repository: string;
29 number: number;
30 url: string;
31 }>`
32 SELECT t.thread_id AS id, t.project_id AS "projectId", t.title,
33 t.archived_at AS "archivedAt",
34 (json_extract(t.payload_json, '$.settledAt') IS NOT NULL
35 OR json_extract(t.payload_json, '$.settledOverride') IS 'settled') AS settled,
36 json_extract(link.value, '$.host') AS host,
37 json_extract(link.value, '$.repository') AS repository,
38 json_extract(link.value, '$.number') AS number,
39 json_extract(link.value, '$.url') AS url,
40 t.updated_at AS "updatedAt"
41 FROM orchestration_v2_projection_threads AS t
42 JOIN json_each(t.payload_json, '$.pullRequests') AS link
43 WHERE json_extract(link.value, '$.number') = ${key.number}
44 AND json_extract(link.value, '$.source') != 'stack-dismissed'
45 AND t.deleted_at IS NULL
46 AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1
47 ORDER BY "updatedAt" DESC, id ASC
48 `.pipe(NodeSqliteClient.readOnly);
49 const threads = rows
50 .filter((row) => threadPullRequestKeysEqual(row, key))
51 .map(({ id, projectId, title, archivedAt, settled }) => ({
52 id,
53 projectId,
54 title,
55 archivedAt,
56 settled: settled === 1,
57 }));
58 return yield* decodeLinkedThreads({ threads });
59 },
60 Effect.mapError(
61 (cause) =>
62 new PullRequestOperationError({
63 operation: "linkedThreads",
64 detail: "Could not load linked threads.",
65 cause,
66 }),
67 ),
68);