1+ import { createLogger } from '@sim/logger'
12import { normalizeEmail } from '@sim/utils/string'
23import { groupToken , sortAccessTokens , userToken } from '@/lib/knowledge/access/tokens'
34import { secureFetchWithRetry } from '@/lib/knowledge/documents/secure-fetch.server'
@@ -13,6 +14,8 @@ import {
1314import type { ConnectorDirectory , ExternalDocument } from '@/connectors/types'
1415import { normalizeGitLabHost } from '@/tools/gitlab/utils'
1516
17+ const logger = createLogger ( 'GitLabPermissions' )
18+
1619const MAX_DIRECTORY_PAGES = 500
1720const PAGE_SIZE = 100
1821const MAX_RESPONSE_BYTES = 4 * 1024 * 1024
@@ -39,13 +42,14 @@ export async function validateGitLabCsvToken(
3942 signal ?: AbortSignal
4043) : Promise < { host : string ; projectId : number ; projectPath : string } > {
4144 const { base, project } = sourceAddress ( sourceConfig )
42- const viewer = await read < GitLabPermissionUser > ( `${ base } /user` , token , true , signal )
45+ const viewer = await read < GitLabPermissionUser > ( `${ base } /user` , token , 'account' , true , signal )
4346 if ( viewer . state !== 'active' || viewer . locked === true || ! Number . isSafeInteger ( viewer . id ) ) {
4447 throw new Error ( 'An active GitLab identity with read_api access is required' )
4548 }
4649 const record = await read < GitLabPermissionProject > (
4750 `${ base } /projects/${ project } ` ,
4851 token ,
52+ 'project' ,
4953 true ,
5054 signal
5155 )
@@ -113,6 +117,7 @@ function sourceAddress(sourceConfig: Record<string, unknown>) {
113117async function read < T > (
114118 url : string ,
115119 token : string ,
120+ operation : 'account' | 'project' ,
116121 validating = false ,
117122 signal ?: AbortSignal
118123) : Promise < T > {
@@ -125,7 +130,34 @@ async function read<T>(
125130 } ,
126131 { ...( validating ? VALIDATE_RETRY_OPTIONS : { } ) , maxResponseBytes : MAX_RESPONSE_BYTES , signal }
127132 )
128- if ( ! response . ok ) throw new Error ( `GitLab permission lookup failed (${ response . status } )` )
133+ if ( ! response . ok ) {
134+ const requestId = response . headers . get ( 'x-request-id' )
135+ logger . warn ( 'GitLab permission read failed' , {
136+ operation,
137+ status : response . status ,
138+ ...( requestId && / ^ [ a - z A - Z 0 - 9 _ - ] { 1 , 128 } $ / . test ( requestId )
139+ ? { providerRequestId : requestId }
140+ : { } ) ,
141+ } )
142+ if ( response . status === 401 ) {
143+ throw new Error ( 'GitLab did not accept the token. Use a valid token with read_api scope.' )
144+ }
145+ if ( response . status === 403 ) {
146+ throw new Error (
147+ 'GitLab denied access. Check read_api scope, token access, and IP restrictions.'
148+ )
149+ }
150+ if ( response . status === 404 ) {
151+ throw new Error (
152+ operation === 'project'
153+ ? 'Could not access this GitLab project. Check the project ID or full path, token access, and IP restrictions.'
154+ : 'Could not verify the GitLab account. Check the host and API access.'
155+ )
156+ }
157+ throw new Error (
158+ `GitLab could not verify ${ operation } access (HTTP ${ response . status } ). Try again.`
159+ )
160+ }
129161 return ( await response . json ( ) ) as T
130162}
131163
@@ -136,7 +168,12 @@ export async function validateGitLabPermissionToken(
136168) : Promise < void > {
137169 await requireAdministrator ( token , sourceConfig )
138170 const { base, project } = sourceAddress ( sourceConfig )
139- const record = await read < GitLabPermissionProject > ( `${ base } /projects/${ project } ` , token , true )
171+ const record = await read < GitLabPermissionProject > (
172+ `${ base } /projects/${ project } ` ,
173+ token ,
174+ 'project' ,
175+ true
176+ )
140177 await discoverGitLabPermissionPolicy ( token , sourceConfig , record , true )
141178}
142179
@@ -146,7 +183,7 @@ async function requireAdministrator(
146183 signal ?: AbortSignal
147184) : Promise < void > {
148185 const { base } = sourceAddress ( sourceConfig )
149- const viewer = await read < GitLabPermissionUser > ( `${ base } /user` , token , true , signal )
186+ const viewer = await read < GitLabPermissionUser > ( `${ base } /user` , token , 'account' , true , signal )
150187 if ( viewer . is_admin !== true || viewer . state !== 'active' || viewer . locked === true ) {
151188 throw new Error (
152189 'Mirror source permissions requires an active GitLab instance administrator token with read_api access'
@@ -295,7 +332,7 @@ async function loadSnapshot(
295332 await requireAdministrator ( token , config , signal )
296333 const { base, project } = sourceAddress ( config )
297334 const [ record , users , members ] = await Promise . all ( [
298- read < GitLabPermissionProject > ( `${ base } /projects/${ project } ` , token , false , signal ) ,
335+ read < GitLabPermissionProject > ( `${ base } /projects/${ project } ` , token , 'project' , false , signal ) ,
299336 listAll < GitLabPermissionUser > (
300337 `${ base } /users?pagination=keyset&order_by=id&sort=asc` ,
301338 token ,
@@ -347,7 +384,13 @@ export async function openGitLabDirectory(
347384 }
348385 }
349386 const { tenant, base, project } = sourceAddress ( config )
350- const identity = await read < { id : number } > ( `${ base } /projects/${ project } ` , token , false , signal )
387+ const identity = await read < { id : number } > (
388+ `${ base } /projects/${ project } ` ,
389+ token ,
390+ 'project' ,
391+ false ,
392+ signal
393+ )
351394 if ( ! Number . isInteger ( identity . id ) ) throw new Error ( 'GitLab did not return a project identity' )
352395 const groups = FEATURES . map ( ( feature ) => ( { id : groupId ( identity . id , feature ) } ) )
353396 let pending : Promise < PermissionSnapshot > | undefined
0 commit comments