From 4d5576b1f42b27f004f8231fb6e2df71a8768e4a Mon Sep 17 00:00:00 2001 From: tjh Date: Wed, 14 May 2025 11:07:58 +0100 Subject: [PATCH] appview,knotserver: pass repo handle to knot when proxying git ops Adds 'x-tangled-repo-owner-handle' header when proxying git requests to the knot. This allows knotserver to attempt to suggest the correct git+ssh URL when rejecting a HTTP git push. --- appview/state/git_http.go | 3 +++ knotserver/git.go | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/appview/state/git_http.go b/appview/state/git_http.go index badefd66..a2ed1e40 100644 --- a/appview/state/git_http.go +++ b/appview/state/git_http.go @@ -73,6 +73,9 @@ func (s *State) proxyRequest(w http.ResponseWriter, r *http.Request, targetURL s // Copy original headers proxyReq.Header = r.Header + repoOwnerHandle := chi.URLParam(r, "user") + proxyReq.Header.Add("x-tangled-repo-owner-handle", repoOwnerHandle) + // Execute request resp, err := client.Do(proxyReq) if err != nil { diff --git a/knotserver/git.go b/knotserver/git.go index e035b284..45444f81 100644 --- a/knotserver/git.go +++ b/knotserver/git.go @@ -6,6 +6,7 @@ import ( "io" "net/http" "path/filepath" + "strings" securejoin "github.com/cyphar/filepath-securejoin" "github.com/go-chi/chi/v5" @@ -113,6 +114,18 @@ func (d *Handle) RejectPush(w http.ResponseWriter, r *http.Request, unqualifiedR w.WriteHeader(http.StatusForbidden) fmt.Fprintf(w, "Welcome to Tangled.sh!\n\nPushes are currently only supported over SSH.") + + // If the appview gave us the repository owner's handle we can attempt to + // construct the correct ssh url. + ownerHandle := r.Header.Get("x-tangled-repo-owner-handle") + if ownerHandle != "" && !strings.ContainsAny(ownerHandle, ":") { + hostname := d.c.Server.Hostname + if strings.Contains(hostname, ":") { + hostname = strings.Split(hostname, ":")[0] + } + + fmt.Fprintf(w, " Try:\ngit remote set-url --push origin git@%s:%s/%s\n\n... and push again.", hostname, ownerHandle, unqualifiedRepoName) + } fmt.Fprintf(w, "\n\n") } -- 2.51.2