fix(github): prioritize private repositories in clone search (#10264)
The general GitHub search can fill its result limit with public repositories before returning a private match. Fetch private matches separately so users can find them by name.
This commit is contained in:
1 parent
a0ed629b7d
commit
cba515ef73
2 files changed
+66
-23
No files matched your search
@@ -75,7 +75,7 @@ describe("GitHub repository search", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("searches accessible repositories for a typed query", async () => {
|
||||
it("respects an explicit visibility qualifier and normalizes the search result", async () => {
|
||||
const runner = createRunner([
|
||||
JSON.stringify([
|
||||
{
|
||||
@@ -96,7 +96,7 @@ describe("GitHub repository search", () => {
|
||||
});
|
||||
|
||||
await expect(
|
||||
service.searchRepositories({ cwd: "/tmp", query: " private project ", limit: 5 }),
|
||||
service.searchRepositories({ cwd: "/tmp", query: " is:private private project ", limit: 5 }),
|
||||
).resolves.toEqual([
|
||||
{
|
||||
id: "42",
|
||||
@@ -113,15 +113,15 @@ describe("GitHub repository search", () => {
|
||||
args: [
|
||||
"search",
|
||||
"repos",
|
||||
"private project",
|
||||
"is:private private project",
|
||||
"--json",
|
||||
"id,name,fullName,description,isPrivate,updatedAt,url",
|
||||
"--limit",
|
||||
"5",
|
||||
"--sort",
|
||||
"updated",
|
||||
"--order",
|
||||
"desc",
|
||||
"--limit",
|
||||
"5",
|
||||
],
|
||||
options: { cwd: "/tmp" },
|
||||
},
|
||||
@@ -131,4 +131,39 @@ describe("GitHub repository search", () => {
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("puts private matches first even when the general search omits some of them", async () => {
|
||||
const repository = (id: number, fullName: string, isPrivate: boolean) => ({
|
||||
id,
|
||||
name: fullName.split("/")[1],
|
||||
fullName,
|
||||
description: "An editor",
|
||||
isPrivate,
|
||||
updatedAt: "2026-07-14T08:00:00Z",
|
||||
url: `https://github.com/${fullName}`,
|
||||
});
|
||||
const privateEditor = repository(1, "example/editor", true);
|
||||
const publicEditor = repository(2, "public/recent-editor", false);
|
||||
const anotherPublicEditor = repository(3, "public/another-editor", false);
|
||||
const recentPrivateEditor = repository(4, "example/recent-editor", true);
|
||||
const service = createGitHubService({
|
||||
runner: async (args) => {
|
||||
if (args[0] === "config") {
|
||||
return { stdout: "https", stderr: "" };
|
||||
}
|
||||
const results = args.includes("--visibility=private")
|
||||
? [privateEditor, recentPrivateEditor]
|
||||
: [publicEditor, recentPrivateEditor, anotherPublicEditor];
|
||||
return { stdout: JSON.stringify(results), stderr: "" };
|
||||
},
|
||||
resolveGhPath: async () => "/usr/bin/gh",
|
||||
});
|
||||
|
||||
const results = await service.searchRepositories({ cwd: "/tmp", query: "editor", limit: 3 });
|
||||
expect(results.map((result) => result.nameWithOwner)).toEqual([
|
||||
"example/editor",
|
||||
"example/recent-editor",
|
||||
"public/recent-editor",
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -2639,26 +2639,34 @@ export function createGitHubService(options: CreateGitHubServiceOptions = {}): G
|
||||
return parseRepositoryList(stdout, cloneProtocol);
|
||||
}
|
||||
|
||||
const [stdout, cloneProtocol] = await Promise.all([
|
||||
run(
|
||||
[
|
||||
"search",
|
||||
"repos",
|
||||
query,
|
||||
"--json",
|
||||
"id,name,fullName,description,isPrivate,updatedAt,url",
|
||||
"--sort",
|
||||
"updated",
|
||||
"--order",
|
||||
"desc",
|
||||
"--limit",
|
||||
String(limit),
|
||||
],
|
||||
{ cwd: input.cwd },
|
||||
),
|
||||
const searchArgs = [
|
||||
"search",
|
||||
"repos",
|
||||
query,
|
||||
"--json",
|
||||
"id,name,fullName,description,isPrivate,updatedAt,url",
|
||||
"--limit",
|
||||
String(limit),
|
||||
];
|
||||
const hasVisibilityFilter = /(?:^|\s)-?is:(?:public|private|internal)(?=\s|$)/i.test(query);
|
||||
// The general search can hit its limit before returning a single private match.
|
||||
const [stdout, privateStdout, cloneProtocol] = await Promise.all([
|
||||
run([...searchArgs, "--sort", "updated", "--order", "desc"], { cwd: input.cwd }),
|
||||
hasVisibilityFilter
|
||||
? "[]"
|
||||
: run([...searchArgs, "--visibility=private"], { cwd: input.cwd }),
|
||||
resolveConfiguredCloneProtocol(input.cwd, run),
|
||||
]);
|
||||
return parseRepositorySearch(stdout, cloneProtocol);
|
||||
const repositories = parseRepositorySearch(stdout, cloneProtocol);
|
||||
const privateMatches = parseRepositorySearch(privateStdout, cloneProtocol);
|
||||
const ranked = new Map(
|
||||
[
|
||||
...privateMatches,
|
||||
...repositories.filter((repository) => repository.visibility === "private"),
|
||||
...repositories,
|
||||
].map((repository) => [repository.id, repository]),
|
||||
);
|
||||
return [...ranked.values()].slice(0, limit);
|
||||
},
|
||||
|
||||
async searchIssuesAndPrs(input) {
|
||||
|
||||
Reference in new issue
Block a user