fix(backend-git-gateway): re-write GitHub pagination links (#3135)

This commit is contained in:
Erez Rokah 2020-01-24 04:14:33 +02:00 committed by Shawn Erquhart
parent 48afa8dfe4
commit 834f6b9e45
5 changed files with 30 additions and 4 deletions

View File

@ -95,4 +95,8 @@ export default class API extends GithubAPI {
body: JSON.stringify(commitParams),
});
}
nextUrlProcessor() {
return (url: string) => url.replace(/^(?:[a-z]+:\/\/.+?\/.+?\/.+?\/)/, `${this.apiRoot}/`);
}
}

View File

@ -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',
);
});
});
});

View File

@ -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)),
);

View File

@ -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);

View File

@ -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++;
}