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

linkedThreads.ts

68 lines2.5 KB

1import {
2 IssueLinkedThreadsResult,
3 IssueOperationError,
4 threadIssueKeysEqual,
5 type ThreadIssueKey,
6} from "@t3tools/contracts";
7import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
8import * as Effect from "effect/Effect";
9import * as Schema from "effect/Schema";
10import * as SqlClient from "effect/sql/SqlClient";
11​
12const decodeLinkedThreads = Schema.decodeUnknownEffect(IssueLinkedThreadsResult);
13​
14/** The threads an issue is linked to; the rows and filters `linkedThreads` uses for pull requests. */
15export const listLinkedIssueThreads = Effect.fn("listLinkedIssueThreads")(
16 function* (key: ThreadIssueKey) {
17 const sql = yield* SqlClient.SqlClient;
18 // The CASE stops SQLite hoisting the side-chat check out of the link loop,
19 // where it would re-parse every thread's payload, not just matching ones.
20 const rows = yield* sql<{
21 id: string;
22 projectId: string;
23 title: string;
24 archivedAt: string | null;
25 settled: number;
26 tracker: ThreadIssueKey["tracker"];
27 host: string;
28 scope: string;
29 key: string;
30 }>`
31 SELECT t.thread_id AS id, t.project_id AS "projectId", t.title,
32 t.archived_at AS "archivedAt",
33 (json_extract(t.payload_json, '$.settledAt') IS NOT NULL
34 OR json_extract(t.payload_json, '$.settledOverride') IS 'settled') AS settled,
35 json_extract(link.value, '$.tracker') AS tracker,
36 json_extract(link.value, '$.host') AS host,
37 json_extract(link.value, '$.scope') AS scope,
38 json_extract(link.value, '$.key') AS key,
39 t.updated_at AS "updatedAt"
40 FROM orchestration_v2_projection_threads AS t
41 JOIN json_each(t.payload_json, '$.issues') AS link
42 WHERE t.deleted_at IS NULL
43 AND CASE WHEN json_extract(link.value, '$.key') = ${key.key.trim()}
44 THEN json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1
45 ELSE 0
46 END
47 ORDER BY "updatedAt" DESC, id ASC
48 `.pipe(NodeSqliteClient.readOnly);
49 const threads = rows
50 .filter((row) => threadIssueKeysEqual(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 IssueOperationError({
63 operation: "linkedThreads",
64 detail: "Could not load linked threads.",
65 cause,
66 }),
67 ),
68);