Skip to content

Commit cc55e8d

Browse files
author
Avish Porwal
committed
Fix issue-743 by sanitizing the URL
1 parent 52e3744 commit cc55e8d

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

cmd/publisher/commands/init.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ func InitCommand() error {
2323

2424
// Detect if we're in a subdirectory of the git repository
2525
subfolder := detectSubfolder()
26+
packageType := detectPackageType()
2627

2728
// Try to detect values from environment
28-
name := detectServerName(subfolder)
29+
name := detectServerName(subfolder, packageType)
2930
description := detectDescription()
3031
version := "1.0.0"
31-
repoURL := detectRepoURL()
32+
packageIdentifier := detectPackageIdentifier(name, packageType)
33+
repoURL := detectRepoURL(packageType)
3234
repoSource := MethodGitHub
3335
if repoURL != "" && !strings.Contains(repoURL, "github.com") {
3436
if strings.Contains(repoURL, "gitlab.com") {
@@ -38,9 +40,6 @@ func InitCommand() error {
3840
}
3941
}
4042

41-
packageType := detectPackageType()
42-
packageIdentifier := detectPackageIdentifier(name, packageType)
43-
4443
// Create example environment variables
4544
envVars := []model.KeyValueInput{
4645
{
@@ -156,9 +155,9 @@ func getNameFromPackageJSON() string {
156155
return fmt.Sprintf("io.github.<your-username>/%s", name)
157156
}
158157

159-
func detectServerName(subfolder string) string {
158+
func detectServerName(subfolder string, packageType string) string {
160159
// Try to get from git remote
161-
repoURL := detectRepoURL()
160+
repoURL := detectRepoURL(packageType)
162161
if repoURL != "" && strings.Contains(repoURL, "github.com") {
163162
name := buildGitHubServerName(repoURL, subfolder)
164163
if name != "" {
@@ -212,7 +211,17 @@ func detectDescription() string {
212211
return "An MCP server that provides [describe what your server does]"
213212
}
214213

215-
func detectRepoURL() string {
214+
func detectRepoURL(packageType string) string {
215+
sanitizeURL := func(url string, packageType string) string {
216+
switch packageType {
217+
// Some package ecosystems (like npm) may embed git repo URLs; be defensive and strip git+ if present
218+
case model.RegistryTypeNPM:
219+
return strings.TrimPrefix(url, "git+")
220+
default:
221+
return url
222+
}
223+
}
224+
216225
// Try git remote
217226
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
218227
defer cancel()
@@ -233,11 +242,11 @@ func detectRepoURL() string {
233242
if json.Unmarshal(data, &pkg) == nil {
234243
if repo, ok := pkg["repository"].(map[string]any); ok {
235244
if url, ok := repo["url"].(string); ok {
236-
return strings.TrimSuffix(url, ".git")
245+
return sanitizeURL(strings.TrimSuffix(url, ".git"), packageType)
237246
}
238247
}
239248
if repo, ok := pkg["repository"].(string); ok {
240-
return strings.TrimSuffix(repo, ".git")
249+
return sanitizeURL(strings.TrimSuffix(repo, ".git"), packageType)
241250
}
242251
}
243252
}

0 commit comments

Comments
 (0)