File osc-mcp-9f1f589-fix-commit-take06.diff of Package mcp-server-osc

diff -Nrub osc-mcp-9f1f589-ori/internal/pkg/osc/commit.go osc-mcp-9f1f589/internal/pkg/osc/commit.go
--- osc-mcp-9f1f589-ori/internal/pkg/osc/commit.go	2025-09-17 12:47:08.343994981 +0200
+++ osc-mcp-9f1f589/internal/pkg/osc/commit.go	2025-09-17 21:14:43.019998347 +0200
@@ -56,10 +56,12 @@
 
 	projectName := params.ProjectName
 	packageName := params.PackageName
-	if projectName == "" || packageName == "" {
-		packageName = filepath.Base(params.Directory)
+	if projectName == "" {
 		projectName = filepath.Base(filepath.Dir(params.Directory))
 	}
+	if packageName == "" {
+		packageName = filepath.Base(params.Directory)
+	}
 	if projectName == "" || packageName == "" {
 		return nil, CommitResult{}, fmt.Errorf("could not determine project and package name from directory: %s", params.Directory)
 	}
@@ -239,7 +241,11 @@
 }
 
 func (cred *OSCCredentials) getRemoteFileList(ctx context.Context, project, pkg string) (*Directory, error) {
-	url := fmt.Sprintf("%s/source/%s/%s", cred.Apiaddr, project, pkg)
+	if cred.Apiaddr == "" {
+		cred.Apiaddr = "https://api.opensuse.org"
+	}
+	normalizedAPI, err := normalizeAPIURL(cred.Apiaddr)
+	url := fmt.Sprintf("%s/source/%s/%s", normalizedAPI, project, pkg)
 	req, err := cred.buildRequest(ctx, "GET", url, nil)
 	if err != nil {
 		return nil, err
@@ -287,7 +293,11 @@
 	}
 	defer file.Close()
 
-	url := fmt.Sprintf("%s/source/%s/%s/%s", cred.Apiaddr, project, pkg, fileName)
+	if cred.Apiaddr == "" {
+		cred.Apiaddr = "https://api.opensuse.org"
+	}
+	normalizedAPI, err := normalizeAPIURL(cred.Apiaddr)
+	url := fmt.Sprintf("%s/source/%s/%s/%s", normalizedAPI, project, pkg, fileName)
 	req, err := cred.buildRequest(ctx, "PUT", url, file)
 	if err != nil {
 		return err
@@ -309,7 +319,15 @@
 
 func (cred *OSCCredentials) commitFiles(ctx context.Context, project, pkg, message string, xmlData []byte) error {
 	escapedMessage := url.QueryEscape(message)
-	url := fmt.Sprintf("%s/source/%s/%s?cmd=commit&comment=%s", cred.Apiaddr, project, pkg, escapedMessage)
+	if cred.Apiaddr == "" {
+		cred.Apiaddr = "https://api.opensuse.org"
+	}
+	normalizedAPI, err := normalizeAPIURL(cred.Apiaddr)
+	if err != nil {
+	    return fmt.Errorf("invalid API address: %w", err)
+	}
+	url := fmt.Sprintf("%s/source/%s/%s?cmd=commit&comment=%s", normalizedAPI, project, pkg, escapedMessage)
+	slog.Debug("Committing to OBS", "url", url)
 	req, err := cred.buildRequest(ctx, "POST", url, bytes.NewReader(xmlData))
 	if err != nil {
 		return err
diff -Nrub osc-mcp-9f1f589-ori/internal/pkg/osc/osc.go osc-mcp-9f1f589/internal/pkg/osc/osc.go
--- osc-mcp-9f1f589-ori/internal/pkg/osc/osc.go	2025-09-17 12:47:08.343994981 +0200
+++ osc-mcp-9f1f589/internal/pkg/osc/osc.go	2025-09-17 21:14:56.547997835 +0200
@@ -31,6 +31,16 @@
 	LastBuildKey string
 }
 
+func normalizeAPIURL(raw string) (string, error) {
+    if raw == "" {
+        return "", fmt.Errorf("API URL is empty")
+    }
+    if !strings.HasPrefix(raw, "http://") && !strings.HasPrefix(raw, "https://") {
+	return strings.TrimRight(fmt.Sprintf("https://%s", strings.TrimLeft(raw, "/")), "/"), nil
+    }
+    return strings.TrimRight(raw, "/"), nil
+}
+
 // GetCredentials reads the osc configuration, determines the api url and
 // returns the stored credentials.
 // It will try to read ~/.config/osc/oscrc, ~/.oscrc and ./.oscrc.
openSUSE Build Service is sponsored by