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
23 changes: 22 additions & 1 deletion plugins/stewards/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,14 @@ describe("roles.json (BB-105)", () => {
expect(roles({ jane: { ops: { alertmanager: { projects: [] } } } })).toMatchObject({ ok: false });
});

it("parses an optional slack handle per holder, and still refuses an unknown key beside it (BB-354)", () => {
const parsed = roles({ jane: { projectSteward: true, slack: "jane-doe" }, bob: {} });
if (!parsed.ok) throw new Error(parsed.error);
expect(parsed.value.roles["jane"]!.slack).toBe("jane-doe");
expect(parsed.value.roles["bob"]!.slack).toBeUndefined();
expect(roles({ jane: { slack: "jane", unknown: true } })).toMatchObject({ ok: false });
});

it("rejects unknown keys, a bad version, and two keys naming one login ignoring case", () => {
expect(roles({ jane: { projectSteward: true } }, { extra: true })).toMatchObject({ ok: false });
expect(roles({ jane: { unknown: true } })).toMatchObject({ ok: false });
Expand Down Expand Up @@ -807,7 +815,7 @@ describe("roles.json (BB-105)", () => {
});

describe("roles.json version 2 (BB-163)", () => {
const v2 = (definitions: Record<string, unknown>, holders: Record<string, string[]>) => parseRolesFile(JSON.stringify({ version: 2, roles: definitions, holders }));
const v2 = (definitions: Record<string, unknown>, holders: Record<string, unknown>) => parseRolesFile(JSON.stringify({ version: 2, roles: definitions, holders }));
const project = () => {
const parsed = parseProjectFile(JSON.stringify({ version: 1, linear: { teams: ["ENG"] }, ops: { targets: [{ kind: "k8s-namespace", name: "web-production" }] } }));
if (!parsed.ok) throw new Error(parsed.error);
Expand All @@ -834,6 +842,19 @@ describe("roles.json version 2 (BB-163)", () => {
expect(roleDepsTargets(parsed.value.roles["bob"]!, ["acme/web", "acme/other"])).toEqual(roleDepsTargets(role, ["acme/web", "acme/other"]));
});

it("parses a holder's slack field in the object form, keeps the array form, and refuses an unknown key beside it (BB-354)", () => {
const parsed = v2({ ops: {} }, { jane: { roles: ["ops", "projectSteward"], slack: "jane-doe" } });
if (!parsed.ok) throw new Error(parsed.error);
expect(parsed.value.holders["jane"]).toEqual(["ops", "projectSteward"]);
expect(parsed.value.roles["jane"]!.slack).toBe("jane-doe");
// The array form still parses, with no slack.
const array = v2({ ops: {} }, { jane: ["ops"] });
if (!array.ok) throw new Error(array.error);
expect(array.value.roles["jane"]!.slack).toBeUndefined();
// Strict: an unknown key beside roles/slack is refused.
expect(v2({ ops: {} }, { jane: { roles: ["ops"], handle: "jane" } })).toMatchObject({ ok: false });
});

it("gives the same bindings, target keys and check result as the equivalent version 1 file", () => {
const entry = { k8sNamespaces: ["prod"], context: "bc-prod", team: "ENG", sentry: { projects: [{ org: "acme", project: "web" }] }, dependabot: ["acme/web"] };
const v1 = parseRolesFile(JSON.stringify({ version: 1, roles: { jane: { projectSteward: true, ops: entry } } }));
Expand Down
3 changes: 2 additions & 1 deletion plugins/stewards/migrations.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,6 @@
"5ce83ed91d277d54c92d9722cea30c9c635e93f80fa3f0bbe8fe6bbe83ca20f5",
"960b74073ed57dfd5c0e9787ddb82d50dbecb4c9420b0acd6ba005403c30dcd4",
"78088b44aa3583bf9a73552a82c0660b7200565a0f32b2bcd38d943c237e28ab",
"d3db22aa9869100ea841b401687e04d65c80bc1c85d9cddde4517fc8ce1f1396"
"d3db22aa9869100ea841b401687e04d65c80bc1c85d9cddde4517fc8ce1f1396",
"7aeaa3687ba3841c531e7afb23806798f6d4615177ea4896eb5ce383636587f2"
]
19 changes: 18 additions & 1 deletion plugins/stewards/non-actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,31 @@ describe("the declining branches record their reason (BB-335)", () => {
written(t, b, "needs-decline-closed");
});

it("wait-slack-unsent: an Ask on Slack that sent nothing", async () => {
const t = await setup();
const b = create(t);
const need = t.hooks.store.fileNeed({
bindingId: b.id,
kind: "decide",
url: "acme/web#9",
what: "The review wait is overdue.",
how: "Choose.",
data: { waitBudget: true, choices: ["Review it yourself", "Reassign", "Keep waiting", "Ask on Slack"], waitKind: "review", waitRef: "acme/web#9", waitSlackCandidates: ["arashout"] },
filedBy: "plugin",
});
const out = await t.harness.behavior.runCli(["wait-slack-refused", need.id, "--candidates", "arashout"]);
expect(out.exitCode).toBe(0);
written(t, b, "wait-slack-unsent");
});

it("covers every code in the closed table", () => {
// The table and the branch tests cannot drift: every code has a test above,
// and no test names a code the table does not have.
const tested: NonActionCode[] = [
"config-unreadable", "project-disabled", "no-playbook", "entity-unreadable", "entity-gone", "stay-ended", "other-binding-open",
"ending-in-progress", "follow-through-ran-out", "nothing-to-do", "deferred", "worker-busy", "pull-foreign-owner", "pull-closed",
"merge-held", "retarget-needed", "merge-steward-filed", "worker-running", "merge-paused", "parked-owns-another", "base-not-default",
"merge-refused", "head-moved", "gate-not-met", "binding-ended-closed", "needs-decline-closed",
"merge-refused", "head-moved", "gate-not-met", "binding-ended-closed", "needs-decline-closed", "wait-slack-unsent",
];
expect([...tested].sort()).toEqual([...NON_ACTION_CODES].sort());
});
Expand Down
4 changes: 4 additions & 0 deletions plugins/stewards/non-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export const NON_ACTION_CODES = [
// The owner's two named cases.
"binding-ended-closed",
"needs-decline-closed",
// BB-354: an Ask on Slack that sent nothing (Slack unavailable, or the
// person not found).
"wait-slack-unsent",
] as const;

export type NonActionCode = (typeof NON_ACTION_CODES)[number];
Expand Down Expand Up @@ -92,6 +95,7 @@ const NON_ACTION_TEXT: Record<NonActionCode, (detail: string | null, day: string
"gate-not-met": (d) => `The merge gate${d === null ? "" : ` for ${d}`} is not met`,
"binding-ended-closed": (d) => `Closed because the binding ended (${d ?? "ended"})`,
"needs-decline-closed": (d) => `Closed the spec approval because the spec was declined${d === null ? "" : ` (${d})`}`,
"wait-slack-unsent": (d) => `Ask on Slack sent nothing: the person was not found or Slack was unavailable${d === null ? "" : ` (${d})`}`,
};

/** The UTC day (YYYY-MM-DD) a moment falls in, so a reason's date rolls at midnight UTC. */
Expand Down
34 changes: 25 additions & 9 deletions plugins/stewards/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ export const roleOpsSchema = z
.strict();
export type RoleOps = z.infer<typeof roleOpsSchema>;

/** One person's role: whether their server is the project steward, and the watches it runs. */
export const roleSchema = z.object({ projectSteward: z.boolean().default(false), ops: roleOpsSchema.optional() }).strict();
/** One person's role: whether their server is the project steward, the watches it runs, and their Slack name (BB-354). */
export const roleSchema = z
.object({ projectSteward: z.boolean().default(false), ops: roleOpsSchema.optional(), slack: z.string().trim().min(1).max(100).optional() })
.strict();
export type Role = z.infer<typeof roleSchema>;

/** The built-in role name for the project steward. It has no `roles` definition. */
Expand Down Expand Up @@ -166,13 +168,24 @@ const rolesFileV1Schema = rolesFileV1RawSchema.transform((file, ctx): RolesFile
return { version: 1, roles, holders, definitions };
});

/**
* A version 2 holder entry: the role names it holds, or an object carrying
* those names and the person's Slack handle (BB-354). The array form is the
* original shape and still parses; the object form is strict, so an unknown
* key beside `roles`/`slack` is refused.
*/
const holderEntrySchema = z.union([
z.array(roleNameSchema).min(1).max(50),
z.object({ roles: z.array(roleNameSchema).min(1).max(50), slack: z.string().trim().min(1).max(100).optional() }).strict(),
]);

const rolesFileV2RawSchema = z
.object({
version: z.literal(2),
/** The named role definitions: today's `roleOpsSchema` body, once per name. */
roles: z.record(roleNameSchema, roleOpsSchema),
/** login -> the role names it holds. `projectSteward` is a built-in name. */
holders: z.record(z.string(), z.array(roleNameSchema).min(1).max(50)),
/** login -> the role names it holds (or an object with those and a `slack` handle). `projectSteward` is a built-in name. */
holders: z.record(z.string(), holderEntrySchema),
})
.strict()
.superRefine((file, ctx) => {
Expand Down Expand Up @@ -228,16 +241,17 @@ function samePullWatch(a: PullWatch, b: PullWatch): boolean {
return a.repo.toLowerCase() === b.repo.toLowerCase() && a.playbook === b.playbook && JSON.stringify(a.match) === JSON.stringify(b.match);
}

/** The effective role a holder runs: the merge of the roles it holds, and whether one is `projectSteward`. */
function effectiveRole(names: readonly string[], definitions: Record<string, RoleOps>): Role {
/** The effective role a holder runs: the merge of the roles it holds, whether one is `projectSteward`, and its Slack handle (BB-354). */
function effectiveRole(names: readonly string[], definitions: Record<string, RoleOps>, slack?: string): Role {
const defs = names.filter((name) => name !== PROJECT_STEWARD_ROLE).map((name) => definitions[name]).filter((def): def is RoleOps => def !== undefined);
return { projectSteward: names.includes(PROJECT_STEWARD_ROLE), ...(defs.length === 0 ? {} : { ops: mergeRoleOps(defs) }) };
return { projectSteward: names.includes(PROJECT_STEWARD_ROLE), ...(defs.length === 0 ? {} : { ops: mergeRoleOps(defs) }), ...(slack === undefined ? {} : { slack }) };
}

/** Folds holder logins to lower case and refuses two that name the same login ignoring case. */
const rolesFileV2Schema = rolesFileV2RawSchema.transform((file, ctx): RolesFile => {
const holders: Record<string, string[]> = {};
for (const [key, names] of Object.entries(file.holders)) {
const slack: Record<string, string | undefined> = {};
for (const [key, entry] of Object.entries(file.holders)) {
const parsed = githubLogin.safeParse(key);
if (!parsed.success) {
ctx.addIssue({ code: "custom", path: ["holders", key], message: `${JSON.stringify(key)} is not a GitHub login` });
Expand All @@ -248,10 +262,12 @@ const rolesFileV2Schema = rolesFileV2RawSchema.transform((file, ctx): RolesFile
ctx.addIssue({ code: "custom", path: ["holders", key], message: `${key} names the same login as another entry, ignoring case` });
return z.NEVER;
}
const names = Array.isArray(entry) ? entry : entry.roles;
holders[login] = [...new Set(names)];
slack[login] = Array.isArray(entry) ? undefined : entry.slack;
}
const roles: Record<string, Role> = {};
for (const [login, names] of Object.entries(holders)) roles[login] = effectiveRole(names, file.roles);
for (const [login, names] of Object.entries(holders)) roles[login] = effectiveRole(names, file.roles, slack[login]);
return { version: 2, roles, holders, definitions: file.roles };
});

Expand Down
Loading
Loading