-
Notifications
You must be signed in to change notification settings - Fork 3
/
tui_cmds.go
143 lines (122 loc) · 3.29 KB
/
tui_cmds.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
package main
import (
"context"
"fmt"
"time"
"github.com/atotto/clipboard"
tea "github.com/charmbracelet/bubbletea"
"github.com/einride/gh-dependabot/internal/gh"
"golang.org/x/time/rate"
)
type errorMessage struct {
err error
}
type pullRequestMerged struct {
pr pullRequest
}
type mergeMethod string
const (
MethodRebase mergeMethod = "--rebase"
MethodMerge mergeMethod = "--merge"
MethodSquash mergeMethod = "--squash"
MethodDependabot mergeMethod = "@dependabot merge"
)
type commander struct {
limiter *rate.Limiter
}
func newCommander() commander {
limiter := rate.NewLimiter(rate.Every(time.Second*2), 1)
return commander{
limiter: limiter,
}
}
func (c commander) mergePullRequest(pr pullRequest, method mergeMethod) tea.Cmd {
return func() tea.Msg {
switch method {
case MethodRebase:
fallthrough
case MethodMerge:
fallthrough
case MethodSquash:
//nolint:errcheck // hard coded limiter burst, can't fail
c.limiter.Wait(context.Background())
if _, err := gh.Run("pr", "review", "--approve", pr.url); err != nil {
return errorMessage{err: err}
}
if _, err := gh.Run("pr", "merge", "--auto", string(method), pr.url); err != nil {
return errorMessage{err: err}
}
case MethodDependabot:
//nolint:errcheck // hard coded limiter burst, can't fail
c.limiter.Wait(context.Background())
if _, err := gh.Run("pr", "review", "--approve", "--body", string(method), pr.url); err != nil {
return errorMessage{err: err}
}
default:
return errorMessage{err: fmt.Errorf("unknown merge method: %q", method)}
}
return pullRequestMerged{pr: pr}
}
}
type pullRequestRebased struct {
pr pullRequest
}
func (c commander) rebasePullRequest(pr pullRequest) tea.Cmd {
return func() tea.Msg {
//nolint:errcheck // hard coded limiter burst, can't fail
c.limiter.Wait(context.Background())
if _, err := gh.Run("pr", "comment", "--body", "@dependabot rebase", pr.url); err != nil {
return errorMessage{err: err}
}
return pullRequestRebased{pr: pr}
}
}
type pullRequestRecreated struct {
pr pullRequest
}
func (c commander) recreatePullRequest(pr pullRequest) tea.Cmd {
return func() tea.Msg {
//nolint:errcheck // hard coded limiter burst, can't fail
c.limiter.Wait(context.Background())
if _, err := gh.Run("pr", "comment", "--body", "@dependabot recreate", pr.url); err != nil {
return errorMessage{err: err}
}
return pullRequestRecreated{pr: pr}
}
}
type pullRequestOpenedInBrowser struct {
pr pullRequest
}
func (c commander) openInBrowser(pr pullRequest) tea.Cmd {
return func() tea.Msg {
if _, err := gh.Run("pr", "view", "--web", pr.url); err != nil {
return errorMessage{err: err}
}
return pullRequestOpenedInBrowser{pr: pr}
}
}
type hidePullRequestDetails struct{}
func hidePullRequestDetailsCmd() tea.Cmd {
return func() tea.Msg {
return hidePullRequestDetails{}
}
}
type viewPullRequestDetails struct {
pr pullRequest
}
func viewPullRequestDetailsCmd(pr pullRequest) tea.Cmd {
return func() tea.Msg {
return viewPullRequestDetails{pr: pr}
}
}
type copyCheckout struct {
pr pullRequest
}
func copyCheckoutCmd(pr pullRequest) tea.Cmd {
return func() tea.Msg {
if err := clipboard.WriteAll("gh pr checkout " + pr.number); err != nil {
return errorMessage{err: err}
}
return copyCheckout{pr: pr}
}
}