Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve issue pagination #24

Merged
merged 12 commits into from
Aug 30, 2023
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"scripts": {
"build": "nuxt build",
"start": "nuxt dev",
"start": "nuxt dev --host 0.0.0.0",
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare",
Expand Down
Binary file modified requirements.txt
Binary file not shown.
5 changes: 2 additions & 3 deletions server/api/repos/[repo_id]/clone.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default defineEventHandler(async (event) => {
console.log('fetching issues ...');
let page = 1;
while (true) {
const { items: issues, total } = await userForgeApi.getIssues(repo.remoteId.toString(), { page, perPage: 50 });
const { items: issues, total } = await userForgeApi.getIssues(repo.remoteId.toString(), { page, perPage: 2 });
anbraten marked this conversation as resolved.
Show resolved Hide resolved
for await (const issue of issues) {
let issueString = `# issue "${issue.title}" (${issue.number})`;
if (issue.labels.length !== 0) {
Expand All @@ -78,8 +78,7 @@ export default defineEventHandler(async (event) => {

console.log('wrote', issues.length, 'issues');

// TODO: improve stop condition
if (issues.length < 50) {
if (issues.length < 50 || page === total) {
anbraten marked this conversation as resolved.
Show resolved Hide resolved
break;
}
page += 1;
Expand Down
19 changes: 16 additions & 3 deletions server/forges/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,26 @@ export class Github implements Forge {
async getIssues(token: string, repoId: string, pagination?: Pagination): Promise<PaginatedList<Issue>> {
const client = this.getClient(token);

const repo = await client.request(`GET /repositories/{repoId}`, {
repoId,
});


const issues = await client.request(`GET /repos/{owner}/{repo}/issues`, {
owner: repoId.split('/')[0],
repo: repoId.split('/')[1],
owner: repo.data.owner.login,
repo: repo.data.name,
per_page: pagination?.perPage || 10,
page: pagination?.page || 1,
});

let total = 1; //if there are no pages. It occurs when the response doesn't have link attribute in headers
anbraten marked this conversation as resolved.
Show resolved Hide resolved

if(issues.headers.link){
const linkToLastPage = issues.headers.link.split(',').find(link=> link.split('; ')[1]==='rel="last"');
total = Number(linkToLastPage?.split('&')[1].split('=')[1].split('>')[0]); //e.g <https://api.github.com/repositories/659184353/issues?per_page=2&page=3>; rel="last"
anbraten marked this conversation as resolved.
Show resolved Hide resolved
}

console.log(total)
return {
items: issues.data.map((issue) => ({
title: issue.title,
Expand All @@ -154,7 +167,7 @@ export class Github implements Forge {
labels: issue.labels.map((label) => (typeof label === 'string' ? label : label.name || '')),
comments: [], // TODO: get comments
})),
total: 0, // TODO: get total
total,
};
}
}
5 changes: 3 additions & 2 deletions server/forges/gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ export class Gitlab implements Forge {
async getIssues(token: string, repoId: string, pagination?: Pagination): Promise<PaginatedList<Issue>> {
const client = this.getClient(token);

const issues = await client.Issues.all({
const {data: issues,paginationInfo} = await client.Issues.all({
projectId: repoId,
perPage: pagination?.perPage || 10,
page: pagination?.page || 1,
showExpanded: true
});

return {
Expand All @@ -148,7 +149,7 @@ export class Gitlab implements Forge {
labels: issue.labels || [],
comments: [], // TODO: get comments
})),
total: 0, // TODO: get total
total: paginationInfo.total
};
}
}
Loading