From ae095c9e11ab0b13d4cb9d6d0f72034626e41e34 Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Tue, 10 Jun 2025 13:36:16 +0100 Subject: [PATCH] hook: apply hook setup on repo create Change-Id: wkpulvpzztsnkmkstnmrwosruypkvvoq Signed-off-by: oppiliappan --- hook/setup.go | 24 ++++++++++++++---------- knotserver/routes.go | 13 +++++++++++++ knotserver/server.go | 4 ++-- nix/vm.nix | 2 +- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/hook/setup.go b/hook/setup.go index 67deb89..3e6b302 100644 --- a/hook/setup.go +++ b/hook/setup.go @@ -36,6 +36,14 @@ func WithInternalApi(api string) setupOpt { } } +func Config(opts ...setupOpt) config { + config := config{} + for _, o := range opts { + o(&config) + } + return config +} + // setup hooks for all users // // directory structure is typically like so: @@ -43,11 +51,7 @@ func WithInternalApi(api string) setupOpt { // did:plc:foobar/repo1 // did:plc:foobar/repo2 // did:web:barbaz/repo1 -func Setup(opts ...setupOpt) error { - config := config{} - for _, o := range opts { - o(&config) - } +func Setup(config config) error { // iterate over all directories in current directory: userDirs, err := os.ReadDir(config.scanPath) if err != nil { @@ -65,7 +69,7 @@ func Setup(opts ...setupOpt) error { } userPath := filepath.Join(config.scanPath, did) - if err := setupUser(&config, userPath); err != nil { + if err := SetupUser(config, userPath); err != nil { return err } } @@ -74,7 +78,7 @@ func Setup(opts ...setupOpt) error { } // setup hooks in /scanpath/did:plc:user -func setupUser(config *config, userPath string) error { +func SetupUser(config config, userPath string) error { repos, err := os.ReadDir(userPath) if err != nil { return err @@ -86,7 +90,7 @@ func setupUser(config *config, userPath string) error { } path := filepath.Join(userPath, repo.Name()) - if err := setup(config, path); err != nil { + if err := SetupRepo(config, path); err != nil { if errors.Is(err, ErrNoGitRepo) { continue } @@ -98,7 +102,7 @@ func setupUser(config *config, userPath string) error { } // setup hook in /scanpath/did:plc:user/repo -func setup(config *config, path string) error { +func SetupRepo(config config, path string) error { if _, err := git.PlainOpen(path); err != nil { return fmt.Errorf("%s: %w", path, ErrNoGitRepo) } @@ -121,7 +125,7 @@ func setup(config *config, path string) error { return nil } -func mkHook(config *config, hookPath string) error { +func mkHook(config config, hookPath string) error { executablePath, err := os.Executable() if err != nil { return err diff --git a/knotserver/routes.go b/knotserver/routes.go index 7424c99..1656e77 100644 --- a/knotserver/routes.go +++ b/knotserver/routes.go @@ -26,6 +26,7 @@ import ( gogit "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" + "tangled.sh/tangled.sh/core/hook" "tangled.sh/tangled.sh/core/knotserver/db" "tangled.sh/tangled.sh/core/knotserver/git" "tangled.sh/tangled.sh/core/patchutil" @@ -673,6 +674,18 @@ func (h *Handle) NewRepo(w http.ResponseWriter, r *http.Request) { return } + err = hook.SetupRepo(hook.Config( + hook.WithScanPath(h.c.Repo.ScanPath), + hook.WithInternalApi(h.c.Server.InternalListenAddr), + ), + repoPath, + ) + if err != nil { + l.Error("setting up hooks", "error", err.Error()) + writeError(w, err.Error(), http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusNoContent) } diff --git a/knotserver/server.go b/knotserver/server.go index 1bf70ed..ee0d7a5 100644 --- a/knotserver/server.go +++ b/knotserver/server.go @@ -47,10 +47,10 @@ func Run(ctx context.Context, cmd *cli.Command) error { return fmt.Errorf("failed to load config: %w", err) } - err = hook.Setup( + err = hook.Setup(hook.Config( hook.WithScanPath(c.Repo.ScanPath), hook.WithInternalApi(c.Server.InternalListenAddr), - ) + )) if err != nil { return fmt.Errorf("failed to setup hooks: %w", err) } diff --git a/nix/vm.nix b/nix/vm.nix index 2167e81..7ffede8 100644 --- a/nix/vm.nix +++ b/nix/vm.nix @@ -21,7 +21,7 @@ nixpkgs.lib.nixosSystem { g = config.services.tangled-knot.gitUser; in [ "d /var/lib/knot 0770 ${u} ${g} - -" # Create the directory first - "f+ /var/lib/knot/secret 0660 ${u} ${g} - KNOT_SERVER_SECRET=38a7c3237c2a585807e06a5bcfac92eb39442063f3da306b7acb15cfdc51d19d" + "f+ /var/lib/knot/secret 0660 ${u} ${g} - KNOT_SERVER_SECRET=40b4db20544e37a12ba3ed7353d4d4421a30e0593385068d2ef85263495794d8" ]; services.tangled-knot = { enable = true; -- 2.43.0