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

fix(gitea): don't crash on empty body during pagination #6598

Merged
merged 5 commits into from Jun 27, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/util/http/__snapshots__/gitea.spec.ts.snap
@@ -1,5 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`util/http/gitea handles pagination with empty response 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "gitea.renovatebot.com",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://gitea.renovatebot.com/api/v1/pagination-example-3",
},
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "gitea.renovatebot.com",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://gitea.renovatebot.com/api/v1/pagination-example-3?page=2",
},
]
`;

exports[`util/http/gitea supports pagination on data property 1`] = `
Array [
Object {
Expand Down
15 changes: 15 additions & 0 deletions lib/util/http/gitea.spec.ts
Expand Up @@ -67,4 +67,19 @@ describe(getName(__filename), () => {
expect(res.body.data).toEqual(['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr']);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('handles pagination with empty response', async () => {
httpMock
.scope(baseUrl)
.get('/pagination-example-3')
.reply(200, { data: ['abc', 'def', 'ghi'] }, { 'x-total-count': '5' })
.get('/pagination-example-3?page=2')
.reply(200, { data: [] });

const res = await giteaHttp.getJson<{ data: any }>('pagination-example-3', {
CirnoT marked this conversation as resolved.
Show resolved Hide resolved
paginate: true,
});
expect(res.body.data).toHaveLength(3);
expect(res.body.data).toEqual(['abc', 'def', 'ghi']);
expect(httpMock.getTrace()).toMatchSnapshot();
});
});
7 changes: 6 additions & 1 deletion lib/util/http/gitea.ts
Expand Up @@ -53,7 +53,12 @@ export class GiteaHttp extends Http<GiteaHttpOptions, GiteaHttpOptions> {
resolvedUrl.searchParams.set('page', nextPage.toString());

const nextRes = await super.request<T>(resolvedUrl.toString(), opts);
pc.push(...getPaginationContainer(nextRes.body));
const nextPc = getPaginationContainer(nextRes.body);
if (nextPc === null) {
break;
}

pc.push(...nextPc);
}
}

Expand Down