CI helpers
@noctcore/harness/ci holds three small decisions a CI pipeline makes around its checks:
- Affected base: which commit a run compares against, so a workspace filter (turbo
--affected, an Nx base) skips only what a completed run already proved green. - Commitlint range: which commits a run lints, so a
--no-verifycommit cannot slip through, with an exemption list by sha for commits already on a protected branch. - Due dates: which dated exceptions (ledger entries with a review date, trivy ignores with an
expired_at) are about to lapse, for a scheduled job that goes red ahead of the date.
Like every module in the package, the functions are pure over the deps you hand them. Git runs
through nodeCiGitDeps(cwd), and the GitHub lookup is a function your repo writes: the package
makes no network call.
What stays in your repo
Section titled “What stays in your repo”The package decides; your repo wires. These stay with you:
- the workflow files, the job names and the step that writes
GITHUB_ENV; - the call to the GitHub API for the last successful push run (
headShaOfLatestRunparses the response); - the exempt commit list for commitlint;
- the ledger files and their paths;
- the scheduled due-date job. A due date turns red on a calendar day, not on a commit, so only a
scheduled workflow (
on: schedule) catches it, and that workflow’s trigger and wiring must stay in your repo. The package ships no workflow.
Affected base
Section titled “Affected base”import { appendFileSync } from 'node:fs';
import { affectedEnvLines, chooseBase, affectedBaseFrom, commitRange, headShaOfLatestRun, nodeCiGitDeps, nodeIsWorkspace,} from '@noctcore/harness/ci';
const env = process.env;const root = process.cwd();const git = nodeCiGitDeps(root);
// The GitHub lookup lives in your repo: the package makes no network call.async function lastGreenPushSha(): Promise<string | null> { const query = new URLSearchParams({ branch: env.GITHUB_REF_NAME ?? '', event: 'push', status: 'success', per_page: '1', }); const url = `${env.GITHUB_API_URL}/repos/${env.GITHUB_REPOSITORY}/actions/workflows/ci.yml/runs?${query.toString()}`; const response = await fetch(url, { headers: { authorization: `Bearer ${env.GITHUB_TOKEN ?? ''}` }, signal: AbortSignal.timeout(10_000), }); if (!response.ok) throw new Error(`HTTP ${String(response.status)}`); return headShaOfLatestRun(await response.json());}
const input = { event: env.GITHUB_EVENT_NAME ?? '', prBaseSha: env.PR_BASE_SHA ?? '', prHeadSha: env.PR_HEAD_SHA ?? '', before: env.PUSH_BEFORE ?? '',};const chosen = await chooseBase(input, { lastGreenPushSha, isAncestor: git.isAncestor });const decision = affectedBaseFrom(chosen, { changedFiles: git.changedFiles, isWorkspace: nodeIsWorkspace(root),});const range = commitRange(input, chosen, git.isAncestor);
console.log(`affected-base: ${decision.reason}`);console.log(`commit range: ${range.from}..${range.to} (${range.reason})`);const lines = [...affectedEnvLines(decision), `LINT_FROM=${range.from}`, `LINT_TO=${range.to}`];if (env.GITHUB_ENV) appendFileSync(env.GITHUB_ENV, `${lines.join('\n')}\n`);chooseBase picks a candidate, and affectedBaseFrom keeps it only when the diff is safe to filter
(decideAffectedBase does both):
| Event | Base | Full run when |
|---|---|---|
pull_request |
The pull request’s base sha. | The base sha is not a full sha. |
push |
The head of the last successful push run of the workflow on the branch, never the previous tip. | The lookup throws, there is no green run yet, or the green sha is not an ancestor of HEAD (a force push). |
| anything else | None. | Always. |
Then, for a candidate base, a full run also follows when git cannot list the diff, or when a changed
path is outside a real workspace: a direct child of a workspace root (apps and packages by
default, workspaceRoots to change them) that has a package.json. A root config, the lockfile or a
stray folder is invisible to a workspace filter, so it runs everything. The reason names the first
five such paths.
Why not the previous tip? Push A then push B while A’s run is pending replaces A’s run, and a base of A would test only A..B: A’s changes never got a completed run. A failed, cancelled or replaced run is not a success, so the next run falls back to the last green one.
affectedEnvLines(decision) returns the GITHUB_ENV lines: TURBO_SCM_BASE=<sha> and
TURBO_AFFECTED=--affected, or both empty on a full run. Pass baseVar, flagVar and flagValue
for another tool.
Commitlint range
Section titled “Commitlint range”commitRange(input, chosen, isAncestor) takes the base chooseBase picked (before the diff check,
since a full run still has a proven base for commits):
pull_request: base sha to head sha, never the synthetic merge commit; the merge commit’s parents (HEAD^1..HEAD^2) when the shas cannot be read.push: the green base to HEAD, so a push whose run failed is read again by the next one; without one, the pushed range frombeforewhen it is a real ancestor; otherwise HEAD alone.- anything else: HEAD alone.
import { createRequire } from 'node:module';
import { type IExemptCommit, nodeCiGitDeps, nodeSpawnInherit, runCommitlint,} from '@noctcore/harness/ci';
// Commits already on main that fail the config and cannot be rewritten.// Each entry needs the full sha, a reason and the message as `git log -1 --format=%B` prints it.const EXEMPT: readonly IExemptCommit[] = [];
const root = process.cwd();const git = nodeCiGitDeps(root);const bin = createRequire(import.meta.url).resolve('@commitlint/cli/cli.js');
const code = runCommitlint( { from: process.env.LINT_FROM ?? 'HEAD~1', to: process.env.LINT_TO ?? 'HEAD', event: process.env.GITHUB_EVENT_NAME ?? '', exempt: EXEMPT, commitlint: { command: process.execPath, args: [bin, '--config', 'commitlint.config.js'] }, }, { ...git, spawn: nodeSpawnInherit(root), log: (line) => console.log(line) },);process.exit(code);commitlint is not a dependency of the package: you pass its command and config arguments.
runCommitlint prints each command before it runs it and returns the exit code:
| Exit | When |
|---|---|
0 |
Every linted commit passed, or the range holds no commit (a rerun of the last green head). |
1 |
git cannot list the range (fetch the full history with fetch-depth: 0), a commit fails, or the exemption list is refused. |
Exempt commits
Section titled “Exempt commits”A commit already on a protected branch that fails your config (a --no-verify commit, a merge
message typed in a web UI) would keep every push red, and the branch cannot be force-pushed. List it
by full sha with a reason and its message exactly as git log -1 --format=%B <sha> prints it:
- a listed commit in range is skipped, and every other commit is linted one by one;
- its message must still match the recorded one, so the list cannot hide a different commit;
- a malformed list (a short sha, a blank reason or message, a sha listed twice) refuses the run;
- a pull request never uses the list: a commit already on the base branch is never in its range, and a pull request must not exempt its own commits.
Due dates
Section titled “Due dates”import { collectDueEntries, dueReport, formatDueReport, readDueSources,} from '@noctcore/harness/ci';
// Your ledgers: JSON objects whose entries carry a `reviewBy` date.const LEDGERS = ['overrides.ledger.json', 'licenses.ledger.json'];
const sources = readDueSources(process.cwd(), LEDGERS, '.trivyignore.yaml');const collection = collectDueEntries(sources);const today = new Date().toISOString().slice(0, 10);const report = dueReport(collection, today);console.log(formatDueReport(report, collection.entries.length).join('\n'));process.exit(report.passed ? 0 : 1);soon 2026-11-30 12 days left overrides.ledger.json lodashdue-dates: passed (9 dated entries, 1 due within 14 days, none due within 7 days or lapsed)A ledger is a JSON object whose entries each carry a date (reviewBy by default, dateField to
change it); top-level keys starting with $ are metadata. A trivy ignore file is read with the same
strict reader as the trivy check, and each expired_at is a due date.
| Option | Default | Meaning |
|---|---|---|
dateField |
reviewBy |
The entry field that holds the date (YYYY-MM-DD, UTC). |
listDays |
14 |
Entries due within this many days are listed. |
failDays |
7 |
An entry due within this many days, or lapsed, fails the run. -1 fails only lapsed entries. |
skipKeyPrefix |
$ |
Top-level keys with this prefix are not entries. |
label |
due-dates |
The name the summary line starts with. |
An entry without a readable date, a ledger that is not a JSON object and a ledger that is missing are failures, never passes: an undated exception never ends. The failing summary says what to do: renew or remove the entries, and date every entry.
The full signatures are in the API reference.