repos / git-pr

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

commit
2e37871
parent
c32b448
author
Eric Bower
date
2024-05-30 16:08:17 -0400 EDT
changes
2 files changed,  +38, -21
M cli.go
M pr.go
M cli.go
+12, -10
 1@@ -123,7 +123,7 @@ Here's how it works:
 2 						Usage: "Submit a new PR",
 3 						Action: func(cCtx *cli.Context) error {
 4 							repoID := cCtx.Args().First()
 5-							request, err := pr.SubmitPatchRequest(pubkey, repoID, sesh)
 6+							request, err := pr.SubmitPatchRequest(repoID, pubkey, sesh)
 7 							if err != nil {
 8 								return err
 9 							}
10@@ -335,7 +335,7 @@ Here's how it works:
11 								return fmt.Errorf("unauthorized, you are not the owner of this PR")
12 							}
13 
14-							patch, err := pr.SubmitPatch(pubkey, prID, isReview, sesh)
15+							patches, err := pr.SubmitPatchSet(prID, pubkey, isReview, sesh)
16 							if err != nil {
17 								return err
18 							}
19@@ -348,19 +348,21 @@ Here's how it works:
20 								reviewTxt = "[review]"
21 							}
22 
23-							wish.Println(sesh, "Patch submitted!")
24+							wish.Println(sesh, "Patches submitted!")
25 							writer := NewTabWriter(sesh)
26 							fmt.Fprintln(
27 								writer,
28 								"ID\tTitle",
29 							)
30-							fmt.Fprintf(
31-								writer,
32-								"%d\t%s %s\n",
33-								patch.ID,
34-								patch.Title,
35-								reviewTxt,
36-							)
37+							for _, patch := range patches {
38+								fmt.Fprintf(
39+									writer,
40+									"%d\t%s %s\n",
41+									patch.ID,
42+									patch.Title,
43+									reviewTxt,
44+								)
45+							}
46 							writer.Flush()
47 							return nil
48 						},
M pr.go
+26, -11
 1@@ -3,6 +3,7 @@ package git
 2 import (
 3 	"crypto/sha256"
 4 	"encoding/hex"
 5+	"errors"
 6 	"fmt"
 7 	"io"
 8 	"strings"
 9@@ -12,11 +13,13 @@ import (
10 	"github.com/jmoiron/sqlx"
11 )
12 
13+var ErrPatchExists = errors.New("patch already exists for patch request")
14+
15 type GitPatchRequest interface {
16 	GetRepos() ([]Repo, error)
17 	GetRepoByID(repoID string) (*Repo, error)
18-	SubmitPatchRequest(repoID int64, pubkey string, patchset io.Reader) (*PatchRequest, error)
19-	SubmitPatchSet(prID int64, pubkey string, review bool, patchset io.Reader) error
20+	SubmitPatchRequest(repoID string, pubkey string, patchset io.Reader) (*PatchRequest, error)
21+	SubmitPatchSet(prID int64, pubkey string, review bool, patchset io.Reader) ([]*Patch, error)
22 	GetPatchRequestByID(prID int64) (*PatchRequest, error)
23 	GetPatchRequests() ([]*PatchRequest, error)
24 	GetPatchRequestsByRepoID(repoID string) ([]*PatchRequest, error)
25@@ -185,6 +188,12 @@ func (cmd PrCmd) parsePatchSet(patchset io.Reader) ([]*Patch, error) {
26 }
27 
28 func (cmd PrCmd) createPatch(tx *sqlx.Tx, patch *Patch) (int64, error) {
29+	var patchExists *Patch
30+	_ = tx.Select(&patchExists, "SELECT * FROM patches WHERE patch_request_id = ? AND content_sha = ?", patch.PatchRequestID, patch.ContentSha)
31+	if patchExists.ID == 0 {
32+		return 0, ErrPatchExists
33+	}
34+
35 	var patchID int64
36 	row := tx.QueryRow(
37 		"INSERT INTO patches (pubkey, patch_request_id, author_name, author_email, author_date, title, body, body_appendix, commit_sha, content_sha, raw_text) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
38@@ -210,7 +219,7 @@ func (cmd PrCmd) createPatch(tx *sqlx.Tx, patch *Patch) (int64, error) {
39 	return patchID, err
40 }
41 
42-func (cmd PrCmd) SubmitPatchRequest(repoID int64, pubkey string, patchset io.Reader) (*PatchRequest, error) {
43+func (cmd PrCmd) SubmitPatchRequest(repoID string, pubkey string, patchset io.Reader) (*PatchRequest, error) {
44 	tx, err := cmd.Backend.DB.Beginx()
45 	if err != nil {
46 		return nil, err
47@@ -271,10 +280,11 @@ func (cmd PrCmd) SubmitPatchRequest(repoID int64, pubkey string, patchset io.Rea
48 	return &pr, err
49 }
50 
51-func (cmd PrCmd) SubmitPatchSet(prID int64, pubkey string, review bool, patchset io.Reader) error {
52+func (cmd PrCmd) SubmitPatchSet(prID int64, pubkey string, review bool, patchset io.Reader) ([]*Patch, error) {
53+	fin := []*Patch{}
54 	tx, err := cmd.Backend.DB.Beginx()
55 	if err != nil {
56-		return err
57+		return fin, err
58 	}
59 
60 	defer func() {
61@@ -286,22 +296,27 @@ func (cmd PrCmd) SubmitPatchSet(prID int64, pubkey string, review bool, patchset
62 
63 	patches, err := cmd.parsePatchSet(patchset)
64 	if err != nil {
65-		return err
66+		return fin, err
67 	}
68 
69 	for _, patch := range patches {
70 		patch.Pubkey = pubkey
71 		patch.PatchRequestID = prID
72-		_, err = cmd.createPatch(tx, patch)
73-		if err != nil {
74-			return err
75+		patchID, err := cmd.createPatch(tx, patch)
76+		if err == nil {
77+			patch.ID = patchID
78+			fin = append(fin, patch)
79+		} else {
80+			if !errors.Is(ErrPatchExists, err) {
81+				return fin, err
82+			}
83 		}
84 	}
85 
86 	err = tx.Commit()
87 	if err != nil {
88-		return err
89+		return fin, err
90 	}
91 
92-	return err
93+	return fin, err
94 }