repos / git-pr

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

commit
3ec9462
parent
cc93308
author
Eric Bower
date
2024-05-22 09:40:12 -0400 EDT
fix: submit patch request exception
4 files changed,  +34, -10
M cli.go
M db.go
M pr.go
M web.go
M cli.go
+2, -2
 1@@ -204,7 +204,7 @@ Here's how it works:
 2 									patch.CommitSha,
 3 									patch.AuthorName,
 4 									patch.AuthorEmail,
 5-									patch.AuthorDate.Format(time.RFC3339Nano),
 6+									patch.AuthorDate,
 7 									patch.BodyAppendix,
 8 									patch.Body,
 9 								)
10@@ -256,7 +256,7 @@ Here's how it works:
11 									patch.CommitSha,
12 									patch.AuthorName,
13 									patch.AuthorEmail,
14-									patch.AuthorDate.Format(time.RFC3339Nano),
15+									patch.AuthorDate,
16 								)
17 							}
18 							w.Flush()
M db.go
+1, -1
1@@ -28,7 +28,7 @@ type Patch struct {
2 	PatchRequestID int64     `db:"patch_request_id"`
3 	AuthorName     string    `db:"author_name"`
4 	AuthorEmail    string    `db:"author_email"`
5-	AuthorDate     time.Time `db:"author_date"`
6+	AuthorDate     string    `db:"author_date"`
7 	Title          string    `db:"title"`
8 	Body           string    `db:"body"`
9 	BodyAppendix   string    `db:"body_appendix"`
M pr.go
+30, -6
 1@@ -146,6 +146,18 @@ func (cmd PrCmd) SubmitPatch(pubkey string, prID int64, review bool, patch io.Re
 2 }
 3 
 4 func (cmd PrCmd) SubmitPatchRequest(pubkey string, repoID string, patches io.Reader) (*PatchRequest, error) {
 5+	tx, err := cmd.Backend.DB.Beginx()
 6+	if err != nil {
 7+		return nil, err
 8+	}
 9+
10+	defer func() {
11+		err := tx.Rollback()
12+		if err != nil {
13+			cmd.Backend.Logger.Error("rollback", "err", err)
14+		}
15+	}()
16+
17 	// need to read io.Reader from session twice
18 	var buf bytes.Buffer
19 	tee := io.TeeReader(patches, &buf)
20@@ -162,7 +174,7 @@ func (cmd PrCmd) SubmitPatchRequest(pubkey string, repoID string, patches io.Rea
21 	prDesc := header.Body
22 
23 	var prID int64
24-	row := cmd.Backend.DB.QueryRow(
25+	row := tx.QueryRow(
26 		"INSERT INTO patch_requests (pubkey, repo_id, name, text, status, updated_at) VALUES(?, ?, ?, ?, ?, ?) RETURNING id",
27 		pubkey,
28 		repoID,
29@@ -179,15 +191,22 @@ func (cmd PrCmd) SubmitPatchRequest(pubkey string, repoID string, patches io.Rea
30 		return nil, fmt.Errorf("could not create patch request")
31 	}
32 
33-	_, err = cmd.Backend.DB.Exec(
34+	authorName := ""
35+	authorEmail := ""
36+	if header.Author != nil {
37+		authorName = header.Author.Name
38+		authorEmail = header.Author.Email
39+	}
40+
41+	_, err = tx.Exec(
42 		"INSERT INTO patches (pubkey, patch_request_id, author_name, author_email, author_date, title, body, body_appendix, commit_sha, raw_text) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
43 		pubkey,
44 		prID,
45-		header.Author.Name,
46-		header.Author.Email,
47+		authorName,
48+		authorEmail,
49 		header.AuthorDate,
50-		header.Title,
51-		header.Body,
52+		prName,
53+		prDesc,
54 		header.BodyAppendix,
55 		header.SHA,
56 		buf.String(),
57@@ -196,6 +215,11 @@ func (cmd PrCmd) SubmitPatchRequest(pubkey string, repoID string, patches io.Rea
58 		return nil, err
59 	}
60 
61+	err = tx.Commit()
62+	if err != nil {
63+		return nil, err
64+	}
65+
66 	var pr PatchRequest
67 	err = cmd.Backend.DB.Get(&pr, "SELECT * FROM patch_requests WHERE id=?", prID)
68 	return &pr, err
M web.go
+1, -1
1@@ -244,7 +244,7 @@ func prHandler(w http.ResponseWriter, r *http.Request) {
2 			truncateSha(patch.CommitSha),
3 			patch.AuthorName,
4 			patch.AuthorEmail,
5-			patch.AuthorDate.Format(time.RFC3339Nano),
6+			patch.AuthorDate,
7 		)
8 	}
9