Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions docs/src/content/docs/guide/ops.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,11 @@ Any non-`report` mode on a `pull_request` trigger also gets `pull-requests: writ

A fountain [`Steward`](/chant/lexicons/fountain/steward/) is the third: every op it lists that carries a cadence becomes a `Schedule` on the teammate, in-thread, and a fire that lands while a turn is running is dropped rather than queued.

Two per-Op options widen what the generated job can do without loosening that set ([#2242](https://github.com/INTENTIUS/chant/issues/2242)). `setup` is an ordered list of steps emitted between the checkout and the `beforeScript` lines, each either `{ uses, with?, env? }` or `{ run, env? }`. It is the only way a generated job reaches a marketplace action, and `aws-actions/configure-aws-credentials` is the motivating one. `permissions` is a map merged over the finding mode's own set, additively: it can add a scope no mode grants, `id-token: write` above all, and the github generator refuses at build time any entry that touches a scope the mode already granted, names `write-all`, names a scope GitHub does not define, or asks for `pull-requests: write` on a trigger with no pull request. A `setup` action reference has to be pinned to something other than the action repository's own default branch. A generated workflow is committed once and then re-run unattended over whatever role it assumes.
Three per-Op options sit beside the finding mode, two of them widening what the generated job can do without loosening that set ([#2242](https://github.com/INTENTIUS/chant/issues/2242)) and the third deciding who may let the job start at all ([#2257](https://github.com/INTENTIUS/chant/issues/2257)). `setup` is an ordered list of steps emitted between the checkout and the `beforeScript` lines, each either `{ uses, with?, env? }` or `{ run, env? }`. It is the only way a generated job reaches a marketplace action, and `aws-actions/configure-aws-credentials` is the motivating one. `permissions` is a map merged over the finding mode's own set, additively: it can add a scope no mode grants, `id-token: write` above all, and the github generator refuses at build time any entry that touches a scope the mode already granted, names `write-all`, names a scope GitHub does not define, or asks for `pull-requests: write` on a trigger with no pull request. A `setup` action reference has to be pinned to something other than the action repository's own default branch. A generated workflow is committed once and then re-run unattended over whatever role it assumes.

The two options land differently per provider. GitHub emits both. Forgejo emits the steps, since its runner runs `uses:` steps, and drops `permissions:` along with the mode's own scopes, because the Forgejo runner ignores the section and issues no OIDC token off a workflow permission. GitLab refuses both by name at build time: a `uses:` step has no GitLab equivalent (jobs run `script` lines, and a `{ run }` entry is emitted as one), and GitLab's OIDC surface is a separate `id_tokens:` declaration chant does not generate, so ignoring the map would emit a job that reads as having OIDC and runs with no credentials.
Those two land differently per provider. GitHub emits both. Forgejo emits the steps, since its runner runs `uses:` steps, and drops `permissions:` along with the mode's own scopes, because the Forgejo runner ignores the section and issues no OIDC token off a workflow permission. GitLab refuses both by name at build time: a `uses:` step has no GitLab equivalent (jobs run `script` lines, and a `{ run }` entry is emitted as one), and GitLab's OIDC surface is a separate `id_tokens:` declaration chant does not generate, so ignoring the map would emit a job that reads as having OIDC and runs with no credentials.

The third option points the other way. `environment`, a `{ name, url? }`, emits `environment:` on that Op's job, which is how a GitHub environment's protection rules (required reviewers, a wait timer, a branch restriction) come to hold a generated apply. It costs no token scope, since environment protection is repository configuration rather than something `GITHUB_TOKEN` carries. Nothing about the environment itself is validated, because GitHub creates an unprotected one on first use rather than failing, so whether reviewers exist cannot be answered from workflow content. What the generator refuses is only the shape that could never bind, meaning a blank name or a `url` that is neither absolute nor an expression the run resolves. GitLab has the same key with the same two fields and a protected environment's approval rule behind it, so it maps across, and the generated header names the environment to protect since the rule is a project setting this file cannot declare. Forgejo Actions has no environments at all, so the key is dropped **and** the generated file gains a header comment naming the environment that was asked for and stating that nothing on the forge enforces it. That is the one option where a silent drop would be worst, because the whole point of it is a human holding an apply.

A `ScheduledOpSpec` isn't limited to cron. Its `trigger` also takes `{ kind: "pull_request", branches? }` and `{ kind: "push", branches? }`, for an Op whose CI trigger is a PR or a push rather than a clock (a Terraform `plan` on `pull_request`, posted as a PR comment, paired with `apply` on `push` to the default branch, is the motivating case). GitHub and Forgejo support all three trigger kinds; GitLab, which has no `pull_request`/`push` event model, throws a clear error if given anything but cron.

Expand All @@ -277,6 +279,18 @@ A `push` job is the one that has to survive a gate. GitHub Actions has no neutra

Forgejo takes the first two and drops the third: the notice job shells to `gh`, which a Forgejo runner neither ships nor can point at its own instance, the same reason `comment` mode is refused there. GitLab takes none of it, because its Op generator has no push pipeline to attach them to.

### Two gates, and how they compose

An Op with an `environment` on a forge that has environments is behind two gates, and they stop different things at different moments.

The **environment reviewer** stops the job before it starts. Nothing is checked out and no credentials are minted; the run sits in the forge's own queue showing "waiting for review" until somebody with access to that environment releases it. It is the forge's gate, configured on the forge, and it applies to whatever the job does, chant included.

**chant's own gate** ([#2119](https://github.com/INTENTIUS/chant/issues/2119)) stops the apply inside a run that already started. The Op plans and reaches its Gate phase. Finding no resolution on the gate ledger, it records a pending fact on the `chant/lifecycle` branch and ends there. Nothing is held open in between. `chant approve <op> <gate> --approver you` records the answer as another fact on that branch, and the next run reads it and applies. Because it is a fact rather than a held connection, the decision survives the run that asked for it, and the approver is reading a plan that already exists rather than a promise of one.

That difference is the reason to want both. The environment reviewer answers whether this job may run at all, cheaply and before any cost is incurred. chant's gate answers whether this exact plan should be applied, which nothing can answer before the job has run far enough to produce the plan. Either alone is coherent. The reviewer alone is the forge-native shape a team already knows, with `gate: "never"` on the root; chant's gate alone needs no forge features and works identically on Forgejo, where there are none. Neither, for a root whose merges should apply unattended, is also a choice. The choice is per environment, so a `staging` environment with no protection and `gate: "never"` sits happily beside a `production` environment with two reviewers and a gated apply.

The follow-up job that reports a pending chant gate stays outside the environment. It exists to say an approval is waiting, and behind the same reviewer it would only become readable after somebody had already acted.

## Replay (policy drift)

`PolicyReplayOp` is the same shape applied to authorization policy. A [dogwood](/chant/lexicons/cedar/) `.dw` policy set carries time-window clauses — `formerly within 1h …` — so what a policy decides depends on a history the build cannot see. Replaying the declared set against a recorded event trace is how that gets checked, and the answer moves as the history does.
Expand Down
23 changes: 17 additions & 6 deletions docs/src/content/docs/lexicon-authoring/ci-integration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,25 @@ in `packages/core/src/lexicon.ts:441`.

`generateOpPipeline` is the cron-triggered sibling for scheduled Ops (chant
#927), with the same "CI providers only" rule. It receives `ScheduledOpSpec[]`
rather than components, and each spec can carry two things the options block
rather than components, and each spec can carry three things the options block
cannot, because they are per Op: `setup`, an ordered list of `{ uses, with?,
env? }` / `{ run, env? }` steps emitted ahead of the `beforeScript` lines, and
env? }` / `{ run, env? }` steps emitted ahead of the `beforeScript` lines,
`permissions`, a map your generator merges additively over whatever the
finding mode already grants (chant #2242). A provider with no equivalent
degrades by name at build time rather than dropping the option: gitlab refuses
a `uses` entry and an additive `permissions` map, forgejo emits the steps and
drops the permissions the way it already drops the mode's own.
finding mode already grants (chant #2242), and `environment`, a `{ name, url?
}` naming the forge deployment environment the job runs in (chant #2257). A
provider with no equivalent degrades by name at build time rather than
dropping the option: gitlab refuses a `uses` entry and an additive
`permissions` map, forgejo emits the steps and drops the permissions the way
it already drops the mode's own.

`environment` is the one of the three where a silent drop would be worst,
because the option exists so a human can hold a job. A provider without
environments therefore both drops the key and says so in the file it
generates; forgejo writes a header comment naming the environment and stating
that nothing on the forge enforces it. GitLab does have environments, and maps
the option onto its own `environment:` key. If your provider has the concept,
emit it and let the protection rules stay where they live, in the provider's
own configuration. If it does not, be loud.

Each job is a **thin trigger**: it runs the component's deploy command. The
deploy logic stays in the component, not in the generated YAML — the whole point
Expand Down
79 changes: 79 additions & 0 deletions lexicons/forgejo/src/components/generate-op-pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { ScheduledOpSpec } from "@intentius/chant/lexicon";

interface ParsedJob {
"runs-on"?: string;
environment?: Record<string, string>;
outputs?: Record<string, string>;
steps: Array<{ id?: string; uses?: string; run?: string }>;
}
Expand Down Expand Up @@ -198,3 +199,81 @@ describe("generateForgejoOpPipeline: the gated apply on push (#2243)", () => {
expect(gh.jobs).toHaveProperty("app-apply-gate-notice");
});
});

/**
* chant #2257 — the option Forgejo has no concept for at all. Unlike
* `permissions:`, which the runner reads and ignores, an environment is an
* object that does not exist on a Forgejo instance: no protection rules, no
* required reviewers, no wait timers. Emitting the key would read as a
* deployment gate and hold nothing back, so it is dropped — and, because the
* whole point of the option is a human holding an apply, the drop is said out
* loud in the file rather than only in a build warning.
*/
describe("generateForgejoOpPipeline: a dropped deployment environment (#2257)", () => {
const APPLY: ScheduledOpSpec = { name: "app-apply", trigger: { kind: "push", branches: ["main"] } };
const GATED: ScheduledOpSpec = { ...APPLY, environment: { name: "production" } };

/** The document a spec with no `environment` emitted before the option existed. */
const AUDIT_YAML_BEFORE_2257 =
[
"on:",
" schedule:",
" - cron: '0 6 * * *'",
" workflow_dispatch: {}",
"",
"concurrency:",
" group: actions-audit",
" cancel-in-progress: false",
"",
"jobs:",
" actions-audit:",
" runs-on: docker",
" container: node:22-slim",
" steps:",
" - uses: https://code.forgejo.org/actions/checkout@v4",
" - run: chant run actions-audit",
" env:",
" GITHUB_TOKEN: '${{ github.token }}'",
" GH_TOKEN: '${{ github.token }}'",
].join("\n") + "\n";

test("a spec with no environment emits the bytes it emitted before the option existed", () => {
const yaml = generateForgejoOpPipeline([
{ name: "actions-audit", schedule: "0 6 * * *", findingMode: "issue" },
]).files[0].yaml;
expect(yaml).toBe(AUDIT_YAML_BEFORE_2257);
});

test("drops the key, and github with the same spec keeps it", () => {
const fj = parseFile(generateForgejoOpPipeline([GATED]).files[0].yaml);
expect(fj.jobs!["app-apply"].environment).toBeUndefined();
// A dialect drop, not a builder that never computed it.
expect(generateGithubOpPipeline([GATED]).files[0].yaml).toContain("environment:");
});

test("says in the generated header which environment did not survive, and what still gates", () => {
const yaml = generateForgejoOpPipeline([GATED]).files[0].yaml;
expect(yaml.startsWith("# chant dropped `environment: production`")).toBe(true);
expect(yaml).toContain("Forgejo Actions has no");
expect(yaml).toContain("not held back by anything on the forge");
// The gate that does survive is chant's own, which needs nothing from the forge.
expect(yaml).toContain("chant/lifecycle");
expect(yaml).toContain("approve <op> <gate>");
});

test("the header only appears for the Op that asked for an environment", () => {
const files = generateForgejoOpPipeline([
{ ...GATED, name: "app-apply" },
{ name: "app-watch", schedule: "0 6 * * *" },
]).files;
expect(files[0].yaml).toContain("# chant dropped `environment: production`");
expect(files[1].yaml).not.toContain("# chant dropped");
expect(files[1].yaml.startsWith("on:")).toBe(true);
});

test("refuses the same malformed environment github refuses, through the shared builder", () => {
expect(() =>
generateForgejoOpPipeline([{ ...APPLY, environment: { name: "production", url: "/deploys" } }]),
).toThrow(/neither an absolute http\(s\) URL nor a/);
});
});
47 changes: 45 additions & 2 deletions lexicons/forgejo/src/components/generate-op-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
* configuration to point `gh` at a Forgejo instance, so this refuses the mode
* by name rather than generating a job whose finding step fails on every run.
*
* A spec's `environment` (#2257) is dropped on the same terms as
* `permissions:`, and for a stronger reason: Forgejo Actions has no
* environments at all — no protection rules, no required reviewers, no
* per-environment secrets — so the key names an object that does not exist on
* the instance. Dropping it silently would be the worst outcome available,
* because the whole point of the option is a human holding an apply, so this
* also writes a comment into the generated file's header saying which
* environment was asked for and that nothing on Forgejo enforces it. What
* still holds the apply here is chant's own gate (#2119), which is a fact on
* the `chant/lifecycle` branch and needs nothing from the forge.
*
* The gated-apply notice job (#2243) does not cross over either, for the same
* reason and by the same mechanism as `permissions:`: it shells to `gh`, which
* a Forgejo `act_runner` neither ships nor can point at its own instance, and
Expand Down Expand Up @@ -64,6 +75,34 @@ function forgejoize(value: Record<string, unknown>, dialect: ForgejoDialectOptio
return transformWorkflowObject(value, dialect).value as Record<string, unknown>;
}

/**
* Strip the job-level `environment:` github emitted (#2257). Forgejo Actions
* has no environments, so the key would read as a deployment gate and hold
* nothing back. Dropped from the rebuilt doc rather than from the dialect's
* own `DROPPED_KEYS`, so this touches the Op generator alone and leaves every
* other document the dialect transforms exactly as it emits today.
*/
function withoutEnvironment(jobsDoc: Record<string, unknown>): Record<string, unknown> {
return Object.fromEntries(
Object.entries(jobsDoc).map(([jobName, job]) => {
if (typeof job !== "object" || job === null || !("environment" in job)) return [jobName, job];
const { environment: _dropped, ...rest } = job as Record<string, unknown>;
return [jobName, rest];
}),
);
}

/** Say in the generated file which environment gate did not survive the crossing. */
function droppedEnvironmentHeader(environmentName: string): string[] {
return [
`# chant dropped \`environment: ${environmentName}\` from this workflow: Forgejo Actions has no`,
"# environments, so there is no protection rule, required reviewer or wait timer for the key to",
"# name. This job is not held back by anything on the forge. What still stops the apply is chant's",
"# own gate: the run records a pending fact on the chant/lifecycle branch and ends, and `chant",
"# approve <op> <gate>` is what lets the next run through.",
];
}

/**
* Synthesize one `.forgejo/workflows/*.yml` per scheduled Op. Reuses github's
* trigger/job structure ({@link buildGithubOpPipelineDocs}) — its `setup`-step
Expand Down Expand Up @@ -92,13 +131,17 @@ export function generateForgejoOpPipeline(
const { files, jobs } = buildGithubOpPipelineDocs(ops, options);

return {
files: files.map(({ name, doc }) => {
// One file per spec, in spec order, which is what lets the header below
// read its Op's own `environment` off the input by position.
files: files.map(({ name, doc }, index) => {
const environment = ops[index].environment;
const forgejoDoc: GithubOpPipelineDoc = {
...(environment ? { header: droppedEnvironmentHeader(environment.name) } : {}),
on: forgejoize(doc.on, dialectOptions),
...(doc.env ? { env: forgejoize(doc.env, dialectOptions) } : {}),
concurrency: forgejoize(doc.concurrency, dialectOptions),
permissions: {},
jobsDoc: forgejoize(doc.jobsDoc, dialectOptions),
jobsDoc: forgejoize(withoutEnvironment(doc.jobsDoc), dialectOptions),
};
return { name, yaml: emitOpPipelineYAML(forgejoDoc) };
}),
Expand Down
Loading
Loading