Repository navigation
Expand file tree
/
Copy pathlinkedThreads.ts
More file actions
68 lines (66 loc) · 2.42 KB
/
Copy pathlinkedThreads.ts
File metadata and controls
68 lines (66 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import {
normalizeThreadPullRequestKey,
threadPullRequestKeysEqual,
} from "../sourceControl/threadPullRequests.ts";
import {
PullRequestLinkedThreadsResult,
PullRequestOperationError,
type ThreadPullRequestKey,
} from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
import * as Schema from "effect/Schema";
import * as SqlClient from "effect/sql/SqlClient";
const decodeLinkedThreads = Schema.decodeUnknownEffect(PullRequestLinkedThreadsResult);
export const listLinkedPullRequestThreads = Effect.fn("listLinkedPullRequestThreads")(
function* (input: ThreadPullRequestKey) {
const key = normalizeThreadPullRequestKey(input);
const sql = yield* SqlClient.SqlClient;
const rows = yield* sql<{
id: string;
projectId: string;
title: string;
archivedAt: string | null;
settled: number;
host: string;
repository: string;
number: number;
url: string;
}>`
SELECT t.thread_id AS id, t.project_id AS "projectId", t.title,
t.archived_at AS "archivedAt",
(json_extract(t.payload_json, '$.settledAt') IS NOT NULL
OR json_extract(t.payload_json, '$.settledOverride') IS 'settled') AS settled,
json_extract(link.value, '$.host') AS host,
json_extract(link.value, '$.repository') AS repository,
json_extract(link.value, '$.number') AS number,
json_extract(link.value, '$.url') AS url,
t.updated_at AS "updatedAt"
FROM orchestration_v2_projection_threads AS t
JOIN json_each(t.payload_json, '$.pullRequests') AS link
WHERE json_extract(link.value, '$.number') = ${key.number}
AND json_extract(link.value, '$.source') != 'stack-dismissed'
AND t.deleted_at IS NULL
AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1
ORDER BY "updatedAt" DESC, id ASC
`.pipe(NodeSqliteClient.readOnly);
const threads = rows
.filter((row) => threadPullRequestKeysEqual(row, key))
.map(({ id, projectId, title, archivedAt, settled }) => ({
id,
projectId,
title,
archivedAt,
settled: settled === 1,
}));
return yield* decodeLinkedThreads({ threads });
},
Effect.mapError(
(cause) =>
new PullRequestOperationError({
operation: "linkedThreads",
detail: "Could not load linked threads.",
cause,
}),
),
);