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

feat(bitbucket): add support for pagelen #22278

Merged
merged 8 commits into from May 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 18 additions & 6 deletions lib/modules/platform/bitbucket/index.spec.ts
Expand Up @@ -813,7 +813,9 @@ describe('modules/platform/bitbucket/index', () => {
};
const scope = await initRepoMock();
scope
.get('/2.0/repositories/some/repo/effective-default-reviewers')
.get(
'/2.0/repositories/some/repo/effective-default-reviewers?pagelen=100'
)
.reply(200, {
values: [projectReviewer, repoReviewer],
})
Expand Down Expand Up @@ -855,7 +857,9 @@ describe('modules/platform/bitbucket/index', () => {
};
const scope = await initRepoMock();
scope
.get('/2.0/repositories/some/repo/effective-default-reviewers')
.get(
'/2.0/repositories/some/repo/effective-default-reviewers?pagelen=100'
)
.reply(200, {
values: [
activeReviewerWithinWorkspace,
Expand Down Expand Up @@ -924,7 +928,9 @@ describe('modules/platform/bitbucket/index', () => {
};
const scope = await initRepoMock();
scope
.get('/2.0/repositories/some/repo/effective-default-reviewers')
.get(
'/2.0/repositories/some/repo/effective-default-reviewers?pagelen=100'
)
.reply(200, {
values: [memberReviewer, notMemberReviewer],
})
Expand Down Expand Up @@ -973,7 +979,9 @@ describe('modules/platform/bitbucket/index', () => {
};
const scope = await initRepoMock();
scope
.get('/2.0/repositories/some/repo/effective-default-reviewers')
.get(
'/2.0/repositories/some/repo/effective-default-reviewers?pagelen=100'
)
.reply(200, {
values: [reviewer],
})
Expand Down Expand Up @@ -1017,7 +1025,9 @@ describe('modules/platform/bitbucket/index', () => {

const scope = await initRepoMock();
scope
.get('/2.0/repositories/some/repo/effective-default-reviewers')
.get(
'/2.0/repositories/some/repo/effective-default-reviewers?pagelen=100'
)
.reply(200, {
values: [reviewer],
})
Expand Down Expand Up @@ -1054,7 +1064,9 @@ describe('modules/platform/bitbucket/index', () => {

const scope = await initRepoMock();
scope
.get('/2.0/repositories/some/repo/effective-default-reviewers')
.get(
'/2.0/repositories/some/repo/effective-default-reviewers?pagelen=100'
)
.reply(200, {
values: [reviewer],
})
Expand Down
77 changes: 70 additions & 7 deletions lib/util/http/bitbucket.spec.ts
Expand Up @@ -56,27 +56,90 @@ describe('util/http/bitbucket', () => {
});
});

it('paginates', async () => {
it('paginates: adds default pagelen if non is present', async () => {
httpMock
.scope(baseUrl)
.get('/some-url')
.get('/some-url?foo=bar&pagelen=100')
.reply(200, {
values: ['a'],
page: '1',
next: `${baseUrl}/some-url?page=2`,
next: `${baseUrl}/some-url?foo=bar&pagelen=100&page=2`,
})
.get('/some-url?page=2')
.get('/some-url?foo=bar&pagelen=100&page=2')
.reply(200, {
values: ['b', 'c'],
page: '2',
next: `${baseUrl}/some-url?page=3`,
next: `${baseUrl}/some-url?foo=bar&pagelen=100&page=3`,
})
.get('/some-url?page=3')
.get('/some-url?foo=bar&pagelen=100&page=3')
.reply(200, {
values: ['d'],
page: '3',
});
const res = await api.getJson('some-url', { paginate: true });
const res = await api.getJson('/some-url?foo=bar', { paginate: true });
expect(res.body).toEqual({
page: '1',
pagelen: 4,
size: 4,
values: ['a', 'b', 'c', 'd'],
next: undefined,
});
});

it('paginates: respects pagelen if already set in path', async () => {
httpMock
.scope(baseUrl)
.get('/some-url?pagelen=10')
.reply(200, {
values: ['a'],
page: '1',
next: `${baseUrl}/some-url?pagelen=10&page=2`,
})
.get('/some-url?pagelen=10&page=2')
.reply(200, {
values: ['b', 'c'],
page: '2',
next: `${baseUrl}/some-url?pagelen=10&page=3`,
})
.get('/some-url?pagelen=10&page=3')
.reply(200, {
values: ['d'],
page: '3',
});
const res = await api.getJson('some-url?pagelen=10', { paginate: true });
expect(res.body).toEqual({
page: '1',
pagelen: 4,
size: 4,
values: ['a', 'b', 'c', 'd'],
next: undefined,
});
});

it('paginates: respects pagelen if set in options', async () => {
httpMock
.scope(baseUrl)
.get('/some-url?pagelen=20')
.reply(200, {
values: ['a'],
page: '1',
next: `${baseUrl}/some-url?pagelen=20&page=2`,
})
.get('/some-url?pagelen=20&page=2')
.reply(200, {
values: ['b', 'c'],
page: '2',
next: `${baseUrl}/some-url?pagelen=20&page=3`,
})
.get('/some-url?pagelen=20&page=3')
.reply(200, {
values: ['d'],
page: '3',
});
const res = await api.getJson('some-url', {
paginate: true,
pagelen: 20,
});
expect(res.body).toEqual({
page: '1',
pagelen: 4,
Expand Down
42 changes: 23 additions & 19 deletions lib/util/http/bitbucket.ts
Expand Up @@ -4,6 +4,8 @@ import { parseUrl, resolveBaseUrl } from '../url';
import type { HttpOptions, HttpResponse } from './types';
import { Http } from '.';

const MAX_PAGELEN = 100;

let baseUrl = 'https://api.bitbucket.org/';

export const setBaseUrl = (url: string): void => {
Expand All @@ -12,6 +14,7 @@ export const setBaseUrl = (url: string): void => {

export interface BitbucketHttpOptions extends HttpOptions {
paginate?: boolean;
pagelen?: number;
}

export class BitbucketHttp extends Http<BitbucketHttpOptions> {
Expand All @@ -25,29 +28,29 @@ export class BitbucketHttp extends Http<BitbucketHttpOptions> {
): Promise<HttpResponse<T>> {
const opts = { baseUrl, ...options };

const result = await super.request<T>(path, opts);
let pathWithPagelen = path;

if (opts.paginate && isPagedResult(result.body)) {
const resultBody = result.body as PagedResult<T>;
if (opts.paginate && !hasPagelen(pathWithPagelen)) {
const pagelen = opts.pagelen ?? MAX_PAGELEN;
pathWithPagelen = addPagelenToPath(pathWithPagelen, pagelen);
}

let nextPage = getPageFromURL(resultBody.next);
const result = await super.request<T>(pathWithPagelen, opts);

while (is.nonEmptyString(nextPage)) {
const nextPath = getNextPagePath(path, nextPage);
if (opts.paginate && isPagedResult(result.body)) {
const resultBody = result.body as PagedResult<T>;

// istanbul ignore if
if (is.nullOrUndefined(nextPath)) {
break;
}
let nextURL = resultBody.next;

while (is.nonEmptyString(nextURL)) {
setchy marked this conversation as resolved.
Show resolved Hide resolved
const nextResult = await super.request<PagedResult<T>>(
nextPath,
nextURL,
options
);

resultBody.values.push(...nextResult.body.values);

nextPage = getPageFromURL(nextResult.body?.next);
nextURL = nextResult.body?.next;
}

// Override other page-related attributes
Expand All @@ -60,25 +63,26 @@ export class BitbucketHttp extends Http<BitbucketHttpOptions> {
}
}

function getPageFromURL(url: string | undefined): string | null {
const resolvedURL = parseUrl(url);
function hasPagelen(path: string): boolean {
const resolvedURL = parseUrl(resolveBaseUrl(baseUrl, path));

// istanbul ignore if
if (is.nullOrUndefined(resolvedURL)) {
return null;
return false;
}

return resolvedURL.searchParams.get('page');
return !is.nullOrUndefined(resolvedURL.searchParams.get('pagelen'));
}

function getNextPagePath(path: string, nextPage: string): string | null {
function addPagelenToPath(path: string, pagelen: number): string {
const resolvedURL = parseUrl(resolveBaseUrl(baseUrl, path));
setchy marked this conversation as resolved.
Show resolved Hide resolved

// istanbul ignore if
if (is.nullOrUndefined(resolvedURL)) {
return null;
return path;
}

resolvedURL.searchParams.set('page', nextPage);
resolvedURL.searchParams.set('pagelen', pagelen.toString());

return resolvedURL.toString();
}
Expand Down