Public: anyone can read this; the crew can change it.

Pillar labels whose pillar is gone are removed at boot, and the chip row offers only labels in use #14815

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14794-pillar-labels-whose-pillar-is-gone-are-r → main 6 commits fork 2 threads · 2 unresolved

  • Merged. scout/14794-pillar-labels-whose-pillar-is-gone-are-r was merged into main as f365a56.

Review

Sign in to review this change.
5 files changed +186 −17
src/actions/pillars.ts +61 −13
@@ -17,8 +17,10 @@ import type { App } from "../app.ts";
1717 import { addDependency } from "../core/dependencies.ts";
1818 import {
1919 createLabel,
20 deleteLabel,
2021 getIssuesByIds,
2122 getLabel,
23 labelCarried,
2224 labelsFor,
2325 setIssueLabels,
2426 type Issue,
@@ -252,25 +254,71 @@ function syncRepo(app: App, repo: Repo): number {
252254 return changed;
253255 }
254256
255 /**
256 * At boot: label what every umbrella already waits for. This repairs what
257 * is derived, for edges made before pillars had labels, so it announces
258 * nothing, the way reconcile does not. Returns how many issues it changed.
259 */
260 export function syncAllPillars(app: App): number {
261 const repoIds = app.db
257 /** The repositories with an open issue titled like an umbrella, not mirrors. */
258 function plannedRepos(app: App): Repo[] {
259 return app.db
262260 .query<{ repo_id: string }, []>(
263261 `SELECT DISTINCT repo_id FROM issue
264262 WHERE kind = 'issue' AND state = 'open'
265263 AND (title GLOB 'P[0-9]*' OR title LIKE 'Later%')`,
266264 )
267265 .all()
268 .map((row) => row.repo_id);
266 .map((row) => getRepoById(app.db, row.repo_id))
267 .filter((repo): repo is Repo => repo !== undefined && !isMirror(app, repo));
268 }
269
270 /**
271 * At boot: label what every umbrella already waits for. This repairs what
272 * is derived, for edges made before pillars had labels, so it announces
273 * nothing, the way reconcile does not. Returns how many issues it changed.
274 */
275 export function syncAllPillars(app: App): number {
269276 let changed = 0;
270 for (const repoId of repoIds) {
271 const repo = getRepoById(app.db, repoId);
272 if (repo === undefined || isMirror(app, repo)) continue;
273 changed += syncRepo(app, repo);
274 }
277 for (const repo of plannedRepos(app)) changed += syncRepo(app, repo);
275278 return changed;
276279 }
280
281 /** A pillar's label as `carry` makes it: `P9 Talk`, no colon. */
outdated

This pattern is the only guard. It deletes hand-made labels such as P1 urgent and P2 later, with no way to tell them from leftovers. Restrict the delete to labels no issue carries.

282 const PILLAR_LABEL = /^P\d+ [^:]+$/;
283
284 /**
285 * The labels in one repository that name a pillar no open umbrella names
286 * now — left by a renumbering or a pillar closed. Where a repository has
287 * pillars, `P<n> Name` is their vocabulary, whoever made the label or its
288 * colour (a renumbering's leftovers were made both ways); a repository with
289 * none keeps every label.
290 */
291 export function stalePillarLabels(app: App, repo: Repo): Label[] {
292 const names = pillarNames(pillarsOf(app.db, repo));
293 if (names.length === 0) return [];
294 const current = new Set(names.map((n) => n.toLowerCase()));
295 return app.db
296 .query<Label, [string]>(
297 "SELECT id, name, color FROM label WHERE repo_id = ?1 AND name GLOB 'P[0-9]*'",
298 )
299 .all(repo.id)
300 .filter(
301 (l) =>
302 PILLAR_LABEL.test(l.name) &&
303 !current.has(l.name.toLowerCase()),
304 );
305 }
306
307 /**
308 * At boot, after `syncAllPillars`: delete each stale pillar label that no
309 * issue or pull, open or closed, carries. One still carried is left as it
310 * is: the name alone cannot tell a renumbering's leftover from a hand-made
311 * `P1 urgent`, and taking it off would lose what a person put there. So no
312 * issue changes and nothing is announced; returns the names deleted,
313 * `owner/repo: name`, for the caller to say once.
314 */
315 export function pruneStalePillarLabels(app: App): string[] {
316 const gone: string[] = [];
317 for (const repo of plannedRepos(app))
outdated

This strips the label from every issue that carries it, silently and irreversibly, and announces nothing. Do not drop issue_label rows on a name match.

318 for (const label of stalePillarLabels(app, repo)) {
319 if (labelCarried(app.db, label.id)) continue;
320 if (deleteLabel(app.db, repo.id, label.id))
321 gone.push(`${repo.ownerName}/${repo.name}: ${label.name}`);
322 }
323 return gone;
324 }
src/core/issues.ts +28
@@ -1026,6 +1026,34 @@ export function openLabelUse(db: Database, repoId: string): Map<string, LabelUse
10261026 return new Map(rows.map((r) => [r.label_id, { issues: r.issues, pulls: r.pulls, all: r.n }]));
10271027 }
10281028
1029 /**
1030 * The ids of the labels some issue in `state` carries, sealed or standing
1031 * alike: what an issues list of that state could narrow to (#14794).
1032 */
1033 export function issueLabelsIn(
1034 db: Database,
1035 repoId: string,
1036 state: "open" | "closed" | "all",
1037 ): Set<string> {
1038 const rows = db
1039 .query<{ label_id: string }, [string, string]>(
1040 `SELECT DISTINCT il.label_id
1041 FROM label l
1042 JOIN issue_label il ON il.label_id = l.id
1043 JOIN issue ON issue.id = il.issue_id AND issue.kind = 'issue'
1044 WHERE l.repo_id = ?1 AND (?2 = 'all' OR issue.state = ?2)`,
1045 )
1046 .all(repoId, state);
1047 return new Set(rows.map((r) => r.label_id));
1048 }
1049
1050 /** Whether any issue or pull, in any state, carries the label. */
1051 export function labelCarried(db: Database, labelId: string): boolean {
1052 return (
1053 db.query("SELECT 1 FROM issue_label WHERE label_id = ?1 LIMIT 1").get(labelId) !== null
1054 );
1055 }
1056
10291057 /** The repository's labels, each with `open` (its open count) when `withCounts`. */
10301058 export function labelsWithOpen(
10311059 db: Database,
src/main.ts +5 −1
@@ -31,7 +31,7 @@ import { loadTrustedHosts } from "./origins/trusted-hosts.ts";
3131 import { flushFlagUses, loadFlags } from "./core/flags.ts";
3232 import { reconcile } from "./core/reconcile.ts";
3333 import { offerStartingBoards } from "./actions/boards.ts";
34 import { syncAllPillars } from "./actions/pillars.ts";
34 import { pruneStalePillarLabels, syncAllPillars } from "./actions/pillars.ts";
3535 import { bootHostedChildren, childAwake } from "./spawn/host.ts";
3636 import { announceSandboxed } from "./spawn/sandboxed.ts";
3737 import { keepSandboxing } from "./spawn/move_cell.ts";
@@ -440,6 +440,10 @@ await reconcile(app);
440440 // Issues linked under a pillar's umbrella before pillars had labels.
441441 const labelled = syncAllPillars(app);
442442 if (labelled > 0) app.log.info("pillars labelled", { issues: labelled });
443 // Labels of pillars no umbrella names now: a renumbering's leftovers (#14794).
444 const unpillared = pruneStalePillarLabels(app);
445 if (unpillared.length > 0)
446 app.log.info("stale pillar labels deleted", { labels: unpillared.join(", ") });
443447 // The host apps answer at, resolved once and before the listener opens: an
444448 // app host never reaches the forge's own router, so `serve` needs it from the
445449 // first connection (#6726). Only the hostname matters here, not the port.
src/web/studio/lists.ts +18 −2
@@ -16,6 +16,7 @@ import {
1616 labelsForIssues,
1717 listIssues,
1818 listLabels,
19 issueLabelsIn,
1920 type Issue,
2021 type Label,
2122 } from "../../core/issues.ts";
@@ -338,6 +339,20 @@ function facetChips(
338339 </div>`;
339340 }
340341
342 /**
343 * The labels the issues list offers as chips: those some issue of the
344 * state the list reads carries, and the one chosen so it can be cleared. A
345 * label nothing there carries narrows the list to nothing (#14794).
346 */
347 export function carried(
348 labels: readonly Label[],
349 inUse: ReadonlySet<string>,
350 chosen: string,
351 ): Label[] {
352 const picked = chosen.toLowerCase();
353 return labels.filter((l) => inUse.has(l.id) || l.name.toLowerCase() === picked);
354 }
355
341356 /** The word for a tell closed with nothing merged to shut it — `tellWord`
342357 * has none, and a list must not drop the row: closed as done is *done*, and
343358 * dropped or split is *cancelled*, so it is found under shipped or stopped
@@ -476,6 +491,7 @@ export async function tellsList(ctx: Ctx): Promise<Response> {
476491 at = performance.now();
477492 const labels = labelsForIssues(db, fetched.map((issue) => issue.id));
478493 const repoLabels = listLabels(db, repo.id);
494 const offered = carried(repoLabels, issueLabelsIn(db, repo.id, fetchState(f)), f.label);
479495 notePhase(ctx.request, "labels", performance.now() - at);
480496 at = performance.now();
481497 const rows = fetched
@@ -569,7 +585,7 @@ export async function tellsList(ctx: Ctx): Promise<Response> {
569585 verbs,
570586 record === undefined ? PLAIN_FILTERS : WORD_FILTERS,
571587 )}
572 ${facetChips(listBase, f, repoLabels)} ${list}
588 ${facetChips(listBase, f, offered)} ${list}
573589 ${pager(
574590 fetched.length === LIST_PAGE && lastNumber !== undefined
575591 ? { next: facetHref(listBase, f, {}, lastNumber) }
@@ -591,7 +607,7 @@ export async function tellsList(ctx: Ctx): Promise<Response> {
591607 name: `${repo.ownerName}/${repo.name}`,
592608 signedIn: ctx.auth.user !== undefined,
593609 reports: waiting,
594 labels: repoLabels.map((l) => l.name),
610 labels: offered.map((l) => l.name),
595611 }),
596612 ],
597613 body,
test/pillars.test.ts +74 −1
@@ -15,7 +15,7 @@ import { createApp, type App } from "../src/app.ts";
1515 import { addToCrew, createAgent } from "../src/actions/crew.ts";
1616 import { linkIssue, unlinkIssue } from "../src/actions/dependencies.ts";
1717 import { openIssue, setIssueState } from "../src/actions/issues.ts";
18 import { syncAllPillars } from "../src/actions/pillars.ts";
18 import { pruneStalePillarLabels, syncAllPillars } from "../src/actions/pillars.ts";
1919 import { mergePull, openPull } from "../src/actions/pulls.ts";
2020 import {
2121 commitToBranch,
@@ -364,6 +364,79 @@ describe("pillars", () => {
364364 expect(events("issue.labeled")).toBe(said);
365365 });
366366
367 test("at boot, a stale pillar label goes only when nothing carries it: hand-made P labels and carried ones stay on their issues", () => {
368 const retitle = (title: string) =>
369 app.db.query("UPDATE issue SET title = ?1 WHERE id = ?2").run(title, feed.id);
370 const labelNames = () => listLabels(app.db, repo.id).map((l) => l.name);
371 const labelIds = (issue: Issue) => labelsFor(app.db, issue.id).map((l) => l.id);
372 // A hand-made label in the pillars' shape, on an issue a person chose.
373 const urgent = createLabel(app, alice, fresh(), "P1 urgent", "aa0000");
374 if (!urgent.ok) throw new Error(urgent.error);
375 const before = labelIds(loose);
376 setIssueLabels(app.db, loose.id, [...before, urgent.value.id]);
377 // Nothing is stale while the umbrellas name every pillar label.
378 expect(pruneStalePillarLabels(app)).toEqual([]);
379 try {
380 retitle("P1 Stream: one front door");
381 syncAllPillars(app);
382 // The old name is still on the issue that served it: kept, not stripped.
383 expect(pruneStalePillarLabels(app)).toEqual([]);
384 expect(names(synced)).toEqual(["P1 Feed", "P1 Stream"]);
385 expect(names(loose)).toContain("P1 urgent");
386 // Once nothing carries it, the old label is deleted; the hand-made one, carried, stays.
387 const feedLabel = listLabels(app.db, repo.id).find((l) => l.name === "P1 Feed")!;
388 const carriers = app.db
389 .query<{ id: string }, [string]>("SELECT issue_id AS id FROM issue_label WHERE label_id = ?1")
390 .all(feedLabel.id)
391 .map((r) => r.id);
392 for (const id of carriers)
393 setIssueLabels(app.db, id, labelsFor(app.db, id).filter((l) => l.id !== feedLabel.id).map((l) => l.id));
394 expect(pruneStalePillarLabels(app)).toEqual(["alice/engine: P1 Feed"]);
395 expect(labelNames()).not.toContain("P1 Feed");
396 expect(labelNames()).toContain("P1 Stream");
397 expect(labelNames()).toContain("P1 urgent");
398 expect(labelNames()).toContain("bug");
399 expect(pruneStalePillarLabels(app)).toEqual([]);
400 } finally {
401 retitle("P1 Feed: one front door");
402 syncAllPillars(app);
403 const stream = listLabels(app.db, repo.id).find((l) => l.name === "P1 Stream");
404 if (stream !== undefined)
405 for (const r of app.db
406 .query<{ id: string }, [string]>("SELECT issue_id AS id FROM issue_label WHERE label_id = ?1")
407 .all(stream.id))
408 setIssueLabels(app.db, r.id, labelsFor(app.db, r.id).filter((l) => l.id !== stream.id).map((l) => l.id));
409 pruneStalePillarLabels(app);
410 setIssueLabels(app.db, loose.id, before);
411 pruneStalePillarLabels(app);
412 }
413 expect(labelNames()).not.toContain("P1 Stream");
414 expect(labelNames()).not.toContain("P1 urgent");
415 expect(names(synced)).toEqual(["P1 Feed"]);
416 });
417
418 test("the issues list offers as chips the labels the issues of its state carry, and the one chosen", async () => {
419 const idle = createLabel(app, alice, fresh(), "idle", "112233");
420 if (!idle.ok) throw new Error(idle.error);
421 const done = createLabel(app, alice, fresh(), "shipped-only", "334455");
422 if (!done.ok) throw new Error(done.error);
423 const old = issue("Closed work carries its own label");
424 setIssueLabels(app.db, old.id, [done.value.id]);
425 expect(setIssueState(app, alice, fresh(), current(old), "closed").ok).toBe(true);
426 const open = await get("/alice/engine/issues");
427 expect(open).toContain("label=P1%20Feed");
428 expect(open).not.toContain("label=idle");
429 expect(open).not.toContain("label=shipped-only");
430 // Closed work can still be narrowed by the label only closed issues carry.
431 for (const state of ["closed", "all"]) {
432 const page = await get(`/alice/engine/issues?state=${state}`);
433 expect(page).toContain("label=shipped-only");
434 expect(page).not.toContain("label=idle");
435 }
436 // Chosen, it stays so it can be cleared.
437 expect(await get("/alice/engine/issues?label=idle")).toContain("idle ✕");
438 });
439
367440 test("the map counts open work by pillar, links each pillar to its issues, and reads no pillar for the rest", async () => {
368441 // One of P0's three open issues waits for another.
369442 expect(link(sandbox, parts[0]!).ok).toBe(true);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs