CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | /**
* Block J6 — Repository rulesets.
*
* A ruleset groups N rules under a named policy at enforcement level active /
* evaluate / disabled. The evaluator is pure — callers (push hook, PR
* merger, web editor) pass a PushContext describing what they're about to
* do, and get back either an allow or the list of violations.
*
* Supported rule types in V1:
* - commit_message_pattern : { pattern: string, flags?: "i", require?: bool }
* - branch_name_pattern : { pattern: string, require?: bool }
* - tag_name_pattern : { pattern: string, require?: bool }
* - blocked_file_paths : { paths: string[] } (glob-lite: *, /)
* - max_file_size : { bytes: number }
* - forbid_force_push : {}
*/
import { and, eq } from "drizzle-orm";
import { db } from "../db";
import {
repoRulesets,
rulesetRules,
type RepoRuleset,
type RulesetRule,
} from "../db/schema";
export type RuleType =
| "commit_message_pattern"
| "branch_name_pattern"
| "tag_name_pattern"
| "blocked_file_paths"
| "max_file_size"
| "forbid_force_push";
export const RULE_TYPES: RuleType[] = [
"commit_message_pattern",
"branch_name_pattern",
"tag_name_pattern",
"blocked_file_paths",
"max_file_size",
"forbid_force_push",
];
export interface CommitLike {
sha?: string;
message: string;
changedPaths?: string[];
maxBlobSize?: number;
}
export interface PushContext {
kind: "push";
refType: "branch" | "tag";
refName: string;
commits: CommitLike[];
forcePush?: boolean;
}
export interface Violation {
rulesetId: string;
rulesetName: string;
enforcement: "active" | "evaluate" | "disabled";
ruleType: RuleType;
message: string;
}
export interface EvalResult {
allowed: boolean;
violations: Violation[];
}
// ----------------------------------------------------------------------------
// Pure rule helpers
// ----------------------------------------------------------------------------
/** glob-lite → RegExp. Supports `*` (non-slash) and `**` (anything). */
export function globToRegex(glob: string): RegExp {
const parts: string[] = [];
let i = 0;
while (i < glob.length) {
const ch = glob[i];
if (ch === "*") {
if (glob[i + 1] === "*") {
parts.push(".*");
i += 2;
} else {
parts.push("[^/]*");
i += 1;
}
} else if (/[.+?^${}()|[\]\\]/.test(ch)) {
parts.push("\\" + ch);
i += 1;
} else {
parts.push(ch);
i += 1;
}
}
return new RegExp("^" + parts.join("") + "$");
}
export function parseParams(raw: string): Record<string, unknown> {
try {
return JSON.parse(raw || "{}");
} catch {
return {};
}
}
function evalRule(
rule: RulesetRule,
ctx: PushContext
): string[] {
const params = parseParams(rule.params);
const out: string[] = [];
switch (rule.ruleType as RuleType) {
case "commit_message_pattern": {
const pattern = String(params.pattern || "");
const flags = String(params.flags || "") || undefined;
const require = params.require !== false;
if (!pattern) return out;
let re: RegExp;
try {
re = new RegExp(pattern, flags);
} catch {
return out;
}
for (const c of ctx.commits) {
const matches = re.test(c.message);
if (require && !matches) {
out.push(
`commit ${c.sha?.slice(0, 7) || "?"} message does not match /${pattern}/`
);
} else if (!require && matches) {
out.push(
`commit ${c.sha?.slice(0, 7) || "?"} message matches forbidden /${pattern}/`
);
}
}
return out;
}
case "branch_name_pattern": {
if (ctx.refType !== "branch") return out;
const pattern = String(params.pattern || "");
const require = params.require !== false;
if (!pattern) return out;
let re: RegExp;
try {
re = new RegExp(pattern);
} catch {
return out;
}
const ok = re.test(ctx.refName);
if (require && !ok) {
out.push(`branch "${ctx.refName}" does not match /${pattern}/`);
} else if (!require && ok) {
out.push(`branch "${ctx.refName}" matches forbidden /${pattern}/`);
}
return out;
}
case "tag_name_pattern": {
if (ctx.refType !== "tag") return out;
const pattern = String(params.pattern || "");
const require = params.require !== false;
if (!pattern) return out;
let re: RegExp;
try {
re = new RegExp(pattern);
} catch {
return out;
}
const ok = re.test(ctx.refName);
if (require && !ok) {
out.push(`tag "${ctx.refName}" does not match /${pattern}/`);
} else if (!require && ok) {
out.push(`tag "${ctx.refName}" matches forbidden /${pattern}/`);
}
return out;
}
case "blocked_file_paths": {
const globs = Array.isArray(params.paths)
? (params.paths as string[])
: [];
if (!globs.length) return out;
const res = globs.map((g) => globToRegex(g));
for (const c of ctx.commits) {
for (const p of c.changedPaths || []) {
for (let i = 0; i < res.length; i++) {
if (res[i].test(p)) {
out.push(
`commit ${c.sha?.slice(0, 7) || "?"} modifies blocked path "${p}" (${globs[i]})`
);
}
}
}
}
return out;
}
case "max_file_size": {
const bytes = Number(params.bytes || 0);
if (!bytes) return out;
for (const c of ctx.commits) {
if (typeof c.maxBlobSize === "number" && c.maxBlobSize > bytes) {
out.push(
`commit ${c.sha?.slice(0, 7) || "?"} has a blob ${c.maxBlobSize}B > ${bytes}B`
);
}
}
return out;
}
case "forbid_force_push": {
if (ctx.forcePush) {
out.push("force push is forbidden by ruleset");
}
return out;
}
default:
return out;
}
}
/** Pure evaluator — takes rulesets + rules + context, returns verdict. */
export function evaluatePush(
rulesets: Array<RepoRuleset & { rules: RulesetRule[] }>,
ctx: PushContext
): EvalResult {
const violations: Violation[] = [];
let blocked = false;
for (const rs of rulesets) {
if (rs.enforcement === "disabled") continue;
for (const r of rs.rules) {
const msgs = evalRule(r, ctx);
for (const m of msgs) {
violations.push({
rulesetId: rs.id,
rulesetName: rs.name,
enforcement: rs.enforcement as "active" | "evaluate",
ruleType: r.ruleType as RuleType,
message: m,
});
if (rs.enforcement === "active") blocked = true;
}
}
}
return { allowed: !blocked, violations };
}
// ----------------------------------------------------------------------------
// DB access
// ----------------------------------------------------------------------------
export async function listRulesetsForRepo(
repositoryId: string
): Promise<Array<RepoRuleset & { rules: RulesetRule[] }>> {
const sets = await db
.select()
.from(repoRulesets)
.where(eq(repoRulesets.repositoryId, repositoryId));
if (sets.length === 0) return [];
const allRules = await db
.select()
.from(rulesetRules);
const byId = new Map<string, RulesetRule[]>();
for (const r of allRules) {
if (!byId.has(r.rulesetId)) byId.set(r.rulesetId, []);
byId.get(r.rulesetId)!.push(r);
}
return sets.map((s) => ({ ...s, rules: byId.get(s.id) || [] }));
}
export async function getRuleset(
rulesetId: string,
repositoryId: string
): Promise<(RepoRuleset & { rules: RulesetRule[] }) | null> {
const [row] = await db
.select()
.from(repoRulesets)
.where(
and(
eq(repoRulesets.id, rulesetId),
eq(repoRulesets.repositoryId, repositoryId)
)
)
.limit(1);
if (!row) return null;
const rules = await db
.select()
.from(rulesetRules)
.where(eq(rulesetRules.rulesetId, rulesetId));
return { ...row, rules };
}
export async function createRuleset(params: {
repositoryId: string;
name: string;
enforcement: "active" | "evaluate" | "disabled";
createdBy: string;
}): Promise<{ ok: true; id: string } | { ok: false; error: string }> {
const name = params.name.trim();
if (!name) return { ok: false, error: "Name is required" };
if (!["active", "evaluate", "disabled"].includes(params.enforcement)) {
return { ok: false, error: "Invalid enforcement" };
}
try {
const [row] = await db
.insert(repoRulesets)
.values({
repositoryId: params.repositoryId,
name,
enforcement: params.enforcement,
createdBy: params.createdBy,
})
.returning();
return { ok: true, id: row.id };
} catch (err: any) {
const msg = String(err?.message || "");
if (msg.includes("duplicate") || msg.includes("unique")) {
return { ok: false, error: "A ruleset with that name already exists" };
}
return { ok: false, error: "Could not save ruleset" };
}
}
export async function updateRulesetEnforcement(
rulesetId: string,
repositoryId: string,
enforcement: "active" | "evaluate" | "disabled"
): Promise<boolean> {
if (!["active", "evaluate", "disabled"].includes(enforcement)) return false;
const rows = await db
.update(repoRulesets)
.set({ enforcement, updatedAt: new Date() })
.where(
and(
eq(repoRulesets.id, rulesetId),
eq(repoRulesets.repositoryId, repositoryId)
)
)
.returning();
return rows.length > 0;
}
export async function deleteRuleset(
rulesetId: string,
repositoryId: string
): Promise<boolean> {
const rows = await db
.delete(repoRulesets)
.where(
and(
eq(repoRulesets.id, rulesetId),
eq(repoRulesets.repositoryId, repositoryId)
)
)
.returning();
return rows.length > 0;
}
export async function addRule(params: {
rulesetId: string;
repositoryId: string;
ruleType: RuleType;
params: Record<string, unknown>;
}): Promise<{ ok: true; id: string } | { ok: false; error: string }> {
if (!RULE_TYPES.includes(params.ruleType)) {
return { ok: false, error: "Unknown rule type" };
}
// Ensure the ruleset belongs to this repo.
const parent = await getRuleset(params.rulesetId, params.repositoryId);
if (!parent) return { ok: false, error: "Ruleset not found" };
try {
const [row] = await db
.insert(rulesetRules)
.values({
rulesetId: params.rulesetId,
ruleType: params.ruleType,
params: JSON.stringify(params.params || {}),
})
.returning();
return { ok: true, id: row.id };
} catch {
return { ok: false, error: "Could not save rule" };
}
}
export async function deleteRule(
ruleId: string,
rulesetId: string,
repositoryId: string
): Promise<boolean> {
const parent = await getRuleset(rulesetId, repositoryId);
if (!parent) return false;
const rows = await db
.delete(rulesetRules)
.where(
and(eq(rulesetRules.id, ruleId), eq(rulesetRules.rulesetId, rulesetId))
)
.returning();
return rows.length > 0;
}
// Test-only surface.
export const __internal = { evalRule, globToRegex, parseParams };
|