Skip to content

Commit

Permalink
feat: option to merge using Dependabot comment
Browse files Browse the repository at this point in the history
Press alt+m to post a "@dependabot merge" comment which will tell
Dependabot to merge the PR itself.

This commit also switches the keybindings for the different merge
options due to the unreliability of detecting <modifier>+Enter in
various different constellations of terminal emulators / tmux etc.
  • Loading branch information
Edholm committed Jul 11, 2022
1 parent 4f3365b commit 52072b0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
19 changes: 17 additions & 2 deletions tui_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,27 @@ type pullRequestMerged struct {
pr pullRequest
}

func mergePullRequest(pr pullRequest, mergeMethod string) tea.Cmd {
type mergeMethod string

const (
MethodRebase mergeMethod = "--rebase"
MethodMerge mergeMethod = "--merge"
MethodSquash mergeMethod = "--squash"
MethodDependabot mergeMethod = "@dependabot merge"
)

func mergePullRequest(pr pullRequest, method mergeMethod) tea.Cmd {
return func() tea.Msg {
if _, err := gh.Run("pr", "review", "--approve", pr.url); err != nil {
return errorMessage{err: err}
}
if _, err := gh.Run("pr", "merge", "--auto", mergeMethod, pr.url); err != nil {
var args []string
if method == MethodDependabot {
args = []string{"pr", "comment", "--body", string(method), pr.url}
} else {
args = []string{"pr", "merge", "--auto", string(method), pr.url}
}
if _, err := gh.Run(args...); err != nil {
return errorMessage{err: err}
}
return pullRequestMerged{pr: pr}
Expand Down
41 changes: 28 additions & 13 deletions tui_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (
var listViewStyle = lipgloss.NewStyle().Padding(1, 2)

type keyMap struct {
mergeRebase key.Binding
mergeDefault key.Binding
mergeSquash key.Binding
rebase key.Binding
view key.Binding
browse key.Binding // open PR in default browser.
mergeRebase key.Binding
mergeDefault key.Binding
mergeSquash key.Binding
mergeDependabot key.Binding
rebase key.Binding
view key.Binding
browse key.Binding // open PR in default browser.
}

func newKeyMap() *keyMap {
Expand All @@ -29,12 +30,16 @@ func newKeyMap() *keyMap {
key.WithHelp("enter", "merge (rebase)"),
),
mergeDefault: key.NewBinding(
key.WithKeys("ctrl+enter"),
key.WithHelp("ctrl+enter", "merge (merge-commit)"),
key.WithKeys("ctrl+m"),
key.WithHelp("ctrl+m", "merge (merge-commit)"),
),
mergeSquash: key.NewBinding(
key.WithKeys("shift+enter"),
key.WithHelp("shift+enter", "merge (squash)"),
key.WithKeys("M"),
key.WithHelp("shift+m", "merge (squash)"),
),
mergeDependabot: key.NewBinding(
key.WithKeys("alt+m"),
key.WithHelp("alt+m", "merge (Dependabot)"),
),
rebase: key.NewBinding(
key.WithKeys("r"),
Expand All @@ -56,6 +61,7 @@ func (d keyMap) Bindings() []key.Binding {
d.mergeRebase,
d.mergeDefault,
d.mergeSquash,
d.mergeDependabot,
d.rebase,
d.view,
d.browse,
Expand Down Expand Up @@ -110,7 +116,7 @@ func (m ListView) Update(msg tea.Msg) (ListView, tea.Cmd) {
cmds = append(
cmds,
m.listModel.StartSpinner(),
mergePullRequest(selectedItem, "--rebase"),
mergePullRequest(selectedItem, MethodRebase),
)
}
case key.Matches(msg, m.keyMap.mergeDefault):
Expand All @@ -119,7 +125,7 @@ func (m ListView) Update(msg tea.Msg) (ListView, tea.Cmd) {
cmds = append(
cmds,
m.listModel.StartSpinner(),
mergePullRequest(selectedItem, "--merge"),
mergePullRequest(selectedItem, MethodMerge),
)
}
case key.Matches(msg, m.keyMap.mergeSquash):
Expand All @@ -128,7 +134,16 @@ func (m ListView) Update(msg tea.Msg) (ListView, tea.Cmd) {
cmds = append(
cmds,
m.listModel.StartSpinner(),
mergePullRequest(selectedItem, "--squash"),
mergePullRequest(selectedItem, MethodSquash),
)
}
case key.Matches(msg, m.keyMap.mergeDependabot):
if selectedItem, ok := m.listModel.SelectedItem().(pullRequest); ok {
m.listModel.RemoveItem(m.listModel.Index())
cmds = append(
cmds,
m.listModel.StartSpinner(),
mergePullRequest(selectedItem, MethodDependabot),
)
}
case key.Matches(msg, m.keyMap.rebase):
Expand Down

0 comments on commit 52072b0

Please sign in to comment.