Skip to content

Commit

Permalink
fix(backend-git-gateway): re-write GitHub pagination links (#3135)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah authored and erquhart committed Jan 24, 2020
1 parent 48afa8d commit 834f6b9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/netlify-cms-backend-git-gateway/src/GitHubAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,8 @@ export default class API extends GithubAPI {
body: JSON.stringify(commitParams),
});
}

nextUrlProcessor() {
return (url: string) => url.replace(/^(?:[a-z]+:\/\/.+?\/.+?\/.+?\/)/, `${this.apiRoot}/`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
});
});
});
11 changes: 10 additions & 1 deletion packages/netlify-cms-backend-github/src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,19 @@ export default class API {
.catch(error => this.handleRequestError(error, responseStatus));
}

nextUrlProcessor() {
return (url: string) => url;
}

async requestAllPages<T>(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)),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions packages/netlify-cms-lib-util/src/backendUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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++;
}
Expand Down

0 comments on commit 834f6b9

Please sign in to comment.