DevelopForge
@holtkeeper/mr-policies
Merge request policies: rules that run on merge request events before (or instead of) analysis. A policy says when it applies (a condition over the event, the merge request, its diff and its pipeline) and what happens (approve, merge, label, comment, close, toggle draft, assign reviewers, enqueue analyzers, call a webhook), and may tell the caller to skip analyzers for that event.
The engine only talks to GitForge. It never touches the job queue: skipping and enqueueing analyzers are returned as intents for the caller (the server’s webhook route).
Evaluating
Section titled “Evaluating”const result = await evaluatePolicies({ policies, // Policy[], e.g. from combinePolicies event, // MergeRequestEvent forge, // GitForge now, // optional Date dryRun, // optional: report what would happen, change nothing memory, // optional PolicyMemory; process-wide in-memory default botUser, // optional: Holtkeeper's forge username (no self-approval) bots, // optional: usernames/globs of bot users for `author.bot` registry, // optional: action providers, the built-ins by default fetch, // optional: used by the webhook action commentsOff, // optional: skip actions that post on the merge request})// → { matched: { policyId, reasons[] }[], unmatched: { policyId, reason }[],// outcomes: ActionOutcome[], skipAnalyzers: '*' | string[], enqueue: { analyzer, options? }[] }- Order — enabled policies whose
scope(andwithin, the settings entry scope) cover the repository, most specific scope first (exact path > path glob, deeper folders first > team > everything), list order among equals. A matching policy withstop: trueends evaluation. - Actions run in list order. Every action yields an
ActionOutcome{ policyId, action, status, detail, data? }—done,dry-run,skipped,deferred,unsupported,failedorintent— with a human-readabledetailfor the audit log. Errors never escape: a throwing condition makes the policy unmatched (error: ...), a throwing action becomesfailed. - Idempotency —
approve,mergeandcomment(by default) happen once per head commit: the keymr-policy:<forge>:<repo>!<id>@<sha>:<policy>:<action JSON>is stored inPolicyMemoryafter adoneoutcome, and a later run with the same key isskipped.deferred,failedand dry-run outcomes are not remembered, so a later event (e.g. the pipeline turning green) retries them. The server should backPolicyMemory(seen(key),remember(key)) with the store;createMemory(limit)is an in-memory LRU. - Comments off — with
commentsOff(the server sets it frommergeRequests.comments), actions whose providerposts(comment,label) areskippedwithmerge request comments are offbefore they run, andapprovedrops itssummaryso it is a bare approval. Other actions run as usual. - Intents —
skipAnalyzersis the union over matched policies (*wins).enqueuecollectsenqueueactions, deduplicated; it is not filtered byskipAnalyzers. - Lazy facts — the head sha (event, else
getMergeRequest, else diff refs), the diff (getMergeRequestDiff) and the latest pipeline for the head sha (listPipelines, highest id) are loaded once per evaluation and only when a condition or action needs them.
interface MergeRequestEvent { repo: RepoRef teams: string[] // owner teams of the repository mr: MergeRequest & { headSha?: string } action: 'open' | 'update' | 'reopen' | 'label' | 'merge' | 'approve' | 'command' labelsAdded?: string[] actor: string isBot: boolean // the actor authorIsBot?: boolean // the MR author; else `bots` globs decide command?: string // for `command` events, without the slash}The engine works on a copy of the event; actions that change labels, state or draft update the copy so later actions see the new values.
Conditions
Section titled “Conditions”All fields optional and AND-ed; checked cheapest first and stopping at the first failure, so the diff and pipelines are only loaded when still needed. Single values are accepted where lists are.
| Field | Holds when |
|---|---|
event |
the event action is one of these |
sourceBranch, targetBranch |
the branch matches one of these globs |
author |
users: name matches a name/glob; bot: author is (not) a bot. A string or list is shorthand for users |
labels |
all present, any present, none present (exact names) |
labelAdded |
one of these labels was added by this event |
title |
title matches the regex, pattern or /pattern/flags |
draft |
draft state equals |
command |
a command event with one of these commands |
changedPaths |
all / any / none changed paths match the globs (new path; old path for deletions). Loads the diff |
size |
maxLines, maxFiles, minLines, minFiles (added + removed lines). Collapsed files exceed every line limit. Loads the diff |
pipeline |
latest pipeline for the head is success, failed (incl. canceled), running (incl. pending, manual) or none (incl. skipped) |
all, any, not |
combinators over nested conditions |
Globs use Bun.Glob: * does not cross /, so write **/*.md for Markdown anywhere. Without getMergeRequestDiff path and size conditions do not hold.
Actions
Section titled “Actions”Each action is an ActionProvider { type, schema, run(action, context) → ActionResult, onceKey?, posts? } (posts: it writes on the merge request itself, so commentsOff skips it; context.commentsOff tells others) in an ActionRegistry; defineAction wraps a provider typed by its zod schema (options are parsed before run, invalid ones fail the action). A new action is one file plus createRegistry([...builtinActions, myAction]). Providers must honour context.dryRun; context.perform(detail, effect) handles dry runs and errors.
| Type | Options | Notes |
|---|---|---|
approve |
summary?, requirePipeline?: 'success', allowSelfApprove? |
submitReview with event: 'approve'. Deferred while the pipeline runs, skipped when not green. Refused for the bot’s own MRs. Once per head |
merge |
squash?, removeSourceBranch?, whenPipelineSucceeds? (default true) |
Skips drafts and non-open MRs. Once per head |
label |
add?, remove? |
updateMergeRequest({ labels }); skipped when nothing changes |
comment |
body, once?: 'headSha' | 'mergeRequest' | 'never' (default headSha) |
Template placeholders {author}, {sourceBranch}, {targetBranch}, {title}, {id}, {repo}, {actor} |
close |
— | updateMergeRequest({ state: 'closed' }) |
draft |
draft? (default true) |
Marks as draft or ready |
assignReviewers |
reviewers |
Needs a forge implementing ReviewerAssignment.assignReviewers; otherwise unsupported. The author is left out |
enqueue |
analyzer, options? |
Returns an intent; the caller enqueues |
webhook |
url: Secret, headers?: Record<string, Secret>, payload? |
POSTs { policy, at, event, payload } as JSON; non-2xx fails. URL and headers never appear in outcomes |
Safety — approve and merge refuse (skipped) when the policy’s scope does not cover the repository; approve refuses merge requests authored by botUser unless allowSelfApprove.
Settings
Section titled “Settings”mergeRequestPoliciesSetting is a structural SettingDefinition (key mergeRequests.policies, section Merge requests, schema policiesSchema, help for the panel, and a form that lists the built-in actions as variants keyed by type and is marked x-editor: merge-request-policies, so the panel edits policies as If/Then blocks) for the server to register so policies can be edited per scope. combinePolicies(entries, repo, registry?) takes setting entries ({ key?, scope, value, updatedAt? }, e.g. SettingEntry) and returns { policies, invalid }:
- entries whose scope covers the repository, most specific first, newest first among equals;
- a policy without
scopetakes the entry’s; one with its ownscopeapplies only inside the entry’s (within); - an id from a more specific entry overrides the same id from less specific ones, so a repository can disable (
enabled: false) or replace a folder policy; - invalid entries are reported in
invalidwith a readable error and otherwise ignored.
policiesSchema validates a list (unique ids, defaults enabled: true, when: {}, actions: []); createPoliciesSchema(registry) validates actions against a custom registry.
Examples
Section titled “Examples”Stored as the value of mergeRequests.policies for a scope (tested in src/examples.test.ts):
[ { "id": "release-auto-approve", "name": "Approve releases from the release bot", "when": { "event": ["open", "update", "reopen"], "sourceBranch": "release/*", "targetBranch": "main", "author": { "users": ["release-bot"], "bot": true } }, "actions": [ { "type": "approve", "requirePipeline": "success", "summary": "Release {sourceBranch} approved: pipeline is green." } ], "skipAnalyzers": "*", "stop": true }, { "id": "renovate-patch-automerge", "name": "Auto-merge Renovate patch updates", "when": { "author": "renovate*", "labels": { "all": ["patch"] }, "draft": false }, "actions": [ { "type": "approve", "requirePipeline": "success" }, { "type": "merge", "squash": true, "removeSourceBranch": true, "whenPipelineSucceeds": true } ], "skipAnalyzers": ["review"] }, { "id": "docs-only", "name": "Label docs-only changes and skip code review", "when": { "event": ["open", "update"], "changedPaths": { "all": ["**/*.md", "docs/**"] } }, "actions": [{ "type": "label", "add": ["docs-only"] }], "skipAnalyzers": ["review"] }, { "id": "large-needs-review", "name": "Large merge requests need a human reviewer", "when": { "event": ["open", "update"], "size": { "minLines": 1000 }, "labels": { "none": ["needs-review"] } }, "actions": [ { "type": "label", "add": ["needs-review"] }, { "type": "comment", "once": "mergeRequest", "body": "@{author} this merge request changes 1000+ lines, so it needs a human review before merging." } ] }]The Renovate example expects Renovate to label patch updates (packageRules: [{ matchUpdateTypes: ["patch"], addLabels: ["patch"] }]). The release approval is deferred while the pipeline runs; the server re-evaluates on the next event (e.g. pipeline success), and the once-per-head key prevents a second approval.
Limitations
Section titled “Limitations”- No
assignReviewersonGitForgeyet: the action isunsupporteduntil an adapter implementsReviewerAssignment. sizecounts lines from the diff body; files the forge collapsed count as exceeding line limits.- The default memory is per process; idempotency across restarts needs a store-backed
PolicyMemory.