Public: anyone can read this; the crew can change it.
"Every repository you work on" is every one: the shipping and checks charts no longer drop the oldest, busiest repository #16336
Check · Review · Land · Live done — Landed and live in production.
talk/scope-all → main 0 commits 2 threads · 2 unresolved
3 files
changed +36
−22
src/core/repos.ts +13
| @@ -1187,6 +1187,19 @@ export function listReposWritableTo( | ||
| 1187 | 1187 | return options.withoutCounts === true ? rows : withCounts(db, rows); |
| 1188 | 1188 | } |
| 1189 | 1189 | |
| 1190 | /** | |
| 1191 | * The ids of every repository this viewer may write (`REPO_WRITABLE`, as `listReposWritableTo`), | |
| 1192 | * with no page and no limit: what "every repository you work on" spans for a count, where cutting | |
| 1193 | * at the newest N would drop the oldest and busiest. One indexed statement; ids only. | |
| 1194 | */ | |
| 1195 | export function everyWritableRepoId(db: Database, viewer: User | undefined): string[] { | |
| 1196 | if (!viewer) return []; | |
| 1197 | return db | |
| 1198 | .query<{ id: string }, [string]>(`SELECT repo.id AS id FROM repo WHERE ${REPO_WRITABLE}`) | |
| 1199 | .all(viewer.id) | |
| 1200 | .map((r) => r.id); | |
| 1201 | } | |
| 1202 | ||
| 1190 | 1203 | /** Repos owned by one user or org, visible to the viewer. */ |
| 1191 | 1204 | export function listReposByOwner( |
| 1192 | 1205 | db: Database, |
src/widgets/registry.ts +12 −22
| @@ -18,7 +18,7 @@ import { mergedPerDay } from "../core/pulls.ts"; | ||
| 18 | 18 | import { historyBrief } from "../system/telemetry.ts"; |
| 19 | 19 | import { withUnit } from "../core/talk_voice.ts"; |
| 20 | 20 | import type { Database } from "bun:sqlite"; |
| 21 | import { canRead, getRepo, listReposWritableTo, type Repo } from "../core/repos.ts"; | |
| 21 | import { canRead, getRepo, everyWritableRepoId } from "../core/repos.ts"; | |
| 22 | 22 | |
| 23 | 23 | /** The forge's renderers a widget may name. */ |
| 24 | 24 | export const RENDERERS = ["chart"] as const; |
| @@ -222,31 +222,23 @@ export const SCOPE_ARGS = { | ||
| 222 | 222 | }, |
| 223 | 223 | } as const satisfies Record<string, Schema>; |
| 224 | 224 | |
| 225 | /** The most repositories "every repository you work on" spans. */ | |
| 226 | export const SCOPE_REPOS = 100; | |
| 227 | ||
| 228 | 225 | /** |
| 229 | * The repositories a widget reads: the one named, when the person may read it, else `not_found`, | |
| 230 | * whether it is missing or hidden; or the ones they work on (their own and their teams'), at most | |
| 231 | * `SCOPE_REPOS`. | |
| 226 | * The repositories a widget reads, as ids: the one named, when the person may read it, else | |
| 227 | * `not_found`, whether it is missing or hidden; or every one they work on (their own, their teams', | |
| 228 | * the ones they crew), however many: a newest-first page would leave out the oldest repository, | |
| 229 | * often the busiest (#16330 counted 19 merges for an owner whose forge merged hundreds). | |
| 232 | 230 | */ |
| 233 | 231 | export function scopeOf( |
| 234 | 232 | app: App, |
| 235 | 233 | user: User | undefined, |
| 236 | 234 | args: Readonly<Record<string, unknown>>, |
| 237 | ): Result<{ repos: Repo[]; where: string }, "not_found"> { | |
| 235 | ): Result<{ repoIds: string[]; where: string }, "not_found"> { | |
| 238 | 236 | const named = typeof args["repo"] === "string" ? args["repo"] : undefined; |
| 239 | if (named === undefined) { | |
| 240 | const repos = listReposWritableTo(app.db, user, { | |
| 241 | withoutCounts: true, | |
| 242 | limit: SCOPE_REPOS, | |
| 243 | }); | |
| 244 | return ok({ repos, where: "every repository you work on" }); | |
| 245 | } | |
| 237 | if (named === undefined) return ok({ repoIds: everyWritableRepoId(app.db, user), where: "every repository you work on" }); | |
| 246 | 238 | const [owner = "", name = ""] = named.split("/"); |
| 247 | 239 | const repo = getRepo(app.db, owner, name); |
| 248 | 240 | return repo !== undefined && canRead(app.db, repo, user) |
| 249 | ? ok({ repos: [repo], where: `${repo.ownerName}/${repo.name}` }) | |
| 241 | ? ok({ repoIds: [repo.id], where: `${repo.ownerName}/${repo.name}` }) | |
| 250 | 242 | : err("not_found"); |
| 251 | 243 | } |
| 252 | 244 | |
| @@ -287,20 +279,18 @@ export function perDay<S extends string>(days: readonly string[], series: readon | ||
| 287 | 279 | return out; |
| 288 | 280 | } |
| 289 | 281 | |
| 290 | /** The repositories as the one JSON list every read binds. */ | |
| 291 | const idsOf = (repos: readonly Repo[]): string => JSON.stringify(repos.map((r) => r.id)); | |
| 292 | 282 | |
Two blank lines are left here after removing |
||
| 293 | 283 | /** |
| 294 | 284 | * Each check finished on these repositories since an instant: when, and how it ended. One covering |
| 295 | 285 | * walk of `run_finished_cov`. |
| 296 | 286 | */ |
| 297 | export const checksSince = (db: Database, repos: readonly Repo[], since: string): { at: string; state: string }[] => | |
| 287 | export const checksSince = (db: Database, repoIds: readonly string[], since: string): { at: string; state: string }[] => | |
| 298 | 288 | db |
| 299 | 289 | .query<{ at: string; state: string }, [string, string]>( |
| 300 | 290 | `SELECT finished_at AS at, state FROM run INDEXED BY run_finished_cov |
| 301 | 291 | WHERE kind = 'check' AND finished_at IS NOT NULL AND finished_at >= ?1 AND repo_id IN (SELECT value FROM json_each(?2))`, |
| 302 | 292 | ) |
| 303 | .all(since, idsOf(repos)); | |
| 293 | .all(since, JSON.stringify(repoIds)); | |
| 304 | 294 | |
| 305 | 295 | /** A number rounded for a sentence: whole above ten, one place below. */ |
| 306 | 296 | export const said = (v: number): string => (Math.abs(v) >= 10 ? Math.round(v) : Math.round(v * 10) / 10).toLocaleString("en-US"); |
| @@ -316,7 +306,7 @@ const checks: Widget = { | ||
| 316 | 306 | const scope = scopeOf(app, user, args); |
| 317 | 307 | if (!scope.ok) return scope; |
| 318 | 308 | const { days, since } = windowOf(args); |
| 319 | const by = perDay(days, ["ok", "failed"] as const, checksSince(app.db, scope.value.repos, since).map((r) => ({ at: r.at, series: r.state === "ok" ? "ok" : r.state === "failed" ? "failed" : undefined }))); | |
| 309 | const by = perDay(days, ["ok", "failed"] as const, checksSince(app.db, scope.value.repoIds, since).map((r) => ({ at: r.at, series: r.state === "ok" ? "ok" : r.state === "failed" ? "failed" : undefined }))); | |
| 320 | 310 | const passed = by.ok.reduce((a, b) => a + b, 0); |
| 321 | 311 | const failed = by.failed.reduce((a, b) => a + b, 0); |
| 322 | 312 | const all = passed + failed; |
| @@ -399,7 +389,7 @@ const shipping: Widget = { | ||
| 399 | 389 | const scope = scopeOf(app, user, args); |
| 400 | 390 | if (!scope.ok) return scope; |
| 401 | 391 | const { days } = windowOf(args); |
| 402 | const { merged } = mergedPerDay(app.db, scope.value.repos.map((r) => r.id), days.length); | |
| 392 | const { merged } = mergedPerDay(app.db, scope.value.repoIds, days.length); | |
| 403 | 393 | const all = merged.reduce((a, b) => a + b, 0); |
| 404 | 394 | const best = merged.indexOf(Math.max(...merged)); |
| 405 | 395 | return ok({ |
test/widgets.test.ts +11
| @@ -191,6 +191,17 @@ describe("shipping", () => { | ||
| 191 | 191 | expect(chart("shipping", alice, { days: 7 }).data.series[0]!.values.reduce((a, b) => a + b, 0)).toBe(4); |
| 192 | 192 | expect(chart("shipping", bob, { days: 7 }).says).toBe("No pulls merged in the last 7 days on every repository you work on."); |
| 193 | 193 | }); |
| 194 | ||
| 195 | test("every repository the person works on is every one, not the newest hundred: the oldest, busiest repository still counts", () => { | |
| 196 | // 120 newer repositories of alice's beside `open`, her oldest with the merges and checks: a | |
| 197 | // newest-first page of 100 left `open` out and counted none of its work (#16330 in production). | |
| 198 | const cols = app.db.query<{ name: string }, []>("SELECT name FROM pragma_table_info('repo')").all().map((c) => c.name); | |
| 199 | const copy = app.db.query(`INSERT INTO repo (${cols.join(", ")}) SELECT ${cols.map((c) => (c === "id" ? "?1" : c === "name" ? "?2" : c)).join(", ")} FROM repo WHERE id = ?3`); | |
| 200 | for (let n = 0; n < 120; n++) copy.run(id(), `newer-${n}`, open.id); | |
| 201 | const all = (name: string) => chart(name, alice, { days: 7 }).data.series.map((s) => s.values.reduce((a, b) => a + b, 0)); | |
| 202 | expect(all("shipping")).toEqual([4]); | |
| 203 | expect(all("checks")).toEqual([5, 3]); | |
| 204 | }); | |
| 194 | 205 | }); |
| 195 | 206 | |
| 196 | 207 | describe("the words that lead to a graph", () => { |
new above
offline
Long single-line
if ... return ok({...}): break it across lines like the rest of the file.