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 paginate http option #22135

Merged
merged 3 commits into from May 13, 2023
Merged
Changes from 1 commit
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
78 changes: 71 additions & 7 deletions lib/util/http/bitbucket.ts
@@ -1,4 +1,7 @@
import type { HttpOptions, HttpResponse, InternalHttpOptions } from './types';
import is from '@sindresorhus/is';
import type { PagedResult } from '../../modules/platform/bitbucket/types';
import { parseUrl, resolveBaseUrl } from '../url';
import type { HttpOptions, HttpResponse } from './types';
import { Http } from '.';

let baseUrl = 'https://api.bitbucket.org/';
Expand All @@ -7,16 +10,77 @@
baseUrl = url;
};

export class BitbucketHttp extends Http {
constructor(type = 'bitbucket', options?: HttpOptions) {
export interface BitbucketHttpOptions extends HttpOptions {
paginate?: boolean;
}

export class BitbucketHttp extends Http<BitbucketHttpOptions> {
constructor(type = 'bitbucket', options?: BitbucketHttpOptions) {
super(type, options);
}

protected override request<T>(
url: string | URL,
options?: InternalHttpOptions
protected override async request<T>(
path: string,
options?: BitbucketHttpOptions
): Promise<HttpResponse<T>> {
const opts = { baseUrl, ...options };
return super.request<T>(url, opts);

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

if (opts.paginate && isPagedResult(result.body)) {
const resultBody = result.body as PagedResult<T>;

Check warning on line 31 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L31

Added line #L31 was not covered by tests

let nextPage = getPageFromURL(resultBody.next);

Check warning on line 33 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L33

Added line #L33 was not covered by tests

while (is.nonEmptyString(nextPage)) {
const nextPath = getNextPagePath(path, nextPage);

Check warning on line 36 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L35-L36

Added lines #L35 - L36 were not covered by tests

if (is.nullOrUndefined(nextPath)) {
break;

Check warning on line 39 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L38-L39

Added lines #L38 - L39 were not covered by tests
}

const nextResult = await super.request<PagedResult<T>>(

Check warning on line 42 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L42

Added line #L42 was not covered by tests
nextPath,
options
);

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

Check warning on line 47 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L47

Added line #L47 was not covered by tests

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

Check warning on line 49 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L49

Added line #L49 was not covered by tests
}

// Override other page-related attributes
resultBody.pagelen = resultBody.values.length;
resultBody.size = resultBody.values.length;
resultBody.next = undefined;

Check warning on line 55 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L53-L55

Added lines #L53 - L55 were not covered by tests
}

return result;
}
}

function getPageFromURL(url: string | undefined): string | null {
const resolvedURL = parseUrl(url);

Check warning on line 63 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L62-L63

Added lines #L62 - L63 were not covered by tests

if (is.nullOrUndefined(resolvedURL)) {
return null;

Check warning on line 66 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L65-L66

Added lines #L65 - L66 were not covered by tests
}

return resolvedURL.searchParams.get('page');

Check warning on line 69 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L69

Added line #L69 was not covered by tests
}

function getNextPagePath(path: string, nextPage: string): string | null {
const resolvedURL = parseUrl(resolveBaseUrl(baseUrl, path));

Check warning on line 73 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L72-L73

Added lines #L72 - L73 were not covered by tests

if (is.nullOrUndefined(resolvedURL)) {
return null;

Check warning on line 76 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L75-L76

Added lines #L75 - L76 were not covered by tests
}

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

Check warning on line 79 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L79

Added line #L79 was not covered by tests

return resolvedURL.toString();

Check warning on line 81 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L81

Added line #L81 was not covered by tests
}

function isPagedResult(obj: any): obj is PagedResult {
return is.nonEmptyObject(obj) && Array.isArray(obj.values);

Check warning on line 85 in lib/util/http/bitbucket.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/bitbucket.ts#L84-L85

Added lines #L84 - L85 were not covered by tests
}