158 lines · 6.0 KB
import { placeholders } from "./db";
import { repoDb, stmt } from "./scoped-db";
import { withStore } from "./git/store";
import { commitChanges, PushRejectedError } from "./git/writer";
import { sanitizeIdent, signatureNow } from "./git/objects";
import { splitLines } from "./git/merge3";
import { triggerPush } from "./events/push-trigger";
import { accountsById } from "./people";
import { MergeError } from "./pulls";
import type { PullRow } from "./pulls";
import type { RepoRow, Viewer } from "~/lib/types";

export const MAX_BATCHED_SUGGESTIONS = 20;
const MAX_SUGGESTED_FILE_BYTES = 2 * 1024 * 1024;
const SUGGESTION_BLOCK = /```suggestion[^\n]*\n([\s\S]*?)^```/m;
const decoder = new TextDecoder("utf-8", { fatal: true });
const encoder = new TextEncoder();

/** Extracts the replacement text of the first ```suggestion block, or null when there is none. */
export function parseSuggestion(body: string): string | null {
  const match = SUGGESTION_BLOCK.exec(body.replace(/\r\n/g, "\n"));
  return match ? match[1] : null;
}

type SuggestionRow = {
  id: string;
  author_id: string;
  path: string;
  line: number;
  start_line: number | null;
  start_side: "LEFT" | "RIGHT" | null;
  side: "LEFT" | "RIGHT";
  in_reply_to: string | null;
  commit_sha: string;
  body: string;
  suggestion_applied_sha: string | null;
};

type Edit = { start: number; end: number; replacement: string };