From 969305fe8951c4c30881038815068fd941952a8d Mon Sep 17 00:00:00 2001 From: msukkari Date: Thu, 13 Nov 2025 21:07:41 -0800 Subject: [PATCH] clear search contexts on init --- packages/web/src/actions.ts | 16 ++++++++++++++++ packages/web/src/initialize.ts | 11 +++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/web/src/actions.ts b/packages/web/src/actions.ts index e194f808..16ce61a5 100644 --- a/packages/web/src/actions.ts +++ b/packages/web/src/actions.ts @@ -1669,6 +1669,22 @@ export const getSearchContexts = async (domain: string) => sew(() => }, /* minRequiredRole = */ OrgRole.GUEST), /* allowAnonymousAccess = */ true )); +export const clearSearchContexts = async (domain: string) => sew(() => + withAuth((userId) => + withOrgMembership(userId, domain, async ({ org }) => { + await prisma.searchContext.deleteMany({ + where: { + orgId: org.id, + }, + }); + + return { + success: true, + }; + }, /* minRequiredRole = */ OrgRole.OWNER) + ) +) + export const getRepoImage = async (repoId: number): Promise => sew(async () => { return await withOptionalAuthV2(async ({ org, prisma }) => { const repo = await prisma.repo.findUnique({ diff --git a/packages/web/src/initialize.ts b/packages/web/src/initialize.ts index fa27febd..a0f95325 100644 --- a/packages/web/src/initialize.ts +++ b/packages/web/src/initialize.ts @@ -7,6 +7,7 @@ import { getOrgFromDomain } from './data/org'; import { SINGLE_TENANT_ORG_DOMAIN, SINGLE_TENANT_ORG_ID, SOURCEBOT_GUEST_USER_ID } from './lib/constants'; import { ServiceErrorException } from './lib/serviceError'; import { getOrgMetadata, isServiceError } from './lib/utils'; +import { clearSearchContexts, getSearchContexts } from './actions'; const logger = createLogger('web-initialize'); @@ -62,6 +63,16 @@ const initSingleTenancy = async () => { } } + // If we don't have the search context entitlement then wipe any existing + // search contexts that may be present in the DB + const hasSearchContextEntitlement = hasEntitlement("search-contexts") + if(!hasSearchContextEntitlement) { + const searchContexts = await getSearchContexts(SINGLE_TENANT_ORG_DOMAIN); + if (!isServiceError(searchContexts) && searchContexts.length > 0) { + clearSearchContexts(SINGLE_TENANT_ORG_DOMAIN) + } + } + // Sync anonymous access config from the config file const config = await loadConfig(env.CONFIG_PATH); const forceEnableAnonymousAccess = config.settings?.enablePublicAccess ?? env.FORCE_ENABLE_ANONYMOUS_ACCESS === 'true';