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
29 changes: 29 additions & 0 deletions plugins/stewards/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,35 @@ describe("a Linear ticket steward", () => {
expect(t.hooks.store.open("p1")[0]!.pending).toEqual([]);
});

it("leases only the linked pull requests still open, and one not read yet", async () => {
const t = await setup();
const link = (n: number) => ({ key: `acme/web#${n}`, url: `https://github.com/acme/web/pull/${n}` });
t.entities.set("uuid-12", issue({ state: "In Progress", stateType: "started", linkedPulls: [link(7), link(8), link(9), link(10)] }));
t.entities.set("acme/web#7", pull());
t.entities.set("acme/web#8", pull({ id: "acme/web#8", key: "acme/web#8", state: "closed" }));
t.entities.set("acme/web#9", pull({ id: "acme/web#9", key: "acme/web#9", state: "merged" }, { merged: true }));
// acme/web#10 has no snapshot: it cannot be read yet.
await t.ring("linear", "uuid-12");
const github = t.rpcCalls.filter((c) => c.method === "sourceSetInterest" && c.pluginId === "github-watcher").at(-1)?.input;
expect(github).toEqual({ units: ["acme/web"], entities: ["acme/web#10", "acme/web#7"] });
});

it("stops leasing a linked pull request once it merges", async () => {
const t = await setup();
const linked = { linkedPulls: [{ key: "acme/web#7", url: "https://github.com/acme/web/pull/7" }] };
t.entities.set("uuid-12", issue({ state: "In Progress", stateType: "started", ...linked }));
t.entities.set("acme/web#7", pull());
await t.ring("linear", "uuid-12");
const github = () => t.rpcCalls.filter((c) => c.method === "sourceSetInterest" && c.pluginId === "github-watcher");
expect(github().at(-1)?.input).toEqual({ units: ["acme/web"], entities: ["acme/web#7"] });
const sent = github().length;

t.entities.set("acme/web#7", pull({ state: "merged" }, { merged: true }));
await t.ring("github", "acme/web#7");
expect(github()).toHaveLength(sent + 1);
expect(github().at(-1)?.input).toEqual({ units: ["acme/web"], entities: [] });
});

it("counts a worker woken again (by a watch, a reviewer) as running", async () => {
const t = await setup();
t.entities.set("uuid-12", issue({ state: "In Progress", stateType: "started" }));
Expand Down
34 changes: 32 additions & 2 deletions plugins/stewards/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,23 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
return pulls;
}

/**
* The pull requests linked to a ticket that are leased for it: those
* still open. One with no snapshot yet (not read, or the read failed) is
* kept, so a new pull request is watched before its first read.
*/
function leasedPulls(b: Pick<Binding, "kind" | "lastSnapshot">): string[] {
if (b.kind !== "ticket" || b.lastSnapshot === null) return [];
const pulls = b.lastSnapshot.pulls;
return b.lastSnapshot.entity.linkedPulls
.map((l) => l.key)
.filter((k): k is string => k !== null)
.filter((k) => {
const seen = pulls.find((p) => lc(p.key) === lc(k));
return seen === undefined || !["closed", "merged"].includes(lc(seen.state));
});
}

// The interest set: what linear and github should lease and ring us
// about. Sent whole, only when it changed, a moment after the change.
const lastInterest = new Map<Source, string>();
Expand All @@ -486,7 +503,7 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
entities: [
...new Set([
...open.filter((b) => b.source === "github").map((b) => b.entityId),
...open.flatMap((b) => (b.kind === "ticket" ? (b.lastSnapshot?.entity.linkedPulls ?? []).map((l) => l.key).filter((k): k is string => k !== null) : [])),
...open.flatMap((b) => leasedPulls(b)),
]),
].sort(),
},
Expand Down Expand Up @@ -875,9 +892,22 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
/**
* One reconcile of one binding: snapshot, lifecycle, compare, and wake
* when something actionable changed (or `force` says this is a turn
* regardless: adoption, the nightly pass).
* regardless: adoption, the nightly pass). A linked pull request that
* appeared, closed or merged changes what is leased, so the interest set
* is pushed again.
*/
async function reconcileBinding(bindingId: string, force: WakeReason | null): Promise<void> {
const before = store.binding(bindingId);
const leased = before === null ? "" : leasedPulls(before).join(" ");
try {
await reconcileOnce(bindingId, force);
} finally {
const after = store.binding(bindingId);
if (after !== null && leasedPulls(after).join(" ") !== leased) pushInterest();
}
}

async function reconcileOnce(bindingId: string, force: WakeReason | null): Promise<void> {
let b = store.binding(bindingId);
if (b === null) return;
const config = await loadConfig(b.projectId);
Expand Down
Loading