@brain-toolkit/issue-forge 0.1.0 → 0.1.1
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 +29 -0
- package/LICENSE +15 -0
- package/README.md +1 -2
- package/dist/chunk-MQ7IDBIZ.js +4 -0
- package/dist/{chunk-RQKDVDLH.js.map → chunk-MQ7IDBIZ.js.map} +1 -1
- package/dist/feedback/index.cjs +4 -871
- package/dist/feedback/index.cjs.map +1 -1
- package/dist/feedback/index.js +4 -527
- package/dist/feedback/index.js.map +1 -1
- package/dist/index.cjs +3 -514
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -22
- package/package.json +19 -33
- package/dist/chunk-RQKDVDLH.js +0 -471
package/dist/feedback/index.js
CHANGED
|
@@ -1,41 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createGithubAppIssueForge,
|
|
3
|
-
createGithubAppRepoContent
|
|
4
|
-
} from "../chunk-RQKDVDLH.js";
|
|
5
|
-
|
|
6
|
-
// src/feedback/types.ts
|
|
7
|
-
var FeedbackIssueSyncStatus = {
|
|
8
|
-
pending: "pending",
|
|
9
|
-
synced: "synced",
|
|
10
|
-
failed: "failed",
|
|
11
|
-
skipped: "skipped"
|
|
12
|
-
};
|
|
13
|
-
var FeedbackIssueStatus = {
|
|
14
|
-
open: "open",
|
|
15
|
-
reviewing: "reviewing",
|
|
16
|
-
resolved: "resolved",
|
|
17
|
-
closed: "closed"
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
// src/feedback/FeedbackIssueTemplateConfig.ts
|
|
21
|
-
var FeedbackIssueTemplateSource = Object.freeze({
|
|
22
|
-
repo: "repo",
|
|
23
|
-
builtin: "builtin"
|
|
24
|
-
});
|
|
25
|
-
var FEEDBACK_ISSUE_TEMPLATE_PATH = ".github/ISSUE_TEMPLATE/feedback.md";
|
|
26
|
-
var FEEDBACK_ISSUE_TEMPLATE_CACHE_TTL_MS = 10 * 60 * 1e3;
|
|
27
|
-
var FEEDBACK_ISSUE_TEMPLATE_FAILURE_TTL_MS = 60 * 1e3;
|
|
28
|
-
var FEEDBACK_ISSUE_DEFAULT_TITLE = "[Feedback][{{category}}] {{summary}}";
|
|
29
|
-
var FEEDBACK_ISSUE_ASSETS_BRANCH = "feedback-assets";
|
|
30
|
-
var FEEDBACK_ISSUE_ASSETS_DIR = "feedback";
|
|
31
|
-
var FEEDBACK_ISSUE_ASSETS_README = `# feedback-assets
|
|
1
|
+
import{g as h,h as E}from"../chunk-MQ7IDBIZ.js";var i={pending:"pending",synced:"synced",failed:"failed",skipped:"skipped"},m={open:"open",reviewing:"reviewing",resolved:"resolved",closed:"closed"};var k=Object.freeze({repo:"repo",builtin:"builtin"}),_=".github/ISSUE_TEMPLATE/feedback.md",x=600*1e3,v=60*1e3,C="[Feedback][{{category}}] {{summary}}",y="feedback-assets",R="feedback",w=`# feedback-assets
|
|
32
2
|
|
|
33
3
|
Feedback screenshots uploaded by the app backend for Issue references.
|
|
34
4
|
|
|
35
5
|
- Do not merge into main
|
|
36
6
|
- May contain user screenshots; keep the branch private if needed
|
|
37
|
-
|
|
38
|
-
var FEEDBACK_ISSUE_DEFAULT_TEMPLATE = `---
|
|
7
|
+
`,U=`---
|
|
39
8
|
name: User feedback
|
|
40
9
|
about: App user feedback (server also uses this template; placeholders replaced server-side)
|
|
41
10
|
title: "[Feedback][{{category}}] {{summary}}"
|
|
@@ -70,498 +39,6 @@ labels: feedback
|
|
|
70
39
|
{{screenshots}}
|
|
71
40
|
|
|
72
41
|
{{user_agent}}
|
|
73
|
-
`;
|
|
74
|
-
|
|
75
|
-
// src/feedback/FeedbackIssueTemplateRenderer.ts
|
|
76
|
-
var TITLE_MAX = 256;
|
|
77
|
-
var PLACEHOLDER_PATTERN = /\{\{\s*([a-z_]+)\s*\}\}/g;
|
|
78
|
-
var FeedbackIssueTemplateRenderer = class {
|
|
79
|
-
render(template, variables) {
|
|
80
|
-
return template.replace(PLACEHOLDER_PATTERN, (_m, key) => {
|
|
81
|
-
return Object.prototype.hasOwnProperty.call(variables, key) ? variables[key] ?? "" : `{{${key}}}`;
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
buildIssue(template, variables, extraLabels = []) {
|
|
85
|
-
const titleTemplate = template.title.trim() || FEEDBACK_ISSUE_DEFAULT_TITLE;
|
|
86
|
-
const title = Array.from(
|
|
87
|
-
this.render(titleTemplate, variables).replace(/\s+/g, " ").trim()
|
|
88
|
-
).slice(0, TITLE_MAX).join("");
|
|
89
|
-
return {
|
|
90
|
-
title,
|
|
91
|
-
body: this.render(template.body, variables).trim(),
|
|
92
|
-
labels: [.../* @__PURE__ */ new Set([...template.labels, ...extraLabels])]
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
// src/feedback/FeedbackIssueTemplateResolver.ts
|
|
98
|
-
var FeedbackIssueTemplateResolver = class {
|
|
99
|
-
client;
|
|
100
|
-
cache;
|
|
101
|
-
logger;
|
|
102
|
-
defaultTemplate;
|
|
103
|
-
constructor(options) {
|
|
104
|
-
this.client = options.client;
|
|
105
|
-
this.cache = options.cache;
|
|
106
|
-
this.logger = options.logger;
|
|
107
|
-
this.defaultTemplate = options.defaultTemplate ?? FEEDBACK_ISSUE_DEFAULT_TEMPLATE;
|
|
108
|
-
}
|
|
109
|
-
async resolve(config) {
|
|
110
|
-
const raw = await this.loadRaw(config);
|
|
111
|
-
if (raw) {
|
|
112
|
-
const template = this.parse(raw, FeedbackIssueTemplateSource.repo);
|
|
113
|
-
if (template.body) {
|
|
114
|
-
return template;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return this.parse(
|
|
118
|
-
this.defaultTemplate,
|
|
119
|
-
FeedbackIssueTemplateSource.builtin
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
async loadRaw(config) {
|
|
123
|
-
const cacheKey = `issue-forge:feedback:issue-template:${config.owner}/${config.repo}`;
|
|
124
|
-
const cached = await this.cache.getItem(cacheKey);
|
|
125
|
-
if (cached) {
|
|
126
|
-
return cached.raw;
|
|
127
|
-
}
|
|
128
|
-
try {
|
|
129
|
-
const content = createGithubAppRepoContent(this.client, config);
|
|
130
|
-
const raw = await content.readTextFile(FEEDBACK_ISSUE_TEMPLATE_PATH);
|
|
131
|
-
await this.cache.setItem(
|
|
132
|
-
cacheKey,
|
|
133
|
-
{ raw },
|
|
134
|
-
{ ttlMs: FEEDBACK_ISSUE_TEMPLATE_CACHE_TTL_MS }
|
|
135
|
-
);
|
|
136
|
-
return raw;
|
|
137
|
-
} catch (error) {
|
|
138
|
-
this.logger?.warn("feedback issue template load failed", {
|
|
139
|
-
message: error instanceof Error ? error.message : String(error)
|
|
140
|
-
});
|
|
141
|
-
await this.cache.setItem(
|
|
142
|
-
cacheKey,
|
|
143
|
-
{ raw: null },
|
|
144
|
-
{ ttlMs: FEEDBACK_ISSUE_TEMPLATE_FAILURE_TTL_MS }
|
|
145
|
-
);
|
|
146
|
-
return null;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
parse(raw, source) {
|
|
150
|
-
const trimmed = raw.replace(/^\uFEFF/, "").trim();
|
|
151
|
-
if (!trimmed.startsWith("---")) {
|
|
152
|
-
return { title: "", labels: ["feedback"], body: trimmed, source };
|
|
153
|
-
}
|
|
154
|
-
const end = trimmed.indexOf("\n---", 3);
|
|
155
|
-
if (end < 0) {
|
|
156
|
-
return { title: "", labels: ["feedback"], body: trimmed, source };
|
|
157
|
-
}
|
|
158
|
-
const front = trimmed.slice(3, end).trim();
|
|
159
|
-
const body = trimmed.slice(end + 4).replace(/^\r?\n/, "");
|
|
160
|
-
const meta = this.parseFrontMatter(front);
|
|
161
|
-
const title = typeof meta.title === "string" ? meta.title : Array.isArray(meta.title) ? meta.title[0] ?? "" : "";
|
|
162
|
-
const labelsRaw = meta.labels;
|
|
163
|
-
const labels = Array.isArray(labelsRaw) ? labelsRaw.map(String) : typeof labelsRaw === "string" ? labelsRaw.split(",").map((s) => s.trim()).filter(Boolean) : ["feedback"];
|
|
164
|
-
return { title, labels, body, source };
|
|
165
|
-
}
|
|
166
|
-
parseFrontMatter(front) {
|
|
167
|
-
const out = {};
|
|
168
|
-
for (const line of front.split(/\r?\n/)) {
|
|
169
|
-
const idx = line.indexOf(":");
|
|
170
|
-
if (idx < 0) continue;
|
|
171
|
-
const key = line.slice(0, idx).trim();
|
|
172
|
-
let value = line.slice(idx + 1).trim();
|
|
173
|
-
if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
|
|
174
|
-
value = value.slice(1, -1);
|
|
175
|
-
}
|
|
176
|
-
if (key === "labels" && value.includes(",")) {
|
|
177
|
-
out[key] = value.split(",").map((s) => s.trim()).filter(Boolean);
|
|
178
|
-
} else {
|
|
179
|
-
out[key] = value;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
return out;
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
// src/feedback/FeedbackIssueStatusResolver.ts
|
|
187
|
-
var ISSUE_ACTION_CLOSED = "closed";
|
|
188
|
-
var ISSUE_ACTION_REOPENED = "reopened";
|
|
189
|
-
var REASON_NOT_PLANNED = "not_planned";
|
|
190
|
-
var REASON_DUPLICATE = "duplicate";
|
|
191
|
-
var FeedbackIssueStatusResolver = class {
|
|
192
|
-
resolve(event) {
|
|
193
|
-
if (event.action === ISSUE_ACTION_REOPENED) {
|
|
194
|
-
return FeedbackIssueStatus.open;
|
|
195
|
-
}
|
|
196
|
-
if (event.action !== ISSUE_ACTION_CLOSED) {
|
|
197
|
-
return null;
|
|
198
|
-
}
|
|
199
|
-
const reason = event.issue.state_reason ?? null;
|
|
200
|
-
if (reason === REASON_NOT_PLANNED || reason === REASON_DUPLICATE) {
|
|
201
|
-
return FeedbackIssueStatus.closed;
|
|
202
|
-
}
|
|
203
|
-
return FeedbackIssueStatus.resolved;
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
|
|
207
|
-
// src/feedback/FeedbackIssueSyncEngine.ts
|
|
208
|
-
var FeedbackIssueSyncEngine = class {
|
|
209
|
-
host;
|
|
210
|
-
client;
|
|
211
|
-
templateResolver;
|
|
212
|
-
renderer;
|
|
213
|
-
screenshotSync;
|
|
214
|
-
logger;
|
|
215
|
-
constructor(options) {
|
|
216
|
-
this.host = options.host;
|
|
217
|
-
this.client = options.client;
|
|
218
|
-
this.templateResolver = options.templateResolver;
|
|
219
|
-
this.renderer = options.renderer;
|
|
220
|
-
this.screenshotSync = options.screenshotSync;
|
|
221
|
-
this.logger = options.logger;
|
|
222
|
-
}
|
|
223
|
-
async syncFeedback(feedbackId, options = {}) {
|
|
224
|
-
const content = await this.host.resolveIssueContent(feedbackId);
|
|
225
|
-
if (!content) {
|
|
226
|
-
return FeedbackIssueSyncStatus.skipped;
|
|
227
|
-
}
|
|
228
|
-
if (content.issueNumber !== null) {
|
|
229
|
-
await this.screenshotSync.syncScreenshots(feedbackId, options);
|
|
230
|
-
return FeedbackIssueSyncStatus.synced;
|
|
231
|
-
}
|
|
232
|
-
const auth = await this.host.resolveForgeAuth(options);
|
|
233
|
-
if (!auth) {
|
|
234
|
-
await this.host.updateSync(feedbackId, {
|
|
235
|
-
status: FeedbackIssueSyncStatus.skipped,
|
|
236
|
-
issueNumber: null,
|
|
237
|
-
issueUrl: null,
|
|
238
|
-
error: null
|
|
239
|
-
});
|
|
240
|
-
return FeedbackIssueSyncStatus.skipped;
|
|
241
|
-
}
|
|
242
|
-
try {
|
|
243
|
-
const template = await this.templateResolver.resolve(auth);
|
|
244
|
-
const forge = createGithubAppIssueForge(this.client, auth);
|
|
245
|
-
const created = await forge.createIssue(
|
|
246
|
-
this.renderer.buildIssue(
|
|
247
|
-
template,
|
|
248
|
-
content.variables,
|
|
249
|
-
content.labels ?? []
|
|
250
|
-
)
|
|
251
|
-
);
|
|
252
|
-
await this.host.updateSync(feedbackId, {
|
|
253
|
-
status: FeedbackIssueSyncStatus.synced,
|
|
254
|
-
issueNumber: created.issueNumber,
|
|
255
|
-
issueUrl: created.issueUrl,
|
|
256
|
-
error: null
|
|
257
|
-
});
|
|
258
|
-
} catch (error) {
|
|
259
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
260
|
-
this.logger?.error("feedback github sync failed", {
|
|
261
|
-
feedbackId,
|
|
262
|
-
message
|
|
263
|
-
});
|
|
264
|
-
await this.host.updateSync(feedbackId, {
|
|
265
|
-
status: FeedbackIssueSyncStatus.failed,
|
|
266
|
-
issueNumber: null,
|
|
267
|
-
issueUrl: null,
|
|
268
|
-
error: message
|
|
269
|
-
});
|
|
270
|
-
return FeedbackIssueSyncStatus.failed;
|
|
271
|
-
}
|
|
272
|
-
await this.screenshotSync.syncScreenshots(feedbackId, options);
|
|
273
|
-
return FeedbackIssueSyncStatus.synced;
|
|
274
|
-
}
|
|
275
|
-
};
|
|
276
|
-
|
|
277
|
-
// src/feedback/FeedbackIssueScreenshotSyncEngine.ts
|
|
278
|
-
var ISSUE_NUMBER_RETRY_DELAYS_MS = [
|
|
279
|
-
200,
|
|
280
|
-
400,
|
|
281
|
-
800,
|
|
282
|
-
800,
|
|
283
|
-
800
|
|
284
|
-
];
|
|
285
|
-
var SCREENSHOT_ERROR_PREFIX = "screenshot: ";
|
|
286
|
-
var FeedbackIssueScreenshotSyncEngine = class _FeedbackIssueScreenshotSyncEngine {
|
|
287
|
-
host;
|
|
288
|
-
client;
|
|
289
|
-
logger;
|
|
290
|
-
sleep;
|
|
291
|
-
constructor(options) {
|
|
292
|
-
this.host = options.host;
|
|
293
|
-
this.client = options.client;
|
|
294
|
-
this.logger = options.logger;
|
|
295
|
-
this.sleep = options.sleep ?? ((ms) => new Promise((resolve) => {
|
|
296
|
-
setTimeout(resolve, ms);
|
|
297
|
-
}));
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* 本地已清理后:用落库的 GitHub raw URL 检查 Issue 评论,缺失则补发(不碰 Storage)。
|
|
301
|
-
*/
|
|
302
|
-
async ensureGithubScreenshotComments(feedbackId, options = {}) {
|
|
303
|
-
const state = await this.host.findScreenshotState(feedbackId);
|
|
304
|
-
if (!state || state.issueNumber === null || state.githubUrls.length === 0) {
|
|
305
|
-
return 0;
|
|
306
|
-
}
|
|
307
|
-
const auth = await this.host.resolveForgeAuth(options);
|
|
308
|
-
if (!auth) {
|
|
309
|
-
return 0;
|
|
310
|
-
}
|
|
311
|
-
const issueNumber = state.issueNumber;
|
|
312
|
-
const urls = state.githubUrls;
|
|
313
|
-
try {
|
|
314
|
-
const forge = createGithubAppIssueForge(this.client, auth);
|
|
315
|
-
const comments = await forge.listComments({ issueNumber });
|
|
316
|
-
const alreadyPosted = urls.every(
|
|
317
|
-
(url) => comments.some((comment) => comment.body.includes(url))
|
|
318
|
-
);
|
|
319
|
-
if (alreadyPosted) {
|
|
320
|
-
return 0;
|
|
321
|
-
}
|
|
322
|
-
await forge.createComment(
|
|
323
|
-
{ issueNumber },
|
|
324
|
-
this.host.formatScreenshotComment(urls, 0)
|
|
325
|
-
);
|
|
326
|
-
await this.host.updateSync(feedbackId, {
|
|
327
|
-
status: FeedbackIssueSyncStatus.synced,
|
|
328
|
-
issueNumber,
|
|
329
|
-
issueUrl: state.issueUrl,
|
|
330
|
-
error: null
|
|
331
|
-
});
|
|
332
|
-
return urls.length;
|
|
333
|
-
} catch (error) {
|
|
334
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
335
|
-
this.logger?.error("feedback screenshot comment ensure failed", {
|
|
336
|
-
feedbackId,
|
|
337
|
-
message
|
|
338
|
-
});
|
|
339
|
-
await this.host.updateSync(feedbackId, {
|
|
340
|
-
status: FeedbackIssueSyncStatus.synced,
|
|
341
|
-
issueNumber,
|
|
342
|
-
issueUrl: state.issueUrl,
|
|
343
|
-
error: `${SCREENSHOT_ERROR_PREFIX}${message}`
|
|
344
|
-
}).catch(() => void 0);
|
|
345
|
-
return 0;
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
async syncScreenshots(feedbackId, options = {}) {
|
|
349
|
-
let state = await this.host.findScreenshotState(feedbackId);
|
|
350
|
-
if (!state || state.paths.length <= state.postedCount) {
|
|
351
|
-
return 0;
|
|
352
|
-
}
|
|
353
|
-
if (state.issueNumber === null) {
|
|
354
|
-
state = await this.waitForIssueNumber(feedbackId, state);
|
|
355
|
-
if (!state || state.issueNumber === null || state.paths.length <= state.postedCount) {
|
|
356
|
-
this.logger?.warn("feedback screenshot sync skipped: issue not ready", {
|
|
357
|
-
feedbackId
|
|
358
|
-
});
|
|
359
|
-
return 0;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
const auth = await this.host.resolveForgeAuth(options);
|
|
363
|
-
if (!auth) {
|
|
364
|
-
return 0;
|
|
365
|
-
}
|
|
366
|
-
const issueNumber = state.issueNumber;
|
|
367
|
-
const from = state.postedCount;
|
|
368
|
-
const to = state.paths.length;
|
|
369
|
-
const claimed = await this.host.compareAndSetScreenshotCount(
|
|
370
|
-
feedbackId,
|
|
371
|
-
from,
|
|
372
|
-
to
|
|
373
|
-
);
|
|
374
|
-
if (!claimed) {
|
|
375
|
-
return 0;
|
|
376
|
-
}
|
|
377
|
-
const localPaths = state.paths.slice(from);
|
|
378
|
-
try {
|
|
379
|
-
const urls = await this.uploadScreenshots(auth, feedbackId, localPaths);
|
|
380
|
-
const forge = createGithubAppIssueForge(this.client, auth);
|
|
381
|
-
const comments = await forge.listComments({ issueNumber });
|
|
382
|
-
const alreadyPosted = urls.every(
|
|
383
|
-
(url) => comments.some((comment) => comment.body.includes(url))
|
|
384
|
-
);
|
|
385
|
-
if (!alreadyPosted) {
|
|
386
|
-
await forge.createComment(
|
|
387
|
-
{ issueNumber },
|
|
388
|
-
this.host.formatScreenshotComment(urls, from)
|
|
389
|
-
);
|
|
390
|
-
}
|
|
391
|
-
await this.host.updateSync(feedbackId, {
|
|
392
|
-
status: FeedbackIssueSyncStatus.synced,
|
|
393
|
-
issueNumber,
|
|
394
|
-
issueUrl: state.issueUrl,
|
|
395
|
-
error: null
|
|
396
|
-
});
|
|
397
|
-
await this.host.releaseLocalScreenshotsAfterGithub(feedbackId, urls, localPaths).catch((cleanupError) => {
|
|
398
|
-
const message = cleanupError instanceof Error ? cleanupError.message : String(cleanupError);
|
|
399
|
-
this.logger?.error("feedback screenshot local cleanup failed", {
|
|
400
|
-
feedbackId,
|
|
401
|
-
message
|
|
402
|
-
});
|
|
403
|
-
});
|
|
404
|
-
return urls.length;
|
|
405
|
-
} catch (error) {
|
|
406
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
407
|
-
this.logger?.error("feedback screenshot sync failed", {
|
|
408
|
-
feedbackId,
|
|
409
|
-
message
|
|
410
|
-
});
|
|
411
|
-
await this.host.compareAndSetScreenshotCount(feedbackId, to, from).catch(() => false);
|
|
412
|
-
await this.host.updateSync(feedbackId, {
|
|
413
|
-
status: FeedbackIssueSyncStatus.synced,
|
|
414
|
-
issueNumber,
|
|
415
|
-
issueUrl: state.issueUrl,
|
|
416
|
-
error: `${SCREENSHOT_ERROR_PREFIX}${message}`
|
|
417
|
-
}).catch(() => void 0);
|
|
418
|
-
return 0;
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
/**
|
|
422
|
-
* 私有仓 Issue Markdown 可渲染的 raw URL(同源 cookie,避免 raw.githubusercontent.com)。
|
|
423
|
-
*/
|
|
424
|
-
static buildRawAssetUrl(auth, branch, path) {
|
|
425
|
-
const encodedPath = path.split("/").filter((segment) => segment.length > 0).map((segment) => encodeURIComponent(segment)).join("/");
|
|
426
|
-
return `https://github.com/${encodeURIComponent(auth.owner)}/${encodeURIComponent(auth.repo)}/raw/${encodeURIComponent(branch)}/${encodedPath}`;
|
|
427
|
-
}
|
|
428
|
-
async waitForIssueNumber(feedbackId, initial) {
|
|
429
|
-
let latest = initial;
|
|
430
|
-
for (const delayMs of ISSUE_NUMBER_RETRY_DELAYS_MS) {
|
|
431
|
-
await this.sleep(delayMs);
|
|
432
|
-
latest = await this.host.findScreenshotState(feedbackId);
|
|
433
|
-
if (latest?.issueNumber !== null && latest?.issueNumber !== void 0) {
|
|
434
|
-
return latest;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
return latest;
|
|
438
|
-
}
|
|
439
|
-
async uploadScreenshots(auth, feedbackId, objectPaths) {
|
|
440
|
-
const content = createGithubAppRepoContent(this.client, auth);
|
|
441
|
-
await content.ensureBranch(FEEDBACK_ISSUE_ASSETS_BRANCH, {
|
|
442
|
-
path: "README.md",
|
|
443
|
-
content: FEEDBACK_ISSUE_ASSETS_README
|
|
444
|
-
});
|
|
445
|
-
const urls = [];
|
|
446
|
-
for (const [index, objectPath] of objectPaths.entries()) {
|
|
447
|
-
const fileName = objectPath.split("/").pop() || `${String(index)}.jpg`;
|
|
448
|
-
const repoPath = `${FEEDBACK_ISSUE_ASSETS_DIR}/${feedbackId}/${fileName}`;
|
|
449
|
-
const alreadyOnBranch = await content.hasFile(
|
|
450
|
-
repoPath,
|
|
451
|
-
FEEDBACK_ISSUE_ASSETS_BRANCH
|
|
452
|
-
);
|
|
453
|
-
if (!alreadyOnBranch) {
|
|
454
|
-
const bytes = await this.host.downloadScreenshot(objectPath);
|
|
455
|
-
await content.uploadFile({
|
|
456
|
-
branch: FEEDBACK_ISSUE_ASSETS_BRANCH,
|
|
457
|
-
path: repoPath,
|
|
458
|
-
contentBase64: bytes.toString("base64"),
|
|
459
|
-
message: `chore(feedback): feedback ${feedbackId} screenshot`
|
|
460
|
-
});
|
|
461
|
-
}
|
|
462
|
-
urls.push(
|
|
463
|
-
_FeedbackIssueScreenshotSyncEngine.buildRawAssetUrl(
|
|
464
|
-
auth,
|
|
465
|
-
FEEDBACK_ISSUE_ASSETS_BRANCH,
|
|
466
|
-
repoPath
|
|
467
|
-
)
|
|
468
|
-
);
|
|
469
|
-
}
|
|
470
|
-
return urls;
|
|
471
|
-
}
|
|
472
|
-
};
|
|
473
|
-
|
|
474
|
-
// src/feedback/FeedbackIssueStatusSyncEngine.ts
|
|
475
|
-
var FeedbackIssueStatusSyncEngine = class {
|
|
476
|
-
host;
|
|
477
|
-
statusResolver;
|
|
478
|
-
logger;
|
|
479
|
-
constructor(options) {
|
|
480
|
-
this.host = options.host;
|
|
481
|
-
this.statusResolver = options.statusResolver ?? new FeedbackIssueStatusResolver();
|
|
482
|
-
this.logger = options.logger;
|
|
483
|
-
}
|
|
484
|
-
async applyIssueEvent(event) {
|
|
485
|
-
const status = this.statusResolver.resolve(event);
|
|
486
|
-
if (status === null) {
|
|
487
|
-
return 0;
|
|
488
|
-
}
|
|
489
|
-
if (!await this.isFeedbackRepo(event.repository.full_name)) {
|
|
490
|
-
this.logger?.warn("feedback status sync skipped: repo mismatch", {
|
|
491
|
-
repo: event.repository.full_name
|
|
492
|
-
});
|
|
493
|
-
return 0;
|
|
494
|
-
}
|
|
495
|
-
const updated = await this.host.updateStatusByIssueNumber(
|
|
496
|
-
event.issue.number,
|
|
497
|
-
status
|
|
498
|
-
);
|
|
499
|
-
if (updated > 0) {
|
|
500
|
-
this.logger?.info("feedback status synced from github issue", {
|
|
501
|
-
issueNumber: event.issue.number,
|
|
502
|
-
action: event.action,
|
|
503
|
-
status,
|
|
504
|
-
updated
|
|
505
|
-
});
|
|
506
|
-
}
|
|
507
|
-
return updated;
|
|
508
|
-
}
|
|
509
|
-
async isFeedbackRepo(fullName) {
|
|
510
|
-
const repo = await this.host.getFeedbackRepoFullName();
|
|
511
|
-
return repo.toLowerCase() === fullName.trim().toLowerCase();
|
|
512
|
-
}
|
|
513
|
-
};
|
|
514
|
-
|
|
515
|
-
// src/feedback/createGithubFeedbackIssueBundle.ts
|
|
516
|
-
function createGithubFeedbackIssueBundle(options) {
|
|
517
|
-
const renderer = new FeedbackIssueTemplateRenderer();
|
|
518
|
-
const templateResolver = new FeedbackIssueTemplateResolver({
|
|
519
|
-
client: options.client,
|
|
520
|
-
cache: options.cache,
|
|
521
|
-
logger: options.logger,
|
|
522
|
-
defaultTemplate: options.defaultTemplate
|
|
523
|
-
});
|
|
524
|
-
const screenshots = new FeedbackIssueScreenshotSyncEngine({
|
|
525
|
-
host: options.host,
|
|
526
|
-
client: options.client,
|
|
527
|
-
logger: options.logger
|
|
528
|
-
});
|
|
529
|
-
const sync = new FeedbackIssueSyncEngine({
|
|
530
|
-
host: options.host,
|
|
531
|
-
client: options.client,
|
|
532
|
-
templateResolver,
|
|
533
|
-
renderer,
|
|
534
|
-
screenshotSync: screenshots,
|
|
535
|
-
logger: options.logger
|
|
536
|
-
});
|
|
537
|
-
const status = new FeedbackIssueStatusSyncEngine({
|
|
538
|
-
host: options.host,
|
|
539
|
-
logger: options.logger
|
|
540
|
-
});
|
|
541
|
-
return {
|
|
542
|
-
sync,
|
|
543
|
-
screenshots,
|
|
544
|
-
status,
|
|
545
|
-
renderer,
|
|
546
|
-
templateResolver
|
|
547
|
-
};
|
|
548
|
-
}
|
|
549
|
-
export {
|
|
550
|
-
FEEDBACK_ISSUE_ASSETS_BRANCH,
|
|
551
|
-
FEEDBACK_ISSUE_ASSETS_DIR,
|
|
552
|
-
FEEDBACK_ISSUE_ASSETS_README,
|
|
553
|
-
FEEDBACK_ISSUE_DEFAULT_TEMPLATE,
|
|
554
|
-
FEEDBACK_ISSUE_DEFAULT_TITLE,
|
|
555
|
-
FEEDBACK_ISSUE_TEMPLATE_PATH,
|
|
556
|
-
FeedbackIssueScreenshotSyncEngine,
|
|
557
|
-
FeedbackIssueStatus,
|
|
558
|
-
FeedbackIssueStatusResolver,
|
|
559
|
-
FeedbackIssueStatusSyncEngine,
|
|
560
|
-
FeedbackIssueSyncEngine,
|
|
561
|
-
FeedbackIssueSyncStatus,
|
|
562
|
-
FeedbackIssueTemplateRenderer,
|
|
563
|
-
FeedbackIssueTemplateResolver,
|
|
564
|
-
FeedbackIssueTemplateSource,
|
|
565
|
-
createGithubFeedbackIssueBundle
|
|
566
|
-
};
|
|
42
|
+
`;var D=256,B=/\{\{\s*([a-z_]+)\s*\}\}/g,g=class{render(e,s){return e.replace(B,(t,r)=>Object.prototype.hasOwnProperty.call(s,r)?s[r]??"":`{{${r}}}`)}buildIssue(e,s,t=[]){let r=e.title.trim()||C;return{title:Array.from(this.render(r,s).replace(/\s+/g," ").trim()).slice(0,D).join(""),body:this.render(e.body,s).trim(),labels:[...new Set([...e.labels,...t])]}}};var S=class{client;cache;logger;defaultTemplate;constructor(e){this.client=e.client,this.cache=e.cache,this.logger=e.logger,this.defaultTemplate=e.defaultTemplate??U}async resolve(e){let s=await this.loadRaw(e);if(s){let t=this.parse(s,k.repo);if(t.body)return t}return this.parse(this.defaultTemplate,k.builtin)}async loadRaw(e){let s=`issue-forge:feedback:issue-template:${e.owner}/${e.repo}`,t=await this.cache.getItem(s);if(t)return t.raw;try{let n=await E(this.client,e).readTextFile(_);return await this.cache.setItem(s,{raw:n},{ttlMs:x}),n}catch(r){return this.logger?.warn("feedback issue template load failed",{message:r instanceof Error?r.message:String(r)}),await this.cache.setItem(s,{raw:null},{ttlMs:v}),null}}parse(e,s){let t=e.replace(/^\uFEFF/,"").trim();if(!t.startsWith("---"))return{title:"",labels:["feedback"],body:t,source:s};let r=t.indexOf(`
|
|
43
|
+
---`,3);if(r<0)return{title:"",labels:["feedback"],body:t,source:s};let n=t.slice(3,r).trim(),o=t.slice(r+4).replace(/^\r?\n/,""),a=this.parseFrontMatter(n),d=typeof a.title=="string"?a.title:Array.isArray(a.title)?a.title[0]??"":"",l=a.labels,u=Array.isArray(l)?l.map(String):typeof l=="string"?l.split(",").map(p=>p.trim()).filter(Boolean):["feedback"];return{title:d,labels:u,body:o,source:s}}parseFrontMatter(e){let s={};for(let t of e.split(/\r?\n/)){let r=t.indexOf(":");if(r<0)continue;let n=t.slice(0,r).trim(),o=t.slice(r+1).trim();(o.startsWith('"')&&o.endsWith('"')||o.startsWith("'")&&o.endsWith("'"))&&(o=o.slice(1,-1)),n==="labels"&&o.includes(",")?s[n]=o.split(",").map(a=>a.trim()).filter(Boolean):s[n]=o}return s}};var G="closed",O="reopened",N="not_planned",M="duplicate",I=class{resolve(e){if(e.action===O)return m.open;if(e.action!==G)return null;let s=e.issue.state_reason??null;return s===N||s===M?m.closed:m.resolved}};var F=class{host;client;templateResolver;renderer;screenshotSync;logger;constructor(e){this.host=e.host,this.client=e.client,this.templateResolver=e.templateResolver,this.renderer=e.renderer,this.screenshotSync=e.screenshotSync,this.logger=e.logger}async syncFeedback(e,s={}){let t=await this.host.resolveIssueContent(e);if(!t)return i.skipped;if(t.issueNumber!==null)return await this.screenshotSync.syncScreenshots(e,s),i.synced;let r=await this.host.resolveForgeAuth(s);if(!r)return await this.host.updateSync(e,{status:i.skipped,issueNumber:null,issueUrl:null,error:null}),i.skipped;try{let n=await this.templateResolver.resolve(r),a=await h(this.client,r).createIssue(this.renderer.buildIssue(n,t.variables,t.labels??[]));await this.host.updateSync(e,{status:i.synced,issueNumber:a.issueNumber,issueUrl:a.issueUrl,error:null})}catch(n){let o=n instanceof Error?n.message:String(n);return this.logger?.error("feedback github sync failed",{feedbackId:e,message:o}),await this.host.updateSync(e,{status:i.failed,issueNumber:null,issueUrl:null,error:o}),i.failed}return await this.screenshotSync.syncScreenshots(e,s),i.synced}};var H=[200,400,800,800,800],L="screenshot: ",f=class c{host;client;logger;sleep;constructor(e){this.host=e.host,this.client=e.client,this.logger=e.logger,this.sleep=e.sleep??(s=>new Promise(t=>{setTimeout(t,s)}))}async ensureGithubScreenshotComments(e,s={}){let t=await this.host.findScreenshotState(e);if(!t||t.issueNumber===null||t.githubUrls.length===0)return 0;let r=await this.host.resolveForgeAuth(s);if(!r)return 0;let n=t.issueNumber,o=t.githubUrls;try{let a=h(this.client,r),d=await a.listComments({issueNumber:n});return o.every(u=>d.some(p=>p.body.includes(u)))?0:(await a.createComment({issueNumber:n},this.host.formatScreenshotComment(o,0)),await this.host.updateSync(e,{status:i.synced,issueNumber:n,issueUrl:t.issueUrl,error:null}),o.length)}catch(a){let d=a instanceof Error?a.message:String(a);return this.logger?.error("feedback screenshot comment ensure failed",{feedbackId:e,message:d}),await this.host.updateSync(e,{status:i.synced,issueNumber:n,issueUrl:t.issueUrl,error:`${L}${d}`}).catch(()=>{}),0}}async syncScreenshots(e,s={}){let t=await this.host.findScreenshotState(e);if(!t||t.paths.length<=t.postedCount)return 0;if(t.issueNumber===null&&(t=await this.waitForIssueNumber(e,t),!t||t.issueNumber===null||t.paths.length<=t.postedCount))return this.logger?.warn("feedback screenshot sync skipped: issue not ready",{feedbackId:e}),0;let r=await this.host.resolveForgeAuth(s);if(!r)return 0;let n=t.issueNumber,o=t.postedCount,a=t.paths.length;if(!await this.host.compareAndSetScreenshotCount(e,o,a))return 0;let l=t.paths.slice(o);try{let u=await this.uploadScreenshots(r,e,l),p=h(this.client,r),P=await p.listComments({issueNumber:n});return u.every(b=>P.some(A=>A.body.includes(b)))||await p.createComment({issueNumber:n},this.host.formatScreenshotComment(u,o)),await this.host.updateSync(e,{status:i.synced,issueNumber:n,issueUrl:t.issueUrl,error:null}),await this.host.releaseLocalScreenshotsAfterGithub(e,u,l).catch(b=>{let A=b instanceof Error?b.message:String(b);this.logger?.error("feedback screenshot local cleanup failed",{feedbackId:e,message:A})}),u.length}catch(u){let p=u instanceof Error?u.message:String(u);return this.logger?.error("feedback screenshot sync failed",{feedbackId:e,message:p}),await this.host.compareAndSetScreenshotCount(e,a,o).catch(()=>!1),await this.host.updateSync(e,{status:i.synced,issueNumber:n,issueUrl:t.issueUrl,error:`${L}${p}`}).catch(()=>{}),0}}static buildRawAssetUrl(e,s,t){let r=t.split("/").filter(n=>n.length>0).map(n=>encodeURIComponent(n)).join("/");return`https://github.com/${encodeURIComponent(e.owner)}/${encodeURIComponent(e.repo)}/raw/${encodeURIComponent(s)}/${r}`}async waitForIssueNumber(e,s){let t=s;for(let r of H)if(await this.sleep(r),t=await this.host.findScreenshotState(e),t?.issueNumber!==null&&t?.issueNumber!==void 0)return t;return t}async uploadScreenshots(e,s,t){let r=E(this.client,e);await r.ensureBranch(y,{path:"README.md",content:w});let n=[];for(let[o,a]of t.entries()){let d=a.split("/").pop()||`${String(o)}.jpg`,l=`${R}/${s}/${d}`;if(!await r.hasFile(l,y)){let p=await this.host.downloadScreenshot(a);await r.uploadFile({branch:y,path:l,contentBase64:p.toString("base64"),message:`chore(feedback): feedback ${s} screenshot`})}n.push(c.buildRawAssetUrl(e,y,l))}return n}};var T=class{host;statusResolver;logger;constructor(e){this.host=e.host,this.statusResolver=e.statusResolver??new I,this.logger=e.logger}async applyIssueEvent(e){let s=this.statusResolver.resolve(e);if(s===null)return 0;if(!await this.isFeedbackRepo(e.repository.full_name))return this.logger?.warn("feedback status sync skipped: repo mismatch",{repo:e.repository.full_name}),0;let t=await this.host.updateStatusByIssueNumber(e.issue.number,s);return t>0&&this.logger?.info("feedback status synced from github issue",{issueNumber:e.issue.number,action:e.action,status:s,updated:t}),t}async isFeedbackRepo(e){return(await this.host.getFeedbackRepoFullName()).toLowerCase()===e.trim().toLowerCase()}};function K(c){let e=new g,s=new S({client:c.client,cache:c.cache,logger:c.logger,defaultTemplate:c.defaultTemplate}),t=new f({host:c.host,client:c.client,logger:c.logger}),r=new F({host:c.host,client:c.client,templateResolver:s,renderer:e,screenshotSync:t,logger:c.logger}),n=new T({host:c.host,logger:c.logger});return{sync:r,screenshots:t,status:n,renderer:e,templateResolver:s}}export{y as FEEDBACK_ISSUE_ASSETS_BRANCH,R as FEEDBACK_ISSUE_ASSETS_DIR,w as FEEDBACK_ISSUE_ASSETS_README,U as FEEDBACK_ISSUE_DEFAULT_TEMPLATE,C as FEEDBACK_ISSUE_DEFAULT_TITLE,_ as FEEDBACK_ISSUE_TEMPLATE_PATH,f as FeedbackIssueScreenshotSyncEngine,m as FeedbackIssueStatus,I as FeedbackIssueStatusResolver,T as FeedbackIssueStatusSyncEngine,F as FeedbackIssueSyncEngine,i as FeedbackIssueSyncStatus,g as FeedbackIssueTemplateRenderer,S as FeedbackIssueTemplateResolver,k as FeedbackIssueTemplateSource,K as createGithubFeedbackIssueBundle};
|
|
567
44
|
//# sourceMappingURL=index.js.map
|