Skip to content

Commit

Permalink
feat: ✨ support gitlab pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
thierrymichel committed Nov 8, 2023
1 parent 2732351 commit c3ccc4d
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions src/gitlab.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit c3ccc4d

Please sign in to comment.