11import { createHash } from 'node:crypto'
22import { PluginApi , type Project , type UniqueRepo } from '@cpn-console/hooks'
3- import type { AccessTokenScopes , CommitAction , GroupSchema , GroupStatisticsSchema , MemberSchema , ProjectVariableSchema , VariableSchema } from '@gitbeaker/rest'
4- import type { AllRepositoryTreesOptions , CondensedProjectSchema , Gitlab , PaginationRequestOptions , ProjectSchema , RepositoryFileExpandedSchema , RepositoryTreeSchema } from '@gitbeaker/core'
3+ import type { AccessTokenScopes , CommitAction , GroupSchema , MemberSchema , ProjectVariableSchema , VariableSchema } from '@gitbeaker/rest'
4+ import type { AllRepositoryTreesOptions , CondensedProjectSchema , Gitlab , ProjectSchema , RepositoryFileExpandedSchema } from '@gitbeaker/core'
55import { AccessLevel } from '@gitbeaker/core'
66import type { VaultProjectApi } from '@cpn-console/vault-plugin/types/vault-project-api.js'
77import { objectEntries } from '@cpn-console/shared'
88import type { GitbeakerRequestError } from '@gitbeaker/requester-utils'
9- import { getApi , getGroupRootId , infraAppsRepoName , internalMirrorRepoName } from './utils.js'
9+ import { find , getApi , getAll , getGroupRootId , infraAppsRepoName , internalMirrorRepoName , iter } from './utils.js'
1010import config from './config.js'
1111
1212type setVariableResult = 'created' | 'updated' | 'already up-to-date'
@@ -69,7 +69,7 @@ export class GitlabApi extends PluginApi {
6969 ) : Promise < boolean > {
7070 let action : CommitAction [ 'action' ] = 'create'
7171
72- const branches = await this . api . Branches . all ( repoId )
72+ const branches = await getAll ( iter ( opts => this . api . Branches . all ( repoId , opts ) ) )
7373 if ( branches . some ( b => b . name === branch ) ) {
7474 let actualFile : RepositoryFileExpandedSchema | undefined
7575 try {
@@ -152,12 +152,12 @@ export class GitlabApi extends PluginApi {
152152 return filesUpdated
153153 }
154154
155- public async listFiles ( repoId : number , options : AllRepositoryTreesOptions & PaginationRequestOptions < 'keyset' > = { } ) {
155+ public async listFiles ( repoId : number , options : AllRepositoryTreesOptions = { } ) {
156156 options . path = options ?. path ?? '/'
157157 options . ref = options ?. ref ?? 'main'
158158 options . recursive = options ?. recursive ?? false
159159 try {
160- const files : RepositoryTreeSchema [ ] = await this . api . Repositories . allRepositoryTrees ( repoId , options )
160+ const files = await getAll ( iter ( opts => this . api . Repositories . allRepositoryTrees ( repoId , { ... options , ... opts } ) ) )
161161 // if (depth >= 0) {
162162 // for (const file of files) {
163163 // if (file.type !== 'tree') {
@@ -199,8 +199,7 @@ export class GitlabZoneApi extends GitlabApi {
199199 public async getOrCreateInfraGroup ( ) : Promise < GroupSchema > {
200200 const rootId = await getGroupRootId ( )
201201 // Get or create projects_root_dir/infra group
202- const searchResult = await this . api . Groups . search ( infraGroupName )
203- const existingParentGroup = searchResult . find ( group => group . parent_id === rootId && group . name === infraGroupName )
202+ const existingParentGroup = await find ( iter ( opts => this . api . Groups . all ( { ...opts , search : infraGroupName } ) ) , group => group . parent_id === rootId && group . name === infraGroupName )
204203 return existingParentGroup || await this . api . Groups . create ( infraGroupName , infraGroupPath , {
205204 parentId : rootId ,
206205 projectCreationLevel : 'maintainer' ,
@@ -216,26 +215,24 @@ export class GitlabZoneApi extends GitlabApi {
216215 }
217216 const infraGroup = await this . getOrCreateInfraGroup ( )
218217 // Get or create projects_root_dir/infra/zone
219- const infraProjects = await this . api . Groups . allProjects ( infraGroup . id , {
218+ const project = await find ( iter ( opts => this . api . Groups . allProjects ( infraGroup . id , {
220219 search : zone ,
221220 simple : true ,
222- perPage : 100 ,
223- } )
224- const project : ProjectSchema = infraProjects . find ( repo => repo . name === zone ) ?? await this . createEmptyRepository ( {
221+ ...opts ,
222+ } ) ) , repo => repo . name === zone ) ?? await this . createEmptyRepository ( {
225223 repoName : zone ,
226224 groupId : infraGroup . id ,
227225 description : 'Repository hosting deployment files for this zone.' ,
228226 createFirstCommit : true ,
229- } ,
230- )
227+ } )
231228 this . infraProjectsByZoneSlug . set ( zone , project )
232229 return project
233230 }
234231}
235232
236233export class GitlabProjectApi extends GitlabApi {
237234 private project : Project | UniqueRepo
238- private gitlabGroup : GroupSchema & { statistics : GroupStatisticsSchema } | undefined
235+ private gitlabGroup : GroupSchema | undefined
239236 private specialRepositories : string [ ] = [ infraAppsRepoName , internalMirrorRepoName ]
240237 private zoneApi : GitlabZoneApi
241238
@@ -248,9 +245,8 @@ export class GitlabProjectApi extends GitlabApi {
248245
249246 // Group Project
250247 private async createProjectGroup ( ) : Promise < GroupSchema > {
251- const searchResult = await this . api . Groups . search ( this . project . slug )
252248 const parentId = await getGroupRootId ( )
253- const existingGroup = searchResult . find ( group => group . parent_id === parentId && group . name === this . project . slug )
249+ const existingGroup = await find ( iter ( opts => this . api . Groups . all ( { ... opts , search : this . project . slug } ) ) , group => group . parent_id === parentId && group . name === this . project . slug )
254250
255251 if ( existingGroup ) return existingGroup
256252
@@ -265,8 +261,7 @@ export class GitlabProjectApi extends GitlabApi {
265261 public async getProjectGroup ( ) : Promise < GroupSchema | undefined > {
266262 if ( this . gitlabGroup ) return this . gitlabGroup
267263 const parentId = await getGroupRootId ( )
268- const searchResult = await this . api . Groups . allSubgroups ( parentId )
269- this . gitlabGroup = searchResult . find ( group => group . name === this . project . slug )
264+ this . gitlabGroup = await find ( iter ( opts => this . api . Groups . allSubgroups ( parentId , opts ) ) , group => group . name === this . project . slug )
270265 return this . gitlabGroup
271266 }
272267
@@ -323,21 +318,15 @@ export class GitlabProjectApi extends GitlabApi {
323318
324319 public async getProjectId ( projectName : string ) {
325320 const projectGroup = await this . getProjectGroup ( )
326- if ( ! projectGroup ) {
327- throw new Error ( 'Parent DSO Project group has not been created yet' )
328- }
329- const projectsInGroup = await this . api . Groups . allProjects ( projectGroup . id , {
321+ if ( ! projectGroup ) throw new Error ( `Gitlab inaccessible, impossible de trouver le groupe ${ this . project . slug } ` )
322+
323+ const project = await find ( iter ( opts => this . api . Groups . allProjects ( projectGroup . id , {
330324 search : projectName ,
331325 simple : true ,
332- perPage : 100 ,
333- } )
334- const project = projectsInGroup . find ( p => p . path === projectName )
326+ ...opts ,
327+ } ) ) , repo => repo . name === projectName )
335328
336- if ( ! project ) {
337- const pathProjectName = `${ config ( ) . projectsRootDir } /${ this . project . slug } /${ projectName } `
338- throw new Error ( `Gitlab project "${ pathProjectName } " not found` )
339- }
340- return project . id
329+ return project ?. id
341330 }
342331
343332 public async getProjectById ( projectId : number ) {
@@ -351,8 +340,7 @@ export class GitlabProjectApi extends GitlabApi {
351340 public async getProjectToken ( tokenName : string ) {
352341 const group = await this . getProjectGroup ( )
353342 if ( ! group ) throw new Error ( 'Unable to retrieve gitlab project group' )
354- const groupTokens = await this . api . GroupAccessTokens . all ( group . id )
355- return groupTokens . find ( token => token . name === tokenName )
343+ return find ( iter ( opts => this . api . GroupAccessTokens . all ( group . id , opts ) ) , token => token . name === tokenName )
356344 }
357345
358346 public async createProjectToken ( tokenName : string , scopes : AccessTokenScopes [ ] ) {
@@ -375,8 +363,7 @@ export class GitlabProjectApi extends GitlabApi {
375363 const gitlabRepositories = await this . listRepositories ( )
376364 const mirrorRepo = gitlabRepositories . find ( repo => repo . name === internalMirrorRepoName )
377365 if ( ! mirrorRepo ) throw new Error ( 'Don\'t know how mirror repo could not exist' )
378- const allTriggerTokens = await this . api . PipelineTriggerTokens . all ( mirrorRepo . id )
379- const currentTriggerToken = allTriggerTokens . find ( token => token . description === tokenDescription )
366+ const currentTriggerToken = await find ( iter ( opts => this . api . PipelineTriggerTokens . all ( mirrorRepo . id , opts ) ) , token => token . description === tokenDescription )
380367
381368 const tokenVaultSecret = await vaultApi . read ( 'GITLAB' , { throwIfNoEntry : false } )
382369
@@ -398,7 +385,7 @@ export class GitlabProjectApi extends GitlabApi {
398385
399386 public async listRepositories ( ) {
400387 const group = await this . getOrCreateProjectGroup ( )
401- const projects = await this . api . Groups . allProjects ( group . id , { simple : false } ) // to refactor with https://github.com/jdalrymple/gitbeaker/pull/3624
388+ const projects = await getAll ( iter ( opts => this . api . Groups . allProjects ( group . id , { simple : false , ... opts } ) ) )
402389 return Promise . all ( projects . map ( async ( project ) => {
403390 if ( this . specialRepositories . includes ( project . name ) && ( ! project . topics || ! project . topics . includes ( pluginManagedTopic ) ) ) {
404391 return this . api . Projects . edit ( project . id , { topics : project . topics ? [ ...project . topics , pluginManagedTopic ] : [ pluginManagedTopic ] } )
@@ -432,7 +419,7 @@ export class GitlabProjectApi extends GitlabApi {
432419 // Group members
433420 public async getGroupMembers ( ) {
434421 const group = await this . getOrCreateProjectGroup ( )
435- return this . api . GroupMembers . all ( group . id )
422+ return getAll ( iter ( opts => this . api . GroupMembers . all ( group . id , opts ) ) )
436423 }
437424
438425 public async addGroupMember ( userId : number , accessLevel : AccessLevelAllowed = AccessLevel . DEVELOPER ) : Promise < MemberSchema > {
@@ -448,7 +435,7 @@ export class GitlabProjectApi extends GitlabApi {
448435 // CI Variables
449436 public async getGitlabGroupVariables ( ) : Promise < VariableSchema [ ] > {
450437 const group = await this . getOrCreateProjectGroup ( )
451- return await this . api . GroupVariables . all ( group . id )
438+ return await getAll ( iter ( opts => this . api . GroupVariables . all ( group . id , opts ) ) )
452439 }
453440
454441 public async setGitlabGroupVariable ( listVars : VariableSchema [ ] , toSetVariable : VariableSchema ) : Promise < setVariableResult > {
@@ -491,7 +478,7 @@ export class GitlabProjectApi extends GitlabApi {
491478 }
492479
493480 public async getGitlabRepoVariables ( repoId : number ) : Promise < VariableSchema [ ] > {
494- return await this . api . ProjectVariables . all ( repoId )
481+ return await getAll ( iter ( opts => this . api . ProjectVariables . all ( repoId , opts ) ) )
495482 }
496483
497484 public async setGitlabRepoVariable ( repoId : number , listVars : VariableSchema [ ] , toSetVariable : ProjectVariableSchema ) : Promise < setVariableResult | 'repository not found' > {
0 commit comments