11import { db } from '@sim/db'
22import { knowledgeBase , knowledgeConnector } from '@sim/db/schema'
3+ import { createLogger } from '@sim/logger'
4+ import { getErrorMessage } from '@sim/utils/errors'
35import { and , eq , inArray , isNull } from 'drizzle-orm'
46import { decryptApiKey } from '@/lib/api-key/crypto'
57import { type ResourceOwner , resourceScopeFromOwner } from '@/lib/core/resource-scope'
@@ -45,6 +47,7 @@ import {
4547} from '@/connectors/gitlab/permissions'
4648import { normalizeGitLabHost } from '@/tools/gitlab/utils'
4749
50+ const logger = createLogger ( 'SimSearchGitLabSources' )
4851const PREFIX = 'gitlab-source:'
4952
5053/** Canonical admin-managed sources only. Neither credentials nor indexed documents leave this module. */
@@ -90,26 +93,45 @@ function hasCsvPermissions(metadata: unknown): boolean {
9093 }
9194}
9295
96+ /** The IDs of CSV-permission sources that grant the viewer no project. */
97+ async function ungrantedSources (
98+ owner : ResourceOwner ,
99+ userId : string ,
100+ ids : string [ ]
101+ ) : Promise < Set < string > > {
102+ const csv = ( await readConnectorPermissionMetadata ( ids ) )
103+ . filter ( ( row ) => hasCsvPermissions ( row . metadata ) )
104+ . map ( ( row ) => row . connectorId )
105+ const own = csv . length ? await viewerToken ( owner , userId ) : undefined
106+ if ( ! own ) return new Set ( )
107+ const granted = await connectorsGrantingSubject ( csv , own )
108+ return new Set ( csv . filter ( ( id ) => ! granted . has ( id ) ) )
109+ }
110+
93111/**
94112 * A source whose uploaded permissions grant the viewer nothing could never return a result, so it
95113 * is left out rather than reported unavailable on every search. Sources that check permissions
96- * with GitLab, and any whose stored permissions cannot be read, stay listed: their session decides.
114+ * with GitLab, any whose stored permissions cannot be read, and every source when this check
115+ * itself fails stay listed: their session decides.
97116 */
98117export async function listAdminGitLabAccounts (
99118 owner : ResourceOwner ,
100119 userId : string
101120) : Promise < LiveAccount [ ] > {
102121 const rows = await sources ( owner )
103122 if ( ! rows . length ) return [ ]
104- const metadata = await readConnectorPermissionMetadata ( rows . map ( ( source ) => source . id ) )
105- const csv = new Set (
106- metadata . filter ( ( row ) => hasCsvPermissions ( row . metadata ) ) . map ( ( row ) => row . connectorId )
107- )
108- // Listing is only a filter: a failed identity lookup lists every source for its session to refuse.
109- const own = csv . size ? await viewerToken ( owner , userId ) . catch ( ( ) => null ) : null
110- const granted = own ? await connectorsGrantingSubject ( [ ...csv ] , own ) : null
123+ const hidden = await ungrantedSources (
124+ owner ,
125+ userId ,
126+ rows . map ( ( source ) => source . id )
127+ ) . catch ( ( error : unknown ) => {
128+ logger . warn ( 'GitLab source grants could not be checked; listing every source' , {
129+ error : getErrorMessage ( error ) ,
130+ } )
131+ return new Set < string > ( )
132+ } )
111133 return rows
112- . filter ( ( source ) => ! granted || ! csv . has ( source . id ) || granted . has ( source . id ) )
134+ . filter ( ( source ) => ! hidden . has ( source . id ) )
113135 . map ( ( source ) => ( {
114136 id : `${ PREFIX } ${ source . id } ` ,
115137 provider : 'gitlab' ,
@@ -120,9 +142,14 @@ export async function listAdminGitLabAccounts(
120142 } ) )
121143}
122144
145+ /** Whether a live account ID names an administrator-managed GitLab source. */
146+ export function isAdminGitLabAccountId ( accountId : string ) : boolean {
147+ return accountId . startsWith ( PREFIX )
148+ }
149+
123150export async function resolveAdminGitLabAccount ( owner : ResourceOwner , account : LiveAccount ) {
124151 const [ source ] = await sources ( owner , account . id . slice ( PREFIX . length ) )
125- if ( ! account . id . startsWith ( PREFIX ) || ! source ?. encryptedApiKey )
152+ if ( ! isAdminGitLabAccountId ( account . id ) || ! source ?. encryptedApiKey )
126153 throw new NativeSearchError (
127154 'unavailable' ,
128155 'Ask an organization admin to check the GitLab connection.'
0 commit comments