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
57 changes: 57 additions & 0 deletions plugins/stewards/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { describe, expect, it } from "vitest";
import {
DEFAULT_LOCAL_PREFIX,
DEFAULT_WAIT_BUDGETS,
DEPENDENCY_BOTS,
depsTargets,
filingTargetOf,
githubStateLabels,
isTerminal,
k8sTargets,
Expand Down Expand Up @@ -328,6 +330,61 @@ describe("ops in the project file", () => {
});
});

describe("the local filing target (BB-367)", () => {
const file = (over: Record<string, unknown>) => parseProjectFile(JSON.stringify({ version: 1, ...over }));

it("resolves a target by its shape and the project's local prefix", () => {
// The regression: a Linear team key and a GitHub repo resolve as before.
expect(filingTargetOf("ENG")).toEqual({ target: "ENG", source: "linear" });
expect(filingTargetOf("acme/web")).toEqual({ target: "acme/web", source: "github" });
// The local prefix, matched case-insensitively; the default is LOCAL.
expect(DEFAULT_LOCAL_PREFIX).toBe("LOCAL");
expect(filingTargetOf("LOCAL")).toEqual({ target: "LOCAL", source: "local" });
expect(filingTargetOf("local")).toEqual({ target: "local", source: "local" });
expect(filingTargetOf("TICKET", "TICKET")).toEqual({ target: "TICKET", source: "local" });
// A team key that is not the prefix is still Linear.
expect(filingTargetOf("LOCAL", "TICKET")).toEqual({ target: "LOCAL", source: "linear" });
});

it("accepts the local prefix as ops.team with no linear.teams, and still checks a non-local key", () => {
const local = file({ linear: { teams: [] }, ops: { targets: [{ kind: "k8s-namespace", name: "prod" }], team: "LOCAL" } });
if (!local.ok) throw new Error(local.error);
expect(local.value.ops.team).toBe("LOCAL");
expect(opsTeam(local.value)).toEqual({ target: "LOCAL", source: "local" });
// A custom prefix is accepted, and `opsTeam` reads it.
const custom = file({ linear: { teams: [] }, localPrefix: "ticket", ops: { targets: [{ kind: "k8s-namespace", name: "prod" }], team: "TICKET" } });
if (!custom.ok) throw new Error(custom.error);
expect(opsTeam(custom.value)).toEqual({ target: "TICKET", source: "local" });
// A non-local key still must be in linear.teams.
expect(file({ linear: { teams: ["ENG"] }, ops: { targets: [{ kind: "k8s-namespace", name: "prod" }], team: "SALES" } })).toMatchObject({ ok: false });
// With no linear.teams and no ops.team, the "needs a filing target" error stands.
expect(file({ linear: { teams: [] }, ops: { targets: [{ kind: "k8s-namespace", name: "prod" }] } })).toMatchObject({ ok: false });
});

it("accepts the local prefix for a project and a role, and still checks a non-local role key", () => {
const project = file({ linear: { teams: [] }, ops: { targets: [{ kind: "k8s-namespace", name: "prod" }], team: "LOCAL" } });
if (!project.ok) throw new Error(project.error);
const roleLocal = parseRolesFile(JSON.stringify({ version: 2, roles: { ops: { team: "LOCAL" } }, holders: { jane: ["ops"] } }));
if (!roleLocal.ok) throw new Error(roleLocal.error);
expect(roleOpsTeam(roleLocal.value.roles["jane"]!, project.value)).toEqual({ target: "LOCAL", source: "local" });
expect(filingTargetProblems(project.value, roleLocal.value, [])).toEqual([]);
const roleLinear = parseRolesFile(JSON.stringify({ version: 2, roles: { ops: { team: "OPS" } }, holders: { jane: ["ops"] } }));
if (!roleLinear.ok) throw new Error(roleLinear.error);
expect(filingTargetProblems(project.value, roleLinear.value, [])).toEqual([expect.stringMatching(/roles\.json jane's ops\.team: OPS is not one of linear\.teams/u)]);
});

it("keeps a Linear team key and a GitHub repo resolving as before", () => {
const linear = file({ linear: { teams: ["ENG"] }, ops: { targets: [{ kind: "k8s-namespace", name: "prod" }], team: "ENG" } });
if (!linear.ok) throw new Error(linear.error);
expect(opsTeam(linear.value)).toEqual({ target: "ENG", source: "linear" });
const github = file({ linear: { teams: [] }, ops: { targets: [{ kind: "k8s-namespace", name: "prod" }], team: "acme/web" } });
if (!github.ok) throw new Error(github.error);
expect(opsTeam(github.value)).toEqual({ target: "acme/web", source: "github" });
expect(filingTargetProblems(github.value, null, ["acme/web"])).toEqual([]);
expect(filingTargetProblems(github.value, null, ["acme/other"])).toEqual([expect.stringMatching(/ops\.team: acme\/web is not in repos\.json/u)]);
});
});

describe("playbooks", () => {
it("parses the four defaults", () => {
for (const [name, text] of [["linear-tickets", LINEAR_TICKETS], ["github-prs", GITHUB_PRS], ["github-reviews", GITHUB_REVIEWS], ["ops-watch", OPS_WATCH]] as const) {
Expand Down
42 changes: 25 additions & 17 deletions plugins/stewards/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,14 @@ export function validTimezone(tz: string): boolean {
export const teamKey = z.string().trim().regex(/^[A-Za-z][A-Za-z0-9]*$/u, "a team key such as ENG").transform((s) => s.toUpperCase());

/**
* A filing target (BB-235): where an ops or dependency watch files its
* tickets, either a Linear team key or a GitHub `owner/repo`. A repo is kept
* as GitHub spells it; a team key is folded to upper case. The source is read
* from the shape; membership in `linear.teams` (checked here) or in the repo
* set (`filingTargetProblems`, where the repo set is known) is a separate
* question. This is what `ops.team` and a role's `ops.team` accept.
* A filing target (BB-235, BB-367): where an ops or dependency watch files
* its tickets — a Linear team key, a GitHub `owner/repo`, or the project's
* local prefix. A repo is kept as GitHub spells it; a team key is folded to
* upper case. The source is read from the shape and the project's
* `localPrefix`; membership in `linear.teams` (checked here, except for the
* local prefix) or in the repo set (`filingTargetProblems`, where the repo
* set is known) is a separate question. This is what `ops.team` and a role's
* `ops.team` accept.
*/
export const filingTargetSchema = z
.string()
Expand All @@ -228,12 +230,16 @@ export const filingTargetSchema = z
return z.NEVER;
});

/** Where a filing target lives: a Linear team or a GitHub repo. */
export type FilingTargetSource = "linear" | "github";
/** Where a filing target lives: a Linear team, a GitHub repo, or this project's local source. */
export type FilingTargetSource = "linear" | "github" | "local";
export type FilingTarget = { target: string; source: FilingTargetSource };

/** The source of a filing target from its shape: a repo has a slash, a team key does not. */
export const filingTargetOf = (target: string): FilingTarget => ({ target, source: REPO_NAME.test(target) ? "github" : "linear" });
/**
* The source of a filing target from its shape and the project's local prefix
* (BB-367): the prefix is the local source, a repo has a slash, anything else
* is a Linear team key.
*/
export const filingTargetOf = (target: string, prefix: string = DEFAULT_LOCAL_PREFIX): FilingTarget => ({ target, source: REPO_NAME.test(target) ? "github" : target.toUpperCase() === prefix.toUpperCase() ? "local" : "linear" });

/** Linear's priorities, as `ops.autoAdopt` names them. */
export const LINEAR_PRIORITIES = ["urgent", "high", "medium", "low"] as const;
Expand Down Expand Up @@ -534,9 +540,10 @@ export const projectFileSchema = z
*/
standingRules: z.array(z.string().trim().min(1).max(400)).max(40).default([]),
/**
* Production to watch: one ops binding per target, which files a Linear
* ticket for each problem it finds and escalates it to the project
* steward. `team` is where tickets are filed (the first of linear.teams
* Production to watch: one ops binding per target, which files a ticket
* for each problem it finds and escalates it to the project steward.
* `team` is where tickets are filed — a Linear team key, a GitHub
* `owner/repo`, or the project's local prefix (the first of linear.teams
* when left out); `context` is the kubeconfig context kubectl reads it
* with; `autoAdopt` names the priorities whose tickets are labelled for
* a steward when filed, without asking.
Expand Down Expand Up @@ -586,8 +593,9 @@ export const projectFileSchema = z
if (file.ops.team === undefined && file.linear.teams.length === 0) ctx.addIssue({ code: "custom", path: ["ops", "team"], message: "ops needs a filing target to file tickets in: set ops.team to a Linear team key, a GitHub owner/repo, or a local unit" });
// A repo target (BB-235) is checked against the repo set by
// `filingTargetProblems`, which the caller runs where repos are known; a
// team key is checked here against the project's own teams.
if (file.ops.team !== undefined && !REPO_NAME.test(file.ops.team) && !file.linear.teams.includes(file.ops.team)) ctx.addIssue({ code: "custom", path: ["ops", "team"], message: `${file.ops.team} is not one of linear.teams, so a ticket filed there would never be stewarded` });
// team key is checked here against the project's own teams. The project's
// local prefix is accepted as the local filing target (BB-367).
if (file.ops.team !== undefined && !REPO_NAME.test(file.ops.team) && file.ops.team.toUpperCase() !== prefix.toUpperCase() && !file.linear.teams.includes(file.ops.team)) ctx.addIssue({ code: "custom", path: ["ops", "team"], message: `${file.ops.team} is not one of linear.teams, so a ticket filed there would never be stewarded` });
});
export type ProjectFile = z.infer<typeof projectFileSchema>;

Expand Down Expand Up @@ -715,10 +723,10 @@ export const depsAlertsPlaybookName = (project: ProjectFile): string => project.
export const backlogPlaybookName = (project: ProjectFile): string => project.playbooks[BACKLOG_PLAYBOOK] ?? BACKLOG_PLAYBOOK;
export const depsPrsPlaybookName = (project: ProjectFile): string => project.playbooks[DEPS_PRS_PLAYBOOK] ?? DEPS_PRS_PLAYBOOK;

/** The Linear team or GitHub repo an ops steward files in, with its source. */
/** The Linear team, GitHub repo or local source an ops steward files in, with its source. */
export const opsTeam = (project: ProjectFile): FilingTarget | null => {
const target = project.ops.team ?? project.linear.teams[0] ?? null;
return target === null ? null : filingTargetOf(target);
return target === null ? null : filingTargetOf(target, project.localPrefix ?? DEFAULT_LOCAL_PREFIX);
};

// ---- migration ---------------------------------------------------------------------
Expand Down
19 changes: 14 additions & 5 deletions plugins/stewards/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,22 @@ export function adoptionQuestion(ticketKey: string, repo: string, alerts: Readon
return `Adopt ${ticketKey}? ${severities.join("/")} Dependabot alert${alerts.length === 1 ? "" : `s (${alerts.length})`} on ${repo}: ${packages.join(", ")} (${ecosystems.join(", ")}), ${fix}${dueDate === null ? "" : `, due ${dueDate}`}.${overdue}`;
}

/** What each choice means, for the item's `how` and the steward's wake. */
export function adoptionHow(dismissAllowed: boolean): string {
/** What each choice means, for the item's `how` and the steward's wake. `local` is a project whose filing target is the local source (BB-367), where the plugin moves the ticket itself because it has no labels. */
export function adoptionHow(dismissAllowed: boolean, local = false): string {
const parts = [
`${ADOPTION_CHOICES.adopt}: the ticket is labelled and moved to Todo, and a ticket steward builds the upgrade.`,
`${ADOPTION_CHOICES.keep}: the ticket stays open and unadopted; the steward moves its due date out, and the plugin asks again when that date arrives.`,
local
? `${ADOPTION_CHOICES.adopt}: the plugin moves the ticket out of Backlog to Ready, and a ticket steward builds the upgrade.`
: `${ADOPTION_CHOICES.adopt}: the ticket is labelled and moved to Todo, and a ticket steward builds the upgrade.`,
local
? `${ADOPTION_CHOICES.keep}: the ticket stays open and unadopted in Backlog; the plugin asks again when the deadline arrives.`
: `${ADOPTION_CHOICES.keep}: the ticket stays open and unadopted; the steward moves its due date out, and the plugin asks again when that date arrives.`,
];
if (dismissAllowed) parts.push(`${ADOPTION_CHOICES.dismiss}: the ticket's alerts are dismissed on GitHub as "vulnerable code is not actually used" and the ticket is closed with the reason.`);
if (dismissAllowed)
parts.push(
local
? `${ADOPTION_CHOICES.dismiss}: the ticket's alerts are dismissed on GitHub as "vulnerable code is not actually used" and the plugin moves the ticket to Canceled.`
: `${ADOPTION_CHOICES.dismiss}: the ticket's alerts are dismissed on GitHub as "vulnerable code is not actually used" and the ticket is closed with the reason.`,
);
return parts.join(" ");
}

Expand Down
14 changes: 9 additions & 5 deletions plugins/stewards/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { z } from "zod";
import { globError } from "./decisions.js";
import { opsTargetKey, type AlertmanagerTarget, type DependabotTarget, type K8sTarget, type OpsTarget, type SentryTarget } from "./ops.js";
import { REPO_NAME } from "./source-contract.js";
import { alertmanagerTargets, DEPENDENCY_BOTS, dependabotRepoSchema, filingTargetSchema, filingTargetOf, k8sTargets, LINEAR_PRIORITIES, playbookName, PROJECT_FILE, roleAlertmanagerSchema, sentrySlug, sentryTargets, STEWARDS_DIR, teamKey, zodMessage, type BindingKind, type FilingTarget, type Parsed, type Playbook, type ProjectFile } from "./config.js";
import { alertmanagerTargets, DEFAULT_LOCAL_PREFIX, DEPENDENCY_BOTS, dependabotRepoSchema, filingTargetSchema, filingTargetOf, k8sTargets, LINEAR_PRIORITIES, playbookName, PROJECT_FILE, roleAlertmanagerSchema, sentrySlug, sentryTargets, STEWARDS_DIR, teamKey, zodMessage, type BindingKind, type FilingTarget, type Parsed, type Playbook, type ProjectFile } from "./config.js";

/** The per-person roles: which watches and the project stewardship a GitHub login's server runs. */
export const ROLES_FILE = `${STEWARDS_DIR}/roles.json`;
Expand Down Expand Up @@ -92,7 +92,7 @@ export const roleOpsSchema = z
k8sNamespaces: z.array(z.string().trim().regex(/^[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?$/u, "a namespace name such as web-production")).max(50).default([]),
/** The kubeconfig context kubectl uses on this person's machine; its current context when left out. */
context: z.string().trim().min(1).max(200).regex(/^[^\s-][^\s]*$/u, "a kubeconfig context name").optional(),
/** Where this person's ops tickets are filed (the first of linear.teams when left out): a Linear team key or a GitHub owner/repo. */
/** Where this person's ops tickets are filed (the first of linear.teams when left out): a Linear team key, a GitHub owner/repo, or the project's local prefix. */
team: filingTargetSchema.optional(),
autoAdopt: z.array(z.enum(LINEAR_PRIORITIES)).max(4).default([]),
sentry: roleSentrySchema,
Expand Down Expand Up @@ -321,10 +321,10 @@ export function rolePullWatches(role: Role | null, repos: readonly string[], dep
return [...synthesized, ...rolePulls(role)];
}

/** The Linear team a role's ops steward files in (the first of linear.teams when the role names none), with its source. */
/** The Linear team, GitHub repo or local source a role's ops steward files in (the first of linear.teams when the role names none), with its source. */
export const roleOpsTeam = (role: Role | null, project: ProjectFile): FilingTarget | null => {
const target = role?.ops?.team ?? project.linear.teams[0] ?? null;
return target === null ? null : filingTargetOf(target);
return target === null ? null : filingTargetOf(target, project.localPrefix ?? DEFAULT_LOCAL_PREFIX);
};

/**
Expand Down Expand Up @@ -563,14 +563,18 @@ export function roleWarnings(roles: RolesFile): string[] {
* that is not in the project's repo set, and a role's Linear team key that is
* not one of `linear.teams`. A project file's team key is checked by the
* schema itself; this adds the repo case the schema cannot see (the repo set
* comes from `repos.json`) and the role cases the schema never checks.
* comes from `repos.json`) and the role cases the schema never checks. The
* project's local prefix (BB-367) is the local filing target, so it needs no
* `linear.teams` entry.
*/
export function filingTargetProblems(project: ProjectFile, roles: RolesFile | null, repos: readonly string[]): string[] {
const out: string[] = [];
const prefix = project.localPrefix ?? DEFAULT_LOCAL_PREFIX;
const repoProblem = (where: string, target: string) => {
if (!repos.some((r) => r.toLowerCase() === target.toLowerCase())) out.push(`${where}: ${target} is not in repos.json, so a ticket filed there would never be stewarded`);
};
const teamProblem = (where: string, target: string) => {
if (target.toUpperCase() === prefix.toUpperCase()) return;
if (!project.linear.teams.includes(target)) out.push(`${where}: ${target} is not one of linear.teams, so a ticket filed there would never be stewarded`);
};
const projectTarget = project.ops.team;
Expand Down
Loading