@mastra/github-signals 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/dist/index.cjs +1421 -1269
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1396 -1262
- package/dist/index.js.map +1 -1
- package/package.json +10 -9
package/dist/index.js
CHANGED
|
@@ -1,477 +1,577 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { readFile } from
|
|
3
|
-
import { homedir } from
|
|
4
|
-
import { join } from
|
|
5
|
-
import { promisify } from
|
|
6
|
-
import { SignalProvider } from
|
|
7
|
-
import { createTool } from
|
|
8
|
-
import z from
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var _execFileAsync;
|
|
1
|
+
import { createHash, randomUUID } from "crypto";
|
|
2
|
+
import { readFile, stat } from "fs/promises";
|
|
3
|
+
import { homedir } from "os";
|
|
4
|
+
import { join } from "path";
|
|
5
|
+
import { promisify } from "util";
|
|
6
|
+
import { SignalProvider } from "@mastra/core/signals";
|
|
7
|
+
import { createTool } from "@mastra/core/tools";
|
|
8
|
+
import z from "zod";
|
|
9
|
+
//#region src/index.ts
|
|
10
|
+
let _execFileAsync;
|
|
12
11
|
async function execFileAsync(file, args, options) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
_execFileAsync = promisify(cp.execFile);
|
|
16
|
-
}
|
|
17
|
-
return _execFileAsync(file, args, options);
|
|
12
|
+
if (!_execFileAsync) _execFileAsync = promisify((await import("child_process")).execFile);
|
|
13
|
+
return _execFileAsync(file, args, options);
|
|
18
14
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
15
|
+
const GITHUB_SUBSCRIBE_PR_TAG = "github-subscribe-pr";
|
|
16
|
+
const GITHUB_UNSUBSCRIBE_PR_TAG = "github-unsubscribe-pr";
|
|
17
|
+
const GITHUB_SYNC_STATUS_TAG = "github-sync-status";
|
|
18
|
+
const GITHUB_SIGNALS_METADATA_KEY = "githubSignals";
|
|
19
|
+
const DEFAULT_AUTHORIZED_PERMISSIONS = [
|
|
20
|
+
"admin",
|
|
21
|
+
"maintain",
|
|
22
|
+
"write"
|
|
23
|
+
];
|
|
24
|
+
const DEFAULT_AUTHORIZED_BOTS = ["coderabbitai[bot]", "devin-ai-integration[bot]"];
|
|
25
|
+
const PERMISSION_CACHE_TTL_MS = 300 * 1e3;
|
|
26
|
+
/** Notification kinds driven by comment/review activity that should be gated by author permission. */
|
|
27
|
+
const AUTHOR_GATED_NOTIFICATION_KINDS = /* @__PURE__ */ new Set(["pull-request-activity", "pull-request-review-activity"]);
|
|
28
|
+
const createGithubTool = createTool;
|
|
28
29
|
function isPlainObject(value) {
|
|
29
|
-
|
|
30
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
30
31
|
}
|
|
31
32
|
function readString(value) {
|
|
32
|
-
|
|
33
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
33
34
|
}
|
|
34
35
|
function readNumber(value) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return void 0;
|
|
36
|
+
if (typeof value === "number" && Number.isInteger(value) && value > 0) return value;
|
|
37
|
+
if (typeof value === "string" && /^\d+$/.test(value)) return Number(value);
|
|
38
38
|
}
|
|
39
39
|
function stableJson(value) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
return JSON.stringify(value);
|
|
40
|
+
if (Array.isArray(value)) return `[${value.map(stableJson).join(",")}]`;
|
|
41
|
+
if (isPlainObject(value)) return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${stableJson(value[key])}`).join(",")}}`;
|
|
42
|
+
return JSON.stringify(value);
|
|
45
43
|
}
|
|
46
44
|
function snapshotHash(value) {
|
|
47
|
-
|
|
45
|
+
return createHash("sha256").update(stableJson(value)).digest("hex");
|
|
48
46
|
}
|
|
49
47
|
function resolveHomePath(path) {
|
|
50
|
-
|
|
48
|
+
return path.startsWith("~/") ? join(homedir(), path.slice(2)) : path;
|
|
49
|
+
}
|
|
50
|
+
async function readDbPathFromGitcrawlConfig(configPath) {
|
|
51
|
+
try {
|
|
52
|
+
const config = await readFile(resolveHomePath(configPath), "utf8");
|
|
53
|
+
const match = /^\s*db_path\s*=\s*['\"]([^'\"]+)['\"]/m.exec(config);
|
|
54
|
+
if (match?.[1]) return resolveHomePath(match[1]);
|
|
55
|
+
} catch {}
|
|
51
56
|
}
|
|
52
|
-
async function
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
} catch {
|
|
60
|
-
}
|
|
61
|
-
return join(homedir(), ".config", "gitcrawl", "gitcrawl.db");
|
|
57
|
+
async function fileExists(path) {
|
|
58
|
+
try {
|
|
59
|
+
await stat(path);
|
|
60
|
+
return true;
|
|
61
|
+
} catch {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
62
64
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Directories gitcrawl uses for its config and database by default. Linux
|
|
67
|
+
* uses the XDG path, but macOS uses ~/Library/Application Support, so probing
|
|
68
|
+
* only ~/.config silently misses the database on macOS.
|
|
69
|
+
*/
|
|
70
|
+
function gitcrawlDefaultDirs() {
|
|
71
|
+
return [join(homedir(), ".config", "gitcrawl"), ...process.platform === "darwin" ? [join(homedir(), "Library", "Application Support", "gitcrawl")] : []];
|
|
67
72
|
}
|
|
68
73
|
function sqlString(value) {
|
|
69
|
-
|
|
74
|
+
return `'${value.replaceAll("'", "''")}'`;
|
|
70
75
|
}
|
|
71
76
|
function getSignalMetadata(message) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
77
|
+
if (message.role !== "signal") return void 0;
|
|
78
|
+
const signal = message.content.metadata?.signal;
|
|
79
|
+
return isPlainObject(signal) ? signal : void 0;
|
|
75
80
|
}
|
|
76
81
|
function getGithubMetadata(threadMetadata) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
82
|
+
const mastra = isPlainObject(threadMetadata?.mastra) ? threadMetadata.mastra : {};
|
|
83
|
+
const githubSignals = isPlainObject(mastra["githubSignals"]) ? mastra[GITHUB_SIGNALS_METADATA_KEY] : {};
|
|
84
|
+
const rawSubscriptions = Array.isArray(githubSignals.subscriptions) ? githubSignals.subscriptions : [];
|
|
85
|
+
const subscriptions = [];
|
|
86
|
+
for (const rawSubscription of rawSubscriptions) {
|
|
87
|
+
if (!isPlainObject(rawSubscription)) continue;
|
|
88
|
+
const owner = readString(rawSubscription.owner);
|
|
89
|
+
const repo = readString(rawSubscription.repo);
|
|
90
|
+
const number = readNumber(rawSubscription.number);
|
|
91
|
+
const subscribedAt = readString(rawSubscription.subscribedAt);
|
|
92
|
+
const updatedAt = readString(rawSubscription.updatedAt);
|
|
93
|
+
const lastSubscribeSignalId = readString(rawSubscription.lastSubscribeSignalId);
|
|
94
|
+
if (!owner || !repo || !number || !subscribedAt || !updatedAt || !lastSubscribeSignalId) continue;
|
|
95
|
+
subscriptions.push({
|
|
96
|
+
owner,
|
|
97
|
+
repo,
|
|
98
|
+
number,
|
|
99
|
+
subscribedAt,
|
|
100
|
+
updatedAt,
|
|
101
|
+
lastSubscribeSignalId,
|
|
102
|
+
...readString(rawSubscription.lastSyncAt) ? { lastSyncAt: readString(rawSubscription.lastSyncAt) } : {},
|
|
103
|
+
...rawSubscription.lastSyncStatus === "success" || rawSubscription.lastSyncStatus === "error" || rawSubscription.lastSyncStatus === "skipped" ? { lastSyncStatus: rawSubscription.lastSyncStatus } : {},
|
|
104
|
+
...readString(rawSubscription.lastSyncError) ? { lastSyncError: readString(rawSubscription.lastSyncError) } : {},
|
|
105
|
+
...readString(rawSubscription.lastSnapshotError) ? { lastSnapshotError: readString(rawSubscription.lastSnapshotError) } : {},
|
|
106
|
+
...readString(rawSubscription.lastObservedGithubUpdatedAt) ? { lastObservedGithubUpdatedAt: readString(rawSubscription.lastObservedGithubUpdatedAt) } : {},
|
|
107
|
+
...readString(rawSubscription.lastObservedContentHash) ? { lastObservedContentHash: readString(rawSubscription.lastObservedContentHash) } : {},
|
|
108
|
+
...readString(rawSubscription.lastObservedThreadContentHash) ? { lastObservedThreadContentHash: readString(rawSubscription.lastObservedThreadContentHash) } : {},
|
|
109
|
+
...readString(rawSubscription.lastObservedHeadSha) ? { lastObservedHeadSha: readString(rawSubscription.lastObservedHeadSha) } : {},
|
|
110
|
+
...readString(rawSubscription.lastObservedState) ? { lastObservedState: readString(rawSubscription.lastObservedState) } : {},
|
|
111
|
+
...readString(rawSubscription.lastObservedMergeableState) ? { lastObservedMergeableState: readString(rawSubscription.lastObservedMergeableState) } : {},
|
|
112
|
+
...readString(rawSubscription.lastObservedCiState) ? { lastObservedCiState: readString(rawSubscription.lastObservedCiState) } : {},
|
|
113
|
+
...readString(rawSubscription.lastObservedReviewStateHash) ? { lastObservedReviewStateHash: readString(rawSubscription.lastObservedReviewStateHash) } : {},
|
|
114
|
+
...readString(rawSubscription.lastNotificationAt) ? { lastNotificationAt: readString(rawSubscription.lastNotificationAt) } : {},
|
|
115
|
+
...readString(rawSubscription.lastNotificationKind) ? { lastNotificationKind: readString(rawSubscription.lastNotificationKind) } : {},
|
|
116
|
+
...rawSubscription.lastNotificationPriority === "medium" || rawSubscription.lastNotificationPriority === "high" ? { lastNotificationPriority: rawSubscription.lastNotificationPriority } : {},
|
|
117
|
+
...readString(rawSubscription.lastNotificationSummary) ? { lastNotificationSummary: readString(rawSubscription.lastNotificationSummary) } : {}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
subscriptions,
|
|
122
|
+
...githubSignals.subscriptionHintShown === true ? { subscriptionHintShown: true } : {}
|
|
123
|
+
};
|
|
118
124
|
}
|
|
119
125
|
function setGithubMetadata(threadMetadata, githubSignals) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
126
|
+
const existing = threadMetadata ?? {};
|
|
127
|
+
const mastra = isPlainObject(existing.mastra) ? existing.mastra : {};
|
|
128
|
+
const existingGithubSignals = isPlainObject(mastra["githubSignals"]) ? mastra[GITHUB_SIGNALS_METADATA_KEY] : {};
|
|
129
|
+
return {
|
|
130
|
+
...existing,
|
|
131
|
+
mastra: {
|
|
132
|
+
...mastra,
|
|
133
|
+
[GITHUB_SIGNALS_METADATA_KEY]: {
|
|
134
|
+
...existingGithubSignals,
|
|
135
|
+
...githubSignals
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
133
139
|
}
|
|
134
140
|
function getFailingChecks(snapshot) {
|
|
135
|
-
|
|
141
|
+
return (snapshot.checks ?? []).filter((check) => check.conclusion === "failure" || check.conclusion === "timed_out");
|
|
136
142
|
}
|
|
137
143
|
function getPendingChecks(snapshot) {
|
|
138
|
-
|
|
144
|
+
return (snapshot.checks ?? []).filter((check) => check.status && check.status !== "completed");
|
|
139
145
|
}
|
|
140
146
|
function getPrLabel(subscription, snapshot) {
|
|
141
|
-
|
|
142
|
-
|
|
147
|
+
const pr = `${subscription.owner}/${subscription.repo}#${subscription.number}`;
|
|
148
|
+
return snapshot?.title ? `${pr}: ${snapshot.title}` : pr;
|
|
143
149
|
}
|
|
144
150
|
function getMergedNotificationSummary(label) {
|
|
145
|
-
|
|
151
|
+
return `${label} was merged. This thread has been automatically unsubscribed from this PR. Resubscribe if you still need updates.`;
|
|
146
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Removes a hidden block (its delimiters *and* its content) starting at every `open` marker.
|
|
155
|
+
*
|
|
156
|
+
* For each `open` occurrence the whole region up to and including the matching `close` is dropped.
|
|
157
|
+
* When `close` is missing the block is treated as unterminated and removed through end-of-string,
|
|
158
|
+
* so large payloads can't survive by omitting their closing marker. Matching is case-insensitive on
|
|
159
|
+
* the markers and uses plain `indexOf` scanning, so there is no regex backtracking (ReDoS-safe).
|
|
160
|
+
*/
|
|
147
161
|
function stripBlocks(text, open, close) {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
162
|
+
const haystack = text.toLowerCase();
|
|
163
|
+
const openLower = open.toLowerCase();
|
|
164
|
+
const closeLower = close.toLowerCase();
|
|
165
|
+
let result = "";
|
|
166
|
+
let cursor = 0;
|
|
167
|
+
for (;;) {
|
|
168
|
+
const start = haystack.indexOf(openLower, cursor);
|
|
169
|
+
if (start === -1) {
|
|
170
|
+
result += text.slice(cursor);
|
|
171
|
+
return result;
|
|
172
|
+
}
|
|
173
|
+
result += text.slice(cursor, start);
|
|
174
|
+
const end = haystack.indexOf(closeLower, start + openLower.length);
|
|
175
|
+
if (end === -1) return result;
|
|
176
|
+
cursor = end + closeLower.length;
|
|
177
|
+
}
|
|
164
178
|
}
|
|
165
|
-
|
|
166
|
-
|
|
179
|
+
/** Sentinel marker wrapping a stashed Markdown code region; `\u0000` cannot appear in GitHub text. */
|
|
180
|
+
const CODE_TOKEN_PREFIX = "\0CODE";
|
|
181
|
+
const CODE_TOKEN_SUFFIX = "\0";
|
|
182
|
+
/**
|
|
183
|
+
* Temporarily removes Markdown code spans and fenced code blocks so tag stripping can't damage
|
|
184
|
+
* human-authored code examples.
|
|
185
|
+
*
|
|
186
|
+
* GitHub renders Markdown, so legitimate code like `` `<Component>` ``, generic type examples, or
|
|
187
|
+
* fenced JSX/TSX must survive sanitization. Each code region is replaced with an opaque token and
|
|
188
|
+
* pushed onto a stash; {@link restore} swaps the tokens back after the surrounding prose has been
|
|
189
|
+
* stripped of markup. Fenced blocks are matched before inline spans so backtick runs inside a fence
|
|
190
|
+
* are not mistaken for inline code.
|
|
191
|
+
*/
|
|
167
192
|
function preserveMarkdownCode(text) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
};
|
|
193
|
+
const preserved = [];
|
|
194
|
+
const stash = (match) => {
|
|
195
|
+
const token = `${CODE_TOKEN_PREFIX}${preserved.length}${CODE_TOKEN_SUFFIX}`;
|
|
196
|
+
preserved.push(match);
|
|
197
|
+
return token;
|
|
198
|
+
};
|
|
199
|
+
return {
|
|
200
|
+
text: text.replace(/```[\s\S]*?```/g, stash).replace(/(`{2,})(?!`)[\s\S]*?[^`]\1(?!`)/g, stash).replace(/`[^`\n]*`/g, stash),
|
|
201
|
+
restore: (sanitized) => sanitized.replace(/\u0000CODE(\d+)\u0000/g, (_, index) => preserved[Number(index)] ?? "")
|
|
202
|
+
};
|
|
179
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* Removes XML/HTML-like markup — and the content it hides — from a PR comment body, leaving only
|
|
206
|
+
* human-readable text while preserving Markdown code examples.
|
|
207
|
+
*
|
|
208
|
+
* Review bots (e.g. CodeRabbit) embed large machine-only payloads in comments: base64 state blobs
|
|
209
|
+
* inside `<!-- ... -->` comments (often >100KB) and verbose collapsed `<details>` sections. Rather
|
|
210
|
+
* than targeting specific bot markers, we strip hidden blocks and tags generically, since none of
|
|
211
|
+
* that markup is useful to downstream consumers and persisting it balloons notification payloads and
|
|
212
|
+
* can overflow agent context windows.
|
|
213
|
+
*
|
|
214
|
+
* Markdown code spans/fenced blocks are stashed first and restored last, so legitimate code such as
|
|
215
|
+
* `` `<Component>` `` or fenced JSX is kept intact while bot markup elsewhere is removed. Block
|
|
216
|
+
* removal (comments, `<details>`) drops the *entire* section including its inner content, and any
|
|
217
|
+
* unterminated block is removed through end-of-string so a missing closing marker can't smuggle the
|
|
218
|
+
* payload through. All scanning is `indexOf`-based and the only regex used is a non-backtracking
|
|
219
|
+
* single-tag matcher, so adversarial input cannot trigger catastrophic backtracking (ReDoS). Any
|
|
220
|
+
* unterminated markup fragment (e.g. a dangling `<script` with no `>`) is dropped through end-of-
|
|
221
|
+
* string, and finally every remaining lone `<` is removed so no partial markup survives — while
|
|
222
|
+
* ordinary prose like `coverage < 80%` keeps its text intact.
|
|
223
|
+
*/
|
|
180
224
|
function sanitizeCommentText(body) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
return restore(stripped);
|
|
225
|
+
const { text: protectedBody, restore } = preserveMarkdownCode(body);
|
|
226
|
+
let text = stripBlocks(protectedBody, "<!--", "-->");
|
|
227
|
+
text = stripBlocks(text, "<details", "</details>");
|
|
228
|
+
return restore(text.replace(/<\/?[a-zA-Z][^<>]*>/g, "").replace(/<[!/a-zA-Z][\s\S]*$/g, "").replace(/</g, "").replace(/\n{3,}/g, "\n\n").trim());
|
|
186
229
|
}
|
|
230
|
+
/** Applies {@link sanitizeCommentText} to an optional comment body, preserving `undefined`. */
|
|
187
231
|
function sanitizeCommentBody(body) {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
232
|
+
if (body === void 0) return void 0;
|
|
233
|
+
const sanitized = sanitizeCommentText(body);
|
|
234
|
+
return sanitized.length > 0 ? sanitized : void 0;
|
|
191
235
|
}
|
|
192
236
|
function getCommentExcerpt(body) {
|
|
193
|
-
|
|
194
|
-
|
|
237
|
+
const excerpt = sanitizeCommentText(body).replace(/\s+/g, " ").trim();
|
|
238
|
+
return excerpt.length > 240 ? `${excerpt.slice(0, 237)}...` : excerpt;
|
|
195
239
|
}
|
|
196
240
|
function getCommentNotificationSummary(pr, snapshot) {
|
|
197
|
-
|
|
198
|
-
|
|
241
|
+
if (!snapshot.latestCommentAuthor || !snapshot.latestCommentBody) return void 0;
|
|
242
|
+
return `${snapshot.latestCommentAuthor} commented on ${pr}: ${getCommentExcerpt(snapshot.latestCommentBody)}`;
|
|
199
243
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
244
|
+
const githubActivityNotificationPriority = {
|
|
245
|
+
high: 0,
|
|
246
|
+
medium: 1
|
|
203
247
|
};
|
|
204
248
|
function getGithubActivityNotificationRank(notification) {
|
|
205
|
-
|
|
249
|
+
return notification.kind === "pull-request-activity" ? 0 : 1;
|
|
206
250
|
}
|
|
207
251
|
function compareGithubActivityNotifications(a, b) {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
252
|
+
if (!a && !b) return 0;
|
|
253
|
+
if (!a) return 1;
|
|
254
|
+
if (!b) return -1;
|
|
255
|
+
const priorityComparison = githubActivityNotificationPriority[a.priority] - githubActivityNotificationPriority[b.priority];
|
|
256
|
+
if (priorityComparison !== 0) return priorityComparison;
|
|
257
|
+
return getGithubActivityNotificationRank(a) - getGithubActivityNotificationRank(b);
|
|
214
258
|
}
|
|
215
259
|
function classifyGithubCommentActivityNotification(input) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
260
|
+
if (isBotOnlyActivity(input.snapshot)) return void 0;
|
|
261
|
+
const summary = getCommentNotificationSummary(`${input.subscription.owner}/${input.subscription.repo}#${input.subscription.number}`, input.snapshot);
|
|
262
|
+
if (!summary) return void 0;
|
|
263
|
+
return {
|
|
264
|
+
kind: "pull-request-activity",
|
|
265
|
+
priority: "high",
|
|
266
|
+
summary
|
|
267
|
+
};
|
|
221
268
|
}
|
|
222
269
|
function getCheckUpdatedTime(check) {
|
|
223
|
-
|
|
224
|
-
|
|
270
|
+
const value = check.updatedAt ? Date.parse(check.updatedAt) : NaN;
|
|
271
|
+
return Number.isFinite(value) ? value : 0;
|
|
225
272
|
}
|
|
226
273
|
function getCheckKey(check) {
|
|
227
|
-
|
|
274
|
+
return `${check.name || "check"}:${check.detailsUrl || check.workflowName || ""}`;
|
|
228
275
|
}
|
|
229
276
|
function normalizeGithubChecksForSnapshot(input) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
byKey.set(key, row);
|
|
246
|
-
continue;
|
|
247
|
-
}
|
|
248
|
-
const rowTime = getCheckUpdatedTime(row);
|
|
249
|
-
const existingTime = getCheckUpdatedTime(existing);
|
|
250
|
-
if (rowTime > existingTime || rowTime === existingTime && existing.source === "workflow" && row.source === "check") {
|
|
251
|
-
byKey.set(key, row);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
return [...byKey.values()].map(({ source: _source, ...check }) => check).sort((a, b) => `${a.name}:${a.detailsUrl ?? ""}`.localeCompare(`${b.name}:${b.detailsUrl ?? ""}`));
|
|
277
|
+
const latestCheckUpdatedAt = input.checkRows.reduce((latest, check) => Math.max(latest, getCheckUpdatedTime(check)), 0);
|
|
278
|
+
const rows = [...input.checkRows, ...input.workflowRows.filter((workflow) => input.checkRows.length === 0 || getCheckUpdatedTime(workflow) >= latestCheckUpdatedAt)];
|
|
279
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
280
|
+
for (const row of rows) {
|
|
281
|
+
const key = getCheckKey(row);
|
|
282
|
+
const existing = byKey.get(key);
|
|
283
|
+
if (!existing) {
|
|
284
|
+
byKey.set(key, row);
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
const rowTime = getCheckUpdatedTime(row);
|
|
288
|
+
const existingTime = getCheckUpdatedTime(existing);
|
|
289
|
+
if (rowTime > existingTime || rowTime === existingTime && existing.source === "workflow" && row.source === "check") byKey.set(key, row);
|
|
290
|
+
}
|
|
291
|
+
return [...byKey.values()].map(({ source: _source, ...check }) => check).sort((a, b) => `${a.name}:${a.detailsUrl ?? ""}`.localeCompare(`${b.name}:${b.detailsUrl ?? ""}`));
|
|
255
292
|
}
|
|
256
293
|
function isBotOnlyActivity(snapshot) {
|
|
257
|
-
|
|
294
|
+
return snapshot.latestCommentIsBot === true && (!snapshot.ciState || snapshot.ciState === "unknown");
|
|
258
295
|
}
|
|
259
296
|
function stringifyEvidence(value) {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
297
|
+
if (typeof value === "string") return value;
|
|
298
|
+
try {
|
|
299
|
+
return JSON.stringify(value);
|
|
300
|
+
} catch {
|
|
301
|
+
return "";
|
|
302
|
+
}
|
|
266
303
|
}
|
|
267
304
|
function detectPrWorkEvidence(input) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
305
|
+
const evidence = [input.text ?? "", ...(input.toolCalls ?? []).map((toolCall) => `${toolCall.toolName} ${stringifyEvidence(toolCall.args)}`)].join("\n");
|
|
306
|
+
if (!evidence.trim()) return void 0;
|
|
307
|
+
const url = /github\.com\/([^\s/#]+)\/([^\s/#]+)\/pull\/(\d+)/i.exec(evidence);
|
|
308
|
+
if (url?.[1] && url[2] && url[3]) return {
|
|
309
|
+
owner: url[1],
|
|
310
|
+
repo: url[2],
|
|
311
|
+
number: Number(url[3])
|
|
312
|
+
};
|
|
313
|
+
const repoRef = /\b([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)#(\d+)\b/.exec(evidence);
|
|
314
|
+
if (repoRef?.[1] && repoRef[2] && repoRef[3]) return {
|
|
315
|
+
owner: repoRef[1],
|
|
316
|
+
repo: repoRef[2],
|
|
317
|
+
number: Number(repoRef[3])
|
|
318
|
+
};
|
|
319
|
+
if (!/\bgh\s+(?:pr\s+(?:view|checks|status|comment|diff|checkout)|run\s+(?:rerun|view))\b/i.test(evidence)) return void 0;
|
|
320
|
+
const numberMatch = /(?:^|\s)#?(\d{2,})(?:\s|$)/.exec(evidence);
|
|
321
|
+
return numberMatch?.[1] ? { number: Number(numberMatch[1]) } : void 0;
|
|
284
322
|
}
|
|
285
323
|
function classifyGithubActivityNotification(input) {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
324
|
+
const pr = `${input.subscription.owner}/${input.subscription.repo}#${input.subscription.number}`;
|
|
325
|
+
const label = getPrLabel(input.subscription, input.snapshot);
|
|
326
|
+
if (input.snapshot.state && input.subscription.lastObservedState !== input.snapshot.state) {
|
|
327
|
+
if (input.snapshot.state === "merged") return {
|
|
328
|
+
kind: "pull-request-merged",
|
|
329
|
+
priority: "high",
|
|
330
|
+
summary: getMergedNotificationSummary(label)
|
|
331
|
+
};
|
|
332
|
+
if (input.snapshot.state === "closed") return {
|
|
333
|
+
kind: "pull-request-closed",
|
|
334
|
+
priority: "high",
|
|
335
|
+
summary: `${label} was closed`
|
|
336
|
+
};
|
|
337
|
+
if (input.subscription.lastObservedState && input.snapshot.state === "open") return {
|
|
338
|
+
kind: "pull-request-reopened",
|
|
339
|
+
priority: "medium",
|
|
340
|
+
summary: `${label} was reopened`
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
const failingChecks = getFailingChecks(input.snapshot);
|
|
344
|
+
if (input.snapshot.ciState === "failure" && input.subscription.lastObservedCiState !== "failure") {
|
|
345
|
+
const names = failingChecks.slice(0, 3).map((check) => check.name).join(", ");
|
|
346
|
+
return {
|
|
347
|
+
kind: "pull-request-ci-failure",
|
|
348
|
+
priority: "high",
|
|
349
|
+
summary: `${pr} has failing CI${names ? `: ${names}` : ""}`
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
if (input.snapshot.mergeableState === "dirty" && input.subscription.lastObservedMergeableState !== "dirty") return {
|
|
353
|
+
kind: "pull-request-conflict",
|
|
354
|
+
priority: "high",
|
|
355
|
+
summary: `${pr} has merge conflicts${input.snapshot.title ? `: ${input.snapshot.title}` : ""}`
|
|
356
|
+
};
|
|
357
|
+
if (input.snapshot.mergeableState && input.subscription.lastObservedMergeableState === "dirty" && input.snapshot.mergeableState !== "dirty") return {
|
|
358
|
+
kind: "pull-request-conflict-resolved",
|
|
359
|
+
priority: "medium",
|
|
360
|
+
summary: `${pr} merge conflicts were resolved`
|
|
361
|
+
};
|
|
362
|
+
if (input.snapshot.mergeableState === "dirty") return void 0;
|
|
363
|
+
if (input.snapshot.ciState === "success" && input.subscription.lastObservedCiState && input.subscription.lastObservedCiState !== "success") return {
|
|
364
|
+
kind: "pull-request-ci-recovered",
|
|
365
|
+
priority: "medium",
|
|
366
|
+
summary: `${pr} CI recovered`
|
|
367
|
+
};
|
|
368
|
+
if (input.snapshot.reviewStateHash && input.subscription.lastObservedReviewStateHash && input.snapshot.reviewStateHash !== input.subscription.lastObservedReviewStateHash && (input.snapshot.unresolvedReviewThreads ?? 0) > 0) return {
|
|
369
|
+
kind: "pull-request-review-activity",
|
|
370
|
+
priority: "medium",
|
|
371
|
+
summary: `${pr} has ${input.snapshot.unresolvedReviewThreads} unresolved review thread${input.snapshot.unresolvedReviewThreads === 1 ? "" : "s"}`
|
|
372
|
+
};
|
|
373
|
+
const pendingChecks = getPendingChecks(input.snapshot);
|
|
374
|
+
if (input.snapshot.ciState === "pending" && input.subscription.lastObservedCiState !== "pending" && pendingChecks.length > 0) {
|
|
375
|
+
const names = pendingChecks.slice(0, 3).map((check) => check.name).join(", ");
|
|
376
|
+
return {
|
|
377
|
+
kind: "pull-request-ci-pending",
|
|
378
|
+
priority: "medium",
|
|
379
|
+
summary: `${pr} has CI still running${names ? `: ${names}` : ""}`
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
if (input.snapshot.ciState === "pending" && input.subscription.lastObservedCiState === "pending") return void 0;
|
|
383
|
+
if (isBotOnlyActivity(input.snapshot)) return void 0;
|
|
384
|
+
const commentSummary = getCommentNotificationSummary(pr, input.snapshot);
|
|
385
|
+
return {
|
|
386
|
+
kind: "pull-request-activity",
|
|
387
|
+
priority: commentSummary ? "high" : "medium",
|
|
388
|
+
summary: commentSummary ?? `${pr} has new activity${input.snapshot.title ? `: ${input.snapshot.title}` : ""}`
|
|
389
|
+
};
|
|
351
390
|
}
|
|
352
391
|
function classifyGithubBaselineNotification(input) {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
392
|
+
const pr = `${input.subscription.owner}/${input.subscription.repo}#${input.subscription.number}`;
|
|
393
|
+
const failingChecks = getFailingChecks(input.snapshot);
|
|
394
|
+
const reviewCount = input.snapshot.unresolvedReviewThreads ?? 0;
|
|
395
|
+
const high = input.snapshot.ciState === "failure" || input.snapshot.mergeableState === "dirty";
|
|
396
|
+
const details = [
|
|
397
|
+
input.snapshot.state ? `state: ${input.snapshot.state}` : void 0,
|
|
398
|
+
input.snapshot.ciState && input.snapshot.ciState !== "unknown" ? `CI: ${input.snapshot.ciState}` : void 0,
|
|
399
|
+
input.snapshot.mergeableState ? `mergeability: ${input.snapshot.mergeableState}` : void 0,
|
|
400
|
+
reviewCount > 0 ? `${reviewCount} unresolved review thread${reviewCount === 1 ? "" : "s"}` : void 0,
|
|
401
|
+
failingChecks.length > 0 ? `failing: ${failingChecks.slice(0, 3).map((check) => check.name).join(", ")}` : void 0
|
|
402
|
+
].filter(Boolean);
|
|
403
|
+
return {
|
|
404
|
+
kind: "pull-request-baseline",
|
|
405
|
+
priority: high ? "high" : "medium",
|
|
406
|
+
summary: `${pr} subscribed${input.snapshot.title ? `: ${input.snapshot.title}` : ""}${details.length ? ` (${details.join("; ")})` : ""}`
|
|
407
|
+
};
|
|
369
408
|
}
|
|
370
409
|
function applySnapshotCursor(subscription, snapshot) {
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
410
|
+
if (snapshot.githubUpdatedAt) subscription.lastObservedGithubUpdatedAt = snapshot.githubUpdatedAt;
|
|
411
|
+
if (snapshot.contentHash) subscription.lastObservedContentHash = snapshot.contentHash;
|
|
412
|
+
if (snapshot.threadContentHash) subscription.lastObservedThreadContentHash = snapshot.threadContentHash;
|
|
413
|
+
if (snapshot.headSha) subscription.lastObservedHeadSha = snapshot.headSha;
|
|
414
|
+
if (snapshot.state) subscription.lastObservedState = snapshot.state;
|
|
415
|
+
if (snapshot.mergeableState) subscription.lastObservedMergeableState = snapshot.mergeableState;
|
|
416
|
+
if (snapshot.ciState) subscription.lastObservedCiState = snapshot.ciState;
|
|
417
|
+
if (snapshot.reviewStateHash) subscription.lastObservedReviewStateHash = snapshot.reviewStateHash;
|
|
379
418
|
}
|
|
380
419
|
function parseGitHubRemoteUrl(remoteUrl) {
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
420
|
+
const trimmed = remoteUrl.trim().replace(/\.git$/, "");
|
|
421
|
+
const httpsMatch = /^https:\/\/github\.com\/([^/]+)\/([^/]+)$/.exec(trimmed);
|
|
422
|
+
if (httpsMatch?.[1] && httpsMatch[2]) return {
|
|
423
|
+
owner: httpsMatch[1],
|
|
424
|
+
repo: httpsMatch[2]
|
|
425
|
+
};
|
|
426
|
+
const sshMatch = /^git@github\.com:([^/]+)\/([^/]+)$/.exec(trimmed);
|
|
427
|
+
if (sshMatch?.[1] && sshMatch[2]) return {
|
|
428
|
+
owner: sshMatch[1],
|
|
429
|
+
repo: sshMatch[2]
|
|
430
|
+
};
|
|
387
431
|
}
|
|
388
432
|
var GitRemoteRepositoryResolver = class {
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
433
|
+
async resolveRepository(input) {
|
|
434
|
+
try {
|
|
435
|
+
const { stdout } = await execFileAsync("git", [
|
|
436
|
+
"remote",
|
|
437
|
+
"get-url",
|
|
438
|
+
"origin"
|
|
439
|
+
], {
|
|
440
|
+
cwd: input.cwd,
|
|
441
|
+
signal: input.abortSignal
|
|
442
|
+
});
|
|
443
|
+
return parseGitHubRemoteUrl(stdout);
|
|
444
|
+
} catch {
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
400
448
|
};
|
|
401
449
|
var GitcrawlSyncClient = class {
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
450
|
+
#command;
|
|
451
|
+
#dbPathPromise;
|
|
452
|
+
constructor(options = {}) {
|
|
453
|
+
this.#command = options.command ?? "gitcrawl";
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Resolve the gitcrawl SQLite database path. Explicit env overrides win,
|
|
457
|
+
* then gitcrawl itself is asked (authoritative across platforms and
|
|
458
|
+
* versions), then known default locations are probed.
|
|
459
|
+
*/
|
|
460
|
+
async #resolveDbPath() {
|
|
461
|
+
if (process.env.GITCRAWL_DB_PATH) return resolveHomePath(process.env.GITCRAWL_DB_PATH);
|
|
462
|
+
if (process.env.GITCRAWL_CONFIG_PATH) {
|
|
463
|
+
const fromEnvConfig = await readDbPathFromGitcrawlConfig(process.env.GITCRAWL_CONFIG_PATH);
|
|
464
|
+
if (fromEnvConfig) return fromEnvConfig;
|
|
465
|
+
}
|
|
466
|
+
try {
|
|
467
|
+
const { stdout } = await execFileAsync(this.#command, ["status", "--json"], { maxBuffer: 10 * 1024 * 1024 });
|
|
468
|
+
const status = JSON.parse(stdout);
|
|
469
|
+
const reported = readString(status.database_path) ?? readString(status.db_path);
|
|
470
|
+
if (reported) return resolveHomePath(reported);
|
|
471
|
+
} catch {}
|
|
472
|
+
const defaultDirs = gitcrawlDefaultDirs();
|
|
473
|
+
for (const dir of defaultDirs) {
|
|
474
|
+
const fromConfig = await readDbPathFromGitcrawlConfig(join(dir, "config.toml"));
|
|
475
|
+
if (fromConfig) return fromConfig;
|
|
476
|
+
}
|
|
477
|
+
for (const dir of defaultDirs) {
|
|
478
|
+
const candidate = join(dir, "gitcrawl.db");
|
|
479
|
+
if (await fileExists(candidate)) return candidate;
|
|
480
|
+
}
|
|
481
|
+
return join(defaultDirs[0], "gitcrawl.db");
|
|
482
|
+
}
|
|
483
|
+
async #queryDb(sql) {
|
|
484
|
+
this.#dbPathPromise ??= this.#resolveDbPath();
|
|
485
|
+
const dbPath = await this.#dbPathPromise;
|
|
486
|
+
try {
|
|
487
|
+
const { stdout } = await execFileAsync("sqlite3", [
|
|
488
|
+
"-json",
|
|
489
|
+
dbPath,
|
|
490
|
+
sql
|
|
491
|
+
], { maxBuffer: 10 * 1024 * 1024 });
|
|
492
|
+
return JSON.parse(stdout || "[]");
|
|
493
|
+
} catch (error) {
|
|
494
|
+
this.#dbPathPromise = void 0;
|
|
495
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
496
|
+
throw new Error(`gitcrawl database query failed (db: ${dbPath}): ${message}`);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
async syncPullRequest(input) {
|
|
500
|
+
try {
|
|
501
|
+
const args = [
|
|
502
|
+
"sync",
|
|
503
|
+
`${input.owner}/${input.repo}`,
|
|
504
|
+
"--numbers",
|
|
505
|
+
String(input.number),
|
|
506
|
+
...input.includeComments === false ? [] : ["--include-comments"],
|
|
507
|
+
"--with",
|
|
508
|
+
"pr-details",
|
|
509
|
+
"--json"
|
|
510
|
+
];
|
|
511
|
+
const { stdout, stderr } = await execFileAsync(this.#command, args, {
|
|
512
|
+
cwd: input.cwd,
|
|
513
|
+
signal: input.abortSignal,
|
|
514
|
+
maxBuffer: 10 * 1024 * 1024
|
|
515
|
+
});
|
|
516
|
+
return {
|
|
517
|
+
ok: true,
|
|
518
|
+
stdout,
|
|
519
|
+
stderr
|
|
520
|
+
};
|
|
521
|
+
} catch (error) {
|
|
522
|
+
return {
|
|
523
|
+
ok: false,
|
|
524
|
+
error: error instanceof Error ? error.message : String(error)
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
async getPullRequestSnapshot(input) {
|
|
529
|
+
const { stdout } = await execFileAsync(this.#command, [
|
|
530
|
+
"threads",
|
|
531
|
+
`${input.owner}/${input.repo}`,
|
|
532
|
+
"--numbers",
|
|
533
|
+
String(input.number),
|
|
534
|
+
"--json"
|
|
535
|
+
], {
|
|
536
|
+
cwd: input.cwd,
|
|
537
|
+
signal: input.abortSignal,
|
|
538
|
+
maxBuffer: 10 * 1024 * 1024
|
|
539
|
+
});
|
|
540
|
+
const thread = JSON.parse(stdout).threads?.find((item) => readNumber(item.number) === input.number);
|
|
541
|
+
if (!thread) return void 0;
|
|
542
|
+
const owner = sqlString(input.owner);
|
|
543
|
+
const repo = sqlString(input.repo);
|
|
544
|
+
const number = input.number;
|
|
545
|
+
const [threadDetails] = await this.#queryDb(`select t.state, t.closed_at_gh, t.merged_at_gh
|
|
446
546
|
from threads t
|
|
447
547
|
join repositories r on r.id=t.repo_id
|
|
448
548
|
where r.owner=${owner} and r.name=${repo} and t.number=${number}
|
|
449
549
|
limit 1`);
|
|
450
|
-
|
|
550
|
+
const [details] = await this.#queryDb(`select d.head_sha, d.head_ref, d.mergeable_state,
|
|
451
551
|
json_extract(d.raw_json, '$.merged_at') as merged_at
|
|
452
552
|
from pull_request_details d
|
|
453
553
|
join threads t on t.id=d.thread_id
|
|
454
554
|
join repositories r on r.id=t.repo_id
|
|
455
555
|
where r.owner=${owner} and r.name=${repo} and t.number=${number}
|
|
456
556
|
limit 1`);
|
|
457
|
-
|
|
458
|
-
|
|
557
|
+
const headSha = readString(details?.head_sha);
|
|
558
|
+
const checkRows = await this.#queryDb(`select c.name, c.status, c.conclusion, c.workflow_name, c.details_url,
|
|
459
559
|
coalesce(c.completed_at, c.started_at, c.fetched_at) as updated_at
|
|
460
560
|
from pull_request_checks c
|
|
461
561
|
join threads t on t.id=c.thread_id
|
|
462
562
|
join repositories r on r.id=t.repo_id
|
|
463
563
|
where r.owner=${owner} and r.name=${repo} and t.number=${number}${headSha ? ` and json_extract(c.raw_json, '$.head_sha')=${sqlString(headSha)}` : ""}`);
|
|
464
|
-
|
|
564
|
+
const workflowRows = details?.head_sha ? await this.#queryDb(`select workflow_name, status, conclusion, html_url, updated_at_gh
|
|
465
565
|
from github_workflow_runs w
|
|
466
566
|
join repositories r on r.id=w.repo_id
|
|
467
567
|
where r.owner=${owner} and r.name=${repo} and w.head_sha=${sqlString(details.head_sha)}`) : [];
|
|
468
|
-
|
|
568
|
+
const [reviewState] = await this.#queryDb(`select count(*) as unresolved_count,
|
|
469
569
|
max(coalesce(first_comment_updated_at, first_comment_created_at, fetched_at)) as latest_review_thread_at
|
|
470
570
|
from pull_request_review_threads rt
|
|
471
571
|
join threads t on t.id=rt.thread_id
|
|
472
572
|
join repositories r on r.id=t.repo_id
|
|
473
573
|
where r.owner=${owner} and r.name=${repo} and t.number=${number} and rt.is_resolved=0`);
|
|
474
|
-
|
|
574
|
+
const latestComments = await this.#queryDb(`select c.author_login, c.author_type, c.is_bot, c.body, json_extract(c.raw_json, '$.html_url') as html_url,
|
|
475
575
|
coalesce(c.updated_at_gh, c.created_at_gh) as updated_at
|
|
476
576
|
from comments c
|
|
477
577
|
join threads t on t.id=c.thread_id
|
|
@@ -479,893 +579,927 @@ var GitcrawlSyncClient = class {
|
|
|
479
579
|
where r.owner=${owner} and r.name=${repo} and t.number=${number}
|
|
480
580
|
order by coalesce(c.updated_at_gh, c.created_at_gh) desc
|
|
481
581
|
limit 20`);
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
return void 0;
|
|
559
|
-
}
|
|
560
|
-
}
|
|
582
|
+
const latestComment = latestComments[0];
|
|
583
|
+
const checks = normalizeGithubChecksForSnapshot({
|
|
584
|
+
checkRows: checkRows.map((row) => ({
|
|
585
|
+
source: "check",
|
|
586
|
+
name: readString(row.name) ?? "check",
|
|
587
|
+
status: readString(row.status),
|
|
588
|
+
conclusion: readString(row.conclusion),
|
|
589
|
+
workflowName: readString(row.workflow_name),
|
|
590
|
+
detailsUrl: readString(row.details_url),
|
|
591
|
+
updatedAt: readString(row.updated_at)
|
|
592
|
+
})),
|
|
593
|
+
workflowRows: workflowRows.map((row) => ({
|
|
594
|
+
source: "workflow",
|
|
595
|
+
name: readString(row.workflow_name) ?? "workflow",
|
|
596
|
+
status: readString(row.status),
|
|
597
|
+
conclusion: readString(row.conclusion),
|
|
598
|
+
workflowName: readString(row.workflow_name),
|
|
599
|
+
detailsUrl: readString(row.html_url),
|
|
600
|
+
updatedAt: readString(row.updated_at_gh)
|
|
601
|
+
}))
|
|
602
|
+
});
|
|
603
|
+
const ciState = checks.some((check) => check.conclusion === "failure" || check.conclusion === "timed_out") ? "failure" : checks.some((check) => check.status && check.status !== "completed") ? "pending" : checks.length > 0 ? "success" : "unknown";
|
|
604
|
+
const threadContentHash = readString(thread.content_hash);
|
|
605
|
+
const unresolvedReviewThreads = Number(reviewState?.unresolved_count ?? 0);
|
|
606
|
+
const reviewStateHash = snapshotHash({
|
|
607
|
+
unresolvedReviewThreads,
|
|
608
|
+
latestReviewThreadAt: reviewState?.latest_review_thread_at
|
|
609
|
+
});
|
|
610
|
+
const contentHash = snapshotHash({
|
|
611
|
+
threadContentHash,
|
|
612
|
+
state: thread.state,
|
|
613
|
+
headSha: details?.head_sha,
|
|
614
|
+
mergeableState: details?.mergeable_state,
|
|
615
|
+
ciState,
|
|
616
|
+
reviewStateHash,
|
|
617
|
+
checks: checks.map((check) => ({
|
|
618
|
+
name: check.name,
|
|
619
|
+
status: check.status,
|
|
620
|
+
conclusion: check.conclusion,
|
|
621
|
+
detailsUrl: check.detailsUrl,
|
|
622
|
+
updatedAt: check.updatedAt
|
|
623
|
+
}))
|
|
624
|
+
});
|
|
625
|
+
return {
|
|
626
|
+
title: readString(thread.title),
|
|
627
|
+
state: readString(details?.merged_at) || readString(threadDetails?.merged_at_gh) ? "merged" : readString(threadDetails?.state) ?? readString(thread.state),
|
|
628
|
+
htmlUrl: readString(thread.html_url),
|
|
629
|
+
githubUpdatedAt: readString(thread.updated_at_gh),
|
|
630
|
+
closedAt: readString(threadDetails?.closed_at_gh),
|
|
631
|
+
mergedAt: readString(details?.merged_at) ?? readString(threadDetails?.merged_at_gh),
|
|
632
|
+
threadContentHash,
|
|
633
|
+
contentHash,
|
|
634
|
+
headSha: readString(details?.head_sha),
|
|
635
|
+
headRef: readString(details?.head_ref),
|
|
636
|
+
mergeableState: readString(details?.mergeable_state),
|
|
637
|
+
checks,
|
|
638
|
+
ciState,
|
|
639
|
+
unresolvedReviewThreads,
|
|
640
|
+
reviewStateHash,
|
|
641
|
+
latestReviewThreadAt: readString(reviewState?.latest_review_thread_at),
|
|
642
|
+
latestCommentAuthor: readString(latestComment?.author_login),
|
|
643
|
+
latestCommentAuthorType: readString(latestComment?.author_type),
|
|
644
|
+
latestCommentIsBot: latestComment?.is_bot === 1,
|
|
645
|
+
latestCommentBody: sanitizeCommentBody(readString(latestComment?.body)),
|
|
646
|
+
latestCommentUrl: readString(latestComment?.html_url),
|
|
647
|
+
latestCommentUpdatedAt: readString(latestComment?.updated_at),
|
|
648
|
+
latestComments: latestComments.map((comment) => ({
|
|
649
|
+
author: readString(comment.author_login),
|
|
650
|
+
authorType: readString(comment.author_type),
|
|
651
|
+
isBot: comment.is_bot === 1,
|
|
652
|
+
body: sanitizeCommentBody(readString(comment.body)),
|
|
653
|
+
url: readString(comment.html_url),
|
|
654
|
+
updatedAt: readString(comment.updated_at)
|
|
655
|
+
}))
|
|
656
|
+
};
|
|
657
|
+
}
|
|
561
658
|
};
|
|
562
659
|
var GithubSignals = class extends SignalProvider {
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
660
|
+
id = "github-signals";
|
|
661
|
+
name = "GitHub Signals";
|
|
662
|
+
#ghMastra;
|
|
663
|
+
static signals = {
|
|
664
|
+
subscribeToPR(input) {
|
|
665
|
+
const normalized = typeof input === "number" ? { number: input } : input;
|
|
666
|
+
return {
|
|
667
|
+
type: "reactive",
|
|
668
|
+
tagName: GITHUB_SUBSCRIBE_PR_TAG,
|
|
669
|
+
contents: `Subscribe to GitHub PR #${normalized.number}`,
|
|
670
|
+
attributes: {
|
|
671
|
+
...normalized.owner ? { owner: normalized.owner } : {},
|
|
672
|
+
...normalized.repo ? { repo: normalized.repo } : {},
|
|
673
|
+
number: normalized.number
|
|
674
|
+
},
|
|
675
|
+
metadata: { github: {
|
|
676
|
+
action: "subscribeToPR",
|
|
677
|
+
...normalized
|
|
678
|
+
} }
|
|
679
|
+
};
|
|
680
|
+
},
|
|
681
|
+
unsubscribeFromPR(input) {
|
|
682
|
+
const normalized = typeof input === "number" ? { number: input } : input;
|
|
683
|
+
return {
|
|
684
|
+
type: "reactive",
|
|
685
|
+
tagName: GITHUB_UNSUBSCRIBE_PR_TAG,
|
|
686
|
+
contents: `Unsubscribe from GitHub PR #${normalized.number}`,
|
|
687
|
+
attributes: {
|
|
688
|
+
...normalized.owner ? { owner: normalized.owner } : {},
|
|
689
|
+
...normalized.repo ? { repo: normalized.repo } : {},
|
|
690
|
+
number: normalized.number
|
|
691
|
+
},
|
|
692
|
+
metadata: { github: {
|
|
693
|
+
action: "unsubscribeFromPR",
|
|
694
|
+
...normalized
|
|
695
|
+
} }
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
#options;
|
|
700
|
+
#syncClient;
|
|
701
|
+
#repositoryResolver;
|
|
702
|
+
#polling = /* @__PURE__ */ new Map();
|
|
703
|
+
#permissionCache = /* @__PURE__ */ new Map();
|
|
704
|
+
#agent;
|
|
705
|
+
#agentOptions = {};
|
|
706
|
+
#subscriptionsChangedHandler;
|
|
707
|
+
#pollingChangedHandler;
|
|
708
|
+
constructor(options = {}) {
|
|
709
|
+
super();
|
|
710
|
+
this.#options = options;
|
|
711
|
+
this.#syncClient = options.syncClient ?? new GitcrawlSyncClient({ command: options.gitcrawlCommand });
|
|
712
|
+
this.#repositoryResolver = options.repositoryResolver ?? new GitRemoteRepositoryResolver();
|
|
713
|
+
if (options.getNotificationStreamOptions) this.#agentOptions = { getNotificationStreamOptions: options.getNotificationStreamOptions };
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* @deprecated Use `Agent({ signals: [githubSignals] })` instead.
|
|
717
|
+
* Kept for backward compatibility.
|
|
718
|
+
*/
|
|
719
|
+
addAgent(agent, options = {}) {
|
|
720
|
+
this.#agent = agent;
|
|
721
|
+
this.#agentOptions = options;
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* Called by the Agent constructor when this provider is passed via `signals: [...]`.
|
|
725
|
+
* Sets the bidirectional link so the provider can send signals back to the agent.
|
|
726
|
+
*/
|
|
727
|
+
connect(agent) {
|
|
728
|
+
super.connect(agent);
|
|
729
|
+
this.#agent = agent;
|
|
730
|
+
}
|
|
731
|
+
getInputProcessors() {
|
|
732
|
+
return [this];
|
|
733
|
+
}
|
|
734
|
+
getOutputProcessors() {
|
|
735
|
+
return [this];
|
|
736
|
+
}
|
|
737
|
+
onSubscriptionsChanged(handler) {
|
|
738
|
+
this.#subscriptionsChangedHandler = handler;
|
|
739
|
+
}
|
|
740
|
+
onPollingChanged(handler) {
|
|
741
|
+
this.#pollingChangedHandler = handler;
|
|
742
|
+
}
|
|
743
|
+
__registerMastra(mastra) {
|
|
744
|
+
super.__registerMastra(mastra);
|
|
745
|
+
this.#ghMastra = mastra;
|
|
746
|
+
}
|
|
747
|
+
async syncThreadNow(input) {
|
|
748
|
+
return this.#pollThread(input, { includeComments: true });
|
|
749
|
+
}
|
|
750
|
+
async subscribeThreadToPR(input) {
|
|
751
|
+
const pr = typeof input.pr === "number" ? { number: input.pr } : input.pr;
|
|
752
|
+
return this.#subscribe({
|
|
753
|
+
id: `github-command-subscribe-${randomUUID()}`,
|
|
754
|
+
...pr,
|
|
755
|
+
threadId: input.threadId,
|
|
756
|
+
resourceId: input.resourceId
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
async unsubscribeThreadFromPR(input) {
|
|
760
|
+
const pr = typeof input.pr === "number" ? { number: input.pr } : input.pr;
|
|
761
|
+
return this.#unsubscribe({
|
|
762
|
+
id: `github-command-unsubscribe-${randomUUID()}`,
|
|
763
|
+
...pr,
|
|
764
|
+
threadId: input.threadId,
|
|
765
|
+
resourceId: input.resourceId
|
|
766
|
+
});
|
|
767
|
+
}
|
|
768
|
+
async startPollingForThread(input, options = {}) {
|
|
769
|
+
if ((await this.#getThreadSubscriptions(input)).length === 0) {
|
|
770
|
+
this.stopPollingForThread(input);
|
|
771
|
+
return false;
|
|
772
|
+
}
|
|
773
|
+
const key = this.#pollingKey(input);
|
|
774
|
+
for (const [pollingKey, state] of this.#polling.entries()) {
|
|
775
|
+
if (pollingKey === key) continue;
|
|
776
|
+
clearInterval(state.timer);
|
|
777
|
+
this.#polling.delete(pollingKey);
|
|
778
|
+
}
|
|
779
|
+
if (this.#polling.has(key)) return true;
|
|
780
|
+
const runPoll = (pollOptions = {}) => {
|
|
781
|
+
this.#pollThread(input, pollOptions).catch((error) => {
|
|
782
|
+
console.warn("GitHub PR polling failed:", error);
|
|
783
|
+
});
|
|
784
|
+
};
|
|
785
|
+
const timer = setInterval(() => {
|
|
786
|
+
runPoll({ includeComments: true });
|
|
787
|
+
}, this.#options.pollIntervalMs ?? 3e5);
|
|
788
|
+
if (options.pollImmediately) runPoll({ includeComments: true });
|
|
789
|
+
timer.unref?.();
|
|
790
|
+
this.#polling.set(key, {
|
|
791
|
+
...input,
|
|
792
|
+
timer,
|
|
793
|
+
running: false
|
|
794
|
+
});
|
|
795
|
+
return true;
|
|
796
|
+
}
|
|
797
|
+
stopPollingForThread(input) {
|
|
798
|
+
const key = this.#pollingKey(input);
|
|
799
|
+
const state = this.#polling.get(key);
|
|
800
|
+
if (!state) return;
|
|
801
|
+
clearInterval(state.timer);
|
|
802
|
+
this.#polling.delete(key);
|
|
803
|
+
}
|
|
804
|
+
isPollingThread(input) {
|
|
805
|
+
return this.#polling.has(this.#pollingKey(input));
|
|
806
|
+
}
|
|
807
|
+
isPollingThreadRunning(input) {
|
|
808
|
+
return this.#polling.get(this.#pollingKey(input))?.running ?? false;
|
|
809
|
+
}
|
|
810
|
+
getPollIntervalMs() {
|
|
811
|
+
return this.#options.pollIntervalMs ?? 3e5;
|
|
812
|
+
}
|
|
813
|
+
stopAllPolling() {
|
|
814
|
+
for (const state of this.#polling.values()) clearInterval(state.timer);
|
|
815
|
+
this.#polling.clear();
|
|
816
|
+
}
|
|
817
|
+
async pollThreadNow(input) {
|
|
818
|
+
return this.#pollThread(input, { includeComments: true });
|
|
819
|
+
}
|
|
820
|
+
async processInputStep(args) {
|
|
821
|
+
const tools = this.#createTools(args);
|
|
822
|
+
if (args.stepNumber !== 0) return { tools };
|
|
823
|
+
const signal = this.#findLatestGithubSignal(args.messages);
|
|
824
|
+
if (!signal) return { tools };
|
|
825
|
+
const threadContext = this.#getThreadContext(args);
|
|
826
|
+
if (signal.tagName === "github-unsubscribe-pr") {
|
|
827
|
+
const result = await this.#unsubscribe({
|
|
828
|
+
...signal,
|
|
829
|
+
...threadContext,
|
|
830
|
+
abortSignal: args.abortSignal
|
|
831
|
+
});
|
|
832
|
+
await this.#sendStatus(args, result, {
|
|
833
|
+
status: result.removed ? "unsubscribed" : "not_subscribed",
|
|
834
|
+
action: "unsubscribeFromPR",
|
|
835
|
+
message: result.removed ? `Unsubscribed from ${result.owner}/${result.repo}#${result.number}.` : `No GitHub subscription found for ${result.owner}/${result.repo}#${result.number}.`
|
|
836
|
+
});
|
|
837
|
+
return { tools };
|
|
838
|
+
}
|
|
839
|
+
const result = await this.#subscribe({
|
|
840
|
+
...signal,
|
|
841
|
+
...threadContext,
|
|
842
|
+
abortSignal: args.abortSignal
|
|
843
|
+
});
|
|
844
|
+
if (result.alreadyProcessed) return { tools };
|
|
845
|
+
await this.#sendStatus(args, result, {
|
|
846
|
+
status: result.syncResult?.ok === false ? "sync_error" : "subscribed",
|
|
847
|
+
action: "subscribeToPR",
|
|
848
|
+
message: result.syncResult?.ok === false ? `Subscribed to ${result.owner}/${result.repo}#${result.number}, but gitcrawl sync failed: ${result.syncResult.error}` : `Subscribed to ${result.owner}/${result.repo}#${result.number}.`
|
|
849
|
+
});
|
|
850
|
+
return { tools };
|
|
851
|
+
}
|
|
852
|
+
async processOutputStep(args) {
|
|
853
|
+
const evidence = detectPrWorkEvidence({
|
|
854
|
+
text: args.text,
|
|
855
|
+
toolCalls: args.toolCalls
|
|
856
|
+
});
|
|
857
|
+
if (!evidence) return;
|
|
858
|
+
const threadContext = this.#getThreadContext(args);
|
|
859
|
+
if (!threadContext.threadId || !threadContext.resourceId) return;
|
|
860
|
+
const { threadStore, loadedThread } = await this.#loadThread(threadContext);
|
|
861
|
+
const githubMetadata = getGithubMetadata(loadedThread.metadata);
|
|
862
|
+
if (githubMetadata.subscriptionHintShown || githubMetadata.subscriptions.length > 0) return;
|
|
863
|
+
let repository;
|
|
864
|
+
try {
|
|
865
|
+
repository = await this.#resolveRepository({
|
|
866
|
+
id: "github-subscription-hint",
|
|
867
|
+
owner: evidence.owner,
|
|
868
|
+
repo: evidence.repo,
|
|
869
|
+
number: evidence.number
|
|
870
|
+
});
|
|
871
|
+
} catch {
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
await threadStore.saveThread({ thread: {
|
|
875
|
+
...loadedThread,
|
|
876
|
+
id: threadContext.threadId,
|
|
877
|
+
resourceId: threadContext.resourceId,
|
|
878
|
+
createdAt: loadedThread.createdAt ?? /* @__PURE__ */ new Date(),
|
|
879
|
+
updatedAt: /* @__PURE__ */ new Date(),
|
|
880
|
+
metadata: setGithubMetadata(loadedThread.metadata, {
|
|
881
|
+
...githubMetadata,
|
|
882
|
+
subscriptionHintShown: true
|
|
883
|
+
})
|
|
884
|
+
} });
|
|
885
|
+
await args.sendSignal?.({
|
|
886
|
+
type: "reactive",
|
|
887
|
+
tagName: "system-reminder",
|
|
888
|
+
contents: `Looks like you're working with ${repository.owner}/${repository.repo}#${evidence.number}. Use /github subscribe ${evidence.number} or the github_subscribe_pr tool to follow updates.`,
|
|
889
|
+
attributes: { type: "github-subscription-hint" },
|
|
890
|
+
metadata: { github: {
|
|
891
|
+
action: "subscriptionHint",
|
|
892
|
+
owner: repository.owner,
|
|
893
|
+
repo: repository.repo,
|
|
894
|
+
number: evidence.number
|
|
895
|
+
} }
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
async #resolveThreadStore() {
|
|
899
|
+
if (this.#options.threadStore) return this.#options.threadStore;
|
|
900
|
+
const storage = this.#ghMastra?.getStorage?.();
|
|
901
|
+
return storage?.getStore ? await storage.getStore("memory") : void 0;
|
|
902
|
+
}
|
|
903
|
+
#getThreadContext(args) {
|
|
904
|
+
const memoryContext = args.requestContext?.get("MastraMemory");
|
|
905
|
+
return {
|
|
906
|
+
threadId: memoryContext?.thread?.id,
|
|
907
|
+
resourceId: memoryContext?.resourceId
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
#createTools(args) {
|
|
911
|
+
const threadContext = this.#getThreadContext(args);
|
|
912
|
+
const getExecutionThreadContext = (context) => ({
|
|
913
|
+
threadId: context?.agent?.threadId ?? threadContext.threadId,
|
|
914
|
+
resourceId: context?.agent?.resourceId ?? threadContext.resourceId
|
|
915
|
+
});
|
|
916
|
+
return {
|
|
917
|
+
...args.tools,
|
|
918
|
+
github_subscribe_pr: createGithubTool({
|
|
919
|
+
id: "github_subscribe_pr",
|
|
920
|
+
description: "Subscribe this thread to a GitHub pull request. Syncs only the requested PR with gitcrawl and stores the subscription on the thread.",
|
|
921
|
+
inputSchema: z.object({
|
|
922
|
+
number: z.number().int().positive(),
|
|
923
|
+
owner: z.string().optional(),
|
|
924
|
+
repo: z.string().optional()
|
|
925
|
+
}),
|
|
926
|
+
execute: async (input, context) => {
|
|
927
|
+
const executionThreadContext = getExecutionThreadContext(context);
|
|
928
|
+
const result = await this.#subscribe({
|
|
929
|
+
id: `github-tool-subscribe-${randomUUID()}`,
|
|
930
|
+
owner: input.owner,
|
|
931
|
+
repo: input.repo,
|
|
932
|
+
number: input.number,
|
|
933
|
+
threadId: executionThreadContext.threadId,
|
|
934
|
+
resourceId: executionThreadContext.resourceId
|
|
935
|
+
});
|
|
936
|
+
return {
|
|
937
|
+
subscribed: true,
|
|
938
|
+
owner: result.owner,
|
|
939
|
+
repo: result.repo,
|
|
940
|
+
number: result.number,
|
|
941
|
+
syncStatus: result.syncResult?.ok === false ? "error" : result.syncResult ? "success" : void 0,
|
|
942
|
+
message: result.syncResult?.ok === false ? `Subscribed to ${result.owner}/${result.repo}#${result.number}, but gitcrawl sync failed: ${result.syncResult.error}` : `Subscribed to ${result.owner}/${result.repo}#${result.number}.`
|
|
943
|
+
};
|
|
944
|
+
}
|
|
945
|
+
}),
|
|
946
|
+
github_unsubscribe_pr: createGithubTool({
|
|
947
|
+
id: "github_unsubscribe_pr",
|
|
948
|
+
description: "Unsubscribe this thread from a GitHub pull request.",
|
|
949
|
+
inputSchema: z.object({
|
|
950
|
+
number: z.number().int().positive(),
|
|
951
|
+
owner: z.string().optional(),
|
|
952
|
+
repo: z.string().optional()
|
|
953
|
+
}),
|
|
954
|
+
execute: async (input, context) => {
|
|
955
|
+
const executionThreadContext = getExecutionThreadContext(context);
|
|
956
|
+
const result = await this.#unsubscribe({
|
|
957
|
+
id: `github-tool-unsubscribe-${randomUUID()}`,
|
|
958
|
+
owner: input.owner,
|
|
959
|
+
repo: input.repo,
|
|
960
|
+
number: input.number,
|
|
961
|
+
threadId: executionThreadContext.threadId,
|
|
962
|
+
resourceId: executionThreadContext.resourceId
|
|
963
|
+
});
|
|
964
|
+
return {
|
|
965
|
+
unsubscribed: result.removed ?? false,
|
|
966
|
+
owner: result.owner,
|
|
967
|
+
repo: result.repo,
|
|
968
|
+
number: result.number,
|
|
969
|
+
remainingSubscriptions: result.remainingSubscriptions,
|
|
970
|
+
message: result.removed ? `Unsubscribed from ${result.owner}/${result.repo}#${result.number}.` : `No GitHub subscription found for ${result.owner}/${result.repo}#${result.number}.`
|
|
971
|
+
};
|
|
972
|
+
}
|
|
973
|
+
})
|
|
974
|
+
};
|
|
975
|
+
}
|
|
976
|
+
async #resolveRepository(input) {
|
|
977
|
+
const resolvedRepository = input.owner && input.repo ? {
|
|
978
|
+
owner: input.owner,
|
|
979
|
+
repo: input.repo
|
|
980
|
+
} : this.#options.owner && this.#options.repo ? {
|
|
981
|
+
owner: this.#options.owner,
|
|
982
|
+
repo: this.#options.repo
|
|
983
|
+
} : await this.#repositoryResolver.resolveRepository({
|
|
984
|
+
cwd: this.#options.cwd,
|
|
985
|
+
abortSignal: input.abortSignal
|
|
986
|
+
});
|
|
987
|
+
if (!resolvedRepository?.owner || !resolvedRepository.repo) throw new Error("GitHub PR subscription requires owner and repo. Run inside a GitHub repo or pass owner and repo.");
|
|
988
|
+
return resolvedRepository;
|
|
989
|
+
}
|
|
990
|
+
async #loadThread(input) {
|
|
991
|
+
const threadStore = await this.#resolveThreadStore();
|
|
992
|
+
if (!threadStore) throw new Error("GitHub PR subscription requires memory-backed thread storage.");
|
|
993
|
+
if (!input.threadId || !input.resourceId) throw new Error("GitHub PR subscription requires threadId and resourceId.");
|
|
994
|
+
const loadedThread = await threadStore.getThreadById({
|
|
995
|
+
threadId: input.threadId,
|
|
996
|
+
resourceId: input.resourceId
|
|
997
|
+
}) ?? void 0;
|
|
998
|
+
if (!loadedThread) throw new Error(`Could not load thread ${input.threadId}.`);
|
|
999
|
+
return {
|
|
1000
|
+
threadStore,
|
|
1001
|
+
loadedThread
|
|
1002
|
+
};
|
|
1003
|
+
}
|
|
1004
|
+
#pollingKey(input) {
|
|
1005
|
+
return `${input.resourceId}:${input.threadId}`;
|
|
1006
|
+
}
|
|
1007
|
+
#getNotificationAgent(_input) {
|
|
1008
|
+
if (this.#agent) return this.#agent;
|
|
1009
|
+
const agentId = _input?.agentId ?? this.#options.agentId;
|
|
1010
|
+
return agentId ? this.#ghMastra?.getAgentById?.(agentId) : void 0;
|
|
1011
|
+
}
|
|
1012
|
+
async #getThreadSubscriptions(input) {
|
|
1013
|
+
const { loadedThread } = await this.#loadThread(input);
|
|
1014
|
+
return getGithubMetadata(loadedThread.metadata).subscriptions;
|
|
1015
|
+
}
|
|
1016
|
+
#notifySubscriptionsChanged(input) {
|
|
1017
|
+
this.#subscriptionsChangedHandler?.(input);
|
|
1018
|
+
}
|
|
1019
|
+
#notifyPollingChanged(input) {
|
|
1020
|
+
this.#pollingChangedHandler?.(input);
|
|
1021
|
+
}
|
|
1022
|
+
async #pollThread(input, options = {}) {
|
|
1023
|
+
const key = this.#pollingKey(input);
|
|
1024
|
+
const state = this.#polling.get(key);
|
|
1025
|
+
if (state?.running) return 0;
|
|
1026
|
+
if (state) state.running = true;
|
|
1027
|
+
this.#notifyPollingChanged({
|
|
1028
|
+
threadId: input.threadId,
|
|
1029
|
+
resourceId: input.resourceId,
|
|
1030
|
+
running: true
|
|
1031
|
+
});
|
|
1032
|
+
try {
|
|
1033
|
+
const { threadStore, loadedThread } = await this.#loadThread(input);
|
|
1034
|
+
const githubMetadata = getGithubMetadata(loadedThread.metadata);
|
|
1035
|
+
if (githubMetadata.subscriptions.length === 0) {
|
|
1036
|
+
this.stopPollingForThread(input);
|
|
1037
|
+
return 0;
|
|
1038
|
+
}
|
|
1039
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1040
|
+
const subscriptions = [];
|
|
1041
|
+
for (const subscription of githubMetadata.subscriptions) {
|
|
1042
|
+
const syncInput = {
|
|
1043
|
+
owner: subscription.owner,
|
|
1044
|
+
repo: subscription.repo,
|
|
1045
|
+
number: subscription.number,
|
|
1046
|
+
cwd: this.#options.cwd,
|
|
1047
|
+
includeComments: options.includeComments
|
|
1048
|
+
};
|
|
1049
|
+
const syncResult = await this.#syncClient.syncPullRequest(syncInput);
|
|
1050
|
+
let snapshot;
|
|
1051
|
+
let snapshotError;
|
|
1052
|
+
if (syncResult.ok) try {
|
|
1053
|
+
snapshot = await this.#syncClient.getPullRequestSnapshot?.(syncInput);
|
|
1054
|
+
} catch (error) {
|
|
1055
|
+
snapshotError = error instanceof Error ? error.message : String(error);
|
|
1056
|
+
}
|
|
1057
|
+
if (snapshot) snapshot = await this.#filterUnauthorizedLatestComment(subscription.owner, subscription.repo, snapshot);
|
|
1058
|
+
const nextSubscription = {
|
|
1059
|
+
...subscription,
|
|
1060
|
+
updatedAt: now,
|
|
1061
|
+
lastSyncAt: now,
|
|
1062
|
+
lastSyncStatus: syncResult.ok ? "success" : "error"
|
|
1063
|
+
};
|
|
1064
|
+
if (syncResult.error) nextSubscription.lastSyncError = syncResult.error;
|
|
1065
|
+
else delete nextSubscription.lastSyncError;
|
|
1066
|
+
if (snapshotError) nextSubscription.lastSnapshotError = snapshotError;
|
|
1067
|
+
else delete nextSubscription.lastSnapshotError;
|
|
1068
|
+
const previousGithubUpdatedAt = subscription.lastObservedGithubUpdatedAt;
|
|
1069
|
+
const previousContentHash = subscription.lastObservedContentHash;
|
|
1070
|
+
const previousThreadContentHash = subscription.lastObservedThreadContentHash;
|
|
1071
|
+
const previousHeadSha = subscription.lastObservedHeadSha;
|
|
1072
|
+
const latestCommentChanged = !!previousGithubUpdatedAt && !!snapshot?.latestCommentUpdatedAt && Date.parse(snapshot.latestCommentUpdatedAt) > Date.parse(previousGithubUpdatedAt);
|
|
1073
|
+
if (snapshot) applySnapshotCursor(nextSubscription, snapshot);
|
|
1074
|
+
const isFirstObservation = syncResult.ok && snapshot && !previousGithubUpdatedAt && !previousContentHash;
|
|
1075
|
+
const legacyAggregateChanged = previousContentHash && snapshot?.contentHash && previousContentHash !== snapshot.contentHash && !previousThreadContentHash && !previousHeadSha;
|
|
1076
|
+
const changed = isFirstObservation || syncResult.ok && snapshot && (legacyAggregateChanged || latestCommentChanged || previousThreadContentHash && snapshot.threadContentHash && previousThreadContentHash !== snapshot.threadContentHash || previousHeadSha && snapshot.headSha && previousHeadSha !== snapshot.headSha || subscription.lastObservedState && snapshot.state && subscription.lastObservedState !== snapshot.state || subscription.lastObservedMergeableState && snapshot.mergeableState && subscription.lastObservedMergeableState !== snapshot.mergeableState || subscription.lastObservedCiState && snapshot.ciState && subscription.lastObservedCiState !== snapshot.ciState || subscription.lastObservedReviewStateHash && snapshot.reviewStateHash && subscription.lastObservedReviewStateHash !== snapshot.reviewStateHash);
|
|
1077
|
+
let shouldKeepSubscription = true;
|
|
1078
|
+
if (changed && snapshot) {
|
|
1079
|
+
const notifications = await this.#sendActivityNotifications({
|
|
1080
|
+
polling: input,
|
|
1081
|
+
subscription,
|
|
1082
|
+
snapshot,
|
|
1083
|
+
previousGithubUpdatedAt,
|
|
1084
|
+
previousContentHash,
|
|
1085
|
+
latestCommentChanged
|
|
1086
|
+
});
|
|
1087
|
+
const primaryNotification = notifications[0];
|
|
1088
|
+
if (primaryNotification) {
|
|
1089
|
+
nextSubscription.lastNotificationAt = now;
|
|
1090
|
+
nextSubscription.lastNotificationKind = primaryNotification.kind;
|
|
1091
|
+
nextSubscription.lastNotificationPriority = primaryNotification.priority;
|
|
1092
|
+
nextSubscription.lastNotificationSummary = primaryNotification.summary;
|
|
1093
|
+
shouldKeepSubscription = notifications.every((notification) => notification.kind !== "pull-request-merged");
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
if (shouldKeepSubscription) subscriptions.push(nextSubscription);
|
|
1097
|
+
}
|
|
1098
|
+
await threadStore.saveThread({ thread: {
|
|
1099
|
+
...loadedThread,
|
|
1100
|
+
id: input.threadId,
|
|
1101
|
+
resourceId: input.resourceId,
|
|
1102
|
+
createdAt: loadedThread.createdAt ?? /* @__PURE__ */ new Date(),
|
|
1103
|
+
updatedAt: /* @__PURE__ */ new Date(),
|
|
1104
|
+
metadata: setGithubMetadata(loadedThread.metadata, { subscriptions })
|
|
1105
|
+
} });
|
|
1106
|
+
this.#notifySubscriptionsChanged({
|
|
1107
|
+
threadId: input.threadId,
|
|
1108
|
+
resourceId: input.resourceId,
|
|
1109
|
+
subscriptions
|
|
1110
|
+
});
|
|
1111
|
+
if (subscriptions.length === 0) this.stopPollingForThread(input);
|
|
1112
|
+
return subscriptions.length;
|
|
1113
|
+
} catch (error) {
|
|
1114
|
+
throw error;
|
|
1115
|
+
} finally {
|
|
1116
|
+
const latestState = this.#polling.get(key);
|
|
1117
|
+
if (latestState) latestState.running = false;
|
|
1118
|
+
this.#notifyPollingChanged({
|
|
1119
|
+
threadId: input.threadId,
|
|
1120
|
+
resourceId: input.resourceId,
|
|
1121
|
+
running: false
|
|
1122
|
+
});
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
#createGithubNotificationInput(input) {
|
|
1126
|
+
const failingChecks = getFailingChecks(input.snapshot);
|
|
1127
|
+
const pendingChecks = getPendingChecks(input.snapshot);
|
|
1128
|
+
const latestCommentExcerpt = input.snapshot.latestCommentBody ? getCommentExcerpt(input.snapshot.latestCommentBody) : void 0;
|
|
1129
|
+
const latestCommentDedupeSuffix = input.notification.kind === "pull-request-activity" && input.snapshot.latestCommentUrl ? `comment:${input.snapshot.latestCommentUrl}:${input.snapshot.latestCommentUpdatedAt ?? ""}` : input.dedupeSuffix;
|
|
1130
|
+
return {
|
|
1131
|
+
source: "github",
|
|
1132
|
+
kind: input.notification.kind,
|
|
1133
|
+
priority: input.notification.priority,
|
|
1134
|
+
summary: input.notification.summary,
|
|
1135
|
+
dedupeKey: `github:${input.subscription.owner}/${input.subscription.repo}#${input.subscription.number}:${latestCommentDedupeSuffix}`,
|
|
1136
|
+
coalesceKey: `github:${input.subscription.owner}/${input.subscription.repo}#${input.subscription.number}:${input.notification.kind}`,
|
|
1137
|
+
attributes: {
|
|
1138
|
+
owner: input.subscription.owner,
|
|
1139
|
+
repo: input.subscription.repo,
|
|
1140
|
+
number: input.subscription.number,
|
|
1141
|
+
...input.snapshot.title ? { title: input.snapshot.title } : {},
|
|
1142
|
+
...input.snapshot.state ? { state: input.snapshot.state } : {},
|
|
1143
|
+
...input.snapshot.htmlUrl ? { url: input.snapshot.htmlUrl } : {},
|
|
1144
|
+
...input.snapshot.githubUpdatedAt ? { githubUpdatedAt: input.snapshot.githubUpdatedAt } : {},
|
|
1145
|
+
...input.previousGithubUpdatedAt ? { previousGithubUpdatedAt: input.previousGithubUpdatedAt } : {},
|
|
1146
|
+
...input.snapshot.mergeableState ? { mergeableState: input.snapshot.mergeableState } : {},
|
|
1147
|
+
...input.snapshot.ciState ? { ciState: input.snapshot.ciState } : {},
|
|
1148
|
+
...input.snapshot.unresolvedReviewThreads !== void 0 ? { unresolvedReviewThreads: input.snapshot.unresolvedReviewThreads } : {},
|
|
1149
|
+
...input.snapshot.latestCommentAuthor ? { latestCommentAuthor: input.snapshot.latestCommentAuthor } : {},
|
|
1150
|
+
...latestCommentExcerpt ? { latestCommentExcerpt } : {},
|
|
1151
|
+
...input.snapshot.latestCommentUrl ? { latestCommentUrl: input.snapshot.latestCommentUrl } : {},
|
|
1152
|
+
...input.snapshot.latestCommentUpdatedAt ? { latestCommentUpdatedAt: input.snapshot.latestCommentUpdatedAt } : {},
|
|
1153
|
+
...failingChecks.length > 0 ? { failingChecks: failingChecks.map((check) => check.name).join(", ") } : {},
|
|
1154
|
+
...pendingChecks.length > 0 ? { pendingChecks: pendingChecks.map((check) => check.name).join(", ") } : {}
|
|
1155
|
+
},
|
|
1156
|
+
metadata: { github: {
|
|
1157
|
+
owner: input.subscription.owner,
|
|
1158
|
+
repo: input.subscription.repo,
|
|
1159
|
+
number: input.subscription.number,
|
|
1160
|
+
title: input.snapshot.title,
|
|
1161
|
+
state: input.snapshot.state,
|
|
1162
|
+
htmlUrl: input.snapshot.htmlUrl,
|
|
1163
|
+
githubUpdatedAt: input.snapshot.githubUpdatedAt,
|
|
1164
|
+
previousGithubUpdatedAt: input.previousGithubUpdatedAt,
|
|
1165
|
+
contentHash: input.snapshot.contentHash,
|
|
1166
|
+
previousContentHash: input.previousContentHash,
|
|
1167
|
+
threadContentHash: input.snapshot.threadContentHash,
|
|
1168
|
+
headSha: input.snapshot.headSha,
|
|
1169
|
+
headRef: input.snapshot.headRef,
|
|
1170
|
+
mergeableState: input.snapshot.mergeableState,
|
|
1171
|
+
ciState: input.snapshot.ciState,
|
|
1172
|
+
closedAt: input.snapshot.closedAt,
|
|
1173
|
+
mergedAt: input.snapshot.mergedAt,
|
|
1174
|
+
unresolvedReviewThreads: input.snapshot.unresolvedReviewThreads,
|
|
1175
|
+
reviewStateHash: input.snapshot.reviewStateHash,
|
|
1176
|
+
latestReviewThreadAt: input.snapshot.latestReviewThreadAt,
|
|
1177
|
+
latestCommentAuthor: input.snapshot.latestCommentAuthor,
|
|
1178
|
+
latestCommentAuthorType: input.snapshot.latestCommentAuthorType,
|
|
1179
|
+
latestCommentIsBot: input.snapshot.latestCommentIsBot,
|
|
1180
|
+
latestCommentExcerpt,
|
|
1181
|
+
latestCommentUrl: input.snapshot.latestCommentUrl,
|
|
1182
|
+
latestCommentUpdatedAt: input.snapshot.latestCommentUpdatedAt,
|
|
1183
|
+
failingChecks,
|
|
1184
|
+
pendingChecks
|
|
1185
|
+
} }
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
async #sendGithubNotification(input) {
|
|
1189
|
+
const notificationInput = this.#createGithubNotificationInput(input);
|
|
1190
|
+
const streamOptions = await this.#agentOptions.getNotificationStreamOptions?.(input.target);
|
|
1191
|
+
await input.agent?.sendNotificationSignal?.(notificationInput, streamOptions ? {
|
|
1192
|
+
...input.target,
|
|
1193
|
+
ifIdle: { streamOptions }
|
|
1194
|
+
} : input.target);
|
|
1195
|
+
}
|
|
1196
|
+
async #sendBaselineNotification(input) {
|
|
1197
|
+
const agent = this.#getNotificationAgent({});
|
|
1198
|
+
if (!agent?.sendNotificationSignal) return;
|
|
1199
|
+
await this.#sendGithubNotification({
|
|
1200
|
+
agent,
|
|
1201
|
+
subscription: input.subscription,
|
|
1202
|
+
snapshot: input.snapshot,
|
|
1203
|
+
notification: classifyGithubBaselineNotification({
|
|
1204
|
+
subscription: input.subscription,
|
|
1205
|
+
snapshot: input.snapshot
|
|
1206
|
+
}),
|
|
1207
|
+
target: {
|
|
1208
|
+
resourceId: input.resourceId,
|
|
1209
|
+
threadId: input.threadId
|
|
1210
|
+
},
|
|
1211
|
+
dedupeSuffix: `baseline:${input.subscription.lastSubscribeSignalId}`
|
|
1212
|
+
});
|
|
1213
|
+
}
|
|
1214
|
+
async #isAuthorizedAuthor(owner, repo, user, metadata = {}) {
|
|
1215
|
+
if (!user) return false;
|
|
1216
|
+
const normalizedUser = user.toLowerCase();
|
|
1217
|
+
if (metadata.isBot === true || metadata.authorType?.toLowerCase() === "bot" || normalizedUser.endsWith("[bot]")) {
|
|
1218
|
+
if ((this.#options.ignoredBots ?? []).some((bot) => bot.toLowerCase() === normalizedUser)) return false;
|
|
1219
|
+
return (this.#options.authorizedBots ?? DEFAULT_AUTHORIZED_BOTS).some((bot) => bot.toLowerCase() === normalizedUser);
|
|
1220
|
+
}
|
|
1221
|
+
const permission = await this.#loadAuthorPermission(owner, repo, user);
|
|
1222
|
+
const authorizedPermissions = this.#options.authorizedPermissions ?? DEFAULT_AUTHORIZED_PERMISSIONS;
|
|
1223
|
+
return !!permission && authorizedPermissions.includes(permission);
|
|
1224
|
+
}
|
|
1225
|
+
async #filterUnauthorizedLatestComment(owner, repo, snapshot) {
|
|
1226
|
+
const comments = snapshot.latestComments?.length ? snapshot.latestComments : [{
|
|
1227
|
+
author: snapshot.latestCommentAuthor,
|
|
1228
|
+
authorType: snapshot.latestCommentAuthorType,
|
|
1229
|
+
isBot: snapshot.latestCommentIsBot,
|
|
1230
|
+
body: snapshot.latestCommentBody,
|
|
1231
|
+
url: snapshot.latestCommentUrl,
|
|
1232
|
+
updatedAt: snapshot.latestCommentUpdatedAt
|
|
1233
|
+
}];
|
|
1234
|
+
if (!comments.some((comment) => comment.author)) return snapshot;
|
|
1235
|
+
if (!comments.some((comment) => comment.body || comment.url || comment.updatedAt)) return snapshot;
|
|
1236
|
+
for (const comment of comments) {
|
|
1237
|
+
if (!await this.#isAuthorizedAuthor(owner, repo, comment.author, {
|
|
1238
|
+
authorType: comment.authorType,
|
|
1239
|
+
isBot: comment.isBot
|
|
1240
|
+
})) continue;
|
|
1241
|
+
return {
|
|
1242
|
+
...snapshot,
|
|
1243
|
+
latestCommentAuthor: comment.author,
|
|
1244
|
+
latestCommentAuthorType: comment.authorType,
|
|
1245
|
+
latestCommentIsBot: comment.isBot,
|
|
1246
|
+
latestCommentBody: comment.body,
|
|
1247
|
+
latestCommentUrl: comment.url,
|
|
1248
|
+
latestCommentUpdatedAt: comment.updatedAt
|
|
1249
|
+
};
|
|
1250
|
+
}
|
|
1251
|
+
return {
|
|
1252
|
+
...snapshot,
|
|
1253
|
+
latestCommentAuthor: void 0,
|
|
1254
|
+
latestCommentAuthorType: void 0,
|
|
1255
|
+
latestCommentIsBot: void 0,
|
|
1256
|
+
latestCommentBody: void 0,
|
|
1257
|
+
latestCommentUrl: void 0,
|
|
1258
|
+
latestCommentUpdatedAt: void 0
|
|
1259
|
+
};
|
|
1260
|
+
}
|
|
1261
|
+
async #loadAuthorPermission(owner, repo, user) {
|
|
1262
|
+
const cacheKey = `${owner}/${repo}:${user.toLowerCase()}`;
|
|
1263
|
+
const cached = this.#permissionCache.get(cacheKey);
|
|
1264
|
+
if (cached && cached.expiresAt > Date.now()) return cached.permission;
|
|
1265
|
+
if (cached) this.#permissionCache.delete(cacheKey);
|
|
1266
|
+
try {
|
|
1267
|
+
let permission;
|
|
1268
|
+
if (this.#options.permissionResolver) permission = await this.#options.permissionResolver.getPermission(owner, repo, user);
|
|
1269
|
+
else {
|
|
1270
|
+
const { stdout } = await execFileAsync("gh", [
|
|
1271
|
+
"api",
|
|
1272
|
+
`repos/${owner}/${repo}/collaborators/${user}/permission`,
|
|
1273
|
+
"--jq",
|
|
1274
|
+
".permission"
|
|
1275
|
+
]);
|
|
1276
|
+
const raw = stdout.trim();
|
|
1277
|
+
permission = [
|
|
1278
|
+
"admin",
|
|
1279
|
+
"maintain",
|
|
1280
|
+
"write",
|
|
1281
|
+
"triage",
|
|
1282
|
+
"read",
|
|
1283
|
+
"none"
|
|
1284
|
+
].includes(raw) ? raw : void 0;
|
|
1285
|
+
}
|
|
1286
|
+
if (permission) this.#permissionCache.set(cacheKey, {
|
|
1287
|
+
permission,
|
|
1288
|
+
expiresAt: Date.now() + PERMISSION_CACHE_TTL_MS
|
|
1289
|
+
});
|
|
1290
|
+
return permission;
|
|
1291
|
+
} catch {
|
|
1292
|
+
this.#permissionCache.delete(cacheKey);
|
|
1293
|
+
return;
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
async #sendActivityNotifications(input) {
|
|
1297
|
+
const agent = this.#getNotificationAgent(input.polling);
|
|
1298
|
+
if (!agent?.sendNotificationSignal) return [];
|
|
1299
|
+
const notifications = [classifyGithubActivityNotification({
|
|
1300
|
+
subscription: input.subscription,
|
|
1301
|
+
snapshot: input.snapshot
|
|
1302
|
+
})];
|
|
1303
|
+
if (input.latestCommentChanged && notifications[0]?.kind !== "pull-request-activity") notifications.push(classifyGithubCommentActivityNotification({
|
|
1304
|
+
subscription: input.subscription,
|
|
1305
|
+
snapshot: input.snapshot
|
|
1306
|
+
}));
|
|
1307
|
+
const sent = [];
|
|
1308
|
+
const notificationInputs = [];
|
|
1309
|
+
for (const notification of notifications.sort(compareGithubActivityNotifications)) {
|
|
1310
|
+
if (!notification) continue;
|
|
1311
|
+
if (AUTHOR_GATED_NOTIFICATION_KINDS.has(notification.kind)) {
|
|
1312
|
+
if (!await this.#isAuthorizedAuthor(input.subscription.owner, input.subscription.repo, input.snapshot.latestCommentAuthor, {
|
|
1313
|
+
authorType: input.snapshot.latestCommentAuthorType,
|
|
1314
|
+
isBot: input.snapshot.latestCommentIsBot
|
|
1315
|
+
})) continue;
|
|
1316
|
+
}
|
|
1317
|
+
notificationInputs.push(this.#createGithubNotificationInput({
|
|
1318
|
+
subscription: input.subscription,
|
|
1319
|
+
snapshot: input.snapshot,
|
|
1320
|
+
notification,
|
|
1321
|
+
dedupeSuffix: input.snapshot.contentHash ?? input.snapshot.githubUpdatedAt ?? String(Date.now()),
|
|
1322
|
+
previousGithubUpdatedAt: input.previousGithubUpdatedAt,
|
|
1323
|
+
previousContentHash: input.previousContentHash
|
|
1324
|
+
}));
|
|
1325
|
+
sent.push(notification);
|
|
1326
|
+
}
|
|
1327
|
+
if (notificationInputs.length > 0) {
|
|
1328
|
+
const target = {
|
|
1329
|
+
resourceId: input.polling.resourceId,
|
|
1330
|
+
threadId: input.polling.threadId
|
|
1331
|
+
};
|
|
1332
|
+
const streamOptions = await this.#agentOptions.getNotificationStreamOptions?.(target);
|
|
1333
|
+
await agent.sendNotificationSignal(notificationInputs, streamOptions ? {
|
|
1334
|
+
...target,
|
|
1335
|
+
ifIdle: { streamOptions }
|
|
1336
|
+
} : target);
|
|
1337
|
+
}
|
|
1338
|
+
return sent;
|
|
1339
|
+
}
|
|
1340
|
+
async #subscribe(input) {
|
|
1341
|
+
const { owner, repo } = await this.#resolveRepository(input);
|
|
1342
|
+
const { threadStore, loadedThread } = await this.#loadThread(input);
|
|
1343
|
+
const githubMetadata = getGithubMetadata(loadedThread.metadata);
|
|
1344
|
+
const existingIndex = githubMetadata.subscriptions.findIndex((subscription) => subscription.owner === owner && subscription.repo === repo && subscription.number === input.number);
|
|
1345
|
+
const existing = existingIndex >= 0 ? githubMetadata.subscriptions[existingIndex] : void 0;
|
|
1346
|
+
if (existing?.lastSubscribeSignalId === input.id) return {
|
|
1347
|
+
owner,
|
|
1348
|
+
repo,
|
|
1349
|
+
number: input.number,
|
|
1350
|
+
subscription: existing,
|
|
1351
|
+
alreadyProcessed: true
|
|
1352
|
+
};
|
|
1353
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1354
|
+
const subscription = {
|
|
1355
|
+
owner,
|
|
1356
|
+
repo,
|
|
1357
|
+
number: input.number,
|
|
1358
|
+
subscribedAt: existing?.subscribedAt ?? now,
|
|
1359
|
+
updatedAt: now,
|
|
1360
|
+
lastSubscribeSignalId: input.id,
|
|
1361
|
+
...existing?.lastSyncAt ? { lastSyncAt: existing.lastSyncAt } : {},
|
|
1362
|
+
...existing?.lastSyncStatus ? { lastSyncStatus: existing.lastSyncStatus } : {},
|
|
1363
|
+
...existing?.lastSyncError ? { lastSyncError: existing.lastSyncError } : {},
|
|
1364
|
+
...existing?.lastObservedGithubUpdatedAt ? { lastObservedGithubUpdatedAt: existing.lastObservedGithubUpdatedAt } : {},
|
|
1365
|
+
...existing?.lastObservedContentHash ? { lastObservedContentHash: existing.lastObservedContentHash } : {},
|
|
1366
|
+
...existing?.lastObservedThreadContentHash ? { lastObservedThreadContentHash: existing.lastObservedThreadContentHash } : {},
|
|
1367
|
+
...existing?.lastObservedHeadSha ? { lastObservedHeadSha: existing.lastObservedHeadSha } : {},
|
|
1368
|
+
...existing?.lastObservedState ? { lastObservedState: existing.lastObservedState } : {},
|
|
1369
|
+
...existing?.lastObservedMergeableState ? { lastObservedMergeableState: existing.lastObservedMergeableState } : {},
|
|
1370
|
+
...existing?.lastObservedCiState ? { lastObservedCiState: existing.lastObservedCiState } : {},
|
|
1371
|
+
...existing?.lastObservedReviewStateHash ? { lastObservedReviewStateHash: existing.lastObservedReviewStateHash } : {}
|
|
1372
|
+
};
|
|
1373
|
+
let syncResult;
|
|
1374
|
+
let baselineSnapshot;
|
|
1375
|
+
if (this.#options.syncOnSubscribe !== false) {
|
|
1376
|
+
const syncInput = {
|
|
1377
|
+
owner,
|
|
1378
|
+
repo,
|
|
1379
|
+
number: input.number,
|
|
1380
|
+
cwd: this.#options.cwd,
|
|
1381
|
+
abortSignal: input.abortSignal
|
|
1382
|
+
};
|
|
1383
|
+
syncResult = await this.#syncClient.syncPullRequest(syncInput);
|
|
1384
|
+
subscription.lastSyncAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1385
|
+
subscription.lastSyncStatus = syncResult.ok ? "success" : "error";
|
|
1386
|
+
if (syncResult.error) subscription.lastSyncError = syncResult.error;
|
|
1387
|
+
else delete subscription.lastSyncError;
|
|
1388
|
+
let snapshot;
|
|
1389
|
+
if (syncResult.ok) try {
|
|
1390
|
+
snapshot = await this.#syncClient.getPullRequestSnapshot?.(syncInput);
|
|
1391
|
+
} catch (error) {
|
|
1392
|
+
subscription.lastSnapshotError = error instanceof Error ? error.message : String(error);
|
|
1393
|
+
}
|
|
1394
|
+
if (snapshot) delete subscription.lastSnapshotError;
|
|
1395
|
+
baselineSnapshot = snapshot;
|
|
1396
|
+
if (snapshot) applySnapshotCursor(subscription, snapshot);
|
|
1397
|
+
} else subscription.lastSyncStatus = "skipped";
|
|
1398
|
+
const subscriptions = [subscription];
|
|
1399
|
+
await threadStore.saveThread({ thread: {
|
|
1400
|
+
...loadedThread,
|
|
1401
|
+
id: input.threadId,
|
|
1402
|
+
resourceId: input.resourceId,
|
|
1403
|
+
createdAt: loadedThread.createdAt ?? /* @__PURE__ */ new Date(),
|
|
1404
|
+
updatedAt: /* @__PURE__ */ new Date(),
|
|
1405
|
+
metadata: setGithubMetadata(loadedThread.metadata, { subscriptions })
|
|
1406
|
+
} });
|
|
1407
|
+
this.#notifySubscriptionsChanged({
|
|
1408
|
+
threadId: input.threadId,
|
|
1409
|
+
resourceId: input.resourceId,
|
|
1410
|
+
subscriptions
|
|
1411
|
+
});
|
|
1412
|
+
if (baselineSnapshot) await this.#sendBaselineNotification({
|
|
1413
|
+
threadId: input.threadId,
|
|
1414
|
+
resourceId: input.resourceId,
|
|
1415
|
+
subscription,
|
|
1416
|
+
snapshot: baselineSnapshot
|
|
1417
|
+
});
|
|
1418
|
+
await this.startPollingForThread({
|
|
1419
|
+
threadId: input.threadId,
|
|
1420
|
+
resourceId: input.resourceId
|
|
1421
|
+
});
|
|
1422
|
+
return {
|
|
1423
|
+
owner,
|
|
1424
|
+
repo,
|
|
1425
|
+
number: input.number,
|
|
1426
|
+
subscription,
|
|
1427
|
+
syncResult
|
|
1428
|
+
};
|
|
1429
|
+
}
|
|
1430
|
+
async #unsubscribe(input) {
|
|
1431
|
+
const { owner, repo } = await this.#resolveRepository(input);
|
|
1432
|
+
const { threadStore, loadedThread } = await this.#loadThread(input);
|
|
1433
|
+
const githubMetadata = getGithubMetadata(loadedThread.metadata);
|
|
1434
|
+
const subscriptions = githubMetadata.subscriptions.filter((subscription) => !(subscription.owner === owner && subscription.repo === repo && subscription.number === input.number));
|
|
1435
|
+
const removed = subscriptions.length !== githubMetadata.subscriptions.length;
|
|
1436
|
+
if (removed) {
|
|
1437
|
+
await threadStore.saveThread({ thread: {
|
|
1438
|
+
...loadedThread,
|
|
1439
|
+
id: input.threadId,
|
|
1440
|
+
resourceId: input.resourceId,
|
|
1441
|
+
createdAt: loadedThread.createdAt ?? /* @__PURE__ */ new Date(),
|
|
1442
|
+
updatedAt: /* @__PURE__ */ new Date(),
|
|
1443
|
+
metadata: setGithubMetadata(loadedThread.metadata, { subscriptions })
|
|
1444
|
+
} });
|
|
1445
|
+
this.#notifySubscriptionsChanged({
|
|
1446
|
+
threadId: input.threadId,
|
|
1447
|
+
resourceId: input.resourceId,
|
|
1448
|
+
subscriptions
|
|
1449
|
+
});
|
|
1450
|
+
if (subscriptions.length === 0) this.stopPollingForThread({
|
|
1451
|
+
threadId: input.threadId,
|
|
1452
|
+
resourceId: input.resourceId
|
|
1453
|
+
});
|
|
1454
|
+
}
|
|
1455
|
+
return {
|
|
1456
|
+
owner,
|
|
1457
|
+
repo,
|
|
1458
|
+
number: input.number,
|
|
1459
|
+
removed,
|
|
1460
|
+
remainingSubscriptions: subscriptions.length
|
|
1461
|
+
};
|
|
1462
|
+
}
|
|
1463
|
+
#findLatestGithubSignal(messages) {
|
|
1464
|
+
const message = messages.at(-1);
|
|
1465
|
+
if (!message) return void 0;
|
|
1466
|
+
const signal = getSignalMetadata(message);
|
|
1467
|
+
if (!signal || signal.tagName !== "github-subscribe-pr" && signal.tagName !== "github-unsubscribe-pr") return;
|
|
1468
|
+
const attributes = isPlainObject(signal.attributes) ? signal.attributes : {};
|
|
1469
|
+
const metadata = isPlainObject(signal.metadata) ? signal.metadata : {};
|
|
1470
|
+
const github = isPlainObject(metadata.github) ? metadata.github : {};
|
|
1471
|
+
const number = readNumber(attributes.number) ?? readNumber(github.number);
|
|
1472
|
+
if (!number) return void 0;
|
|
1473
|
+
return {
|
|
1474
|
+
tagName: String(signal.tagName),
|
|
1475
|
+
id: readString(signal.id) ?? message.id,
|
|
1476
|
+
owner: readString(attributes.owner) ?? readString(github.owner),
|
|
1477
|
+
repo: readString(attributes.repo) ?? readString(github.repo),
|
|
1478
|
+
number
|
|
1479
|
+
};
|
|
1480
|
+
}
|
|
1481
|
+
async #sendStatus(args, signal, status) {
|
|
1482
|
+
await args.sendSignal?.({
|
|
1483
|
+
type: "reactive",
|
|
1484
|
+
tagName: GITHUB_SYNC_STATUS_TAG,
|
|
1485
|
+
contents: status.message,
|
|
1486
|
+
attributes: {
|
|
1487
|
+
status: status.status,
|
|
1488
|
+
owner: signal.owner,
|
|
1489
|
+
repo: signal.repo,
|
|
1490
|
+
number: signal.number
|
|
1491
|
+
},
|
|
1492
|
+
metadata: { github: {
|
|
1493
|
+
action: status.action,
|
|
1494
|
+
status: status.status,
|
|
1495
|
+
owner: signal.owner,
|
|
1496
|
+
repo: signal.repo,
|
|
1497
|
+
number: signal.number
|
|
1498
|
+
} }
|
|
1499
|
+
});
|
|
1500
|
+
}
|
|
1367
1501
|
};
|
|
1368
|
-
|
|
1502
|
+
//#endregion
|
|
1369
1503
|
export { GITHUB_SIGNALS_METADATA_KEY, GITHUB_SUBSCRIBE_PR_TAG, GITHUB_SYNC_STATUS_TAG, GITHUB_UNSUBSCRIBE_PR_TAG, GitRemoteRepositoryResolver, GitcrawlSyncClient, GithubSignals, normalizeGithubChecksForSnapshot, sanitizeCommentText };
|
|
1370
|
-
|
|
1504
|
+
|
|
1371
1505
|
//# sourceMappingURL=index.js.map
|