From ed582948b087031589c6f942277adda179f70158 Mon Sep 17 00:00:00 2001 From: Samuel Shuert Date: Fri, 26 Sep 2025 19:49:46 +0000 Subject: [PATCH] hooks: add create PR message on git push Change-Id: pylzpxmqulmpyyntlyswrsxkqzkxyvwp Respond to a successful push with a URL to create a PR pointing to the default branch. This behavior is made to mimic other Git forges. Signed-off-by: Samuel Shuert --- hook/hook.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/hook/hook.go b/hook/hook.go index c53fdfbd..1b099eca 100644 --- a/hook/hook.go +++ b/hook/hook.go @@ -10,6 +10,8 @@ import ( "strings" "github.com/urfave/cli/v3" + "tangled.org/core/idresolver" + "tangled.org/core/knotserver/git" ) type HookResponse struct { @@ -68,6 +70,28 @@ func postRecieve(ctx context.Context, cmd *cli.Command) error { client := &http.Client{} + gr, err := git.PlainOpen(gitDir) + defaultBranch, err := gr.FindMainBranch() + if err != nil { + fmt.Fprintln(os.Stderr, "failed to retrieve default branch") + return nil + } + gitPath := strings.Split(gitDir, "/") + repoName := gitPath[len(gitPath)-1] + userIdent, err := idresolver.DefaultResolver().ResolveIdent(ctx, userDid) + lines, err := git.ParsePostReceive(strings.NewReader(payload)) + if err != nil { + return fmt.Errorf("failed to parse post-receive payload: %w", err) + } + for _, line := range lines { + if line.OldSha.IsZero() && line.OldSha != line.NewSha { + fmt.Fprintln(os.Stderr, "​") + fmt.Fprintf(os.Stderr, "Create a PR pointing to %s\n", defaultBranch) + fmt.Fprintf(os.Stderr, "\thttps://tangled.org/@%s/%s/compare/%s...%s\n", userIdent.Handle.String(), repoName, defaultBranch, strings.TrimPrefix(line.Ref, "refs/heads/")) + fmt.Fprintln(os.Stderr, "​") + } + } + req, err := http.NewRequest("POST", "http://"+endpoint+"/hooks/post-receive", strings.NewReader(payload)) if err != nil { return fmt.Errorf("failed to create request: %w", err) -- 2.43.0