From 42155d935df35db17e507b4ae585c99b8d42a2e7 Mon Sep 17 00:00:00 2001 From: BrookJeynes Date: Tue, 13 May 2025 01:09:20 +1000 Subject: [PATCH] knotserver: routes: add handler/route to sync fork. --- knotserver/handler.go | 5 +++- knotserver/routes.go | 53 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/knotserver/handler.go b/knotserver/handler.go index c33a4ff..afd1828 100644 --- a/knotserver/handler.go +++ b/knotserver/handler.go @@ -126,7 +126,10 @@ func Setup(ctx context.Context, c *config.Config, db *db.DB, e *rbac.Enforcer, j r.Use(h.VerifySignature) r.Put("/new", h.NewRepo) r.Delete("/", h.RemoveRepo) - r.Post("/fork", h.RepoFork) + r.Route("/fork", func(r chi.Router) { + r.Post("/", h.RepoFork) + r.Post("/sync/{branch}", h.RepoForkSync) + }) }) r.Route("/member", func(r chi.Router) { diff --git a/knotserver/routes.go b/knotserver/routes.go index 3bc24a2..adb1e3c 100644 --- a/knotserver/routes.go +++ b/knotserver/routes.go @@ -631,6 +631,59 @@ func (h *Handle) NewRepo(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) } +func (h *Handle) RepoForkSync(w http.ResponseWriter, r *http.Request) { + l := h.l.With("handler", "RepoForkSync") + + data := struct { + Did string `json:"did"` + Source string `json:"source"` + Name string `json:"name,omitempty"` + }{} + + if err := json.NewDecoder(r.Body).Decode(&data); err != nil { + writeError(w, "invalid request body", http.StatusBadRequest) + return + } + + did := data.Did + source := data.Source + + if did == "" || source == "" { + l.Error("invalid request body, empty did or name") + w.WriteHeader(http.StatusBadRequest) + return + } + + var name string + if data.Name != "" { + name = data.Name + } else { + name = filepath.Base(source) + } + + branch := chi.URLParam(r, "branch") + branch, _ = url.PathUnescape(branch) + + relativeRepoPath := filepath.Join(did, name) + repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, relativeRepoPath) + + gr, err := git.PlainOpen(repoPath) + if err != nil { + log.Println(err) + notFound(w) + return + } + + err = gr.Sync(branch) + if err != nil { + l.Error("syncing repo fork", "error", err.Error()) + writeError(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusNoContent) +} + func (h *Handle) RepoFork(w http.ResponseWriter, r *http.Request) { l := h.l.With("handler", "RepoFork") -- 2.51.2