111 lines · 3.9 KB
import { repoDb } from "../scoped-db";
import { locate } from "../scoped-db/locators";
import { DEFAULT_THRESHOLDS } from "./decide";
import type { Thresholds } from "./decide";
import { notifyMergeFixResult } from "../notify/mergefix";
import type { MergeFixMode } from "./proposal";

export type RepoMergeFixSettings = {
  queueEnabled: boolean;
  allowPrivate: boolean;
  autoApply: boolean;
  thresholds: Thresholds;
};

type SettingsRow = {
  queue_enabled: number;
  allow_private: number;
  auto_apply: number;
  min_confidence: number;
  min_margin: number;
};

export async function loadRepoSettings(repoId: string): Promise<RepoMergeFixSettings> {
  const row = await repoDb(repoId).first<SettingsRow>("SELECT * FROM repo_mergefix_settings WHERE repo_id = ?", repoId);
  return {
    queueEnabled: row?.queue_enabled === 1,
    allowPrivate: row?.allow_private === 1,
    autoApply: row?.auto_apply === 1,
    thresholds: row ? { confidence: row.min_confidence, margin: row.min_margin } : DEFAULT_THRESHOLDS,
  };
}

export async function saveRepoSettings(repoId: string, settings: RepoMergeFixSettings): Promise<void> {
  await repoDb(repoId).run(
    `INSERT INTO repo_mergefix_settings (repo_id, queue_enabled, allow_private, auto_apply, min_confidence, min_margin, updated_at)
     VALUES (?, ?, ?, ?, ?, ?, ?)
     ON CONFLICT (repo_id) DO UPDATE SET queue_enabled = excluded.queue_enabled, allow_private = excluded.allow_private,
       auto_apply = excluded.auto_apply, min_confidence = excluded.min_confidence, min_margin = excluded.min_margin, updated_at = excluded.updated_at`,
    repoId,
    settings.queueEnabled ? 1 : 0,