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
4 changes: 4 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
"includes": ["src/**", "!src/cli/inputs.ts"],
"plugins": ["./lint/never-throw.grit"]
},
{
"includes": ["src/**", "!src/sections/shared/schema-helpers.ts"],
"plugins": ["./lint/rule-gate.grit"]
},
{
"includes": ["src/action/**"],
"linter": {
Expand Down
14 changes: 14 additions & 0 deletions lint/rule-gate.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Every refinement under src/ is authored through rule() (src/sections/shared/schema-helpers.ts): zod skips a bare
// .refine() or .superRefine() once a nested value failed, so a section's own rule would go silent exactly when the
// document has a second mistake. Reach and the one exemption are the plugin's `overrides` entry in biome.json (see
// never-throw.grit for why):
// src/sections/shared/schema-helpers.ts -> rule() itself is the one superRefine

or {
`$_.superRefine($_)`,
`$_.superRefine<$_>($_)`,
`$_.refine($_)`,
`$_.refine<$_>($_)`
} as $call where {
register_diagnostic(span=$call, message="bare refinement: zod skips it once a nested value failed; wrap it in rule() so it runs beside nested failures")
}
9 changes: 2 additions & 7 deletions src/engine/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import { z } from "zod";
import { nonPlainKind } from "../plain-data.js";
import type { ProblemOf } from "../problem.js";
import { LIST_SECTIONS, type ListSection, SECTION_KEYS, type SettingsFile } from "../schema.js";
import {
checksReportingBesideFailures,
type DeclaredIssue,
type DeclaredSecretValue,
} from "../sections/contract/module.js";
import type { DeclaredIssue, DeclaredSecretValue } from "../sections/contract/module.js";
import { listLayering, sectionModule, sectionShape } from "../sections/registry.js";
import { valueAt } from "../sections/shared/list-section.js";
import { agree, countNoun } from "../text.js";
Expand Down Expand Up @@ -215,8 +211,7 @@ export function validateSectionShapes(
problems.push(nonPlain);
continue;
}
// The section may compose a rule onto its loosened shape; loosen() itself covers every check beneath.
const parsed = checksReportingBesideFailures(shape).safeParse(declared);
const parsed = shape.safeParse(declared);
if (!parsed.success) {
const issues = parsed.error.issues;
for (const issue of issues.slice(0, 5)) {
Expand Down
97 changes: 52 additions & 45 deletions src/sections/actions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { z } from "zod";
import { describeValue } from "../../plain-data.js";
import { rule } from "../shared/schema-helpers.js";

/** GitHub's rule for an OIDC claim key; the PUT 422s on anything else. */
const CLAIM_KEY = /^[A-Za-z0-9_]+$/;
Expand Down Expand Up @@ -45,24 +46,26 @@ const ClaimKeys = z
'a claim key holds only letters, digits, and underscores (such as "repo" or "job_workflow_ref")',
}),
)
.superRefine((keys, refineCtx) => {
// A raw item beside its own shape issue is passed over (see ../shared/raw-values.ts): a YAML alias repeats one
// mapping at two items, and rendering it into the message can throw.
const seen = new Set<string>();
keys.forEach((key, index) => {
if (typeof key !== "string") {
return;
}
if (seen.has(key)) {
refineCtx.addIssue({
code: "custom",
path: [index],
message: `"${key}" repeats an earlier claim key; GitHub requires the keys to be unique`,
});
}
seen.add(key);
});
});
.check(
rule((keys, refineCtx) => {
// A raw item beside its own shape issue is passed over (see ../shared/raw-values.ts): a YAML alias repeats one
// mapping at two items, and rendering it into the message can throw.
const seen = new Set<string>();
keys.forEach((key, index) => {
if (typeof key !== "string") {
return;
}
if (seen.has(key)) {
refineCtx.addIssue({
code: "custom",
path: [index],
message: `"${key}" repeats an earlier claim key; GitHub requires the keys to be unique`,
});
}
seen.add(key);
});
}),
);

/**
* The two templates are two variants, discriminated on use_default, so the claim-key list has no
Expand All @@ -79,18 +82,20 @@ const OidcTemplate = z
use_immutable_subject: z.boolean().optional(),
}),
])
.superRefine((declared, refineCtx) => {
// A raw use_default beside its own shape issue is truthy without being true and selects no template.
if (declared.use_default === true && Object.hasOwn(declared, "include_claim_keys")) {
refineCtx.addIssue({
code: "custom",
path: ["include_claim_keys"],
message:
"GitHub ignores include_claim_keys under use_default: true, so the declared list could never take; " +
"set use_default: false for a custom template, or remove the list",
});
}
});
.check(
rule((declared, refineCtx) => {
// A raw use_default beside its own shape issue is truthy without being true and selects no template.
if (declared.use_default === true && Object.hasOwn(declared, "include_claim_keys")) {
refineCtx.addIssue({
code: "custom",
path: ["include_claim_keys"],
message:
"GitHub ignores include_claim_keys under use_default: true, so the declared list could never take; " +
"set use_default: false for a custom template, or remove the list",
});
}
}),
);

export const ActionsConfig = z
.object({
Expand Down Expand Up @@ -139,20 +144,22 @@ export const ActionsConfig = z
})
.optional(),
})
.superRefine((declared, refineCtx) => {
// Checked in the shape, not in plan(), so both modes reject the document before ANY section
// writes; a plan-time throw would fire after earlier sections already wrote.
refuseReportedOnly(refineCtx, declared);
if (declared.selected_actions === undefined || declared.allowed_actions === undefined) {
return;
}
if (declared.allowed_actions !== "selected") {
refineCtx.addIssue({
code: "custom",
path: ["selected_actions"],
message: `selected_actions is declared together with allowed_actions: ${describeValue(declared.allowed_actions)}, but an allowlist only applies under allowed_actions: "selected". Set allowed_actions to "selected", or remove selected_actions`,
});
}
})
.check(
rule((declared, refineCtx) => {
// Checked in the shape, not in plan(), so both modes reject the document before ANY section
// writes; a plan-time throw would fire after earlier sections already wrote.
refuseReportedOnly(refineCtx, declared);
if (declared.selected_actions === undefined || declared.allowed_actions === undefined) {
return;
}
if (declared.allowed_actions !== "selected") {
refineCtx.addIssue({
code: "custom",
path: ["selected_actions"],
message: `selected_actions is declared together with allowed_actions: ${describeValue(declared.allowed_actions)}, but an allowlist only applies under allowed_actions: "selected". Set allowed_actions to "selected", or remove selected_actions`,
});
}
}),
)
.meta({ id: "ActionsConfig" });
export type ActionsConfig = z.infer<typeof ActionsConfig>;
100 changes: 51 additions & 49 deletions src/sections/branches/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from "../contract/module.js";
import type { SectionPermission } from "../contract/permissions.js";
import { plainData, type Read } from "../contract/plan.js";
import { layeredList } from "../shared/schema-helpers.js";
import { layeredList, rule } from "../shared/schema-helpers.js";
import { ENDPOINTS, MISSING_BRANCH } from "./endpoints.js";
import {
type BranchesContext,
Expand Down Expand Up @@ -261,61 +261,63 @@ export const branchesSection = {
graphql: GRAPHQL,
// The wildcard key sweep composes HERE, not in schema.ts: it reads the GraphQL translation tables,
// which are this section's own machinery, and nothing outside them can reach a wildcard rule.
shape: loosen(layeredList(BranchesConfig)).superRefine((declared, refineCtx) => {
// The routed shape parsed one of the two forms; under the wrapper an issue's path starts at `entries`.
const wrapped = !Array.isArray(declared);
const entries = listEntries(declared as BranchConfig[] | { entries: BranchConfig[] });
const at = (index: number, ...rest: string[]): (string | number)[] => [
...(wrapped ? ["entries"] : []),
index,
...rest,
];
entries.forEach((entry: BranchConfig, index) => {
// The entry, its name, or its protection may be raw or missing beside its own shape issue; a regex would
// coerce the name (`[object Object]` wildcards), and a non-mapping protection holds no keys to sweep.
if (
!isPlainObject(entry) ||
typeof entry.name !== "string" ||
!isWildcardPattern(entry.name)
) {
return;
}
const protection = entry.protection;
if (!isPlainObject(protection)) {
return;
}
for (const key of Object.keys(protection)) {
if (!WILDCARD_KEY_SET.has(key)) {
refineCtx.addIssue({
code: "custom",
path: at(index, "protection", key),
message: WILDCARD_KEY_ERROR(entry.name, key),
});
}
}
// The structured pairs translate NAMED sub-keys only, so an unknown sub-key would be silently
// lost; the schema already holds each pair to a mapping or null.
const nested: Array<[string, Record<string, string>]> = [
["required_status_checks", GRAPHQL_STATUS_CHECK_TWINS],
["required_pull_request_reviews", GRAPHQL_REVIEW_TWINS],
shape: loosen(layeredList(BranchesConfig)).check(
rule((declared, refineCtx) => {
// The routed shape parsed one of the two forms; under the wrapper an issue's path starts at `entries`.
const wrapped = !Array.isArray(declared);
const entries = listEntries(declared as BranchConfig[] | { entries: BranchConfig[] });
const at = (index: number, ...rest: string[]): (string | number)[] => [
...(wrapped ? ["entries"] : []),
index,
...rest,
];
for (const [key, twins] of nested) {
const value = protection[key];
if (!isPlainObject(value)) {
continue;
entries.forEach((entry: BranchConfig, index) => {
// The entry, its name, or its protection may be raw or missing beside its own shape issue; a regex would
// coerce the name (`[object Object]` wildcards), and a non-mapping protection holds no keys to sweep.
if (
!isPlainObject(entry) ||
typeof entry.name !== "string" ||
!isWildcardPattern(entry.name)
) {
return;
}
const protection = entry.protection;
if (!isPlainObject(protection)) {
return;
}
for (const subKey of Object.keys(value)) {
if (!(subKey in twins)) {
for (const key of Object.keys(protection)) {
if (!WILDCARD_KEY_SET.has(key)) {
refineCtx.addIssue({
code: "custom",
path: at(index, "protection", key, subKey),
message: WILDCARD_KEY_ERROR(entry.name, `${key}.${subKey}`),
path: at(index, "protection", key),
message: WILDCARD_KEY_ERROR(entry.name, key),
});
}
}
}
});
}),
// The structured pairs translate NAMED sub-keys only, so an unknown sub-key would be silently
// lost; the schema already holds each pair to a mapping or null.
const nested: Array<[string, Record<string, string>]> = [
["required_status_checks", GRAPHQL_STATUS_CHECK_TWINS],
["required_pull_request_reviews", GRAPHQL_REVIEW_TWINS],
];
for (const [key, twins] of nested) {
const value = protection[key];
if (!isPlainObject(value)) {
continue;
}
for (const subKey of Object.keys(value)) {
if (!(subKey in twins)) {
refineCtx.addIssue({
code: "custom",
path: at(index, "protection", key, subKey),
message: WILDCARD_KEY_ERROR(entry.name, `${key}.${subKey}`),
});
}
}
}
});
}),
),
async plan(ctx, desired): Promise<Result<BranchesPlan, SectionFailure>> {
const section = this;
return safeTry(async function* () {
Expand Down
Loading
Loading