hook: apply hook setup on repo create #241

merged
opened by oppi.li targeting master from push-qoplqnlvlqqo
Changed files
+30 -13
hook
knotserver
nix
+14 -10
hook/setup.go
···
}
}
+
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:
···
// 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 {
···
}
userPath := filepath.Join(config.scanPath, did)
-
if err := setupUser(&config, userPath); err != nil {
+
if err := SetupUser(config, userPath); err != nil {
return err
}
}
···
}
// 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
···
}
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
}
···
}
// 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)
}
···
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
+13
knotserver/routes.go
···
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"
···
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)
}
+2 -2
knotserver/server.go
···
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)
}
+1 -1
nix/vm.nix
···
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;