Load all branches in gitlab data connector (#2325)

* Fix gitlab data connector for self-hosted instances (#2315)

* Linting fix.

* Load all branches in the GitLab data connector #2319

* #2319 lint fixes.

* update fetch on fail

---------

Co-authored-by: Błażej Owczarczyk <blazeyy@gmail.com>
This commit is contained in:
Timothy Carambat 2024-09-19 13:34:38 -05:00 committed by GitHub
parent b7c7c0db98
commit 4fa3d6d333
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -159,34 +159,55 @@ class GitLabRepoLoader {
async getRepoBranches() { async getRepoBranches() {
if (!this.#validGitlabUrl() || !this.projectId) return []; if (!this.#validGitlabUrl() || !this.projectId) return [];
await this.#validateAccessToken(); await this.#validateAccessToken();
this.branches = [];
let fetching = true;
let page = 1;
let perPage = 50;
while (fetching) {
try { try {
this.branches = await fetch( const params = new URLSearchParams({
`${this.apiBase}/api/v4/projects/${this.projectId}/repository/branches`, per_page: perPage,
page,
});
const response = await fetch(
`${this.apiBase}/api/v4/projects/${
this.projectId
}/repository/branches?${params.toString()}`,
{ {
method: "GET", method: "GET",
headers: { headers: {
Accepts: "application/json", Accepts: "application/json",
...(this.accessToken ? { "PRIVATE-TOKEN": this.accessToken } : {}), ...(this.accessToken
? { "PRIVATE-TOKEN": this.accessToken }
: {}),
}, },
} }
) )
.then((res) => res.json()) .then((res) => res.json())
.then((branches) => { .then((branches) => {
if (!Array.isArray(branches) || branches.length === 0) {
fetching = false;
return [];
}
return branches.map((b) => b.name); return branches.map((b) => b.name);
}) })
.catch((e) => { .catch((e) => {
console.error(e); console.error(e);
fetching = false;
return []; return [];
}); });
return this.#branchPrefSort(this.branches); this.branches.push(...response);
page++;
} catch (err) { } catch (err) {
console.log(`RepoLoader.getRepoBranches`, err); console.log(`RepoLoader.getRepoBranches`, err);
this.branches = []; fetching = false;
return []; return [];
} }
} }
return this.#branchPrefSort(this.branches);
}
/** /**
* Returns list of all file objects from tree API for GitLab * Returns list of all file objects from tree API for GitLab