import { z } from "zod";
import { repoDb } from "../scoped-db";
import { randomId } from "../crypto";
import { conflict, HttpError, notFoundError } from "../errors";
import { commitFileChange } from "../git/web-commit";
import { assertBelowCap, CAPS, limitWrite } from "../limits";
import { toProtection } from "../protection";
import type { BranchProtection, BranchProtectionRow } from "../protection";
import { loadSecuritySettings, saveSecuritySettings } from "../security/settings";
import {
noContent,
readJson,
requireScopedAdminWrite,
requireScopedRepo,
requireTokenWrite,
unprocessable,
} from "./http";
import {
deleteProtectionRule,
listProtectionRules,
loadProtectionRule,
saveProtectionRule,
setCodeOwnerRequirement,
} from "../protection-write";
import type { ProtectionInput } from "../protection-write";
import type { ApiContext } from "./http";
const TAG_PREFIX = "refs/tags/";
const HTTP_CONFLICT = 409;
const MAX_CONTENT_BYTES = 5 * 1024 * 1024;
function ruleJson(rule: BranchProtection) {
const kind = rule.pattern.startsWith(TAG_PREFIX) ? "tag" : "branch";
return {
id: rule.id,
pattern: kind === "tag" ? rule.pattern.slice(TAG_PREFIX.length) : rule.pattern,
kind,
allow_force_push: rule.allowForcePushes,
allow_deletions: rule.allowDeletions,
require_pull_request: rule.requirePullRequest,