Skip to content

Implement searchRepos for Bitbucket #18856

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

Merged
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
1 change: 1 addition & 0 deletions components/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@octokit/rest": "18.6.1",
"@probot/get-private-key": "^1.1.1",
"@types/jaeger-client": "^3.18.3",
"async-batch": "^1.1.2",
"base-64": "^1.0.0",
"bitbucket": "^2.7.0",
"body-parser": "^1.19.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,21 @@ export class BitbucketServerRepositoryProvider implements RepositoryProvider {
return commits.map((c) => c.id);
}

// TODO: implement repo search
public async searchRepos(user: User, searchString: string): Promise<RepositoryInfo[]> {
return [];
// Only load 1 page of 10 results for our searchString
const results = await this.api.getRepos(user, { maxPages: 1, limit: 10, searchString });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bitbucket Server search is limited to prefix searching only, so the name must match from the beginning. For example:

test would match test-repo but would not match repo-test


const repos: RepositoryInfo[] = [];
results.forEach((r) => {
const cloneUrl = r.links.clone.find((u) => u.name === "http")?.href;
if (cloneUrl) {
repos.push({
url: cloneUrl.replace("http://", "https://"),
name: r.name,
});
}
});

return repos;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { URL } from "url";
import { RepoURL } from "../repohost/repo-url";
import { RepositoryProvider } from "../repohost/repository-provider";
import { BitbucketApiFactory } from "./bitbucket-api-factory";
import asyncBatch from "async-batch";

@injectable()
export class BitbucketRepositoryProvider implements RepositoryProvider {
Expand Down Expand Up @@ -157,8 +158,63 @@ export class BitbucketRepositoryProvider implements RepositoryProvider {
return commits.map((v: Schema.Commit) => v.hash!);
}

// TODO: implement repo search
//
// Searching Bitbucket requires a workspace to be specified
// This results in a two step process:
// 1. Get all workspaces for the user
// 2. Fan out and search each workspace for the repos
//
public async searchRepos(user: User, searchString: string): Promise<RepositoryInfo[]> {
return [];
const api = await this.apiFactory.create(user);

const workspaces = await api.workspaces.getWorkspaces({ pagelen: 25 });

const workspaceSlugs: string[] = (
workspaces.data.values?.map((w) => {
return w.slug || "";
}) ?? []
).filter(Boolean);

if (workspaceSlugs.length === 0) {
return [];
}

// Batch our requests to the api so we only make up to 5 calls in parallel
const results = await asyncBatch(
workspaceSlugs,
async (workspaceSlug) => {
return api.repositories.list({
workspace: workspaceSlug,
// name includes searchString
q: `name ~ "${searchString}"`,
// sort by most recently updatd first
sort: "-updated_on",
// limit to the first 10 results per workspace
pagelen: 10,
});
},
// 5 calls in parallel
5,
);

// Convert into RepositoryInfo
const repos: RepositoryInfo[] = [];

results
.map((result) => {
return result.data.values ?? [];
})
// flatten out the array of arrays
.flat()
// convert into the format we want to return
.forEach((repo) => {
const name = repo.name;
const url = repo.links?.html?.href;
if (name && url) {
repos.push({ name, url });
}
});

return repos;
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4424,6 +4424,11 @@ ast-types-flow@^0.0.7:
resolved "https://registry.npmjs.org/ast-types-flow/-/ast-types-flow-0.0.7.tgz"
integrity sha1-9wtzXGvKGlycItmCw+Oef+ujva0=

async-batch@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/async-batch/-/async-batch-1.1.2.tgz#ecac7dee0f02a1a3a846438ba7d3f96746239d79"
integrity sha512-tBR4YIKi7s1cCtNHuguyXVBdRSkS6N8OhM1czrpE0W2j2yovj/IAxXD187cNX0d+kGxBbZLPwW5ASonYuZJprA==

async@^3.2.3:
version "3.2.4"
resolved "https://registry.yarnpkg.com/async/-/async-3.2.4.tgz#2d22e00f8cddeb5fde5dd33522b56d1cf569a81c"
Expand Down