repos / git-pr

a self-hosted git collaboration server
git clone https://github.com/picosh/git-pr.git

commit
584e21b
parent
59749e8
author
Eric Bower
date
2024-05-02 11:23:59 -0400 EDT
progress
2 files changed,  +55, -8
M db.go
M mdw.go
M db.go
+1, -0
1@@ -34,6 +34,7 @@ CREATE TABLE IF NOT EXISTS patches (
2   body TEXT NOT NULL,
3   commit_sha TEXT NOT NULL,
4   commit_date DATETIME NOT NULL,
5+  review BOOLEAN NOT NULL DEFAULT false,
6   raw_text TEXT NOT NULL,
7   created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
8   CONSTRAINT pr_id_fk
M mdw.go
+54, -8
  1@@ -2,6 +2,7 @@ package git
  2 
  3 import (
  4 	"bytes"
  5+	"flag"
  6 	"fmt"
  7 	"io"
  8 	"os"
  9@@ -56,6 +57,12 @@ func try(sesh ssh.Session, err error) {
 10 	}
 11 }
 12 
 13+func flagSet(sesh ssh.Session, cmdName string) *flag.FlagSet {
 14+	cmd := flag.NewFlagSet(cmdName, flag.ContinueOnError)
 15+	cmd.SetOutput(sesh)
 16+	return cmd
 17+}
 18+
 19 func GitServerMiddleware(be *Backend) wish.Middleware {
 20 	return func(next ssh.Handler) ssh.Handler {
 21 		return func(sesh ssh.Session) {
 22@@ -81,14 +88,14 @@ func GitServerMiddleware(be *Backend) wish.Middleware {
 23 					}
 24 				}
 25 			} else if cmd == "pr" {
 26-				if len(args) < 2 {
 27-					wish.Fatal(sesh, "must provide repo name")
 28-					return
 29-				}
 30+				prCmd := flagSet(sesh, "pr")
 31+				repo := prCmd.String("repo", "", "repository to target")
 32+				out := prCmd.Bool("stdout", false, "print patchset to stdout")
 33+				id := prCmd.Int("id", 0, "patch request id")
 34 
 35-				repoName := utils.SanitizeRepo(args[1])
 36+				repoName := utils.SanitizeRepo(*repo)
 37 
 38-				if len(args) > 2 {
 39+				if *out == true {
 40 					prID, err := strconv.ParseInt(args[2], 10, 64)
 41 					try(sesh, err)
 42 
 43@@ -99,7 +106,7 @@ func GitServerMiddleware(be *Backend) wish.Middleware {
 44 						prID,
 45 					)
 46 					if len(patches) == 0 {
 47-						wish.Printf(sesh, "No patches found for Patch Request ID: %d\n", prID)
 48+						wish.Printf(sesh, "no patches found for Patch Request ID: %d\n", prID)
 49 						return
 50 					}
 51 
 52@@ -111,6 +118,45 @@ func GitServerMiddleware(be *Backend) wish.Middleware {
 53 					for _, patch := range patches {
 54 						wish.Printf(sesh, "%s\n\n\n", patch.RawText)
 55 					}
 56+				} else if *id != 0 {
 57+					prID := *id
 58+					pr := PatchRequest{}
 59+					be.DB.Get(&pr, "SELECT * FROM patch_requests WHERE id=?", prID)
 60+					if pr.ID == 0 {
 61+						wish.Fatalln(sesh, "patch request does not exist")
 62+						return
 63+					}
 64+
 65+					review := false
 66+					if pr.Pubkey != pubkey {
 67+						review = true
 68+					}
 69+
 70+					// need to read io.Reader from session twice
 71+					var buf bytes.Buffer
 72+					tee := io.TeeReader(sesh, &buf)
 73+
 74+					_, preamble, err := gitdiff.Parse(tee)
 75+					try(sesh, err)
 76+					header, err := gitdiff.ParsePatchHeader(preamble)
 77+					try(sesh, err)
 78+
 79+					_, err = be.DB.Exec(
 80+						"INSERT INTO patches (pubkey, patch_request_id, author_name, author_email, title, body, commit_sha, commit_date, review, raw_text) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
 81+						pubkey,
 82+						prID,
 83+						header.Author.Name,
 84+						header.Author.Email,
 85+						header.Title,
 86+						header.Body,
 87+						header.SHA,
 88+						header.CommitterDate,
 89+						review,
 90+						buf.String(),
 91+					)
 92+					try(sesh, err)
 93+
 94+					wish.Printf(sesh, "submitted review!\n")
 95 				} else {
 96 					err := git.EnsureWithin(be.ReposDir(), be.RepoName(repoName))
 97 					try(sesh, err)
 98@@ -163,7 +209,7 @@ func GitServerMiddleware(be *Backend) wish.Middleware {
 99 
100 					wish.Printf(
101 						sesh,
102-						"Create Patch Request!\nID: %d\nTitle: %s\n",
103+						"created patch request!\nID: %d\nTitle: %s\n",
104 						prID,
105 						prName,
106 					)