fix(github): paginationField for check runs (#10003)

This commit is contained in:
Rhys Arkins 2021-05-15 12:14:34 +02:00 committed by GitHub
parent b210ef668f
commit f946b7407e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 3 deletions

View file

@ -920,6 +920,7 @@ export async function getBranchStatus(
accept: 'application/vnd.github.antiope-preview+json',
},
paginate: true,
paginationField: 'check_runs',
};
const checkRunsRaw = (
await githubApi.getJson<{

View file

@ -59,6 +59,36 @@ describe(getName(), () => {
const trace = httpMock.getTrace();
expect(trace).toHaveLength(3);
});
it('uses paginationField', async () => {
const url = '/some-url';
httpMock
.scope(githubApiHost)
.get(url)
.reply(
200,
{ the_field: ['a'], total: 4 },
{
link: `<${url}?page=2>; rel="next", <${url}?page=3>; rel="last"`,
}
)
.get(`${url}?page=2`)
.reply(
200,
{ the_field: ['b', 'c'], total: 4 },
{
link: `<${url}?page=3>; rel="next", <${url}?page=3>; rel="last"`,
}
)
.get(`${url}?page=3`)
.reply(200, { the_field: ['d'], total: 4 });
const res: any = await githubApi.getJson('some-url', {
paginate: true,
paginationField: 'the_field',
});
expect(res.body.the_field).toEqual(['a', 'b', 'c', 'd']);
const trace = httpMock.getTrace();
expect(trace).toHaveLength(3);
});
it('attempts to paginate', async () => {
const url = '/some-url';
httpMock

View file

@ -25,6 +25,7 @@ interface GithubInternalOptions extends InternalHttpOptions {
export interface GithubHttpOptions extends InternalHttpOptions {
paginate?: boolean | string;
paginationField?: string;
pageLimit?: number;
token?: string;
}
@ -208,12 +209,22 @@ export class GithubHttp extends Http<GithubHttpOptions, GithubHttpOptions> {
}
);
const pages = await pAll(queue, { concurrency: 5 });
if (opts.paginationField) {
result.body[opts.paginationField] = result.body[
opts.paginationField
].concat(
...pages
.filter(Boolean)
.map((page) => page.body[opts.paginationField])
);
} else {
result.body = result.body.concat(
...pages.filter(Boolean).map((page) => page.body)
);
}
}
}
}
} catch (err) {
handleGotError(err, url, opts);
}