/
stack_submit.go
87 lines (80 loc) · 2.34 KB
/
stack_submit.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
package main
import (
"context"
"emperror.dev/errors"
"github.com/aviator-co/av/internal/actions"
"github.com/aviator-co/av/internal/config"
"github.com/aviator-co/av/internal/gh"
"github.com/aviator-co/av/internal/meta"
"github.com/aviator-co/av/internal/utils/cleanup"
"github.com/shurcooL/githubv4"
"github.com/spf13/cobra"
)
var stackSubmitCmd = &cobra.Command{
Use: "submit",
Short: "Create pull requests for every branch in the stack",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
// Get the all branches in the stack
repo, err := getRepo()
if err != nil {
return err
}
db, err := getDB(repo)
if err != nil {
return err
}
tx := db.WriteTx()
cu := cleanup.New(func() { tx.Abort() })
defer cu.Cleanup()
currentBranch, err := repo.CurrentBranchName()
if err != nil {
return err
}
previousBranches, err := meta.PreviousBranches(tx, currentBranch)
if err != nil {
return err
}
subsequentBranches := meta.SubsequentBranches(tx, currentBranch)
var branchesToSubmit []string
branchesToSubmit = append(branchesToSubmit, previousBranches...)
branchesToSubmit = append(branchesToSubmit, currentBranch)
branchesToSubmit = append(branchesToSubmit, subsequentBranches...)
// ensure pull requests for each branch in the stack
ctx := context.Background()
client, err := getGitHubClient()
if err != nil {
return err
}
for _, branchName := range branchesToSubmit {
// TODO: should probably commit database after every call to this
// since we're just syncing state from GitHub
result, err := actions.CreatePullRequest(
ctx, repo, client, tx,
actions.CreatePullRequestOpts{
BranchName: branchName,
Draft: config.Av.PullRequest.Draft,
},
)
if err != nil {
return err
}
// make sure the base branch of the PR is up to date if it already exists
if !result.Created && result.Pull.BaseRefName != result.Branch.Parent.Name {
if _, err := client.UpdatePullRequest(
ctx, githubv4.UpdatePullRequestInput{
PullRequestID: githubv4.ID(result.Branch.PullRequest.ID),
BaseRefName: gh.Ptr(githubv4.String(result.Branch.Parent.Name)),
},
); err != nil {
return errors.Wrap(err, "failed to update PR base branch")
}
}
}
cu.Cancel()
if err := tx.Commit(); err != nil {
return err
}
return nil
},
}