From c3ccc4d1d95b18d4a89373f31dc80e31546d8178 Mon Sep 17 00:00:00 2001 From: Thierry Michel Date: Wed, 8 Nov 2023 14:38:50 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20support=20gitlab=20paginati?= =?UTF-8?q?on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gitlab.mjs | 50 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/src/gitlab.mjs b/src/gitlab.mjs index 4318cdf..c90c174 100644 --- a/src/gitlab.mjs +++ b/src/gitlab.mjs @@ -25,21 +25,12 @@ function getProjectPath() { return projectPath } -async function getIssues() { +async function fetchIssues(url, config, page = 1, issues = []) { try { - // ? manage pagination? - const page = 1 const perPage = 100 - const { api, token } = getApiInfos() - const projectPath = getProjectPath() - const url = `${api}/projects/${projectPath}/issues` const res = await fetch( `${url}?state=opened&per_page=${perPage}&page=${page}`, - { - headers: { - 'PRIVATE-TOKEN': token, - }, - } + config ) if (!res.ok) { @@ -53,10 +44,39 @@ async function getIssues() { } const data = await res.json() - const issues = data.map(issue => ({ - name: `#${issue.iid} - ${issue.title}`, - value: issue.iid, - })) + const formattedIssues = issues.concat( + data.map(issue => ({ + name: `#${issue.iid} - ${issue.title}`, + value: issue.iid, + })) + ) + + const nextPage = Number(res.headers.get('x-next-page')) + + if (nextPage > 0) { + return getIssues(url, config, nextPage, formattedIssues) + } + + return formattedIssues + } catch (error) { + console.error(error.message) + + return [] + } +} + +async function getIssues() { + try { + const { api, token } = getApiInfos() + const projectPath = getProjectPath() + const url = `${api}/projects/${projectPath}/issues` + const config = { + headers: { + 'PRIVATE-TOKEN': token, + }, + } + + const issues = await fetchIssues(url, config) return issues } catch (error) {