Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(gitea): don't crash on empty body during pagination (#6598)
  • Loading branch information
CirnoT committed Jun 27, 2020
1 parent 4944798 commit 58f42b3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
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
27 changes: 24 additions & 3 deletions lib/util/http/gitea.spec.ts
Expand Up @@ -60,11 +60,32 @@ describe(getName(__filename), () => {
.get('/pagination-example-2?page=3')
.reply(200, { data: ['mno', 'pqr'] });

const res = await giteaHttp.getJson<{ data: any }>('pagination-example-2', {
paginate: true,
});
const res = await giteaHttp.getJson<{ data: string[] }>(
'pagination-example-2',
{
paginate: true,
}
);
expect(res.body.data).toHaveLength(6);
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: string[] }>(
'pagination-example-3',
{
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

0 comments on commit 58f42b3

Please sign in to comment.