From 86adca3a18f25ab74d1c6702bafab250f005ceec Mon Sep 17 00:00:00 2001 From: Bartholomew Date: Mon, 11 Nov 2019 16:06:29 +0100 Subject: [PATCH] fix(backend-github): editorial workflow commits (#2867) --- .../netlify-cms-backend-github/src/API.js | 2 +- .../src/__tests__/backendUtil.spec.js | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 packages/netlify-cms-lib-util/src/__tests__/backendUtil.spec.js diff --git a/packages/netlify-cms-backend-github/src/API.js b/packages/netlify-cms-backend-github/src/API.js index a62de887..b98d02a2 100644 --- a/packages/netlify-cms-backend-github/src/API.js +++ b/packages/netlify-cms-backend-github/src/API.js @@ -797,7 +797,7 @@ export default class API { * Get the list of commits for a given pull request. */ getPullRequestCommits(prNumber) { - return this.request(`${this.originRepoURL}/pulls/${prNumber}/commits`); + return this.requestAllPages(`${this.originRepoURL}/pulls/${prNumber}/commits`); } /** diff --git a/packages/netlify-cms-lib-util/src/__tests__/backendUtil.spec.js b/packages/netlify-cms-lib-util/src/__tests__/backendUtil.spec.js new file mode 100644 index 00000000..97e5ed0c --- /dev/null +++ b/packages/netlify-cms-lib-util/src/__tests__/backendUtil.spec.js @@ -0,0 +1,71 @@ +import { parseLinkHeader, getAllResponses } from '../backendUtil'; +import { oneLine } from 'common-tags'; +import nock from 'nock'; + +describe('parseLinkHeader', () => { + it('should return the right rel urls', () => { + const url = 'https://api.github.com/resource'; + const link = oneLine` + <${url}?page=1>; rel="first", + <${url}?page=2>; rel="prev", + <${url}?page=4>; rel="next", + <${url}?page=5>; rel="last" + `; + const linkHeader = parseLinkHeader(link); + + expect(linkHeader.next).toBe(`${url}?page=4`); + expect(linkHeader.last).toBe(`${url}?page=5`); + expect(linkHeader.first).toBe(`${url}?page=1`); + expect(linkHeader.prev).toBe(`${url}?page=2`); + }); +}); + +describe('getAllResponses', () => { + const generatePulls = length => { + return Array.from({ length }, (_, id) => { + return { id: id + 1, number: `134${id}`, state: 'open' }; + }); + }; + + function createLinkHeaders({ page, pageCount }) { + const pageNum = parseInt(page, 10); + const pageCountNum = parseInt(pageCount, 10); + const url = 'https://api.github.com/pulls'; + const link = linkPage => `<${url}?page=${linkPage}>`; + + const linkHeader = oneLine` + ${pageNum === 1 ? '' : `${link(1)}; rel="first",`} + ${pageNum === pageCountNum ? '' : `${link(pageCount)}; rel="last",`} + ${pageNum === 1 ? '' : `${link(pageNum - 1)}; rel="prev",`} + ${pageNum === pageCountNum ? '' : `${link(pageNum + 1)}; rel="next",`} + `.slice(0, -1); + + return { Link: linkHeader }; + } + + function interceptCall({ perPage = 30, repeat = 1, data = [] } = {}) { + nock('https://api.github.com') + .get('/pulls') + .query(true) + .times(repeat) + .reply(uri => { + const searchParams = new URLSearchParams(uri.split('?')[1]); + const page = searchParams.get('page') || 1; + const pageCount = data.length <= perPage ? 1 : Math.ceil(data.length / perPage); + const pageLastIndex = page * perPage; + const pageFirstIndex = pageLastIndex - perPage; + const resp = data.slice(pageFirstIndex, pageLastIndex); + return [200, resp, createLinkHeaders({ page, pageCount })]; + }); + } + + it('should return all paged response', async () => { + interceptCall({ repeat: 3, data: generatePulls(70) }); + const res = await getAllResponses('https://api.github.com/pulls'); + const pages = await Promise.all(res.map(res => res.json())); + + expect(pages[0]).toHaveLength(30); + expect(pages[1]).toHaveLength(30); + expect(pages[2]).toHaveLength(10); + }); +});