Skip to content
Snippets Groups Projects
Unverified Commit f00846ee authored by NateScarlet's avatar NateScarlet Committed by GitHub
Browse files

fix(gitea): pagination (#5798)

Fixes #5797
parent fb87d714
No related branches found
No related tags found
No related merge requests found
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`platform/gitea/gitea-got-wrapper supports root-level pagination 1`] = `
Array [
Array [
"https://gitea.renovatebot.com/api/v1/pagination-example-1",
Object {
"baseUrl": "https://gitea.renovatebot.com/api/v1",
"hostType": "gitea",
"json": true,
"method": "GET",
"paginate": true,
},
],
Array [
"https://gitea.renovatebot.com/api/v1/pagination-example-1?page=2",
Object {
"baseUrl": "https://gitea.renovatebot.com/api/v1",
"hostType": "gitea",
"json": true,
"method": "GET",
"paginate": true,
},
],
Array [
"https://gitea.renovatebot.com/api/v1/pagination-example-1?page=3",
Object {
"baseUrl": "https://gitea.renovatebot.com/api/v1",
"hostType": "gitea",
"json": true,
"method": "GET",
"paginate": true,
},
],
]
`;
...@@ -48,10 +48,13 @@ describe('platform/gitea/gitea-got-wrapper', () => { ...@@ -48,10 +48,13 @@ describe('platform/gitea/gitea-got-wrapper', () => {
}) })
); );
const res = await api.get('pagination-example-1', { paginate: true }); const res = await api.get(`${baseURL}/pagination-example-1`, {
paginate: true,
});
expect(res.body).toHaveLength(6); expect(res.body).toHaveLength(6);
expect(res.body).toEqual(['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr']); expect(res.body).toEqual(['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr']);
expect(got.mock.calls).toMatchSnapshot();
expect(got).toHaveBeenCalledTimes(3); expect(got).toHaveBeenCalledTimes(3);
}); });
......
import URL from 'url'; import { URL } from 'url';
import { GotApi, GotApiOptions, GotResponse } from '../common';
import { PLATFORM_TYPE_GITEA } from '../../constants/platforms'; import { PLATFORM_TYPE_GITEA } from '../../constants/platforms';
import got from '../../util/got'; import got from '../../util/got';
import { GotApi, GotApiOptions, GotResponse } from '../common';
const hostType = PLATFORM_TYPE_GITEA; const hostType = PLATFORM_TYPE_GITEA;
let baseUrl: string; let baseUrl: string;
...@@ -28,15 +28,15 @@ async function get(path: string, options?: any): Promise<GotResponse> { ...@@ -28,15 +28,15 @@ async function get(path: string, options?: any): Promise<GotResponse> {
const res = await got(path, opts); const res = await got(path, opts);
const pc = getPaginationContainer(res.body); const pc = getPaginationContainer(res.body);
if (opts.paginate && pc) { if (opts.paginate && pc) {
const url = URL.parse(res.url, true); const url = new URL(res.url);
const total = parseInt(res.headers['x-total-count'] as string, 10); const total = parseInt(res.headers['x-total-count'] as string, 10);
let nextPage = parseInt(url.query.page as string, 10) || 1 + 1; let nextPage = parseInt(url.searchParams.get('page') || '1', 10);
while (total && pc.length < total) { while (total && pc.length < total) {
nextPage += 1; nextPage += 1;
url.query.page = nextPage.toString(); url.searchParams.set('page', nextPage.toString());
const nextRes = await got(URL.format(url), opts); const nextRes = await got(url.toString(), opts);
pc.push(...getPaginationContainer(nextRes.body)); pc.push(...getPaginationContainer(nextRes.body));
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment