-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcli.go
More file actions
869 lines (752 loc) · 22.5 KB
/
Copy pathcli.go
File metadata and controls
869 lines (752 loc) · 22.5 KB
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
package patchbin
import (
"bytes"
"fmt"
"io"
"strconv"
"strings"
"text/tabwriter"
"time"
"github.com/picosh/pico/pkg/pssh"
"github.com/urfave/cli/v2"
)
func NewTabWriter(out io.Writer) *tabwriter.Writer {
return tabwriter.NewWriter(out, 0, 0, 1, ' ', tabwriter.TabIndent)
}
func strToInt(str string) (int64, error) {
prID, err := strconv.ParseInt(str, 10, 64)
return prID, err
}
// readStdinLimited reads all of stdin, rejecting input over maxBytes rather
// than silently truncating it.
func readStdinLimited(r io.Reader, maxBytes int64) ([]byte, error) {
limited := io.LimitReader(r, maxBytes+1)
body, err := io.ReadAll(limited)
if err != nil {
return nil, err
}
if int64(len(body)) > maxBytes {
return nil, fmt.Errorf("stdin exceeds max size of %d bytes", maxBytes)
}
return body, nil
}
func getPatchsetFromOpt(patchsets []*Patchset, optPatchsetID string) (*Patchset, error) {
if optPatchsetID == "" {
return patchsets[len(patchsets)-1], nil
}
id, err := getPatchsetID(optPatchsetID)
if err != nil {
return nil, err
}
for _, ps := range patchsets {
if ps.ID == id {
return ps, nil
}
}
return nil, fmt.Errorf("cannot find patchset: %s", optPatchsetID)
}
func prSummary(be *Backend, pr GitPatchRequest, sesh *pssh.SSHServerConnSession, prID int64) error {
request, err := pr.GetPatchRequestByID(prID)
if err != nil {
return err
}
sesh.Printf("Info\n====\n")
sesh.Printf("URL: https://%s/prs/%d\n", be.Cfg.Url, prID)
sesh.Printf("Repo: %s\n\n", request.RepoName)
writer := NewTabWriter(sesh)
_, _ = fmt.Fprintln(writer, "ID\tName\tStatus\tDate")
_, _ = fmt.Fprintf(
writer,
"%d\t%s\t[%s]\t%s\n",
request.ID, request.Name, request.Status, request.CreatedAt.Format(be.Cfg.TimeFormat),
)
_ = writer.Flush()
patchsets, err := pr.GetPatchsetsByPrID(prID)
if err != nil {
return err
}
sesh.Printf("\nPatchsets\n====\n")
writerSet := NewTabWriter(sesh)
_, _ = fmt.Fprintln(writerSet, "ID\tUser\tDate")
for _, patchset := range patchsets {
user, err := pr.GetUserByID(patchset.UserID)
if err != nil {
be.Logger.Error("cannot find user for patchset", "err", err)
continue
}
displayName := be.ComputeUserName(user.Pubkey)
_, _ = fmt.Fprintf(
writerSet,
"%s\t%s\t%s\n",
getFormattedPatchsetID(patchset.ID),
displayName,
patchset.CreatedAt.Format(be.Cfg.TimeFormat),
)
}
_ = writerSet.Flush()
latest, err := getPatchsetFromOpt(patchsets, "")
if err != nil {
return err
}
patches, err := pr.GetPatchesByPatchsetID(latest.ID)
if err != nil {
return err
}
sesh.Printf("\nPatches from latest patchset\n====\n")
opatches := patches
w := NewTabWriter(sesh)
_, _ = fmt.Fprintln(w, "Idx\tTitle\tCommit\tAuthor\tDate")
for idx, patch := range opatches {
timestamp := patch.AuthorDate.Format(be.Cfg.TimeFormat)
_, _ = fmt.Fprintf(
w,
"%d\t%s\t%s\t%s <%s>\t%s\n",
idx,
patch.Title,
truncateSha(patch.CommitSha),
patch.AuthorName,
patch.AuthorEmail,
timestamp,
)
}
_ = w.Flush()
return nil
}
// printCoverLetterFromPrID prints patches with a cover letter and discussion.
func printCoverLetterFromPrID(sesh *pssh.SSHServerConnSession, be *Backend, gpr GitPatchRequest, prID int64) error {
pr, err := gpr.GetPatchRequestByID(prID)
if err != nil {
return err
}
patchsets, err := gpr.GetPatchsetsByPrID(prID)
if err != nil {
return err
}
ps := patchsets[len(patchsets)-1]
patches, err := gpr.GetPatchesByPatchsetID(ps.ID)
if err != nil {
return err
}
events, err := gpr.GetEventLogsByPrID(prID)
if err != nil {
return err
}
users := resolveUsers(gpr, events)
mbox := GenerateMboxWithCoverLetter(pr, patches, events, users, be.Cfg.Url)
sesh.Println(mbox)
return nil
}
// printCoverLetterFromPsID prints patches with a cover letter and discussion.
func printCoverLetterFromPsID(sesh *pssh.SSHServerConnSession, be *Backend, gpr GitPatchRequest, psID int64) error {
ps, err := gpr.GetPatchsetByID(psID)
if err != nil {
return err
}
pr, err := gpr.GetPatchRequestByID(ps.PatchRequestID)
if err != nil {
return err
}
patches, err := gpr.GetPatchesByPatchsetID(ps.ID)
if err != nil {
return err
}
events, err := gpr.GetEventLogsByPrID(ps.PatchRequestID)
if err != nil {
return err
}
users := resolveUsers(gpr, events)
mbox := GenerateMboxWithCoverLetter(pr, patches, events, users, be.Cfg.Url)
sesh.Println(mbox)
return nil
}
// resolveUsers loads user records for all user IDs referenced in events.
func resolveUsers(gpr GitPatchRequest, events []*EventLog) map[int64]*User {
users := make(map[int64]*User)
for _, event := range events {
if _, ok := users[event.UserID]; !ok {
user, err := gpr.GetUserByID(event.UserID)
if err == nil {
users[event.UserID] = user
}
}
}
return users
}
func NewCli(sesh *pssh.SSHServerConnSession, be *Backend, pr GitPatchRequest) *cli.App {
url := be.Cfg.Url
desc := fmt.Sprintf(`patchbin (v%s): a pastebin for patches, supercharged for git collaboration.
Contributions are anonymous: connect with an SSH key, no signup. A patch
request works like a pull request, except both sides collaborate by
sending rounds of patchsets -- as commits, not comments -- back and forth
on top of each other. Reviewing means pulling the code down, not clicking
through a diff viewer. An issue is just a patch request without any code
attached yet, so anyone can follow up with a real patch request on top of it.
There's no accept/reject step. A PR is either draft (visible only to you)
or open (visible to everyone, appears in RSS). It goes inactive after 30
days without activity; a reviewer who's happy just pulls it, merges it, and
pushes upstream themselves.
COMMANDS
pr - manage patch requests
pr create {repo}
Submit a new PR from stdin (starts as draft).
git format-patch main --stdout | ssh %[2]s pr create {repo}
pr add {prID}
Add a new patchset to an existing PR from stdin.
git format-patch main --stdout | ssh %[2]s pr add {prID}
pr open {prID} [--comment]
Transition draft -> open, enables RSS notifications.
ssh %[2]s pr open {prID}
pr draft {prID} [--comment]
Transition open -> draft, disables RSS notifications.
ssh %[2]s pr draft {prID}
pr edit {prID} {title}
Rename a PR.
ssh %[2]s pr edit {prID} "new title"
pr summary {prID}
Show metadata, patchsets, and patches for a PR.
ssh %[2]s pr summary {prID}
pr ls [repo] [--draft|--open|--active|--inactive|--mine]
List PRs.
ssh %[2]s pr ls {repo} --open
issue - text-only patch requests (no code required)
issue create {repo} [--title]
Submit a new issue from stdin (starts as open).
echo "steps to reproduce..." | ssh %[2]s issue create {repo} --title "bug: crash on startup"
ps - manage patchsets
ps rm {patchsetID}
Remove a patchset and its patches (creator only).
ssh %[2]s ps rm ps-{patchsetID}
print - print patches for checkout
print pr-{prID}
Print the latest patchset for a PR.
ssh %[2]s print pr-{prID} | git am -3
print ps-{patchsetID}
Print a specific patchset.
ssh %[2]s print ps-{patchsetID} | git am -3
Cover letters are stored as an empty commit. If you want to keep them
when applying, use "git am --keep-empty" (or set it globally with
"git config --global am.keepEmpty true").
logs - event history
logs [--pr ID] [--pubkey]
List event logs, optionally filtered to a PR or your own activity.
ssh %[2]s logs --pr {prID}
STDIN
pr create, pr add expect the output of "git format-patch --stdout"
issue create expects free-form text (the issue body)
pr open/draft --comment expects free-form text (a comment to attach to the status change)
GUARDS
To limit abuse, submissions (pr create, pr add, issue create) are capped
at %[3]d bytes of stdin, and globally rate limited to %[4]d submissions
per %[5]s across all users. Contact an admin if you hit these limits.
Admins with shell access to the host can ban a pubkey or IP address by
inserting a row directly into the "acl" table of the sqlite database:
sqlite3 data/pr.db "INSERT INTO acl (pubkey, permission) VALUES ('{pubkey}', 'banned')"
sqlite3 data/pr.db "INSERT INTO acl (ip_address, permission) VALUES ('{ip}', 'banned')"
Banned pubkeys/IPs are rejected at SSH auth time. There is currently no
SSH command for this; it requires direct database access.
Self-host your own patchbin: https://github.com/picosh/patchbin
`, GITPR_VERSION, url, be.Cfg.MaxStdinBytes, be.Cfg.RateLimitCount, be.Cfg.RateLimitInterval)
pubkey := be.Pubkey(sesh.PublicKey())
app := &cli.App{
Name: "ssh",
Description: desc,
Usage: "A pastebin for patches, supercharged for git collaboration",
CustomAppHelpTemplate: "{{.Description}}\n",
Writer: sesh,
ErrWriter: sesh,
ExitErrHandler: func(cCtx *cli.Context, err error) {
if err != nil {
sesh.Fatal(fmt.Errorf("err: %w", err))
}
},
OnUsageError: func(cCtx *cli.Context, err error, isSubcommand bool) error {
if err != nil {
sesh.Fatal(fmt.Errorf("err: %w", err))
}
return nil
},
Commands: []*cli.Command{
{
Name: "issue",
Usage: "Manage issues (text-only patch requests)",
Subcommands: []*cli.Command{
{
Name: "create",
Usage: "Submit a new issue (starts as open)",
Args: true,
ArgsUsage: "repoName",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "title",
Usage: "issue title (default: first line of stdin)",
},
},
Action: func(cCtx *cli.Context) error {
if !be.Limiter.Allow() {
return be.Limiter.Error()
}
user, err := pr.UpsertUserByPubkey(pubkey)
if err != nil {
return err
}
args := cCtx.Args()
if !args.Present() {
return fmt.Errorf("must provide a repo name")
}
repoName := args.First()
body, err := readStdinLimited(sesh, be.Cfg.MaxStdinBytes)
if err != nil {
return fmt.Errorf("failed to read issue body from stdin: %w", err)
}
bodyStr := strings.TrimSpace(string(body))
if bodyStr == "" {
return fmt.Errorf("must provide issue body via stdin")
}
title := cCtx.String("title")
if title == "" {
// Use first line as title
lines := strings.SplitN(bodyStr, "\n", 2)
title = lines[0]
if len(lines) > 1 {
bodyStr = strings.TrimSpace(lines[1])
} else {
bodyStr = ""
}
}
prq, err := pr.SubmitIssue(user.ID, pubkey, repoName, title, bodyStr)
if err != nil {
return err
}
sesh.Printf("Issue created! #%d\n", prq.ID)
return prSummary(be, pr, sesh, prq.ID)
},
},
},
},
{
Name: "logs",
Usage: "List event logs with filters",
Args: true,
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "pr",
Usage: "show all events related to the provided patch request",
},
&cli.BoolFlag{
Name: "pubkey",
Usage: "show all events related to your pubkey",
},
},
Action: func(cCtx *cli.Context) error {
user, err := pr.UpsertUserByPubkey(pubkey)
if err != nil {
return err
}
isPubkey := cCtx.Bool("pubkey")
prID := cCtx.Int64("pr")
var eventLogs []*EventLog
if isPubkey {
eventLogs, err = pr.GetEventLogsByUserID(user.ID)
} else if prID != 0 {
eventLogs, err = pr.GetEventLogsByPrID(prID)
} else {
eventLogs, err = pr.GetEventLogs()
}
if err != nil {
return err
}
writer := NewTabWriter(sesh)
_, _ = fmt.Fprintln(writer, "PrID\tPatchsetID\tEvent\tCreated\tData")
for _, eventLog := range eventLogs {
_, _ = fmt.Fprintf(
writer,
"%d\t%s\t%s\t%s\t%s\n",
eventLog.PatchRequestID.Int64,
getFormattedPatchsetID(eventLog.PatchsetID.Int64),
eventLog.Event,
eventLog.CreatedAt.Format(be.Cfg.TimeFormat),
eventLog.Data,
)
}
_ = writer.Flush()
return nil
},
},
{
Name: "ps",
Usage: "Manage patchsets",
Subcommands: []*cli.Command{
{
Name: "rm",
Usage: "Remove a patchset and its patches",
Args: true,
ArgsUsage: "[patchsetID]",
Action: func(cCtx *cli.Context) error {
args := cCtx.Args()
if !args.Present() {
return fmt.Errorf("must provide a patchset ID")
}
patchsetID, err := getPatchsetID(args.First())
if err != nil {
return err
}
patchset, err := pr.GetPatchsetByID(patchsetID)
if err != nil {
return err
}
user, err := pr.GetUserByID(patchset.UserID)
if err != nil {
return err
}
if pubkey != user.Pubkey {
return fmt.Errorf("you are not authorized to delete this patchset (only the creator can delete)")
}
err = pr.DeletePatchsetByID(user.ID, patchset.PatchRequestID, patchsetID)
if err != nil {
return err
}
sesh.Printf("successfully removed patchset: %d\n", patchsetID)
return nil
},
},
},
},
{
Name: "print",
Usage: "Print patches in a patchset",
Args: true,
ArgsUsage: "[pr-X] or [ps-X]",
Action: func(cCtx *cli.Context) error {
args := cCtx.Args()
raw := args.First()
split := strings.Split(raw, "-")
if len(split) < 2 {
return fmt.Errorf("must provide ID in format: pr-X, ps-X")
}
prefix := split[0]
id, err := strToInt(split[1])
if err != nil {
return err
}
switch prefix {
case "pr":
err = printCoverLetterFromPrID(sesh, be, pr, id)
case "ps":
err = printCoverLetterFromPsID(sesh, be, pr, id)
default:
return fmt.Errorf("unknown prefix %q, must be one of: pr, ps", prefix)
}
return err
},
},
{
Name: "pr",
Usage: "Manage patch requests (PR)",
Subcommands: []*cli.Command{
{
Name: "ls",
Usage: "List all PRs",
Args: true,
ArgsUsage: "[repoName]",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "draft",
Usage: "only show draft PRs",
},
&cli.BoolFlag{
Name: "open",
Usage: "only show open PRs",
},
&cli.BoolFlag{
Name: "active",
Usage: "only show active PRs (activity in last 30 days)",
},
&cli.BoolFlag{
Name: "inactive",
Usage: "only show inactive PRs (no activity in 30 days)",
},
&cli.BoolFlag{
Name: "mine",
Usage: "only show your own PRs",
},
},
Action: func(cCtx *cli.Context) error {
args := cCtx.Args()
repoName := args.First()
var prs []*PatchRequest
var err error
if repoName == "" {
prs, err = pr.GetPatchRequests()
if err != nil {
return err
}
} else {
prs, err = pr.GetPatchRequestsByRepoName(repoName)
if err != nil {
return err
}
}
onlyDraft := cCtx.Bool("draft")
onlyOpen := cCtx.Bool("open")
onlyActive := cCtx.Bool("active")
onlyInactive := cCtx.Bool("inactive")
onlyMine := cCtx.Bool("mine")
cutoff := time.Now().AddDate(0, 0, -30)
writer := NewTabWriter(sesh)
_, _ = fmt.Fprintln(writer, "ID\tRepo\tName\tStatus\tPatchsets\tUser\tLast Activity")
for _, req := range prs {
if onlyDraft && req.Status != StatusDraft {
continue
}
if onlyOpen && req.Status != StatusOpen {
continue
}
if onlyActive && req.LastActivity.Before(cutoff) {
continue
}
if onlyInactive && req.LastActivity.After(cutoff) {
continue
}
user, err := pr.GetUserByID(req.UserID)
if err != nil {
be.Logger.Error("could not get user for pr", "err", err)
continue
}
if onlyMine && user.Pubkey != pubkey {
continue
}
patchsets, err := pr.GetPatchsetsByPrID(req.ID)
if err != nil {
be.Logger.Error("could not get patchsets for pr", "err", err)
continue
}
displayName := be.ComputeUserName(user.Pubkey)
_, _ = fmt.Fprintf(
writer,
"%d\t%s\t%s\t[%s]\t%d\t%s\t%s\n",
req.ID,
req.RepoName,
req.Name,
req.Status,
len(patchsets),
displayName,
req.LastActivity.Format(be.Cfg.TimeFormat),
)
}
_ = writer.Flush()
return nil
},
},
{
Name: "create",
Usage: "Submit a new PR (starts as draft)",
Args: true,
ArgsUsage: "repoName",
Action: func(cCtx *cli.Context) error {
if !be.Limiter.Allow() {
return be.Limiter.Error()
}
user, err := pr.UpsertUserByPubkey(pubkey)
if err != nil {
return err
}
args := cCtx.Args()
if !args.Present() {
return fmt.Errorf("must provide a repo name")
}
repoName := args.First()
body, err := readStdinLimited(sesh, be.Cfg.MaxStdinBytes)
if err != nil {
return fmt.Errorf("failed to read patchset from stdin: %w", err)
}
prq, err := pr.SubmitPatchRequest(user.ID, pubkey, repoName, bytes.NewReader(body))
if err != nil {
return err
}
sesh.Println(
"PR submitted as draft! Use `pr open <id>` to make it visible.",
)
return prSummary(be, pr, sesh, prq.ID)
},
},
{
Name: "open",
Usage: "Transition PR to open (enable RSS notifications)",
Args: true,
ArgsUsage: "[prID]",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "comment",
Usage: "If this flag is provided, pass comment through stdin",
},
},
Action: func(cCtx *cli.Context) error {
args := cCtx.Args()
if !args.Present() {
return fmt.Errorf("must provide a patch request ID")
}
prID, err := strToInt(args.First())
if err != nil {
return err
}
prq, err := pr.GetPatchRequestByID(prID)
if err != nil {
return err
}
if prq.Status == StatusOpen {
return fmt.Errorf("PR is already open")
}
comment := cCtx.Bool("comment")
var commentTxt []byte
if comment {
commentTxt, err = io.ReadAll(sesh)
if err != nil {
return fmt.Errorf("when comment flag enabled must provide it from stdin")
}
}
err = pr.UpdatePatchRequestStatus(prID, pubkey, StatusOpen, string(commentTxt))
if err != nil {
return err
}
sesh.Printf("Opened PR %s (#%d)\n", prq.Name, prq.ID)
return prSummary(be, pr, sesh, prID)
},
},
{
Name: "draft",
Usage: "Transition PR to draft (disable RSS notifications)",
Args: true,
ArgsUsage: "[prID]",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "comment",
Usage: "If this flag is provided, pass comment through stdin",
},
},
Action: func(cCtx *cli.Context) error {
args := cCtx.Args()
if !args.Present() {
return fmt.Errorf("must provide a patch request ID")
}
prID, err := strToInt(args.First())
if err != nil {
return err
}
prq, err := pr.GetPatchRequestByID(prID)
if err != nil {
return err
}
if prq.Status == StatusDraft {
return fmt.Errorf("PR is already a draft")
}
comment := cCtx.Bool("comment")
var commentTxt []byte
if comment {
commentTxt, err = io.ReadAll(sesh)
if err != nil {
return fmt.Errorf("when comment flag enabled must provide it from stdin")
}
}
err = pr.UpdatePatchRequestStatus(prID, pubkey, StatusDraft, string(commentTxt))
if err != nil {
return err
}
sesh.Printf("Drafted PR %s (#%d)\n", prq.Name, prq.ID)
return prSummary(be, pr, sesh, prID)
},
},
{
Name: "summary",
Usage: "Show metadata, patchsets, and patches for a PR",
Args: true,
ArgsUsage: "[prID]",
Action: func(cCtx *cli.Context) error {
args := cCtx.Args()
if !args.Present() {
return fmt.Errorf("must provide a patch request ID")
}
prID, err := strToInt(args.First())
if err != nil {
return err
}
return prSummary(be, pr, sesh, prID)
},
},
{
Name: "edit",
Usage: "Edit a PR's title",
Args: true,
ArgsUsage: "[prID] [title]",
Action: func(cCtx *cli.Context) error {
args := cCtx.Args()
if !args.Present() {
return fmt.Errorf("must provide a patch request ID")
}
prID, err := strToInt(args.First())
if err != nil {
return err
}
prq, err := pr.GetPatchRequestByID(prID)
if err != nil {
return err
}
tail := cCtx.Args().Tail()
title := strings.Join(tail, " ")
if title == "" {
return fmt.Errorf("must provide title")
}
err = pr.UpdatePatchRequestName(prID, pubkey, title)
if err != nil {
return err
}
sesh.Printf("New title: %s (%d)\n", title, prq.ID)
return err
},
},
{
Name: "add",
Usage: "Add a new patchset to a PR",
Args: true,
ArgsUsage: "[prID]",
Action: func(cCtx *cli.Context) error {
if !be.Limiter.Allow() {
return be.Limiter.Error()
}
args := cCtx.Args()
if !args.Present() {
return fmt.Errorf("must provide a patch request ID")
}
prID, err := strToInt(args.First())
if err != nil {
return err
}
_, err = pr.GetPatchRequestByID(prID)
if err != nil {
return err
}
user, err := pr.UpsertUserByPubkey(pubkey)
if err != nil {
return err
}
body, err := readStdinLimited(sesh, be.Cfg.MaxStdinBytes)
if err != nil {
return fmt.Errorf("failed to read patchset from stdin: %w", err)
}
patches, err := pr.SubmitPatchset(prID, user.ID, OpNormal, bytes.NewReader(body))
if err != nil {
return err
}
if len(patches) == 0 {
sesh.Println("Patches submitted! However none were saved, probably because they already exist in the system")
return nil
}
sesh.Println("Patches submitted!")
return prSummary(be, pr, sesh, prID)
},
},
},
},
},
}
return app
}