guard: add create PR message on git push #611

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 me@thecoded.prof

Changed files
+24
hook
+24
hook/hook.go
··· 10 10 "strings" 11 11 12 12 "github.com/urfave/cli/v3" 13 + "tangled.org/core/idresolver" 14 + "tangled.org/core/knotserver/git" 13 15 ) 14 16 15 17 type HookResponse struct { ··· 68 70 69 71 client := &http.Client{} 70 72 73 + gr, err := git.PlainOpen(gitDir) 74 + defaultBranch, err := gr.FindMainBranch() 75 + if err != nil { 76 + fmt.Fprintln(os.Stderr, "failed to retrieve default branch") 77 + return nil 78 + } 79 + gitPath := strings.Split(gitDir, "/") 80 + repoName := gitPath[len(gitPath)-1] 81 + userIdent, err := idresolver.DefaultResolver().ResolveIdent(ctx, userDid) 82 + lines, err := git.ParsePostReceive(strings.NewReader(payload)) 83 + if err != nil { 84 + return fmt.Errorf("failed to parse post-receive payload: %w", err) 85 + } 86 + for _, line := range lines { 87 + if line.OldSha.IsZero() && line.OldSha != line.NewSha { 88 + fmt.Fprintln(os.Stderr, "​") 89 + fmt.Fprintf(os.Stderr, "Create a PR pointing to %s\n", defaultBranch) 90 + fmt.Fprintf(os.Stderr, "\thttps://tangled.org/@%s/%s/compare/%s...%s\n", userIdent.Handle.String(), repoName, defaultBranch, strings.TrimPrefix(line.Ref, "refs/heads/")) 91 + fmt.Fprintln(os.Stderr, "​") 92 + } 93 + } 94 + 71 95 req, err := http.NewRequest("POST", "http://"+endpoint+"/hooks/post-receive", strings.NewReader(payload)) 72 96 if err != nil { 73 97 return fmt.Errorf("failed to create request: %w", err)