Skip to content

Commit

Permalink
feat(approve): Add PR approval cmd (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
merkata committed Jun 29, 2024
1 parent 96cca1a commit f060107
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ keybindings:
The list of available builtin commands are:
1. `universal`: up, down, firstLine, lastLine, togglePreview, openGithub, refresh, refreshAll, pageDown, pageUp, nextSection, prevSection, search, copyurl, copyNumber, help, quit
2. `prs`: assign, unassign, comment, diff, checkout, close, ready, reopen, merge, watchChecks, viewIssues
2. `prs`: approve, assign, unassign, comment, diff, checkout, close, ready, reopen, merge, watchChecks, viewIssues
3. `Issues`: assign, unassign, comment, close, reopen, viewPrs
#### Defining custom keybindings
Expand Down
54 changes: 54 additions & 0 deletions ui/components/prsidebar/approve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package prsidebar

import (
"fmt"
"os/exec"

tea "github.com/charmbracelet/bubbletea"
"github.com/dlvhdr/gh-dash/v4/ui/components/prssection"
"github.com/dlvhdr/gh-dash/v4/ui/constants"
"github.com/dlvhdr/gh-dash/v4/ui/context"
)

func (m *Model) approve(comment string) tea.Cmd {
pr := m.pr.Data
prNumber := pr.GetNumber()
taskId := fmt.Sprintf("pr_approve_%d", prNumber)
task := context.Task{
Id: taskId,
StartText: fmt.Sprintf("Approving pr #%d", prNumber),
FinishedText: fmt.Sprintf("pr #%d has been approved", prNumber),
State: context.TaskStart,
Error: nil,
}

commandArgs := []string{
"pr",
"review",
"-R",
pr.GetRepoNameWithOwner(),
fmt.Sprint(prNumber),
"--approve",
}
if comment != "" {
commandArgs = append(commandArgs, "--body", comment)
}

startCmd := m.ctx.StartTask(task)
return tea.Batch(startCmd, func() tea.Msg {
c := exec.Command("gh", commandArgs...)

err := c.Run()
if err != nil {
}
return constants.TaskFinishedMsg{
SectionId: m.sectionId,
SectionType: prssection.SectionType,
TaskId: taskId,
Err: err,
Msg: prssection.UpdatePRMsg{
PrNumber: prNumber,
},
}
})
}
43 changes: 41 additions & 2 deletions ui/components/prsidebar/prsidebar.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Model struct {
width int

isCommenting bool
isApproving bool
isAssigning bool
isUnassigning bool

Expand All @@ -38,6 +39,7 @@ func NewModel(ctx context.ProgramContext) Model {
pr: nil,

isCommenting: false,
isApproving: false,
isAssigning: false,
isUnassigning: false,

Expand Down Expand Up @@ -71,6 +73,25 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, nil
}

m.inputBox, taCmd = m.inputBox.Update(msg)
cmds = append(cmds, cmd, taCmd)
} else if m.isApproving {
switch msg.Type {

case tea.KeyCtrlD:
if len(strings.Trim(m.inputBox.Value(), " ")) != 0 {
cmd = m.approve(m.inputBox.Value())
}
m.inputBox.Blur()
m.isApproving = false
return m, cmd

case tea.KeyEsc, tea.KeyCtrlC:
m.inputBox.Blur()
m.isApproving = false
return m, nil
}

m.inputBox, taCmd = m.inputBox.Update(msg)
cmds = append(cmds, cmd, taCmd)
} else if m.isAssigning {
Expand Down Expand Up @@ -147,7 +168,7 @@ func (m Model) View() string {
s.WriteString("\n\n")
s.WriteString(m.renderActivity())

if m.isCommenting || m.isAssigning || m.isUnassigning {
if m.isCommenting || m.isApproving || m.isAssigning || m.isUnassigning {
s.WriteString(m.inputBox.View())
}

Expand Down Expand Up @@ -288,7 +309,7 @@ func (m *Model) SetWidth(width int) {
}

func (m *Model) IsTextInputBoxFocused() bool {
return m.isCommenting || m.isAssigning || m.isUnassigning
return m.isCommenting || m.isAssigning || m.isApproving || m.isUnassigning
}

func (m *Model) GetIsCommenting() bool {
Expand Down Expand Up @@ -317,6 +338,24 @@ func (m *Model) getIndentedContentWidth() int {
return m.width - 4
}

func (m *Model) GetIsApproving() bool {
return m.isApproving
}

func (m *Model) SetIsApproving(isApproving bool) tea.Cmd {
if !m.isApproving && isApproving {
m.inputBox.Reset()
}
m.isApproving = isApproving
m.inputBox.SetPrompt("Approve with comment...")
m.inputBox.SetValue("LGTM")

if isApproving {
return tea.Sequence(textarea.Blink, m.inputBox.Focus())
}
return nil
}

func (m *Model) GetIsAssigning() bool {
return m.isAssigning
}
Expand Down
8 changes: 8 additions & 0 deletions ui/keys/prKeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

type PRKeyMap struct {
Approve key.Binding
Assign key.Binding
Unassign key.Binding
Comment key.Binding
Expand All @@ -24,6 +25,10 @@ type PRKeyMap struct {
}

var PRKeys = PRKeyMap{
Approve: key.NewBinding(
key.WithKeys("v"),
key.WithHelp("v", "approve"),
),
Assign: key.NewBinding(
key.WithKeys("a"),
key.WithHelp("a", "assign"),
Expand Down Expand Up @@ -72,6 +77,7 @@ var PRKeys = PRKeyMap{

func PRFullHelp() []key.Binding {
return []key.Binding{
PRKeys.Approve,
PRKeys.Assign,
PRKeys.Unassign,
PRKeys.Comment,
Expand All @@ -97,6 +103,8 @@ func rebindPRKeys(keys []config.Keybinding) error {
var key *key.Binding

switch prKey.Builtin {
case "approve":
key = &PRKeys.Approve
case "assign":
key = &PRKeys.Assign
case "unassign":
Expand Down
8 changes: 8 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,14 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

case m.ctx.View == config.PRsView:
switch {
case key.Matches(msg, keys.PRKeys.Approve):
m.sidebar.IsOpen = true
cmd = m.prSidebar.SetIsApproving(true)
m.syncMainContentWidth()
m.syncSidebar()
m.sidebar.ScrollToBottom()
return m, cmd

case key.Matches(msg, keys.PRKeys.Assign):
m.sidebar.IsOpen = true
cmd = m.prSidebar.SetIsAssigning(true)
Expand Down

0 comments on commit f060107

Please sign in to comment.