Skip to content

Commit 44609e5

Browse files
refactor: extract DiffPR to common package
Move the DiffPR function from notificationssection to the common package so it can be shared between prssection and notificationssection without duplication.
1 parent e84ab28 commit 44609e5

6 files changed

Lines changed: 81 additions & 87 deletions

File tree

internal/tui/common/diff.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package common
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
7+
tea "github.com/charmbracelet/bubbletea"
8+
9+
"github.com/dlvhdr/gh-dash/v4/internal/tui/constants"
10+
)
11+
12+
// DiffPR opens a diff view for a PR using the gh CLI.
13+
// The env parameter should be the result of Config.GetFullScreenDiffPagerEnv().
14+
func DiffPR(prNumber int, repoName string, env []string) tea.Cmd {
15+
c := exec.Command(
16+
"gh",
17+
"pr",
18+
"diff",
19+
fmt.Sprint(prNumber),
20+
"-R",
21+
repoName,
22+
)
23+
c.Env = env
24+
25+
return tea.ExecProcess(c, func(err error) tea.Msg {
26+
if err != nil {
27+
return constants.ErrMsg{Err: err}
28+
}
29+
return nil
30+
})
31+
}

internal/tui/common/diff_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package common
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestDiffPR(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
prNumber int
11+
repoName string
12+
wantNil bool
13+
}{
14+
{
15+
name: "returns command for valid PR",
16+
prNumber: 123,
17+
repoName: "owner/repo",
18+
wantNil: false,
19+
},
20+
{
21+
name: "returns command for PR number 0",
22+
prNumber: 0,
23+
repoName: "owner/repo",
24+
wantNil: false,
25+
},
26+
{
27+
name: "returns command with hyphenated repo name",
28+
prNumber: 456,
29+
repoName: "my-org/my-repo",
30+
wantNil: false,
31+
},
32+
}
33+
34+
for _, tt := range tests {
35+
t.Run(tt.name, func(t *testing.T) {
36+
cmd := DiffPR(tt.prNumber, tt.repoName, nil)
37+
38+
if tt.wantNil && cmd != nil {
39+
t.Errorf("DiffPR() returned non-nil, want nil")
40+
}
41+
if !tt.wantNil && cmd == nil {
42+
t.Errorf("DiffPR() returned nil, want non-nil")
43+
}
44+
})
45+
}
46+
}

internal/tui/components/notificationssection/commands.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -252,27 +252,6 @@ func (m *Model) openInBrowser() tea.Cmd {
252252
)
253253
}
254254

255-
// DiffPR opens a diff view for a PR. This is a standalone function that can be called
256-
// from ui.go with the PR details from the notification view.
257-
func DiffPR(ctx *context.ProgramContext, prNumber int, repoName string) tea.Cmd {
258-
c := exec.Command(
259-
"gh",
260-
"pr",
261-
"diff",
262-
fmt.Sprint(prNumber),
263-
"-R",
264-
repoName,
265-
)
266-
c.Env = ctx.Config.GetFullScreenDiffPagerEnv()
267-
268-
return tea.ExecProcess(c, func(err error) tea.Msg {
269-
if err != nil {
270-
return constants.ErrMsg{Err: err}
271-
}
272-
return nil
273-
})
274-
}
275-
276255
// CheckoutPR checks out a PR. This is a standalone function that can be called
277256
// from ui.go with the PR details from the notification view.
278257
func CheckoutPR(ctx *context.ProgramContext, prNumber int, repoName string) (tea.Cmd, error) {

internal/tui/components/notificationssection/commands_test.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,6 @@ func noopStartTask(task context.Task) tea.Cmd {
1414
return nil
1515
}
1616

17-
func TestDiffPR(t *testing.T) {
18-
tests := []struct {
19-
name string
20-
prNumber int
21-
repoName string
22-
wantNil bool
23-
}{
24-
{
25-
name: "returns command for valid PR",
26-
prNumber: 123,
27-
repoName: "owner/repo",
28-
wantNil: false,
29-
},
30-
{
31-
name: "returns command for PR number 0",
32-
prNumber: 0,
33-
repoName: "owner/repo",
34-
wantNil: false,
35-
},
36-
{
37-
name: "returns command with hyphenated repo name",
38-
prNumber: 456,
39-
repoName: "my-org/my-repo",
40-
wantNil: false,
41-
},
42-
}
43-
44-
for _, tt := range tests {
45-
t.Run(tt.name, func(t *testing.T) {
46-
ctx := &context.ProgramContext{
47-
Config: &config.Config{},
48-
}
49-
50-
cmd := DiffPR(ctx, tt.prNumber, tt.repoName)
51-
52-
if tt.wantNil && cmd != nil {
53-
t.Errorf("DiffPR() returned non-nil, want nil")
54-
}
55-
if !tt.wantNil && cmd == nil {
56-
t.Errorf("DiffPR() returned nil, want non-nil")
57-
}
58-
})
59-
}
60-
}
61-
6217
func TestCheckoutPR(t *testing.T) {
6318
tests := []struct {
6419
name string
Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package prssection
22

33
import (
4-
"fmt"
5-
"os/exec"
6-
74
tea "github.com/charmbracelet/bubbletea"
8-
"github.com/dlvhdr/gh-dash/v4/internal/tui/constants"
5+
6+
"github.com/dlvhdr/gh-dash/v4/internal/tui/common"
97
)
108

119
func (m Model) diff() tea.Cmd {
@@ -14,20 +12,5 @@ func (m Model) diff() tea.Cmd {
1412
return nil
1513
}
1614

17-
c := exec.Command(
18-
"gh",
19-
"pr",
20-
"diff",
21-
fmt.Sprint(currRowData.GetNumber()),
22-
"-R",
23-
m.GetCurrRow().GetRepoNameWithOwner(),
24-
)
25-
c.Env = m.Ctx.Config.GetFullScreenDiffPagerEnv()
26-
27-
return tea.ExecProcess(c, func(err error) tea.Msg {
28-
if err != nil {
29-
return constants.ErrMsg{Err: err}
30-
}
31-
return nil
32-
})
15+
return common.DiffPR(currRowData.GetNumber(), currRowData.GetRepoNameWithOwner(), m.Ctx.Config.GetFullScreenDiffPagerEnv())
3316
}

internal/tui/ui.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
583583

584584
case prview.PRActionDiff:
585585
if pr := m.notificationView.GetSubjectPR(); pr != nil {
586-
cmd = notificationssection.DiffPR(m.ctx, pr.GetNumber(), pr.GetRepoNameWithOwner())
586+
cmd = common.DiffPR(pr.GetNumber(), pr.GetRepoNameWithOwner(), m.ctx.Config.GetFullScreenDiffPagerEnv())
587587
}
588588
return m, cmd
589589

0 commit comments

Comments
 (0)