Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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