/
pr.go
759 lines (695 loc) · 22.3 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
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
package actions
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/aviator-co/av/internal/utils/errutils"
"emperror.dev/errors"
"github.com/aviator-co/av/internal/config"
"github.com/aviator-co/av/internal/editor"
"github.com/aviator-co/av/internal/gh"
"github.com/aviator-co/av/internal/git"
"github.com/aviator-co/av/internal/meta"
"github.com/aviator-co/av/internal/utils/browser"
"github.com/aviator-co/av/internal/utils/colors"
"github.com/aviator-co/av/internal/utils/sanitize"
"github.com/aviator-co/av/internal/utils/stringutils"
"github.com/aviator-co/av/internal/utils/templateutils"
"github.com/fatih/color"
"github.com/shurcooL/githubv4"
"github.com/sirupsen/logrus"
)
type CreatePullRequestOpts struct {
// The HEAD branch to create a pull request for.
BranchName string
// The pull request title.
Title string
// The pull request body (description).
Body string
// If true, create the pull request as a GitHub draft PR.
Draft bool
// If true, do not push the branch to GitHub
NoPush bool
// If true, force push the branch to GitHub
ForcePush bool
// If true, create a PR even if we think one already exists
Force bool
// If true, open an editor for editing the title and body
Edit bool
}
type CreatePullRequestResult struct {
// True if the pull request was created
Created bool
// The (updated) branch metadata.
Branch meta.Branch
// The pull request object that was returned from GitHub
Pull *gh.PullRequest
}
// getPRMetadata constructs the PRMetadata for the current state of the branch.
// TODO:
// The way we pass/load all the relevant data here is not great(tm). The
// `parent` argument is optional because sometimes it's loadeed by the
// calling function and sometimes not. :shrug: It can also be nil if the
// branch doesn't have a parent (i.e., the branch is a stack root).
func getPRMetadata(
tx meta.ReadTx,
branch meta.Branch,
parent *meta.Branch,
) (PRMetadata, error) {
trunk, _ := meta.Trunk(tx, branch.Name)
prMeta := PRMetadata{
Parent: branch.Parent.Name,
ParentHead: branch.Parent.Head,
Trunk: trunk,
}
if parent == nil && branch.Parent.Name != "" {
p, _ := tx.Branch(branch.Parent.Name)
parent = &p
}
if parent != nil && parent.PullRequest != nil {
prMeta.ParentPull = parent.PullRequest.Number
}
return prMeta, nil
}
type errPullRequestClosed struct {
*gh.PullRequest
}
func (e errPullRequestClosed) Error() string {
return fmt.Sprintf("pull request #%d is %s", e.Number, e.State)
}
// getExistingOpenPR returns an existing pull request for the given branch if
// any exist and are open.
func getExistingOpenPR(
ctx context.Context,
client *gh.Client,
repoMeta meta.Repository,
branchMeta meta.Branch,
baseRefName string,
) (*gh.PullRequest, error) {
if branchMeta.PullRequest != nil {
logrus.WithField("pr", branchMeta.PullRequest.Number).
Debug("querying data for existing PR from GitHub")
pr, err := client.PullRequest(ctx, branchMeta.PullRequest.ID)
if err != nil {
return nil, errors.WrapIf(err, "querying existing pull request")
}
if pr.State != githubv4.PullRequestStateOpen {
return nil, errPullRequestClosed{pr}
}
return pr, nil
}
logrus.WithField("branch", branchMeta.Name).Debug("querying existing open PRs from GitHub")
existing, err := client.GetPullRequests(ctx, gh.GetPullRequestsInput{
Owner: repoMeta.Owner,
Repo: repoMeta.Name,
HeadRefName: branchMeta.Name,
BaseRefName: baseRefName,
States: []githubv4.PullRequestState{githubv4.PullRequestStateOpen},
})
if err != nil {
return nil, errors.WrapIf(err, "querying existing pull requests")
}
if len(existing.PullRequests) > 1 {
return nil, errors.Errorf("multiple existing PRs found for %q", branchMeta.Name)
} else if len(existing.PullRequests) == 1 {
return &existing.PullRequests[0], nil
}
return nil, nil
}
// CreatePullRequest creates a pull request on GitHub for the current branch, if
// one doesn't already exist.
func CreatePullRequest(
ctx context.Context,
repo *git.Repo,
client *gh.Client,
tx meta.WriteTx,
opts CreatePullRequestOpts,
) (_ *CreatePullRequestResult, reterr error) {
if opts.BranchName == "" {
logrus.Panicf("internal invariant error: CreatePullRequest called with empty branch name")
}
repoMeta, ok := tx.Repository()
if !ok {
return nil, ErrRepoNotInitialized
}
branchMeta, _ := tx.Branch(opts.BranchName)
var existingPR *gh.PullRequest
if !opts.Force {
var err error
existingPR, err = getExistingOpenPR(ctx, client, repoMeta, branchMeta, opts.BranchName)
if closed, ok := errutils.As[errPullRequestClosed](err); ok {
_, _ = fmt.Fprint(os.Stderr,
colors.Failure("Existing pull request for branch "),
colors.UserInput(opts.BranchName),
colors.Failure(" is "), colors.UserInput(closed.State),
colors.Failure(": "), colors.UserInput(closed.Permalink),
"\n",
)
_, _ = fmt.Fprint(os.Stderr,
colors.Faint(" - use "), colors.CliCmd("av pr create --force"),
colors.Faint(" to create a new pull request for this branch\n"),
)
return nil, err
} else if err != nil {
return nil, errors.WrapIf(err, "failed to get existing pull request")
}
} else {
// If we're forcing creation of a new PR, we always want to force push.
// This prevents errors where we try to use `--force-with-lease` but the
// remote branch has been deleted.
opts.ForcePush = true
}
verb := "Creating"
if existingPR != nil {
verb = "Updating"
}
_, _ = fmt.Fprint(os.Stderr,
verb, " pull request for branch ", colors.UserInput(opts.BranchName), ":",
"\n",
)
if !opts.NoPush || opts.ForcePush {
pushFlags := []string{"push"}
if opts.ForcePush {
pushFlags = append(pushFlags, "--force")
} else {
// Use --force-with-lease to allow pushing branches that have been
// rebased but don't overwrite changes if we don't expect them to
// be there.
pushFlags = append(pushFlags, "--force-with-lease")
}
// NOTE: This assumes that the user use the default push strategy (simple). It would
// be rare to use the upstream strategy.
pushFlags = append(pushFlags, "origin", opts.BranchName)
logrus.Debug("pushing latest changes")
_, _ = fmt.Fprint(os.Stderr,
" - pushing to ", color.CyanString("origin/%s", opts.BranchName),
"\n",
)
if _, err := repo.Git(pushFlags...); err != nil {
return nil, errors.WrapIf(err, "failed to push")
}
} else {
_, _ = fmt.Fprint(os.Stderr,
" - skipping push to GitHub",
"\n",
)
}
// figure this out based on whether or not we're on a stacked branch
parentState := branchMeta.Parent
if parentState.Name == "" {
defaultBranch, err := repo.DefaultBranch()
if err != nil {
return nil, errors.WrapIf(err, "failed to determine default branch")
}
parentState = meta.BranchState{
Name: defaultBranch,
Trunk: true,
}
branchMeta.Parent = parentState
}
prCompareRef := parentState.Name
var parentMeta meta.Branch
if !parentState.Trunk {
// check if the base branch also has an associated PR
var ok bool
parentMeta, ok = tx.Branch(parentState.Name)
if !ok {
return nil, errors.Errorf("failed to read branch metadata for %q", parentState.Name)
}
if parentMeta.PullRequest == nil {
// TODO:
// We should automagically create PRs for every branch in the stack
return nil, errors.Errorf(
"base branch %q does not have an associated pull request "+
"(create one by checking out the branch and running `av pr create`)",
parentState.Name,
)
}
} else {
logrus.WithField("base", parentState.Name).Debug("base branch is a trunk branch")
prCompareRef = "origin/" + parentState.Name
}
commitsList, err := repo.Git("rev-list", "--reverse", fmt.Sprintf("%s..%s", prCompareRef, opts.BranchName))
if err != nil {
return nil, errors.WrapIf(err, "failed to determine commits to include in PR")
}
if commitsList == "" {
return nil, errors.Errorf("no commits between %q and %q", prCompareRef, opts.BranchName)
}
if existingPR != nil {
// If there's an existing PR, use that as the new PR title and body. If --edit is
// used, an editor is opened later.
if opts.Title == "" {
opts.Title = existingPR.Title
}
if opts.Body == "" {
opts.Body = existingPR.Body
}
}
if opts.Edit || opts.Body == "" || opts.Title == "" {
var commits []git.CommitInfo
for _, commitHash := range strings.Split(commitsList, "\n") {
commit, err := repo.CommitInfo(git.CommitInfoOpts{Rev: commitHash})
if err != nil {
return nil, errors.WrapIff(err, "failed to get commit info for %q", commitHash)
}
commits = append(commits, *commit)
}
// If a saved pull request description exists, use that.
saveFile := filepath.Join(
repo.AvTmpDir(),
fmt.Sprintf("av-pr-%s.md", sanitize.FileName(opts.BranchName)),
)
if _, err := os.Stat(saveFile); err == nil {
contents, err := os.ReadFile(saveFile)
if err != nil {
logrus.WithError(err).Warn("failed to read saved pull request description")
} else {
title, body := stringutils.ParseSubjectBody(string(contents))
if opts.Title == "" {
opts.Title = title
}
if opts.Body == "" {
opts.Body = body
}
}
}
// Try to populate the editor text using contextual information from the
// repository and commits included in this pull request.
if opts.Title == "" {
opts.Title = commits[0].Subject
}
// Reasonable defaults for body:
// 1. Try and find a pull request template
if opts.Body == "" {
opts.Body = readDefaultPullRequestTemplate(repo)
}
// 2. Use the commit message from the first PR
if opts.Body == "" {
opts.Body = commits[0].Body
}
editorText := templateutils.MustString(prBodyTemplate, prBodyTemplateData{
Branch: opts.BranchName,
Title: opts.Title,
Body: opts.Body,
Commits: commits,
})
res, err := editor.Launch(repo, editor.Config{
Text: editorText,
TmpFilePattern: "pr-*.av.md",
CommentPrefix: "%%",
})
if err != nil {
if res != "" {
savePRDescriptionToTemporaryFile(saveFile, res)
}
return nil, errors.WrapIf(err, "text editor failed")
}
opts.Title, opts.Body = stringutils.ParseSubjectBody(res)
if opts.Title == "" {
return nil, errors.New("aborting pull request due to empty message")
}
defer func() {
// If we created the PR successfully, just make sure to clean up any
// lingering files.
if reterr == nil {
_ = os.Remove(saveFile)
return
}
// Otherwise, save what the user entered to a file so that it's not
// lost forever (and we can re-use it if they try again).
savePRDescriptionToTemporaryFile(saveFile, res)
}()
}
prMeta, err := getPRMetadata(tx, branchMeta, &parentMeta)
if err != nil {
return nil, err
}
draft := opts.Draft
if !config.Av.PullRequest.NoWIPDetection && strings.Contains(opts.Title, "WIP") {
draft = true
}
pull, didCreatePR, err := ensurePR(ctx, client, repoMeta, ensurePROpts{
baseRefName: parentState.Name,
headRefName: opts.BranchName,
title: opts.Title,
body: opts.Body,
meta: prMeta,
draft: draft,
existingPR: existingPR,
})
if err != nil {
_, _ = fmt.Fprint(os.Stderr,
colors.Failure(" - failed to create pull request: "), err, "\n",
)
return nil, errors.WrapIf(err, "failed to create PR")
}
branchMeta.PullRequest = &meta.PullRequest{
Number: pull.Number,
ID: pull.ID,
Permalink: pull.Permalink,
}
// It's possible that a new PR is created with the same branch. Reset the MergeCommit.
branchMeta.MergeCommit = ""
var action string
if didCreatePR {
action = "created"
} else {
action = "synchronized"
}
_, _ = fmt.Fprint(os.Stderr,
" - ", action, " pull request ",
colors.UserInput(pull.Permalink), "\n",
)
if didCreatePR && config.Av.PullRequest.OpenBrowser {
if err := browser.Open(pull.Permalink); err != nil {
_, _ = fmt.Fprint(os.Stderr,
" - couldn't open browser ",
colors.UserInput(err),
" for pull request link ",
colors.UserInput(pull.Permalink),
)
}
}
tx.SetBranch(branchMeta)
return &CreatePullRequestResult{didCreatePR, branchMeta, pull}, nil
}
func savePRDescriptionToTemporaryFile(saveFile string, contents string) {
if err := os.WriteFile(saveFile, []byte(contents), 0644); err != nil {
logrus.WithError(err).
Error("failed to write pull request description to temporary file")
return
}
_, _ = fmt.Fprint(os.Stderr,
" - saved pull request description to ", colors.UserInput(saveFile),
" (it will be automatically re-used if you try again)\n",
)
}
type prBodyTemplateData struct {
Branch string
Title string
Body string
Commits []git.CommitInfo
}
var templateFuncs = template.FuncMap{"trimSpace": strings.TrimSpace}
var prBodyTemplate = template.Must(
template.New("prBody").
Funcs(templateFuncs).
Parse(`%% Creating pull request for branch '{{ .Branch }}'
%% Lines starting with '%%' will be ignored and an empty message aborts the
%% creation of the pull request.
%% Pull request title (single line)
{{ .Title }}
%% Pull request body (multiple lines)
{{ .Body }}
%% This branch includes the following commits:
{{- range $c := .Commits }}
%% {{ $c.ShortHash }} {{ $c.Subject }}
{{- if trimSpace $c.Body }}
{{- range $line := $c.BodyWithPrefix "%% " }}
{{ trimSpace $line }}
{{- end }}
{{- end }}
{{- end }}
`),
)
func readDefaultPullRequestTemplate(repo *git.Repo) string {
for _, dir := range []string{"", ".github", "data"} {
for _, f := range []string{
"PULL_REQUEST_TEMPLATE.md",
"pull_request_template.md",
} {
tpl := filepath.Join(repo.Dir(), dir, f)
data, err := os.ReadFile(tpl)
if err != nil {
continue
}
return string(data)
}
}
return ""
}
type ensurePROpts struct {
baseRefName string
headRefName string
title string
body string
meta PRMetadata
draft bool
existingPR *gh.PullRequest
}
// ensurePR returns the pull request for the given input, creating a new
// pull request if one doesn't exist. It returns the pull request, a boolean
// indicating whether or not the pull request was created, and an error if one
// occurred.
func ensurePR(
ctx context.Context,
client *gh.Client,
repoMeta meta.Repository,
opts ensurePROpts,
) (*gh.PullRequest, bool, error) {
if opts.existingPR != nil {
newBody := AddPRMetadata(opts.body, opts.meta)
updatedPR, err := client.UpdatePullRequest(ctx, githubv4.UpdatePullRequestInput{
PullRequestID: opts.existingPR.ID,
Title: gh.Ptr(githubv4.String(opts.title)),
Body: gh.Ptr(githubv4.String(newBody)),
BaseRefName: gh.Ptr(githubv4.String(opts.baseRefName)),
})
if err != nil {
return nil, false, errors.WithStack(err)
}
return updatedPR, false, nil
}
pull, err := client.CreatePullRequest(ctx, githubv4.CreatePullRequestInput{
RepositoryID: githubv4.ID(repoMeta.ID),
BaseRefName: githubv4.String(opts.baseRefName),
HeadRefName: githubv4.String(opts.headRefName),
Title: githubv4.String(opts.title),
Body: gh.Ptr(githubv4.String(AddPRMetadata(opts.body, opts.meta))),
Draft: gh.Ptr(githubv4.Boolean(opts.draft)),
})
if err != nil {
return nil, false, errors.WithStack(err)
}
return pull, true, nil
}
type UpdatePullRequestResult struct {
// True if the pull request information changed (e.g., a new pull request
// was found or if the pull request changed state)
Changed bool
// The pull request object that was returned from GitHub
Pull *gh.PullRequest
}
// UpdatePullRequestState fetches the latest pull request information from GitHub
// and writes the relevant branch metadata.
func UpdatePullRequestState(
ctx context.Context,
client *gh.Client,
tx meta.WriteTx,
branchName string,
) (*UpdatePullRequestResult, error) {
repoMeta, ok := tx.Repository()
if !ok {
return nil, ErrRepoNotInitialized
}
branch, _ := tx.Branch(branchName)
_, _ = fmt.Fprint(os.Stderr,
" - fetching latest pull request information for ", colors.UserInput(branchName),
"\n",
)
page, err := client.GetPullRequests(ctx, gh.GetPullRequestsInput{
Owner: repoMeta.Owner,
Repo: repoMeta.Name,
HeadRefName: branchName,
})
if err != nil {
return nil, errors.WrapIf(err, "querying GitHub pull requests")
}
if len(page.PullRequests) == 0 {
// branch has no pull request
if branch.PullRequest != nil {
// This should never happen?
logrus.WithFields(logrus.Fields{
"branch": branch.Name,
"pull": branch.PullRequest.Permalink,
}).Error("GitHub reported no pull requests for branch but local metadata has pull request")
return nil, errors.New(
"GitHub reported no pull requests for branch but local metadata has pull request",
)
}
return &UpdatePullRequestResult{false, nil}, nil
}
// The latest info for the pull request that we have stored in local metadata
// (we can use this to check if the pull was closed/merged)
var currentPull *gh.PullRequest
// The current open pull request (if any)
var openPull *gh.PullRequest
for i := range page.PullRequests {
pull := &page.PullRequests[i]
if branch.PullRequest != nil && pull.ID == branch.PullRequest.ID {
currentPull = pull
}
if pull.State != githubv4.PullRequestStateOpen {
continue
}
// GH only allows one open pull for a given (head, base) pair, but
// we only support one open pull per head branch (the workflow of
// opening a pull from a head branch into multiple base branches is
// rare). This probably isn't necessary but better to be defensive
// here.
if openPull != nil {
return nil, errors.Errorf(
"multiple open pull requests for branch %q (#%d into %q and #%d into %q)",
branchName,
openPull.Number, openPull.BaseRefName,
pull.Number, pull.BaseRefName,
)
}
openPull = pull
}
changed := false
var oldId string
if branch.PullRequest != nil {
oldId = branch.PullRequest.ID
}
var newPull *gh.PullRequest
if openPull != nil {
if oldId != openPull.ID {
_, _ = fmt.Fprint(os.Stderr,
" - found new pull request for ", colors.UserInput(branchName),
": ", colors.UserInput(openPull.Permalink),
"\n",
)
changed = true
}
branch.PullRequest = &meta.PullRequest{
ID: openPull.ID,
Number: openPull.Number,
Permalink: openPull.Permalink,
State: openPull.State,
}
newPull = openPull
} else {
// openPull is nil so the PR should be merged or closed
if currentPull != nil {
branch.MergeCommit = currentPull.GetMergeCommit()
branch.PullRequest = &meta.PullRequest{
ID: currentPull.ID,
Number: currentPull.Number,
Permalink: currentPull.Permalink,
State: currentPull.State,
}
} else {
// openPull and currentPull is nil
if branch.PullRequest != nil {
_, _ = fmt.Fprint(os.Stderr,
" - ", colors.Failure("ERROR:"),
" pull request for ", colors.UserInput(branchName),
" could not be found on GitHub: ", colors.UserInput(branch.PullRequest.Permalink),
" (removing reference from local state)\n",
)
changed = true
}
branch.PullRequest = nil
}
newPull = currentPull
}
tx.SetBranch(branch)
return &UpdatePullRequestResult{changed, newPull}, nil
}
type PRMetadata struct {
Parent string `json:"parent"`
ParentHead string `json:"parentHead"`
ParentPull int64 `json:"parentPull,omitempty"`
Trunk string `json:"trunk"`
}
const PRMetadataCommentStart = "<!-- av pr metadata\n"
const PRMetadataCommentHelpText = "This information is embedded by the av CLI when creating PRs to track the status of stacks when using Aviator. Please do not delete or edit this section of the PR.\n"
const PRMetadataCommentEnd = "-->\n"
func ParsePRMetadata(
input string,
) (commentStart int, commentEnd int, prMeta PRMetadata, reterr error) {
buf := bytes.NewBufferString(input)
// Read until we find the "<!-- av pr metadata" line
if err := readLineUntil(buf, PRMetadataCommentStart); err != nil {
reterr = errors.WrapIff(err, "expecting %q", PRMetadataCommentStart)
return
}
commentStart = len(input) - buf.Len() - len(PRMetadataCommentStart)
// Read until we find the "```" line (which indicates that json starts
// on the following line)
if err := readLineUntil(buf, "```\n"); err != nil {
reterr = errors.WrapIff(err, "expecting \"```\"")
return
}
// We need to create a copy of the buffer here since json.Decoder may read
// past the end of the JSON data (and we need to access that data below!)
if err := json.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(&prMeta); err != nil {
reterr = errors.WrapIff(err, "decoding PR metadata")
return
}
// This will skip over any data lines (since those weren't consumed by buf,
// only by the copy of buf).
if err := readLineUntil(buf, "```\n"); err != nil {
reterr = errors.WrapIff(err, "expecting closing \"```\"")
return
}
if err := readLineUntil(buf, PRMetadataCommentEnd); err != nil {
reterr = errors.WrapIff(err, "expecting %q", PRMetadataCommentEnd)
return
}
commentEnd = len(input) - buf.Len()
return
}
func ReadPRMetadata(body string) (PRMetadata, error) {
_, _, prMeta, err := ParsePRMetadata(body)
return prMeta, err
}
func AddPRMetadata(body string, prMeta PRMetadata) string {
buf := bytes.NewBufferString(body)
if commentStart, commentEnd, _, err := ParsePRMetadata(body); err != nil {
// No existing metadata comment, so add one.
logrus.WithError(err).Debug("could not parse PR metadata (assuming it doesn't exist)")
buf.WriteString("\n\n")
} else {
buf.Truncate(commentStart)
if commentEnd < len(body) {
// The PR body doesn't end with the metadata comment. This probably
// means that the PR was edited after it was created with the av CLI
// (so we should preserve that text that comes after the comment).
buf.WriteString(body[commentEnd:])
// We also need newlines here to separate the metadata comment from
// the text that comes before it.
buf.WriteString("\n\n")
}
}
buf.WriteString(PRMetadataCommentStart)
buf.WriteString(PRMetadataCommentHelpText)
buf.WriteString("```\n")
// Note: Encoder.Encode implicitly adds a newline at the end of the JSON
// which is important here so that the ``` below appears on its own line.
if err := json.NewEncoder(buf).Encode(prMeta); err != nil {
// shouldn't ever happen since we're encoding a simple struct to a buffer
panic(errors.WrapIff(err, "encoding PR metadata"))
}
buf.WriteString("```\n")
buf.WriteString(PRMetadataCommentEnd)
return buf.String()
}
func readLineUntil(b *bytes.Buffer, line string) error {
for {
l, err := b.ReadString('\n')
if err != nil {
return err
}
if l == line {
return nil
}
}
}