From dde453655dd8ee16b9075beead9d842ddd254beb Mon Sep 17 00:00:00 2001 From: erezrokah Date: Thu, 23 Jan 2020 18:38:32 +0200 Subject: [PATCH] fix(backend-git-gateway): re-write GitHub pagination links --- .../netlify-cms-backend-git-gateway/src/GitHubAPI.ts | 4 ++++ .../src/__tests__/GitHubAPI.spec.js | 12 ++++++++++++ packages/netlify-cms-backend-github/src/API.ts | 11 ++++++++++- .../src/__tests__/backendUtil.spec.js | 2 +- packages/netlify-cms-lib-util/src/backendUtil.ts | 5 +++-- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/packages/netlify-cms-backend-git-gateway/src/GitHubAPI.ts b/packages/netlify-cms-backend-git-gateway/src/GitHubAPI.ts index 3fc919489d08..f502b2828496 100644 --- a/packages/netlify-cms-backend-git-gateway/src/GitHubAPI.ts +++ b/packages/netlify-cms-backend-git-gateway/src/GitHubAPI.ts @@ -95,4 +95,8 @@ export default class API extends GithubAPI { body: JSON.stringify(commitParams), }); } + + nextUrlProcessor() { + return (url: string) => url.replace(/^(?:[a-z]+:\/\/.+?\/.+?\/.+?\/)/, `${this.apiRoot}/`); + } } diff --git a/packages/netlify-cms-backend-git-gateway/src/__tests__/GitHubAPI.spec.js b/packages/netlify-cms-backend-git-gateway/src/__tests__/GitHubAPI.spec.js index 381c44284e66..ab4f4c282f14 100644 --- a/packages/netlify-cms-backend-git-gateway/src/__tests__/GitHubAPI.spec.js +++ b/packages/netlify-cms-backend-git-gateway/src/__tests__/GitHubAPI.spec.js @@ -84,4 +84,16 @@ describe('github API', () => { ); }); }); + + describe('nextUrlProcessor', () => { + it('should re-write github url', () => { + const api = new API({ + apiRoot: 'https://site.netlify.com/.netlify/git/github', + }); + + expect(api.nextUrlProcessor()('https://api.github.com/repositories/10000/pulls')).toEqual( + 'https://site.netlify.com/.netlify/git/github/pulls', + ); + }); + }); }); diff --git a/packages/netlify-cms-backend-github/src/API.ts b/packages/netlify-cms-backend-github/src/API.ts index 555120c73ea9..d1927ab9a4e1 100644 --- a/packages/netlify-cms-backend-github/src/API.ts +++ b/packages/netlify-cms-backend-github/src/API.ts @@ -264,10 +264,19 @@ export default class API { .catch(error => this.handleRequestError(error, responseStatus)); } + nextUrlProcessor() { + return (url: string) => url; + } + async requestAllPages(url: string, options: Options = {}) { const headers = await this.requestHeaders(options.headers || {}); const processedURL = this.urlFor(url, options); - const allResponses = await getAllResponses(processedURL, { ...options, headers }); + const allResponses = await getAllResponses( + processedURL, + { ...options, headers }, + 'next', + this.nextUrlProcessor(), + ); const pages: T[][] = await Promise.all( allResponses.map((res: Response) => this.parseResponse(res)), ); diff --git a/packages/netlify-cms-lib-util/src/__tests__/backendUtil.spec.js b/packages/netlify-cms-lib-util/src/__tests__/backendUtil.spec.js index 0e6b562facf8..9d603c0caba3 100644 --- a/packages/netlify-cms-lib-util/src/__tests__/backendUtil.spec.js +++ b/packages/netlify-cms-lib-util/src/__tests__/backendUtil.spec.js @@ -66,7 +66,7 @@ describe('getAllResponses', () => { it('should return all paged response', async () => { interceptCall({ repeat: 3, data: generatePulls(70) }); - const res = await getAllResponses('https://api.github.com/pulls'); + const res = await getAllResponses('https://api.github.com/pulls', {}, 'next', url => url); const pages = await Promise.all(res.map(res => res.json())); expect(pages[0]).toHaveLength(30); diff --git a/packages/netlify-cms-lib-util/src/backendUtil.ts b/packages/netlify-cms-lib-util/src/backendUtil.ts index 73d16f11ed6e..0250f085a96c 100644 --- a/packages/netlify-cms-lib-util/src/backendUtil.ts +++ b/packages/netlify-cms-lib-util/src/backendUtil.ts @@ -80,7 +80,8 @@ export const parseLinkHeader = flow([ export const getAllResponses = async ( url: string, options: { headers?: {} } = {}, - linkHeaderRelName = 'next', + linkHeaderRelName: string, + nextUrlProcessor: (url: string) => string, ) => { const maxResponses = 30; let responseCount = 1; @@ -95,7 +96,7 @@ export const getAllResponses = async ( const nextURL = linkHeader && parseLinkHeader(linkHeader)[linkHeaderRelName]; const { headers = {} } = options; - req = nextURL && unsentRequest.fromFetchArguments(nextURL, { headers }); + req = nextURL && unsentRequest.fromFetchArguments(nextUrlProcessor(nextURL), { headers }); pageResponses.push(pageResponse); responseCount++; }