Skip to content

Commit cb0b25c

Browse files
committed
refactor(sections): trim the factory's secret facet to the one shape a section declares; the guard runs before a section's live conflicts
SecretPath admits a dotted path under mapping only, where both carriers declare the unverifiable facet; the top-level arm rode update and no section used it. The snapshot reference id drops the multi-secret suffix and the store is the noun itself. missingDrift and unreadableSecretNote (one caller each) are inlined; Remedies.value is a string suffix again; liveByIdentity's describe defaults to the key and the call sites that repeated it drop the argument; custom_properties and secret_scanning_custom_patterns guard through one closure each; the secrets engine's listSecretValues goes through secretValuesOf. The live-duplicate guard now runs before a section's own live conflicts, so two deploy keys under one title are reported before a colliding holder's material. The upgrade doc's duplicate-webhook example carries the hook ids the guard prints, and the two #204 snapshot scenarios pin the SECRET_WEBHOOK_<id> placeholder.
1 parent d38309a commit cb0b25c

15 files changed

Lines changed: 115 additions & 212 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ Snapshot targets ride the same seal now: a private target's notes, file path, an
194194
v2 picked one silently (the factory sections refused only a claimed pair; milestones, rulesets, webhooks, custom properties, secret scanning patterns, environment secrets and variables kept the last one listed). v3 refuses every list section the same way, whether or not the settings file declares the pair:
195195

196196
```text
197-
webhooks: GitHub holds webhooks that resolve to one identity: "https://ci.example.com/hook" and "https://ci.example.com/hook". This section manages one webhook per identity, so it cannot tell them apart; delete all but one of each on GitHub, then run again
197+
webhooks: GitHub holds webhooks that resolve to one identity: "https://ci.example.com/hook (hook id 11)" and "https://ci.example.com/hook (hook id 12)". This section manages one webhook per identity, so it cannot tell them apart; delete all but one of each on GitHub, then run again
198198
```
199199

200200
Snapshot says the same. Delete the duplicates on GitHub, then re-run.

‎src/sections/contract/live.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function liveByIdentity<T, Key extends string>(
2525
noun: string,
2626
items: readonly T[],
2727
keyOf: (item: T) => Key,
28-
describe: (item: T) => string,
28+
describe: (item: T) => string = keyOf,
2929
): Map<Key, T> {
3030
const collisions = collidingPairs(items, keyOf, describe);
3131
if (collisions.length > 0) {

‎src/sections/contract/module.ts‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -693,31 +693,17 @@ export function undeclaredNote(opts: {
693693
return `${opts.subject} ${state} in the settings file; kept under "_undeclared: keep" - add ${add} to the settings file to manage ${manage}, or set "_undeclared: delete" to have apply ${opts.action}`;
694694
}
695695

696-
/**
697-
* The drift line for a declared resource the live side lacks. `where` names the home when "the repo"
698-
* understates it (a nested list says "the environment"); `action` when apply does more than create it.
699-
*/
700-
export function missingDrift(
701-
label: string,
702-
opts: { where?: string; action?: string } = {},
703-
): string {
704-
const where = opts.where ?? "the repo";
705-
const action = opts.action ?? "create it";
706-
return `${label}: missing - declared in the settings file but not on ${where}; apply will ${action}`;
707-
}
708-
709696
/**
710697
* The drift line for a field whose live value differs, operands always in this order: declared first,
711-
* live second. Both arrive rendered (JSON.stringify, or a section's own spelling such as "unset");
712-
* `remedy: null` drops the clause when a generic line beside it names the remedy.
698+
* live second. Both arrive rendered (JSON.stringify, or a section's own spelling such as "unset").
713699
*/
714700
export function valueDrift(
715701
label: string,
716702
declared: string,
717703
live: string,
718-
remedy: string | null = "apply will set the declared value",
704+
remedy = "apply will set the declared value",
719705
): string {
720-
return `${label}: declared ${declared} != live ${live}${remedy === null ? "" : `; ${remedy}`}`;
706+
return `${label}: declared ${declared} != live ${live}; ${remedy}`;
721707
}
722708

723709
/**

‎src/sections/custom_properties/index.ts‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { liveByIdentity, parseLive } from "../contract/live.js";
1010
import {
1111
defaultUndeclaredPolicy,
1212
loosen,
13+
type SectionMeta,
1314
type SectionModule,
1415
undeclaredDrift,
1516
undeclaredNote,
@@ -110,6 +111,13 @@ const LiveProperty = z.looseObject({
110111
value: z.union([z.string(), z.array(z.string()), z.null()]),
111112
});
112113

114+
function propertiesByName(
115+
section: SectionMeta,
116+
live: readonly z.infer<typeof LiveProperty>[],
117+
): Map<string, z.infer<typeof LiveProperty>> {
118+
return liveByIdentity(section, "custom property", live, (p) => p.property_name);
119+
}
120+
113121
interface PendingUpdate {
114122
readonly property_name: string;
115123
readonly value: WireValue;
@@ -156,13 +164,7 @@ export const customPropertiesSection = {
156164
}
157165
// Not paginated upstream: one GET carries every value.
158166
const live = parseLive(this, ENDPOINTS.list, z.array(LiveProperty), await ctx.read.list.call());
159-
const liveByName = liveByIdentity(
160-
this,
161-
"custom property",
162-
live,
163-
(p) => p.property_name,
164-
(p) => p.property_name,
165-
);
167+
const liveByName = propertiesByName(this, live);
166168
const declaredNames = new Set(desired.map((p) => p.property_name));
167169

168170
// A live null and an absent live entry both mean "unset".
@@ -245,13 +247,7 @@ export const customPropertiesSection = {
245247
};
246248
}
247249
const live = parseLive(this, ENDPOINTS.list, z.array(LiveProperty), await ctx.read.list.call());
248-
liveByIdentity(
249-
this,
250-
"custom property",
251-
live,
252-
(p) => p.property_name,
253-
(p) => p.property_name,
254-
);
250+
propertiesByName(this, live);
255251
const set = live.flatMap((property) => {
256252
if (property.value === null) {
257253
return [];

‎src/sections/deploy_keys/deploy_keys.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ describe("deploy_keys conflicts", () => {
113113
await expect(plan(api, [])).rejects.toThrow(/resolve to one identity/);
114114
expect(api.mutations()).toEqual([]);
115115
});
116+
117+
test("the guard runs before the section's live conflicts: a duplicated title wins over a colliding holder's material", async () => {
118+
const api = new MockApi({
119+
[LIST]: { data: [liveKey(7, "dup", BOT_KEY), liveKey(8, "dup", MIRROR_KEY)] },
120+
});
121+
await expect(plan(api, [{ title: "other", key: BOT_KEY }])).rejects.toThrow(
122+
'deploy_keys: GitHub holds deploy keys that resolve to one identity: "dup (key id 7)" and "dup (key id 8)". ' +
123+
"This section manages one deploy key per identity, so it cannot tell them apart; delete all but one of each on GitHub, then run again",
124+
);
125+
expect(api.mutations()).toEqual([]);
126+
});
116127
});
117128

118129
describe("deploy_keys loud live extraction", () => {

‎src/sections/environments/branch-policies.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,8 @@ export async function planBranchPolicies(
139139
} else if (liveEnv !== undefined) {
140140
live = await listBranchPolicies(ctx, section, envName);
141141
}
142-
const liveByName = liveByIdentity(
143-
section,
144-
"deployment branch policy",
145-
live,
146-
(pattern) => livePolicyName(pattern, envName),
147-
(pattern) => livePolicyName(pattern, envName),
142+
const liveByName = liveByIdentity(section, "deployment branch policy", live, (pattern) =>
143+
livePolicyName(pattern, envName),
148144
);
149145
const declared = new Set(entries.map((pattern) => pattern.name));
150146

‎src/sections/environments/protection-rules.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ export async function planProtectionRules(
149149
"deployment protection rule",
150150
live.filter((rule) => rule.enabled !== false),
151151
(rule) => liveRuleSlug(rule, envName),
152-
(rule) => liveRuleSlug(rule, envName),
153152
);
154153
const declared = new Set(entries.map((rule) => rule.app));
155154
const planned: NestedPlan = { ops: [], notes: [] };

‎src/sections/environments/snapshot.ts‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,8 @@ export async function snapshotNested(
134134
() => listBranchPolicies(ctx, section, envName),
135135
);
136136
if ("value" in policies && policies.value.length > 0) {
137-
liveByIdentity(
138-
section,
139-
"deployment branch policy",
140-
policies.value,
141-
(policy) => String(policy.name),
142-
(policy) => String(policy.name),
137+
liveByIdentity(section, "deployment branch policy", policies.value, (policy) =>
138+
String(policy.name),
143139
);
144140
nested.deployment_branch_policies = wrapped(
145141
"deployment_branch_policies",
@@ -155,12 +151,8 @@ export async function snapshotNested(
155151
);
156152
if ("value" in rules) {
157153
const enabled = rules.value.filter((rule) => rule.enabled !== false);
158-
liveByIdentity(
159-
section,
160-
"deployment protection rule",
161-
enabled,
162-
(rule) => liveRuleSlug(rule, envName),
163-
(rule) => liveRuleSlug(rule, envName),
154+
liveByIdentity(section, "deployment protection rule", enabled, (rule) =>
155+
liveRuleSlug(rule, envName),
164156
);
165157
if (enabled.length > 0) {
166158
nested.deployment_protection_rules = wrapped(

‎src/sections/secret_scanning_custom_patterns/index.ts‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { liveByIdentity, parseLive } from "../contract/live.js";
1313
import {
1414
defaultUndeclaredPolicy,
1515
loosen,
16+
type SectionMeta,
1617
type SectionModule,
1718
undeclaredDrift,
1819
undeclaredNote,
@@ -132,6 +133,13 @@ function matches(declaredValue: string | string[], liveValue: unknown): boolean
132133
return JSON.stringify(liveComparable) === JSON.stringify(declaredValue);
133134
}
134135

136+
function patternsByName<T extends { name: string }>(
137+
section: SectionMeta,
138+
live: readonly T[],
139+
): Map<string, T> {
140+
return liveByIdentity(section, "secret scanning custom pattern", live, (p) => p.name);
141+
}
142+
135143
const key = "secret_scanning_custom_patterns";
136144

137145
export const secretScanningPatternsSection = {
@@ -168,13 +176,7 @@ export const secretScanningPatternsSection = {
168176
z.array(LivePatternEntry),
169177
await ctx.read.list.listAll(),
170178
).map(liveFrom);
171-
const liveByName = liveByIdentity(
172-
this,
173-
"secret scanning custom pattern",
174-
live,
175-
(p) => p.name,
176-
(p) => p.name,
177-
);
179+
const liveByName = patternsByName(this, live);
178180
const declaredNames = new Set(desired.map((p) => p.name));
179181

180182
const plan: SectionPlan<PlannedOp<typeof ENDPOINTS>> = { ops: [], notes: [], drift: [] };
@@ -276,13 +278,7 @@ export const secretScanningPatternsSection = {
276278
if (live.length === 0) {
277279
return { value: undefined, notes: [] };
278280
}
279-
liveByIdentity(
280-
this,
281-
"secret scanning custom pattern",
282-
live,
283-
(p) => p.name,
284-
(p) => p.name,
285-
);
281+
patternsByName(this, live);
286282
const entries = live.map((pattern) => projectOntoSchema(SecretScanningPatternConfig, pattern));
287283
return { value: knobbedSnapshot(this, entries), notes: [] };
288284
},

0 commit comments

Comments
 (0)