yolont 0.2.0 → 0.3.0

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/dist/cli.js CHANGED
@@ -467,12 +467,16 @@ function listFindingDiscussionsForMr(db, mergeRequestId) {
467
467
  return db.prepare(`SELECT * FROM finding_discussions WHERE merge_request_id = ? ORDER BY created_at ASC`).all(mergeRequestId);
468
468
  }
469
469
  function insertFindingResponse(db, r) {
470
- const result = db.prepare(
471
- `INSERT INTO finding_responses (finding_discussion_id, note_id, author_username, body, created_at)
472
- VALUES (?, ?, ?, ?, ?)
473
- ON CONFLICT(note_id) DO NOTHING`
470
+ const prev = db.prepare(`SELECT body FROM finding_responses WHERE note_id = ?`).get(r.noteId);
471
+ if (prev) {
472
+ if (prev.body === r.body) return null;
473
+ db.prepare(`UPDATE finding_responses SET body = ? WHERE note_id = ?`).run(r.body, r.noteId);
474
+ return "updated";
475
+ }
476
+ db.prepare(
477
+ `INSERT INTO finding_responses (finding_discussion_id, note_id, author_username, body, created_at) VALUES (?, ?, ?, ?, ?)`
474
478
  ).run(r.findingDiscussionId, r.noteId, r.authorUsername, r.body, r.createdAt);
475
- return Number(result.changes) > 0;
479
+ return "new";
476
480
  }
477
481
  function listAuthorFindingResponses(db, author, limit = 20, projectId) {
478
482
  return db.prepare(
@@ -981,6 +985,27 @@ function resolveDiffPosition(diffText, filePath, newLine) {
981
985
  }
982
986
  return null;
983
987
  }
988
+ function numberDiffLines(diffText) {
989
+ let newLineNum = 0;
990
+ let inHunk = false;
991
+ const blank = " ".repeat(6);
992
+ return diffText.split("\n").map((line) => {
993
+ if (line.startsWith("diff --git ")) {
994
+ inHunk = false;
995
+ return line;
996
+ }
997
+ const hunkHeader = line.match(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
998
+ if (hunkHeader) {
999
+ newLineNum = Number(hunkHeader[1]);
1000
+ inHunk = true;
1001
+ return line;
1002
+ }
1003
+ if (!inHunk) return line;
1004
+ if (line.startsWith("+") || line.startsWith(" ")) return `${String(newLineNum++).padStart(5)} ${line}`;
1005
+ if (line.startsWith("-") || line.startsWith("\\")) return `${blank}${line}`;
1006
+ return line;
1007
+ }).join("\n");
1008
+ }
984
1009
  function truncateDiff(text3, maxBytes) {
985
1010
  const { text: filtered, omittedFiles } = omitGeneratedFiles(text3);
986
1011
  const stats = computeDiffStats(filtered);
@@ -1200,7 +1225,7 @@ function buildReviewJsonSchema(categoryNames, zoneIds = [], withFocus = false) {
1200
1225
  properties: {
1201
1226
  severity: { type: "string", enum: ["minor", "major", "critical"] },
1202
1227
  file: { type: "string" },
1203
- line: { type: "number" },
1228
+ line: { type: "number", description: "The number printed at the start of the diff line the issue is on (new-file numbering)." },
1204
1229
  issue: { type: "string" },
1205
1230
  suggestion: { type: "string" }
1206
1231
  },
@@ -1231,8 +1256,9 @@ ${input.authorHistorySummary}
1231
1256
 
1232
1257
  ${input.zones?.length ? `${buildZonesPromptSection(input.zones)}
1233
1258
  ` : ""}${input.truncated ? "NOTE: this diff was truncated to fit size limits; review only what is shown.\n" : ""}
1234
- Diff:
1235
- ${input.diff}
1259
+ Diff (each added "+" or unchanged " " line starts with its line number in the new file; removed "-" lines have none).
1260
+ For a finding's "line", copy that number exactly from the line the issue is on, preferably a "+" line. Never count lines yourself.
1261
+ ${numberDiffLines(input.diff)}
1236
1262
  `;
1237
1263
  }
1238
1264
  function buildFocusPromptSection(focus) {
@@ -4496,48 +4522,38 @@ function cancelled(db) {
4496
4522
  }
4497
4523
 
4498
4524
  // local/review/syncReplies.ts
4525
+ async function syncRepliesForMr(db, forge, forgeProject, mrIid, mergeRequestId) {
4526
+ const tracked = listFindingDiscussionsForMr(db, mergeRequestId);
4527
+ const result = { tracked: tracked.length, newReplies: 0, updatedReplies: 0, threads: 0 };
4528
+ if (tracked.length === 0) return result;
4529
+ const me = await forge.getCurrentUser();
4530
+ const threadsById = new Map((await forge.listMrDiscussionThreads(forgeProject, mrIid)).map((t) => [t.discussionId, t.notes]));
4531
+ for (const t of tracked) {
4532
+ const replies = (threadsById.get(t.discussion_id) ?? []).filter((n) => n.id !== t.posted_note_id && n.authorUsername !== me.username);
4533
+ let changed = false;
4534
+ for (const reply of replies) {
4535
+ const outcome = insertFindingResponse(db, { findingDiscussionId: t.id, noteId: reply.id, authorUsername: reply.authorUsername, body: reply.body, createdAt: reply.createdAt });
4536
+ if (outcome === "new") result.newReplies++;
4537
+ if (outcome === "updated") result.updatedReplies++;
4538
+ if (outcome) changed = true;
4539
+ }
4540
+ if (changed) result.threads++;
4541
+ }
4542
+ return result;
4543
+ }
4544
+ var describeSyncReplies = (r) => r.tracked === 0 ? "This review has no posted finding threads, so there are no replies to get." : `${r.newReplies} new repl${r.newReplies === 1 ? "y" : "ies"}${r.updatedReplies ? `, ${r.updatedReplies} edited` : ""} across ${r.threads} of ${r.tracked} finding thread(s).`;
4499
4545
  async function runSyncReplies(opts) {
4500
4546
  const db = openDb();
4501
- const client = opts.forge;
4502
- const forgeId = typeof opts.project === "number" || /^\d+$/.test(opts.project) ? opts.project : (await client.getProject(opts.project)).id;
4503
- const dbProjectId = findProjectId(db, client.host, forgeId);
4504
- const mrRow = dbProjectId === void 0 ? void 0 : getMergeRequestByProjectAndIid(db, dbProjectId, opts.mrIid);
4505
- if (!mrRow) {
4506
- throw new Error(`No tracked findings for ${client.host} project ${opts.project} ${changeLabel(client.type, opts.mrIid)} \u2014 run 'review' and post it first.`);
4507
- }
4508
- const trackedDiscussions = listFindingDiscussionsForMr(db, mrRow.id);
4509
- if (trackedDiscussions.length === 0) {
4510
- console.log(`[sync-replies] no finding threads tracked for this MR yet \u2014 nothing to sync.`);
4547
+ try {
4548
+ const client = opts.forge;
4549
+ const forgeId = typeof opts.project === "number" || /^\d+$/.test(opts.project) ? opts.project : (await client.getProject(opts.project)).id;
4550
+ const dbProjectId = findProjectId(db, client.host, forgeId);
4551
+ const mrRow = dbProjectId === void 0 ? void 0 : getMergeRequestByProjectAndIid(db, dbProjectId, opts.mrIid);
4552
+ if (!mrRow) throw new Error(`No tracked findings for ${client.host} project ${opts.project} ${changeLabel(client.type, opts.mrIid)} \u2014 run 'review' and post it first.`);
4553
+ console.log(`[sync-replies] ${describeSyncReplies(await syncRepliesForMr(db, client, forgeId, opts.mrIid, mrRow.id))}`);
4554
+ } finally {
4511
4555
  db.close();
4512
- return;
4513
- }
4514
- const me = await client.getCurrentUser();
4515
- const threads = await client.listMrDiscussionThreads(forgeId, opts.mrIid);
4516
- const threadsById = new Map(threads.map((t) => [t.discussionId, t.notes]));
4517
- let newReplies = 0;
4518
- let threadsWithReplies = 0;
4519
- for (const tracked of trackedDiscussions) {
4520
- const notes = threadsById.get(tracked.discussion_id) ?? [];
4521
- const replies = notes.filter((n) => n.id !== tracked.posted_note_id && n.authorUsername !== me.username);
4522
- if (replies.length === 0) continue;
4523
- let threadHasNewReply = false;
4524
- for (const reply of replies) {
4525
- const inserted = insertFindingResponse(db, {
4526
- findingDiscussionId: tracked.id,
4527
- noteId: reply.id,
4528
- authorUsername: reply.authorUsername,
4529
- body: reply.body,
4530
- createdAt: reply.createdAt
4531
- });
4532
- if (inserted) {
4533
- newReplies++;
4534
- threadHasNewReply = true;
4535
- }
4536
- }
4537
- if (threadHasNewReply) threadsWithReplies++;
4538
4556
  }
4539
- console.log(`[sync-replies] ${newReplies} repl${newReplies === 1 ? "y" : "ies"} across ${threadsWithReplies} finding thread(s).`);
4540
- db.close();
4541
4557
  }
4542
4558
 
4543
4559
  // local/backoffice/server.ts
@@ -5464,6 +5480,20 @@ async function buildPanel(opts) {
5464
5480
  zoneHits: listZoneHitsForMr(db, mergeRequestId)
5465
5481
  };
5466
5482
  });
5483
+ app.post("/api/reviews/:id/sync-replies", async (req, reply) => {
5484
+ const mr = getMergeRequestById(db, Number(req.params.id));
5485
+ if (!mr) return reply.code(404).send({ error: "not found" });
5486
+ const project2 = db.prepare(`SELECT forge_host, forge_project_id FROM projects WHERE id = ?`).get(mr.project_id);
5487
+ if (!project2?.forge_host || !project2.forge_project_id) return reply.code(400).send({ error: "This review's git host isn't known, so replies can't be fetched." });
5488
+ if (!opts.forgeFor) return reply.code(400).send({ error: "No git host configured" });
5489
+ try {
5490
+ const ref = /^\d+$/.test(project2.forge_project_id) ? Number(project2.forge_project_id) : project2.forge_project_id;
5491
+ const result = await syncRepliesForMr(db, opts.forgeFor(project2.forge_host), ref, mr.mr_iid, mr.id);
5492
+ return { ...result, summary: describeSyncReplies(result) };
5493
+ } catch (err) {
5494
+ return reply.code(502).send({ error: `Couldn't get replies from ${project2.forge_host}: ${err.message}` });
5495
+ }
5496
+ });
5467
5497
  app.get("/api/zones", async (req, reply) => {
5468
5498
  const projectId = Number(req.query.project);
5469
5499
  if (!projectId) return reply.code(400).send({ error: "project is required" });