Skip to content

Commit b79de07

Browse files
authored
fix: search endpoint (#395)
1 parent 0375ae7 commit b79de07

File tree

3 files changed

+55
-30
lines changed

3 files changed

+55
-30
lines changed

packages/app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@octokit/auth-unauthenticated": "^5.0.1",
4545
"@pkg-pr-new/utils": "workspace:^",
4646
"@simulacrum/github-api-simulator": "^0.5.4",
47+
"@types/string-similarity": "^4.0.2",
4748
"@typescript-eslint/eslint-plugin": "^5.62.0",
4849
"@typescript-eslint/parser": "^5.62.0",
4950
"cross-env": "^7.0.3",

packages/app/server/api/repo/search.get.ts

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { z } from "zod";
22
import { useOctokitApp } from "../../utils/octokit";
33
import stringSimilarity from "string-similarity";
4+
import type { RepoNode } from "../../utils/types";
45

56
const querySchema = z.object({
67
text: z.string(),
@@ -20,32 +21,47 @@ export default defineEventHandler(async (event) => {
2021
const searchText = query.text.toLowerCase();
2122
const matches: RepoNode[] = [];
2223

23-
await app.eachRepository(async ({ repository }) => {
24+
await app.eachInstallation(async ({ octokit, installation }) => {
2425
if (signal.aborted) return;
25-
if (repository.private) return;
26+
27+
if (installation.suspended_at) {
28+
console.warn(`Skipping suspended installation ${installation.id}`);
29+
return;
30+
}
2631

27-
const repoName = repository.name.toLowerCase();
28-
const ownerLogin = repository.owner.login.toLowerCase();
32+
try {
33+
const repos = await octokit.paginate("GET /installation/repositories");
2934

30-
const nameScore = stringSimilarity.compareTwoStrings(
31-
repoName,
32-
searchText,
33-
);
34-
const ownerScore = stringSimilarity.compareTwoStrings(
35-
ownerLogin,
36-
searchText,
37-
);
35+
for (const repository of repos) {
36+
if (signal.aborted) return;
37+
if (repository.private) continue;
3838

39-
matches.push({
40-
id: repository.id,
41-
name: repository.name,
42-
owner: {
43-
login: repository.owner.login,
44-
avatarUrl: repository.owner.avatar_url,
45-
},
46-
stars: repository.stargazers_count || 0,
47-
score: Math.max(nameScore, ownerScore),
48-
});
39+
const repoName = repository.name.toLowerCase();
40+
const ownerLogin = repository.owner.login.toLowerCase();
41+
42+
const nameScore = stringSimilarity.compareTwoStrings(
43+
repoName,
44+
searchText,
45+
);
46+
const ownerScore = stringSimilarity.compareTwoStrings(
47+
ownerLogin,
48+
searchText,
49+
);
50+
51+
matches.push({
52+
id: repository.id,
53+
name: repository.name,
54+
owner: {
55+
login: repository.owner.login,
56+
avatarUrl: repository.owner.avatar_url,
57+
},
58+
stars: repository.stargazers_count || 0,
59+
score: Math.max(nameScore, ownerScore),
60+
});
61+
}
62+
} catch (error) {
63+
console.warn(`Error fetching repositories for installation ${installation.id}:`, error);
64+
}
4965
});
5066

5167
matches.sort((a, b) =>

pnpm-lock.yaml

Lines changed: 16 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)