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
5 changes: 3 additions & 2 deletions plugins/stewards/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ describe("playbooks", () => {
}
});

it("tells the default playbooks to file credentials as provide-secret needs and to escalate build-time blockers", () => {
it("tells the default playbooks to raise credentials as a secret escalation and to escalate build-time blockers", () => {
const linear = parsePlaybook(LINEAR_TICKETS, "linear-tickets");
if (!linear.ok) throw new Error(linear.error);
expect(phaseInstructions(linear.value, "In Progress")).toMatch(/provide-secret/u);
expect(phaseInstructions(linear.value, "In Progress")).toMatch(/steward_report \{ escalate/u);
expect(phaseInstructions(linear.value, "In Progress")).toMatch(/resolution: "secret"/u);
expect(phaseInstructions(linear.value, "In Progress")).toMatch(/writeEnv/u);
expect(phaseInstructions(linear.value, "In Progress")).toMatch(/changes what the ticket needs/u);
expect(phaseInstructions(linear.value, "In Progress")).toMatch(/escalated as a `decision`/u);
Expand Down
23 changes: 22 additions & 1 deletion plugins/stewards/decisions.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { defaultDecideList, parseProjectFile, type BigDecision } from "./config.js";
import { decideFor, decideForEntry, isSelfWidening, SELF_WIDENING_PATHS, touchesAny } from "./decisions.js";
import { decideFor, decideForEntry, decideForPull, decideForRefusal, isSelfWidening, SELF_WIDENING_PATHS, touchesAny } from "./decisions.js";

const decisions: BigDecision[] = [
{ text: "Changing what a ticket's requester asked for", decide: "user", paths: [] },
Expand Down Expand Up @@ -49,6 +49,27 @@ describe("the decide list fails closed", () => {
expect(decideFor([], null, decisions)).toBe("user");
expect(decideFor(["bb-plugins/plugins/linear/foo.ts"], null, decisions)).toBe("user");
});

it("places a pull request by matching its files to a project entry (BB-51)", () => {
// A file under a project entry's paths is the project steward's.
expect(decideForPull(["bb-plugins/plugins/linear/store.ts"], decisions)).toBe("project");
// A user entry, no matching entry, an empty or unreadable file list, and a
// self-widening change are all the user's.
expect(decideForPull(["bb-plugins/plugins/linear/source-contract.ts"], decisions)).toBe("user");
expect(decideForPull(["bb-plugins/plugins/linear/wake.ts"], decisions)).toBe("user");
expect(decideForPull([], decisions)).toBe("user");
expect(decideForPull(null, decisions)).toBe("user");
expect(decideForPull(["bb-plugins/plugins/stewards/store.ts"], decisions)).toBe("user");
});

it("gives the failed files fetch its own refusal text, not the self-widening one (BB-51)", () => {
expect(decideForRefusal(null, "A store migration", decisions)).toBe("the changed files could not be read");
expect(decideForRefusal([], "A store migration", decisions)).toBe("the pull request changes no files");
expect(decideForRefusal(["bb-plugins/plugins/stewards/store.ts"], "A store migration", decisions)).toBe("the change touches the decide list or the code that enforces it");
expect(decideForRefusal(["bb-plugins/plugins/linear/store.ts"], "A store migration", decisions)).toBeNull();
expect(decideForRefusal(["bb-plugins/plugins/linear/wake.ts"], "A store migration", decisions)).toMatch(/do not touch/u);
expect(decideForRefusal(["bb-plugins/plugins/linear/source-contract.ts"], null, decisions)).toBe("no decide-list entry was given");
});
});

describe("no self-widening", () => {
Expand Down
32 changes: 32 additions & 0 deletions plugins/stewards/decisions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,35 @@ export function decideFor(files: readonly string[] | null | undefined, entry: st
if (isSelfWidening(files)) return "user";
return decideForEntry(entry, decisions, files);
}

/**
* The mark for a pull request the plugin has to place itself, from its changed
* files: a `project` entry whose paths they touch is the project steward's,
* and everything else — no matching entry, a `user` entry, an entry with no
* paths, a self-widening change, or a file list that could not be read — is
* the user's (fail closed). This is the rule the project steward's own
* `approve` applies, so the page and the gate agree (BB-51).
*/
export function decideForPull(files: readonly string[] | null | undefined, decisions: readonly BigDecision[]): DecideMark {
if (files === null || files === undefined || files.length === 0) return "user";
if (isSelfWidening(files)) return "user";
const match = decisions.find((d) => d.decide === "project" && d.paths.length > 0 && touchesAny(files, d.paths));
return match === undefined ? "user" : decideForEntry(match.text, decisions, files);
}

/**
* Why a pull request is the user's to approve, in words, or null when a
* project-steward approval may be recorded. The null/empty file list gets its
* own text: it is not the self-widening refusal, it is a failed read (BB-51).
*/
export function decideForRefusal(files: readonly string[] | null | undefined, entry: string | null | undefined, decisions: readonly BigDecision[]): string | null {
if (files === null || files === undefined) return "the changed files could not be read";
if (files.length === 0) return "the pull request changes no files";
if (isSelfWidening(files)) return "the change touches the decide list or the code that enforces it";
const wanted = (entry ?? "").trim();
if (wanted === "") return "no decide-list entry was given";
const found = decisions.find((d) => d.text === wanted || d.text.toLowerCase() === wanted.toLowerCase());
if (found === undefined) return `the entry ${JSON.stringify(entry)} is not in the decide list`;
if (found.paths.length > 0 && !touchesAny(files, found.paths)) return `the changed files do not touch ${JSON.stringify(found.text)}'s paths`;
return found.decide === "project" ? null : `the entry ${JSON.stringify(found.text)} is not marked project`;
}
15 changes: 15 additions & 0 deletions plugins/stewards/reconcile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,21 @@ describe("the pull request gate (build-tickets)", () => {
expect(prGate(project, { projectApproved: false }).needsOwnerApproval).toBe(true);
});

it("makes the decide mark the whole gate: a project entry the project steward, everything else the owner (BB-51)", () => {
const project = pull({ comments: [marker(head, "approved", "owner-reviewed")] }, green);
const projectWaiting = prGate(project, { decision: "project", projectApproved: false });
expect(projectWaiting).toMatchObject({ decision: "project", needsProjectApproval: true, needsOwnerApproval: false, gateMet: false, missing: "project steward approval needed" });
expect(prGate(project, { decision: "project", projectApproved: true })).toMatchObject({ gateMet: true, needsProjectApproval: false });
// A user entry needs the owner, and a project-steward approval is not the gate.
const userWaiting = prGate(project, { decision: "user", projectApproved: false });
expect(userWaiting).toMatchObject({ decision: "user", needsOwnerApproval: true, needsProjectApproval: false, gateMet: false, missing: "owner approval needed" });
expect(prGate(project, { decision: "user", projectApproved: true })).toMatchObject({ projectApproved: false, gateMet: false, needsOwnerApproval: true });
// An owner approval satisfies the user lane, not the project lane.
const ownerApproved = pull({ comments: [marker(head, "approved", "owner-reviewed"), owner("LGTM")] }, green);
expect(prGate(ownerApproved, { decision: "user" }).gateMet).toBe(true);
expect(prGate(ownerApproved, { decision: "project", projectApproved: false }).gateMet).toBe(false);
});

it("fails closed on a lane the plugin cannot read (BB-30)", () => {
// An unknown lane is treated as owner-reviewed, not no-review.
const unknown = prGate(pull({ comments: [marker(head, "approved", "something-else")] }, green));
Expand Down
14 changes: 11 additions & 3 deletions plugins/stewards/reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,10 @@ export type PrGate = {
gateMet: boolean;
/** What is missing, in words; null when the gate is met. */
missing: string | null;
/** The plugin's `approve-pr` condition: owner lane, marker approved, checks green, no owner approval yet. */
/** The plugin's `approve-pr` condition: user lane, marker approved, checks green, no owner approval yet. */
needsOwnerApproval: boolean;
/** The project steward's side: a `decide: project` entry, marker approved, checks green, no project approval yet. */
needsProjectApproval: boolean;
/** The decide mark applied to this pull request; `user` unless the project steward classified it under a `project` entry. */
decision: "project" | "user";
/** Whether the project steward recorded an approval bound to this head sha. */
Expand All @@ -354,9 +356,14 @@ export function prGate(entity: EntitySnapshot, opts: PrGateOptions = {}): PrGate
// Only an explicit `no-review` marker skips the owner; a marker the plugin
// cannot read, or an unknown lane, is the user's (fail closed).
const ownerLane = lane !== "no-review";
// The gate belongs to whoever the decide list names. An explicit decision
// wins; without one, a recorded project approval means the project steward
// (a project approval is only ever recorded for a `project` entry), and
// everything else is the user's (fail closed).
const decision: "project" | "user" = opts.decision === "project" || (opts.decision === undefined && opts.projectApproved === true) ? "project" : "user";
const projectApproved = opts.projectApproved === true && decision === "project";
const approved = owner.approved || projectApproved;
const approved = decision === "project" ? projectApproved : owner.approved;
const approvalNeeded = ownerLane && markerApproved && checksGreen && !approved;
const gateMet = pull !== null && markerApproved && checksGreen && (!ownerLane || approved);
const missing = gateMet
? null
Expand All @@ -383,7 +390,8 @@ export function prGate(entity: EntitySnapshot, opts: PrGateOptions = {}): PrGate
ownerApprovalAt: owner.at,
gateMet,
missing,
needsOwnerApproval: ownerLane && markerApproved && checksGreen && !approved,
needsOwnerApproval: approvalNeeded && decision === "user",
needsProjectApproval: approvalNeeded && decision === "project",
decision,
projectApproved,
};
Expand Down
4 changes: 2 additions & 2 deletions plugins/stewards/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import { redactLine, type FingerprintRow, type OpsTarget, type SentryTarget, typ
import type { Change } from "./reconcile.js";

export const SENTRY_API_URL = "https://sentry.io";
/** The look's error when the plugin has no Sentry token to read with; the steward files the provide-secret need for it. */
/** The look's error when the plugin has no Sentry token to read with; the steward escalates for it. */
export const NO_TOKEN = "no Sentry token is configured";
/** The setting the token lives in, named so the steward's provide-secret need says where it goes. */
/** The setting the token lives in, named so the steward's escalation says where it goes. */
export const TOKEN_SETTING = "sentryToken";
/** Where a Sentry user creates an auth token. */
export const TOKEN_PAGE = "https://sentry.io/settings/account/api/auth-tokens/";
Expand Down
Loading
Loading