diff --git a/ExpressAPI/src/managers/GitlabManager.ts b/ExpressAPI/src/managers/GitlabManager.ts index 9bc2e876a8989befea92474bed0a78d6fd18aba7..01fc48f8474ca9be96df8a67a78851ae928d4f32 100644 --- a/ExpressAPI/src/managers/GitlabManager.ts +++ b/ExpressAPI/src/managers/GitlabManager.ts @@ -8,6 +8,9 @@ import GitlabVisibility from '../shared/types/Gitlab/GitlabVisibility'; import ApiRequest from '../types/ApiRequest'; import GitlabUser from '../shared/types/Gitlab/GitlabUser'; import GitlabRoutes from '../shared/types/Gitlab/GitlabRoutes'; +import GitlabTreeFile from '../shared/types/Gitlab/GitlabTreeFile'; +import parseLinkHeader from 'parse-link-header'; +import GitlabFile from '../shared/types/Gitlab/GitlabFile'; class GitlabManager { @@ -145,6 +148,45 @@ class GitlabManager { return response.data; } + + async getRepositoryTree(repoId: number, recursive: boolean = true, branch: string = 'main'): Promise<Array<GitlabTreeFile>> { + let address: string | undefined = this.getApiUrl(GitlabRoutes.REPOSITORY_TREE).replace('{{id}}', String(repoId)); + let params: any = { + pagination: 'keyset', + recursive : recursive, + per_page : 100, + ref : branch + }; + + let results: Array<GitlabTreeFile> = []; + + while ( params !== undefined ) { + const response = await axios.get<Array<GitlabTreeFile>>(address, { + params: params + }); + + results.push(...response.data); + + if ( 'link' in response.headers ) { + const link = parseLinkHeader(response.headers['link']); + params = link.next; + } else { + params = undefined; + } + } + + return results; + } + + async getFile(repoId: number, filePath: string, branch: string = 'main'): Promise<GitlabFile> { + const response = await axios.get<GitlabFile>(this.getApiUrl(GitlabRoutes.REPOSITORY_FILE).replace('{{id}}', String(repoId)).replace('{{filePath}}', encodeURIComponent(filePath)), { + params: { + ref: branch + } + }); + + return response.data; + } }