Skip to content

Commit 9071aa0

Browse files
authored
feat(pages)!: refuse an empty pages mapping when the file is parsed (#548)
1 parent 260d393 commit 9071aa0

9 files changed

Lines changed: 33 additions & 22 deletions

File tree

‎docs/reference/coverage.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ The side-by-side comparison with the Probot Settings app lives in the migration
376376

377377
**Fields the Pages update ignores** (`pages`)
378378

379-
- Parsing the file refuses the fields only the site GET reports (url, html_url, status, custom_404, protected_domain_state, pending_domain_unverified_at, https_certificate) and a source.path other than / or /docs, the only directories Pages publishes from.
379+
- Parsing the file refuses the fields only the site GET reports (url, html_url, status, custom_404, protected_domain_state, pending_domain_unverified_at, https_certificate), a source.path other than / or /docs, the only directories Pages publishes from, and an empty mapping, which the update endpoint rejects; the published schema carries the same three.
380380
- Sent, GitHub would drop the fields and report them as drift on every run, and the path would 422 at apply.
381381
- A workflow-built site still reports its source, so build_type: workflow beside a source converges.
382382

‎docs/sections/pages.docs.yml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ coverage:
2828
notes:
2929
- >-
3030
Parsing the file refuses the fields only the site GET reports (url, html_url, status,
31-
custom_404, protected_domain_state, pending_domain_unverified_at, https_certificate) and a
32-
source.path other than / or /docs, the only directories Pages publishes from.
31+
custom_404, protected_domain_state, pending_domain_unverified_at, https_certificate), a
32+
source.path other than / or /docs, the only directories Pages publishes from, and an empty
33+
mapping, which the update endpoint rejects; the published schema carries the same three.
3334
- >-
3435
Sent, GitHub would drop the fields and report them as drift on every run, and the path would
3536
422 at apply.

‎docs/upgrading/v2-to-v3.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ The detail is in [v2-to-v3/refusals-repository.md](v2-to-v3/refusals-repository.
8181
| Break | v2 | v3 | What the old form does now |
8282
|---|---|---|---|
8383
| Repository: GET-only keys, commit-message pairs, topics, and security sub-keys | `has_downloads: false` drifted on every run; an illegal squash pair or a bad topic 422ed at apply | Refused at parse: a GET-only key (`has_downloads`, `has_pages`, `custom_properties`, ids, urls, counts), an unknown `security_and_analysis` sub-key, a commit message without its title or in an illegal squash pair, a malformed or empty topic, more than 20 topics | Validation fails naming the key and the fix; [section 37](v2-to-v3/refusals-repository.md#37-repository-get-only-keys-commit-pairs-topics-and-security-sub-keys) |
84-
| Pages: GET-only site fields and the source path | `custom_404: true` rode the PUT, was dropped, and drifted forever; `source.path: /src` 422ed | `url`, `html_url`, `status`, `custom_404`, `protected_domain_state`, `pending_domain_unverified_at`, and `https_certificate` are refused; `source.path` is `/` or `/docs` | Validation fails naming the key; [section 38](v2-to-v3/refusals-repository.md#38-pages-get-only-fields-and-the-source-path) |
84+
| Pages: GET-only site fields, the source path, and the empty mapping | `custom_404: true` rode the PUT, was dropped, and drifted forever; `source.path: /src` 422ed; `pages: {}` ran and configured nothing | `url`, `html_url`, `status`, `custom_404`, `protected_domain_state`, `pending_domain_unverified_at`, and `https_certificate` are refused; `source.path` is `/` or `/docs`; an empty mapping is refused naming its three fixes | Validation fails naming the key; [section 38](v2-to-v3/refusals-repository.md#38-pages-get-only-fields-and-the-source-path) |
8585
| Actions: reported-only fields, a closed `selected_actions`, enums and bounds | `maximum_allowed_days: 400` was re-PUT on every run; a misspelled `selected_actions` key was re-PUT silently forever | `selected_actions_url`, `artifact_and_log_retention.maximum_allowed_days`, and `oidc_customization_sub.sub_claim_prefix` are refused; `selected_actions` is closed to its three keys and typed; `approval_policy` is an enum; claim keys match `^[A-Za-z0-9_]+$` and are unique; retention days and cache limits are positive integers; `include_claim_keys` beside `use_default: true` is refused; `sha_pinning_required` is a known boolean | Validation fails naming the key and the fix; [section 39](v2-to-v3/refusals-repository.md#39-actions-fields-that-could-never-converge) |
8686
| Code scanning and code quality setup: GET-only keys, languages, the runner pair | `schedule: weekly` and a `runner_label` under `runner_type: standard` each drifted forever; `languages: [javascript]` never matched the pair GitHub reports | `schedule` and `updated_at` are refused; languages are spelled as the PATCH takes them (`javascript-typescript`, no `rust`); a string `runner_label` goes only with `runner_type: labeled`, which requires one | Validation fails naming the key and the spelling to write; a key the GET never echoes earns the never-converges note; [section 40](v2-to-v3/refusals-repository.md#40-setup-sections-get-only-keys-languages-and-the-runner-pair) |
8787
| Check-suite preferences: `app_id` | `app_id: 0` and a negative id were PATCHed on every run; a repeated id was collapsed by GitHub with nothing reading it back | `app_id` is a positive integer and appears once | Validation fails naming the entries; [section 44](v2-to-v3/refusals-repository.md#44-check-suite-preferences-app_id) |

‎docs/upgrading/v2-to-v3/refusals-repository.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ v3 pages.custom_404: GitHub reports this field on the Pages site and the updat
4747

4848
`url`, `html_url`, `status`, `custom_404`, `protected_domain_state`, `pending_domain_unverified_at`, and `https_certificate` are refused. `public: false` stays declarable: Enterprise Cloud shares the same host, so the check run's note beside the drift says why it cannot converge elsewhere.
4949

50+
`pages: {}` is refused as well: the update endpoint rejects an empty body, so the mapping configured nothing, and the pre-release v3 builds only noted it at plan time. The message names the three intents it could have meant: declare a field, declare `pages: null` to disable the site, or remove the section.
51+
5052
For `@vivswan/github-settings-as-code` consumers, `source.path` under `SectionInput<"pages">` (and so `SettingsFile`) is `"/" | "/docs"`, where the pre-release v3 builds typed `string`; another path there stops compiling.
5153

5254
Fix: delete the reported field from the file, and publish from `/` or `/docs`.

‎src/sections/pages/index.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,6 @@ export const pagesSection = {
9191
});
9292
return plan;
9393
}
94-
if (Object.keys(desired).length === 0) {
95-
plan.notes.push(
96-
"pages: declared as an empty mapping, which configures nothing (the update endpoint rejects an empty body). Declare at least one field, use pages: null to disable the site, or remove the section",
97-
);
98-
return plan;
99-
}
10094
// The source is split off so the no-source form never carries a source key at all: an own
10195
// `source: undefined` would count as a remainder below.
10296
const { source, ...restConfig } = desired;

‎src/sections/pages/schema.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ const READ_ONLY_SITE_FIELDS = {
3333
"GitHub provisions the certificate for `cname`; declare `cname` and `https_enforced`",
3434
} satisfies Record<ReadOnlySiteField, string>;
3535

36+
/** The refusal for `pages: {}`: each of the three fixes is a different intent, so all three are named. */
37+
const EMPTY_SITE_FIX =
38+
"an empty mapping configures nothing; declare at least one field, declare pages: null to disable the site, or remove the section";
39+
3640
// Not exported: consumers spell it NonNullable<PagesConfig>. The definition id stays "PagesConfig";
3741
// moving it onto the nullable wrapper would change the published schema.
3842
const PagesSite = open({
@@ -44,6 +48,14 @@ const PagesSite = open({
4448
})
4549
.check(
4650
rule((site, refineCtx) => {
51+
// The update endpoint rejects an empty body, so a mapping with no field can only ever be a mistake. A
52+
// library caller's `{ field: undefined }` serializes to the same empty body, so an undefined field is no field.
53+
if (Object.values(site).every((value) => value === undefined)) {
54+
refineCtx.addIssue({
55+
code: "custom",
56+
message: EMPTY_SITE_FIX,
57+
});
58+
}
4759
// The type hides these keys; the open() parse lets them reach here.
4860
for (const [key, fix] of Object.entries(READ_ONLY_SITE_FIELDS)) {
4961
if ((site as Record<string, unknown>)[key] !== undefined) {
@@ -60,7 +72,8 @@ const PagesSite = open({
6072
)
6173
.meta({
6274
id: "PagesConfig",
63-
// The published schema carries the same refusal, so an editor flags the key as the file is written.
75+
// The published schema carries the same refusals, so an editor flags the key or the empty mapping as the file is written.
76+
minProperties: 1,
6477
not: { anyOf: Object.keys(READ_ONLY_SITE_FIELDS).map((key) => ({ required: [key] })) },
6578
});
6679

‎test/published-schema.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ describe("the published schema and the runtime agree on the shapes the corpus ne
6868
false,
6969
],
7070
["a bad policy value", { rulesets: { _undeclared: "remove", entries: [] } }, false],
71+
// The update endpoint rejects an empty body; minProperties mirrors the parse refusal so an editor flags it too.
72+
["an empty pages mapping", { pages: {} }, false],
73+
["a pages mapping with one field", { pages: { https_enforced: true } }, true],
7174
[
7275
"the nested variables knob, wrapped",
7376
prod({ variables: { _undeclared: "keep", entries: [{ name: "A", value: "1" }] } }),

‎test/sections/pages/pages.test.ts‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,6 @@ describe("pages", () => {
197197
expect(result.ops[0]?.payload).toEqual({ source: { branch: "main", path: "/" } });
198198
});
199199

200-
test("an empty pages mapping is a note, not an empty PUT", async () => {
201-
const api = new MockApi({ [GET]: { data: {} } });
202-
expect(await plan(api, {})).toEqual({
203-
ops: [],
204-
notes: [
205-
"pages: declared as an empty mapping, which configures nothing (the update endpoint rejects an empty body). Declare at least one field, use pages: null to disable the site, or remove the section",
206-
],
207-
drift: [],
208-
});
209-
});
210-
211200
test("a passthrough key named like a prototype member still reaches the second PUT", async () => {
212201
// The create body is a plain object, so an `in` check would see Object.prototype's members and drop such a key from the remainder.
213202
const api = new MockApi({});

‎test/sections/pages/schema.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ describe("a Pages field the update cannot set is refused at parse, naming the ke
5656
]);
5757
});
5858

59+
test("an empty mapping is refused at parse, naming the three intents it could have meant", () => {
60+
// The update endpoint rejects an empty body, so the old plan-time note left a file that ran and configured nothing.
61+
const refusal =
62+
"pages: an empty mapping configures nothing; declare at least one field, declare pages: null to disable the site, or remove the section";
63+
expect(issues({})).toEqual([refusal]);
64+
// A library caller's undefined field is dropped by JSON, so the body GitHub would see is the same empty one.
65+
expect(issues({ https_enforced: undefined })).toEqual([refusal]);
66+
});
67+
5968
test("the fields the update takes parse, and null turns the site off", () => {
6069
expect(
6170
issues({

0 commit comments

Comments
 (0)