-
Notifications
You must be signed in to change notification settings - Fork 96
fix: search route #378
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
fix: search route #378
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
5579280
search
AmirSa12 3062d20
update
AmirSa12 da97d56
update
AmirSa12 ca5d51e
update
AmirSa12 effc3e7
update stream
AmirSa12 f5a2472
better streams
AmirSa12 2631484
update streams
AmirSa12 fb822d1
request cancellation
AmirSa12 c0e9bab
cleanup
AmirSa12 160a070
revisions
AmirSa12 ab9f193
test - not sure
AmirSa12 da70d46
prettier
AmirSa12 148eb35
another test
AmirSa12 826d25b
cleanup - another test
AmirSa12 ac15156
update - test
AmirSa12 d264736
update - Jaccard algo
AmirSa12 a8cd641
handle console errs
AmirSa12 5e3294e
trigger ci
AmirSa12 c6b49aa
track time
AmirSa12 0808df6
cf headers
AmirSa12 54a022d
headers
AmirSa12 30598d9
fix time log
AmirSa12 7b095ae
test
AmirSa12 2ba90f4
benchmark
AmirSa12 a4ccd1f
update workflow
AmirSa12 0bc4ada
update workflow
AmirSa12 f907ba5
update workflow
AmirSa12 8e93cdd
revert
AmirSa12 999fc04
remove streams
AmirSa12 2b99b56
empty - trigger ci
AmirSa12 ddcca9c
use package
AmirSa12 a3ed3cc
revisions
AmirSa12 e795c7d
no score
AmirSa12 a4929f8
update
AmirSa12 f8759a6
update
AmirSa12 9902758
config
AmirSa12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,86 +1,66 @@ | ||
| import { z } from "zod"; | ||
| import { useOctokitApp } from "../../utils/octokit"; | ||
| import stringSimilarity from "string-similarity"; | ||
|
|
||
| const querySchema = z.object({ | ||
| text: z.string(), | ||
| }); | ||
|
|
||
| export default defineEventHandler(async (event) => { | ||
| const r2Binding = useBinding(event); | ||
| const request = toWebRequest(event); | ||
| const signal = request.signal; | ||
|
|
||
| try { | ||
| const query = await getValidatedQuery(event, (data) => | ||
| querySchema.parse(data), | ||
| ); | ||
| if (!query.text) return { nodes: [] }; | ||
|
|
||
| if (!query.text) { | ||
| return { nodes: [] }; | ||
| } | ||
|
|
||
| const app = useOctokitApp(event); | ||
| const searchText = query.text.toLowerCase(); | ||
| const matches: RepoNode[] = []; | ||
|
|
||
| // Internal pagination: iterate until uniqueNodes is filled or no more objects | ||
| let cursor: string | undefined; | ||
| const seen = new Set<string>(); | ||
| const uniqueNodes = []; | ||
| const maxNodes = 10; | ||
| let keepGoing = true; | ||
| await app.eachRepository(async ({ repository }) => { | ||
| if (signal.aborted) return; | ||
| if (repository.private) return; | ||
|
|
||
| while (uniqueNodes.length < maxNodes && keepGoing && !signal.aborted) { | ||
| const listResult = await r2Binding.list({ | ||
| prefix: usePackagesBucket.base, | ||
| limit: 1000, | ||
| cursor, | ||
| }); | ||
| const { objects, truncated } = listResult; | ||
| cursor = truncated ? listResult.cursor : undefined; | ||
| const repoName = repository.name.toLowerCase(); | ||
| const ownerLogin = repository.owner.login.toLowerCase(); | ||
|
|
||
| const nameScore = stringSimilarity.compareTwoStrings( | ||
| repoName, | ||
| searchText, | ||
| ); | ||
| const ownerScore = stringSimilarity.compareTwoStrings( | ||
| ownerLogin, | ||
| searchText, | ||
| ); | ||
|
|
||
| for (const obj of objects) { | ||
| const parts = parseKey(obj.key); | ||
| const orgRepo = `${parts.org}/${parts.repo}`.toLowerCase(); | ||
| const applies = | ||
| parts.org.toLowerCase().includes(searchText) || | ||
| parts.repo.toLowerCase().includes(searchText) || | ||
| orgRepo.includes(searchText); | ||
| if (!applies) continue; | ||
| matches.push({ | ||
| id: repository.id, | ||
| name: repository.name, | ||
| owner: { | ||
| login: repository.owner.login, | ||
| avatarUrl: repository.owner.avatar_url, | ||
| }, | ||
| stars: repository.stargazers_count || 0, | ||
| score: Math.max(nameScore, ownerScore), | ||
| }); | ||
| }); | ||
|
|
||
| const key = `${parts.org}/${parts.repo}`; | ||
| if (!seen.has(key)) { | ||
| seen.add(key); | ||
| uniqueNodes.push({ | ||
| name: parts.repo, | ||
| owner: { | ||
| login: parts.org, | ||
| avatarUrl: `https://github.com/${parts.org}.png`, | ||
| }, | ||
| }); | ||
| if (uniqueNodes.length >= maxNodes) break; | ||
| } | ||
| } | ||
| matches.sort((a, b) => | ||
| b.score !== a.score ? b.score - a.score : b.stars - a.stars, | ||
| ); | ||
|
|
||
| if (!truncated || uniqueNodes.length >= maxNodes) { | ||
| keepGoing = false; | ||
| } | ||
| } | ||
| const top = matches.slice(0, 10).map((node) => ({ | ||
| id: node.id, | ||
| name: node.name, | ||
| owner: node.owner, | ||
| stars: node.stars, | ||
| })); | ||
|
|
||
| return { | ||
| nodes: uniqueNodes, | ||
| }; | ||
| return { nodes: top }; | ||
| } catch (error) { | ||
| console.error("Error in repository search:", error); | ||
| return { | ||
| nodes: [], | ||
| error: true, | ||
| message: (error as Error).message, | ||
| }; | ||
| return { nodes: [], error: true, message: (error as Error).message }; | ||
| } | ||
| }); | ||
|
|
||
| function parseKey(key: string) { | ||
| const parts = key.split(":"); | ||
| return { | ||
| org: parts[2], | ||
| repo: parts[3], | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.