From 961a8844cea1268778f9cd9d6c5edb3e6d42eaad Mon Sep 17 00:00:00 2001 From: Cirno the Strongest <1447794+CirnoT@users.noreply.github.com> Date: Sat, 27 Jun 2020 08:22:53 +0200 Subject: [PATCH 1/5] fix(gitea); don't crash on empty body during pagination --- lib/util/http/gitea.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/util/http/gitea.ts b/lib/util/http/gitea.ts index d9d48672420627..896b67dc079737 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); } } From 8fdcb3c6d807ec407f1792e396b45b615b741ba3 Mon Sep 17 00:00:00 2001 From: CirnoT <1447794+CirnoT@users.noreply.github.com> Date: Sat, 27 Jun 2020 09:06:41 +0200 Subject: [PATCH 2/5] fix tests --- lib/util/http/gitea.spec.ts | 15 +++++++++++++++ lib/util/http/gitea.ts | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/util/http/gitea.spec.ts b/lib/util/http/gitea.spec.ts index 47de55353a5e8f..792226a2216f0a 100644 --- a/lib/util/http/gitea.spec.ts +++ b/lib/util/http/gitea.spec.ts @@ -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', { + 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 896b67dc079737..6078596c3cba25 100644 --- a/lib/util/http/gitea.ts +++ b/lib/util/http/gitea.ts @@ -57,7 +57,7 @@ export class GiteaHttp extends Http { if (nextPc === null) { break; } - + pc.push(...nextPc); } } From abc9642e24025089544f42b741e88fbb26da21e0 Mon Sep 17 00:00:00 2001 From: CirnoT <1447794+CirnoT@users.noreply.github.com> Date: Sat, 27 Jun 2020 09:13:17 +0200 Subject: [PATCH 3/5] missing snapshot --- .../http/__snapshots__/gitea.spec.ts.snap | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) 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 { From 11d536a211680ad842e641b32f4eb06bc4d50443 Mon Sep 17 00:00:00 2001 From: CirnoT <1447794+CirnoT@users.noreply.github.com> Date: Sat, 27 Jun 2020 09:39:12 +0200 Subject: [PATCH 4/5] requested changes --- lib/util/http/gitea.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/util/http/gitea.spec.ts b/lib/util/http/gitea.spec.ts index 792226a2216f0a..9c94a1b1d551d9 100644 --- a/lib/util/http/gitea.spec.ts +++ b/lib/util/http/gitea.spec.ts @@ -60,7 +60,7 @@ describe(getName(__filename), () => { .get('/pagination-example-2?page=3') .reply(200, { data: ['mno', 'pqr'] }); - const res = await giteaHttp.getJson<{ data: any }>('pagination-example-2', { + const res = await giteaHttp.getJson<{ data: string[] }>('pagination-example-2', { paginate: true, }); expect(res.body.data).toHaveLength(6); @@ -75,7 +75,7 @@ describe(getName(__filename), () => { .get('/pagination-example-3?page=2') .reply(200, { data: [] }); - const res = await giteaHttp.getJson<{ data: any }>('pagination-example-3', { + const res = await giteaHttp.getJson<{ data: string[] }>('pagination-example-3', { paginate: true, }); expect(res.body.data).toHaveLength(3); From 7a4fbb8b7886423b15b61b28f627238ae6244327 Mon Sep 17 00:00:00 2001 From: CirnoT <1447794+CirnoT@users.noreply.github.com> Date: Sat, 27 Jun 2020 09:45:46 +0200 Subject: [PATCH 5/5] satisfy linter --- lib/util/http/gitea.spec.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/util/http/gitea.spec.ts b/lib/util/http/gitea.spec.ts index 9c94a1b1d551d9..b24c852bd51ba6 100644 --- a/lib/util/http/gitea.spec.ts +++ b/lib/util/http/gitea.spec.ts @@ -60,9 +60,12 @@ describe(getName(__filename), () => { .get('/pagination-example-2?page=3') .reply(200, { data: ['mno', 'pqr'] }); - const res = await giteaHttp.getJson<{ data: string[] }>('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(); @@ -75,9 +78,12 @@ describe(getName(__filename), () => { .get('/pagination-example-3?page=2') .reply(200, { data: [] }); - const res = await giteaHttp.getJson<{ data: string[] }>('pagination-example-3', { - paginate: true, - }); + 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();