Skip to content

Commit 2382064

Browse files
committed
fix: cache self-hosted orgs + wire --fresh to org cache
- Self-hosted early return in listOrganizationsUncached now calls setOrgRegions before returning (Seer: cache was skipped) - Add disableOrgCache() to db/regions.ts, called by applyFreshFlag so --fresh bypasses the org listing cache (BugBot: stale data) - getCachedOrganizations returns empty when cache is disabled
1 parent d0238c0 commit 2382064

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/lib/api/organizations.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,17 @@ export async function listOrganizationsUncached(): Promise<
111111

112112
if (regions.length === 0) {
113113
// Fall back to default API for self-hosted instances
114-
return listOrganizationsInRegion(getApiBaseUrl());
114+
const orgs = await listOrganizationsInRegion(getApiBaseUrl());
115+
const baseUrl = getApiBaseUrl();
116+
await setOrgRegions(
117+
orgs.map((org) => ({
118+
slug: org.slug,
119+
regionUrl: baseUrl,
120+
orgId: org.id,
121+
orgName: org.name,
122+
}))
123+
);
124+
return orgs;
115125
}
116126

117127
const results = await Promise.all(

src/lib/db/regions.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ import { runUpsert } from "./utils.js";
1515

1616
const TABLE = "org_regions";
1717

18+
/** When true, getCachedOrganizations() returns empty (forces API fetch). */
19+
let orgCacheDisabled = false;
20+
21+
/** Disable the org listing cache for this invocation (e.g., `--fresh` flag). */
22+
export function disableOrgCache(): void {
23+
orgCacheDisabled = true;
24+
}
25+
26+
/** Re-enable the org listing cache. Exported for testing. */
27+
export function enableOrgCache(): void {
28+
orgCacheDisabled = false;
29+
}
30+
1831
type OrgRegionRow = {
1932
org_slug: string;
2033
org_id: string | null;
@@ -176,9 +189,16 @@ const ORG_CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000;
176189
* (from before schema v9) or stale `updated_at` are excluded — callers
177190
* should fall back to the API when the result is empty.
178191
*
179-
* @returns Array of cached org entries, or empty if cache is cold/stale/incomplete
192+
* Returns empty when the cache is disabled via {@link disableOrgCache}
193+
* (e.g., `--fresh` flag).
194+
*
195+
* @returns Array of cached org entries, or empty if cache is cold/stale/disabled/incomplete
180196
*/
181197
export async function getCachedOrganizations(): Promise<CachedOrg[]> {
198+
if (orgCacheDisabled) {
199+
return [];
200+
}
201+
182202
const db = getDatabase();
183203
const cutoff = Date.now() - ORG_CACHE_TTL_MS;
184204
const rows = db

src/lib/list-command.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { Aliases, Command, CommandContext } from "@stricli/core";
1717
import type { SentryContext } from "../context.js";
1818
import { parseOrgProjectArg } from "./arg-parsing.js";
1919
import { buildCommand, numberParser } from "./command.js";
20+
import { disableOrgCache } from "./db/regions.js";
2021
import { disableDsnCache } from "./dsn/index.js";
2122
import { warning } from "./formatters/colors.js";
2223
import {
@@ -146,6 +147,7 @@ export function applyFreshFlag(flags: { readonly fresh: boolean }): void {
146147
if (flags.fresh) {
147148
disableResponseCache();
148149
disableDsnCache();
150+
disableOrgCache();
149151
}
150152
}
151153

0 commit comments

Comments
 (0)