-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpr.go
727 lines (638 loc) · 16.7 KB
/
pr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
package git
import (
"database/sql"
"errors"
"fmt"
"io"
"strings"
"time"
"github.com/jmoiron/sqlx"
)
var ErrPatchExists = errors.New("patch already exists for patch request")
type PatchsetOp int
const (
OpNormal PatchsetOp = iota
OpReview
OpAccept
OpClose
)
type GitPatchRequest interface {
GetUsers() ([]*User, error)
GetUserByID(userID int64) (*User, error)
GetUserByName(name string) (*User, error)
GetUserByPubkey(pubkey string) (*User, error)
GetRepos() ([]*Repo, error)
GetRepoByID(repoID int64) (*Repo, error)
GetRepoByName(user *User, repoName string) (*Repo, error)
CreateRepo(user *User, repoName string) (*Repo, error)
UpsertUser(pubkey, name string) (*User, error)
IsBanned(pubkey, ipAddress string) error
SubmitPatchRequest(repoID int64, userID int64, patchset io.Reader) (*PatchRequest, error)
SubmitPatchset(prID, userID int64, op PatchsetOp, patchset io.Reader) ([]*Patch, error)
GetPatchRequestByID(prID int64) (*PatchRequest, error)
GetPatchRequests() ([]*PatchRequest, error)
GetPatchRequestsByRepoID(repoID int64) ([]*PatchRequest, error)
GetPatchsetsByPrID(prID int64) ([]*Patchset, error)
GetPatchsetByID(patchsetID int64) (*Patchset, error)
GetLatestPatchsetByPrID(prID int64) (*Patchset, error)
GetPatchesByPatchsetID(prID int64) ([]*Patch, error)
UpdatePatchRequestStatus(prID, userID int64, status string) error
UpdatePatchRequestName(prID, userID int64, name string) error
DeletePatchsetByID(userID, prID int64, patchsetID int64) error
CreateEventLog(tx *sqlx.Tx, eventLog EventLog) error
GetEventLogs() ([]*EventLog, error)
GetEventLogsByRepoName(user *User, repoName string) ([]*EventLog, error)
GetEventLogsByPrID(prID int64) ([]*EventLog, error)
GetEventLogsByUserID(userID int64) ([]*EventLog, error)
DiffPatchsets(aset *Patchset, bset *Patchset) ([]*RangeDiffOutput, error)
}
type PrCmd struct {
Backend *Backend
}
var (
_ GitPatchRequest = PrCmd{}
_ GitPatchRequest = (*PrCmd)(nil)
)
func (pr PrCmd) IsBanned(pubkey, ipAddress string) error {
acl := []*Acl{}
err := pr.Backend.DB.Select(
&acl,
"SELECT * FROM acl WHERE permission='banned' AND (pubkey=? OR ip_address=?)",
pubkey,
ipAddress,
)
if len(acl) > 0 {
return fmt.Errorf("user has been banned")
}
return err
}
func (pr PrCmd) GetUsers() ([]*User, error) {
users := []*User{}
err := pr.Backend.DB.Select(&users, "SELECT * FROM app_users")
return users, err
}
func (pr PrCmd) GetUserByName(name string) (*User, error) {
var user User
err := pr.Backend.DB.Get(&user, "SELECT * FROM app_users WHERE name=?", name)
return &user, err
}
func (pr PrCmd) GetUserByID(id int64) (*User, error) {
var user User
err := pr.Backend.DB.Get(&user, "SELECT * FROM app_users WHERE id=?", id)
return &user, err
}
func (pr PrCmd) GetUserByPubkey(pubkey string) (*User, error) {
var user User
err := pr.Backend.DB.Get(&user, "SELECT * FROM app_users WHERE pubkey=?", pubkey)
return &user, err
}
func (pr PrCmd) computeUserName(name string) (string, error) {
var user User
err := pr.Backend.DB.Get(&user, "SELECT * FROM app_users WHERE name=?", name)
if err != nil {
return name, nil
}
// collision, generate random number and append
return fmt.Sprintf("%s%s", name, randSeq(4)), nil
}
func (pr PrCmd) CreateRepo(user *User, repoName string) (*Repo, error) {
var repoID int64
row := pr.Backend.DB.QueryRow(
"INSERT INTO repos (user_id, name) VALUES (?, ?) RETURNING id",
user.ID,
repoName,
)
err := row.Scan(&repoID)
if err != nil {
return nil, err
}
return pr.GetRepoByID(repoID)
}
func (pr PrCmd) GetRepoByID(repoID int64) (*Repo, error) {
var repo Repo
err := pr.Backend.DB.Get(&repo, "SELECT * FROM repos WHERE id=?", repoID)
return &repo, err
}
func (pr PrCmd) GetRepos() (repos []*Repo, err error) {
err = pr.Backend.DB.Select(
&repos,
"SELECT * from repos",
)
if err != nil {
return repos, err
}
if len(repos) == 0 {
return repos, fmt.Errorf("no repos found")
}
return repos, nil
}
func (pr PrCmd) GetRepoByName(user *User, repoName string) (*Repo, error) {
var repo Repo
var err error
if user == nil {
err = pr.Backend.DB.Get(&repo, "SELECT * FROM repos WHERE name=?", repoName)
} else {
err = pr.Backend.DB.Get(&repo, "SELECT * FROM repos WHERE user_id=? AND name=?", user.ID, repoName)
}
if err != nil {
return nil, fmt.Errorf("repo not found: %s", repoName)
}
return &repo, nil
}
func (pr PrCmd) createUser(pubkey, name string) (*User, error) {
if pubkey == "" {
return nil, fmt.Errorf("must provide pubkey when creating user")
}
if name == "" {
return nil, fmt.Errorf("must provide user name when creating user")
}
userName, err := pr.computeUserName(name)
if err != nil {
pr.Backend.Logger.Error("could not compute username", "err", err)
}
var userID int64
row := pr.Backend.DB.QueryRow(
"INSERT INTO app_users (pubkey, name) VALUES (?, ?) RETURNING id",
pubkey,
userName,
)
err = row.Scan(&userID)
if err != nil {
return nil, err
}
if userID == 0 {
return nil, fmt.Errorf("could not create user")
}
user, err := pr.GetUserByID(userID)
return user, err
}
func (pr PrCmd) UpsertUser(pubkey, name string) (*User, error) {
sanName := strings.ToLower(name)
if pubkey == "" {
return nil, fmt.Errorf("must provide pubkey during upsert")
}
user, err := pr.GetUserByPubkey(pubkey)
if err != nil {
user, err = pr.createUser(pubkey, sanName)
}
return user, err
}
func (pr PrCmd) GetPatchsetsByPrID(prID int64) ([]*Patchset, error) {
patchsets := []*Patchset{}
err := pr.Backend.DB.Select(
&patchsets,
"SELECT * FROM patchsets WHERE patch_request_id=? ORDER BY created_at ASC",
prID,
)
if err != nil {
return patchsets, err
}
if len(patchsets) == 0 {
return patchsets, fmt.Errorf("no patchsets found for patch request: %d", prID)
}
return patchsets, nil
}
func (pr PrCmd) GetPatchsetByID(patchsetID int64) (*Patchset, error) {
var patchset Patchset
err := pr.Backend.DB.Get(
&patchset,
"SELECT * FROM patchsets WHERE id=?",
patchsetID,
)
return &patchset, err
}
func (pr PrCmd) GetLatestPatchsetByPrID(prID int64) (*Patchset, error) {
patchsets, err := pr.GetPatchsetsByPrID(prID)
if err != nil {
return nil, err
}
if len(patchsets) == 0 {
return nil, fmt.Errorf("not patchsets found for patch request: %d", prID)
}
return patchsets[len(patchsets)-1], nil
}
func (pr PrCmd) GetPatchesByPatchsetID(patchsetID int64) ([]*Patch, error) {
patches := []*Patch{}
err := pr.Backend.DB.Select(
&patches,
"SELECT * FROM patches WHERE patchset_id=? ORDER BY created_at ASC, id ASC",
patchsetID,
)
return patches, err
}
func (cmd PrCmd) GetPatchRequests() ([]*PatchRequest, error) {
prs := []*PatchRequest{}
err := cmd.Backend.DB.Select(
&prs,
"SELECT * FROM patch_requests ORDER BY id DESC",
)
return prs, err
}
func (cmd PrCmd) GetPatchRequestsByRepoID(repoID int64) ([]*PatchRequest, error) {
prs := []*PatchRequest{}
err := cmd.Backend.DB.Select(
&prs,
"SELECT * FROM patch_requests WHERE repo_id=? ORDER BY id DESC",
repoID,
)
return prs, err
}
func (cmd PrCmd) GetPatchRequestByID(prID int64) (*PatchRequest, error) {
pr := PatchRequest{}
err := cmd.Backend.DB.Get(
&pr,
"SELECT * FROM patch_requests WHERE id=? ORDER BY created_at DESC",
prID,
)
return &pr, err
}
// Status types: open, closed, accepted, reviewed.
func (cmd PrCmd) UpdatePatchRequestStatus(prID int64, userID int64, status string) error {
tx, err := cmd.Backend.DB.Beginx()
if err != nil {
return err
}
defer func() {
_ = tx.Rollback()
}()
_, err = tx.Exec(
"UPDATE patch_requests SET status=? WHERE id=?",
status,
prID,
)
if err != nil {
return err
}
pr, err := cmd.GetPatchRequestByID(prID)
if err != nil {
return err
}
err = cmd.CreateEventLog(tx, EventLog{
UserID: userID,
RepoID: sql.NullInt64{Int64: pr.RepoID, Valid: true},
PatchRequestID: sql.NullInt64{Int64: prID, Valid: true},
Event: "pr_status_changed",
Data: fmt.Sprintf(`{"status":"%s"}`, status),
})
if err != nil {
return err
}
return tx.Commit()
}
func (cmd PrCmd) UpdatePatchRequestName(prID int64, userID int64, name string) error {
if name == "" {
return fmt.Errorf("must provide name or text in order to update patch request")
}
tx, err := cmd.Backend.DB.Beginx()
if err != nil {
return err
}
defer func() {
_ = tx.Rollback()
}()
_, err = tx.Exec(
"UPDATE patch_requests SET name=? WHERE id=?",
name,
prID,
)
if err != nil {
return err
}
pr, err := cmd.GetPatchRequestByID(prID)
if err != nil {
return err
}
err = cmd.CreateEventLog(tx, EventLog{
UserID: userID,
RepoID: sql.NullInt64{Int64: pr.RepoID, Valid: true},
PatchRequestID: sql.NullInt64{Int64: prID, Valid: true},
Event: "pr_name_changed",
Data: fmt.Sprintf(`{"name":"%s"}`, name),
})
if err != nil {
return err
}
return tx.Commit()
}
func (cmd PrCmd) CreateEventLog(tx *sqlx.Tx, eventLog EventLog) error {
if eventLog.RepoID.Valid && eventLog.PatchRequestID.Valid {
var pr PatchRequest
err := tx.Get(
&pr,
"SELECT repo_id FROM patch_requests WHERE id=?",
eventLog.PatchRequestID,
)
if err != nil {
cmd.Backend.Logger.Error(
"could not find pr when creating eventLog",
"err", err,
)
return nil
}
eventLog.RepoID = sql.NullInt64{Int64: pr.RepoID, Valid: true}
}
_, err := tx.Exec(
"INSERT INTO event_logs (user_id, repo_id, patch_request_id, patchset_id, event, data) VALUES (?, ?, ?, ?, ?, ?)",
eventLog.UserID,
eventLog.RepoID,
eventLog.PatchRequestID.Int64,
eventLog.PatchsetID.Int64,
eventLog.Event,
eventLog.Data,
)
if err != nil {
cmd.Backend.Logger.Error(
"could not create eventLog",
"err", err,
)
}
return err
}
func (cmd PrCmd) createPatch(tx *sqlx.Tx, patch *Patch) (int64, error) {
patchExists := []Patch{}
_ = cmd.Backend.DB.Select(&patchExists, "SELECT * FROM patches WHERE patchset_id=? AND content_sha=?", patch.PatchsetID, patch.ContentSha)
if len(patchExists) > 0 {
return 0, ErrPatchExists
}
var patchID int64
row := tx.QueryRow(
"INSERT INTO patches (user_id, patchset_id, author_name, author_email, author_date, title, body, body_appendix, commit_sha, content_sha, base_commit_sha, raw_text) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id",
patch.UserID,
patch.PatchsetID,
patch.AuthorName,
patch.AuthorEmail,
patch.AuthorDate,
patch.Title,
patch.Body,
patch.BodyAppendix,
patch.CommitSha,
patch.ContentSha,
patch.BaseCommitSha,
patch.RawText,
)
err := row.Scan(&patchID)
if err != nil {
return 0, err
}
if patchID == 0 {
return 0, fmt.Errorf("could not create patch request")
}
return patchID, err
}
func (cmd PrCmd) SubmitPatchRequest(repoID int64, userID int64, patchset io.Reader) (*PatchRequest, error) {
tx, err := cmd.Backend.DB.Beginx()
if err != nil {
return nil, err
}
defer func() {
_ = tx.Rollback()
}()
patches, err := ParsePatchset(patchset)
if err != nil {
return nil, err
}
if len(patches) == 0 {
return nil, fmt.Errorf("after parsing patchset we did't find any patches, did you send us an empty patchset?")
}
prName := ""
prText := ""
if len(patches) > 0 {
prName = patches[0].Title
prText = patches[0].Body
}
var prID int64
row := tx.QueryRow(
"INSERT INTO patch_requests (user_id, repo_id, name, text, status, updated_at) VALUES(?, ?, ?, ?, ?, ?) RETURNING id",
userID,
repoID,
prName,
prText,
"open",
time.Now(),
)
err = row.Scan(&prID)
if err != nil {
return nil, err
}
if prID == 0 {
return nil, fmt.Errorf("could not create patch request")
}
var patchsetID int64
row = tx.QueryRow(
"INSERT INTO patchsets (user_id, patch_request_id) VALUES(?, ?) RETURNING id",
userID,
prID,
)
err = row.Scan(&patchsetID)
if err != nil {
return nil, err
}
if patchsetID == 0 {
return nil, fmt.Errorf("could not create patchset")
}
for _, patch := range patches {
patch.UserID = userID
patch.PatchsetID = patchsetID
_, err = cmd.createPatch(tx, patch)
if err != nil {
return nil, err
}
}
err = cmd.CreateEventLog(tx, EventLog{
UserID: userID,
RepoID: sql.NullInt64{Int64: repoID, Valid: true},
PatchRequestID: sql.NullInt64{Int64: prID, Valid: true},
PatchsetID: sql.NullInt64{Int64: patchsetID, Valid: true},
Event: "pr_created",
})
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
var pr PatchRequest
err = cmd.Backend.DB.Get(&pr, "SELECT * FROM patch_requests WHERE id=?", prID)
return &pr, err
}
func (cmd PrCmd) SubmitPatchset(prID int64, userID int64, op PatchsetOp, patchset io.Reader) ([]*Patch, error) {
fin := []*Patch{}
tx, err := cmd.Backend.DB.Beginx()
if err != nil {
return fin, err
}
defer func() {
_ = tx.Rollback()
}()
patches, err := ParsePatchset(patchset)
if err != nil {
return fin, err
}
isReview := op == OpReview || op == OpAccept || op == OpClose
var patchsetID int64
row := tx.QueryRow(
"INSERT INTO patchsets (user_id, patch_request_id, review) VALUES(?, ?, ?) RETURNING id",
userID,
prID,
isReview,
)
err = row.Scan(&patchsetID)
if err != nil {
return nil, err
}
if patchsetID == 0 {
return nil, fmt.Errorf("could not create patchset")
}
for _, patch := range patches {
patch.UserID = userID
patch.PatchsetID = patchsetID
patchID, err := cmd.createPatch(tx, patch)
if err == nil {
patch.ID = patchID
fin = append(fin, patch)
} else {
if !errors.Is(ErrPatchExists, err) {
return fin, err
}
}
}
if len(fin) > 0 {
event := "pr_patchset_added"
if op == OpReview {
event = "pr_reviewed"
}
pr, err := cmd.GetPatchRequestByID(prID)
if err != nil {
return fin, err
}
err = cmd.CreateEventLog(tx, EventLog{
UserID: userID,
RepoID: sql.NullInt64{Int64: pr.RepoID, Valid: true},
PatchRequestID: sql.NullInt64{Int64: prID, Valid: true},
PatchsetID: sql.NullInt64{Int64: patchsetID, Valid: true},
Event: event,
})
if err != nil {
return fin, err
}
}
err = tx.Commit()
if err != nil {
return fin, err
}
return fin, err
}
func (cmd PrCmd) DeletePatchsetByID(userID int64, prID int64, patchsetID int64) error {
tx, err := cmd.Backend.DB.Beginx()
if err != nil {
return err
}
defer func() {
_ = tx.Rollback()
}()
_, err = tx.Exec(
"DELETE FROM patchsets WHERE id=?", patchsetID,
)
if err != nil {
return err
}
pr, err := cmd.GetPatchRequestByID(prID)
if err != nil {
return err
}
err = cmd.CreateEventLog(tx, EventLog{
UserID: userID,
RepoID: sql.NullInt64{Int64: pr.RepoID, Valid: true},
PatchRequestID: sql.NullInt64{Int64: prID, Valid: true},
PatchsetID: sql.NullInt64{Int64: patchsetID, Valid: true},
Event: "pr_patchset_deleted",
})
if err != nil {
return err
}
return tx.Commit()
}
func (cmd PrCmd) GetEventLogs() ([]*EventLog, error) {
eventLogs := []*EventLog{}
err := cmd.Backend.DB.Select(
&eventLogs,
"SELECT * FROM event_logs ORDER BY created_at DESC",
)
return eventLogs, err
}
func (cmd PrCmd) GetEventLogsByRepoName(user *User, repoName string) ([]*EventLog, error) {
repo, err := cmd.GetRepoByName(user, repoName)
if err != nil {
return nil, err
}
eventLogs := []*EventLog{}
err = cmd.Backend.DB.Select(
&eventLogs,
"SELECT * FROM event_logs WHERE repo_id=? ORDER BY created_at DESC",
repo.ID,
)
return eventLogs, err
}
func (cmd PrCmd) GetEventLogsByPrID(prID int64) ([]*EventLog, error) {
eventLogs := []*EventLog{}
err := cmd.Backend.DB.Select(
&eventLogs,
"SELECT * FROM event_logs WHERE patch_request_id=? ORDER BY created_at DESC",
prID,
)
return eventLogs, err
}
func (cmd PrCmd) GetEventLogsByUserID(userID int64) ([]*EventLog, error) {
eventLogs := []*EventLog{}
query := `SELECT * FROM event_logs
WHERE user_id=?
OR patch_request_id IN (
SELECT id FROM patch_requests WHERE user_id=?
)
ORDER BY created_at DESC`
err := cmd.Backend.DB.Select(
&eventLogs,
query,
userID,
userID,
)
return eventLogs, err
}
func (cmd PrCmd) DiffPatchsets(prev *Patchset, next *Patchset) ([]*RangeDiffOutput, error) {
output := []*RangeDiffOutput{}
patches, err := cmd.GetPatchesByPatchsetID(next.ID)
if err != nil {
return output, err
}
for idx, patch := range patches {
patchStr := patch.RawText
if idx > 0 {
patchStr = startOfPatch + patch.RawText
}
diffFiles, _, err := ParsePatch(patchStr)
if err != nil {
continue
}
patch.Files = diffFiles
}
if prev == nil {
return output, nil
}
prevPatches, err := cmd.GetPatchesByPatchsetID(prev.ID)
if err != nil {
return output, fmt.Errorf("cannot get previous patchset patches: %w", err)
}
for idx, patch := range prevPatches {
patchStr := patch.RawText
if idx > 0 {
patchStr = startOfPatch + patch.RawText
}
diffFiles, _, err := ParsePatch(patchStr)
if err != nil {
continue
}
patch.Files = diffFiles
}
return RangeDiff(prevPatches, patches), nil
}