From 985f486586e8661863ad105cc78db16619f15121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me> Date: Mon, 31 Jul 2023 23:46:23 +0200 Subject: [PATCH] GitlabManager => Add functions to get tree of a repo and for get a file --- ExpressAPI/src/managers/GitlabManager.ts | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/ExpressAPI/src/managers/GitlabManager.ts b/ExpressAPI/src/managers/GitlabManager.ts index 9bc2e87..01fc48f 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; + } } -- GitLab