From 58f42b32732cb189093ff8495c71b9c14fa002df Mon Sep 17 00:00:00 2001 From: Cirno the Strongest <1447794+CirnoT@users.noreply.github.com> Date: Sat, 27 Jun 2020 10:25:20 +0200 Subject: [PATCH] fix(gitea): don't crash on empty body during pagination (#6598) --- .../http/__snapshots__/gitea.spec.ts.snap | 25 +++++++++++++++++ lib/util/http/gitea.spec.ts | 27 ++++++++++++++++--- lib/util/http/gitea.ts | 7 ++++- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/lib/util/http/__snapshots__/gitea.spec.ts.snap b/lib/util/http/__snapshots__/gitea.spec.ts.snap index 42efc4a783c63f..b22be4778cbc0d 100644 --- a/lib/util/http/__snapshots__/gitea.spec.ts.snap +++ b/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 { diff --git a/lib/util/http/gitea.spec.ts b/lib/util/http/gitea.spec.ts index 47de55353a5e8f..b24c852bd51ba6 100644 --- a/lib/util/http/gitea.spec.ts +++ b/lib/util/http/gitea.spec.ts @@ -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(); + }); }); diff --git a/lib/util/http/gitea.ts b/lib/util/http/gitea.ts index d9d48672420627..6078596c3cba25 100644 --- a/lib/util/http/gitea.ts +++ b/lib/util/http/gitea.ts @@ -53,7 +53,12 @@ export class GiteaHttp extends Http { resolvedUrl.searchParams.set('page', nextPage.toString()); const nextRes = await super.request(resolvedUrl.toString(), opts); - pc.push(...getPaginationContainer(nextRes.body)); + const nextPc = getPaginationContainer(nextRes.body); + if (nextPc === null) { + break; + } + + pc.push(...nextPc); } }