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

[Search npm] Fix URL parsing issue #16824

Merged
merged 2 commits into from
Feb 5, 2025
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
4 changes: 4 additions & 0 deletions extensions/search-npm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Search npm Changelog

## [Fixes] - 2025-02-05

- Fix URL parsing issue

## [Added a shortcut] - 2025-01-22

- Added a shortcut to copy the package version
Expand Down
50 changes: 31 additions & 19 deletions extensions/search-npm/src/utils/parseRepoUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,38 @@ interface ParseRepoUrlResponse {
}

export const parseRepoUrl = (repoUrl?: string): ParseRepoUrlResponse => {
if (!repoUrl) {
return {
owner: null,
name: null,
type: undefined,
repoUrl: undefined,
}
const invalidUrl = {
owner: null,
name: null,
type: undefined,
repoUrl: undefined,
}
const parsedUrl = gitUrlParse(repoUrl)
const cleanedUrl = cleanGitUrl(parsedUrl.toString('https'))
const isGithubRepo = cleanedUrl.includes('github.com')
const isGitlabRepo = cleanedUrl.includes('gitlab.com')
const owner = parsedUrl.owner
const name = parsedUrl.name
const type = isGithubRepo ? 'github' : isGitlabRepo ? 'gitlab' : undefined
if (!repoUrl) return invalidUrl

try {
try {
const url = new URL(repoUrl)
url.protocol = 'https:'
repoUrl = url.toString()
} catch {
// `get-url-parse` doesn't support some protocols like `git+https`.
// So we force replaced the protocol to `https:`.
}
const parsedUrl = gitUrlParse(repoUrl)
const cleanedUrl = cleanGitUrl(parsedUrl.toString('https'))
const isGithubRepo = cleanedUrl.includes('github.com')
const isGitlabRepo = cleanedUrl.includes('gitlab.com')
const owner = parsedUrl.owner
const name = parsedUrl.name
const type = isGithubRepo ? 'github' : isGitlabRepo ? 'gitlab' : undefined

return {
owner,
name,
type,
repoUrl: cleanedUrl,
return {
owner,
name,
type,
repoUrl: cleanedUrl,
}
} catch {
return invalidUrl
}
}