Skip to content

Programmatic API

You can run policies from your own code as well as from the CLI. repopo has three entry points:

ImportContents
repopo/apiPolicy types, makePolicy() and the policy definers, adapters, runPolicies, and PolicyRunner.
repopo/policiesThe built-in policies and their configuration types.
repopoEverything in repopo/api, plus the CLI’s run function from OCLIF.

Import from repopo/api in programs so you don’t load the CLI entry point.

runPolicies runs configured policies and returns a promise of the results:

import { makePolicy, runPolicies } from "repopo/api";
import { NoJsFileExtensions } from "repopo/policies";
const results = await runPolicies({
policies: [makePolicy(NoJsFileExtensions)],
root: process.cwd(),
files: ["src/main.mjs"],
fix: false,
});
const failed =
results.errors.length > 0 ||
results.findings.some((finding) => finding.fixStatus !== "fixed");

If your code already uses Effection, use PolicyRunner. It takes the same options and returns the same results: yield* new PolicyRunner(options).run() inside an operation.

OptionDescription
policiesPolicies configured with makePolicy().
rootAbsolute path of the repository root.
filesRepo-relative paths that file policies check. Defaults to inventory.
inventoryThe full repository list, as string paths or annotated records. See below.
excludePatterns that exclude repo-relative paths from every policy, like excludeFiles in a configuration file.
fixtrue to apply fixes, like check --fix.
concurrencyMaximum files processed at once in read-only runs. Defaults to 8. Fixes always run one file at a time.
signalAn AbortSignal to cancel the run.

Selected files and the repository inventory

Section titled “Selected files and the repository inventory”

The two file lists do different jobs:

  • files is the selection that file policies check.
  • inventory is the full list of repository files, filtered by each repository/change policy’s optional match and exclusions.

When any repository or change policy is configured, you must pass inventory. repopo never uses files as a substitute: a repository policy that checks for a missing README would give the wrong answer if it saw only the changed files. These policies run once each, even when the selection, inventory, or regex match is empty.

An array is a fixed snapshot. When fixes can create or remove files, pass an async function instead; repopo calls it again after file fixes and before each repository policy:

await runPolicies({
policies,
root,
files: changedFiles,
inventory: async () => listAllRepositoryFiles(),
fix: true,
});

listAllRepositoryFiles stands for your own function, such as one that runs git ls-files. The CLI loads the full Git inventory this way regardless of --stdin.

Pass the full inventory with a status on each file:

import { collectRepositoryInventory, makePolicy, runPolicies } from "repopo/api";
import { RequireCompanionChanges } from "repopo/policies";
const inventory = await collectRepositoryInventory(root, {
mode: "pr",
base: "main",
});
const results = await runPolicies({
root,
files: [],
inventory,
policies: [
makePolicy(RequireCompanionChanges, {
config: { trigger: /^src\//, companion: /^docs\// },
}),
],
});

The collector accepts { mode?, base? }; an explicit base overrides CI target information. The CLI uses CI before its configured base. The collector returns RepositoryFile[]: unchanged files plus added, modified, deleted, type-changed, and renamed entries. Each entry has path and status; renames also have previousPath. Optional exists records working-tree presence when status alone is not enough: for example, a committed addition can be absent from the working tree after an unstaged deletion.

Repository handlers receive this list as entries. Existing handlers can keep using files, the derived string-path view of files present in the working tree. Legacy string inventories remain supported and produce unchanged entries. File selection stays separate. Absent inventory entries are not used as the default selection; an explicit files selection retains its existing behavior.

Set scope: "changes" to request Git statuses in the CLI. Both "repository" and "changes" use RepositoryPolicyDefinition; no separate handler or configuration API is needed. Programmatic callers supply annotated inventory themselves; change policies require a status on every entry. An empty inventory is valid.

An optional match regex filters entries before either handler runs, matching either rename endpoint without discarding its metadata. Omit it to receive the full eligible inventory. The files view derives from those same matching entries, and the shared inventory remains unchanged for other policies.

The runner copies and freezes the initial inventory before handlers run. An inventory loader can refresh live paths between fixes, but statuses stay fixed for the run: files created by fixes appear as unchanged and cannot satisfy a companion-change requirement. Global and per-policy exclusions filter entries by path, including deleted entries, just as they filter files.

The results have four parts:

FieldContents
findingsEvery finding, including fixed ones. Each has the policy name, the policyId, the scope (file, repository, or changes), and a file (file policies) or optional files (repository/change policies).
errorsExecution errors: a policy threw or broke the result contract. Each has the phase ("handle" or "resolve"), the kind ("exception" or "contract"), a message, and the original cause.
skippedWork that didn’t run, with a reason: "excluded", "file-fix-error", or "repository-fix-error".
perfStatsCounts and per-policy timings in milliseconds.

A run has failed if there are any errors or any finding whose fixStatus isn’t "fixed". Skipped work because of exclusions isn’t a failure.

  • Invalid input, such as a bad exclusion pattern or a policy missing required configuration, throws before any policy runs.
  • In read-only runs, execution errors are collected and the run continues.
  • In fix runs, an error stops the remaining work on that file, but other files continue and repository/change policies are skipped. An error in either scope skips the repository/change policies after it.
  • Cancelling with signal releases resources before the run rejects. Fixes that were already written aren’t rolled back.

A file policy can provide its fix as a separate resolver instead of fixing in the handler. See Standalone file resolvers.

See the API reference for every exported type and function, and Migrate to the 1.0 API if you’re updating code written against an earlier version.