Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions internal/gh/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ func CreatePullRequest(pr *PullRequest) (string, error) {
return "", fmt.Errorf("pull request body is required")
}

_, err := runGHCommand("pr", "view", "--json", "url")
if err == nil {
_, err := runGHCommand("pr", "edit", "--title", pr.Title, "--body", pr.Body)
if err != nil {
return "", err
}
return "Pull request updated successfully", nil
}

output, err := runGHCommand("pr", "create", "--title", pr.Title, "--body", pr.Body)
if err != nil {
return "", err
Expand Down
77 changes: 59 additions & 18 deletions internal/gh/gh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"errors"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -300,22 +299,36 @@ func TestCreatePullRequest(t *testing.T) {
tests := []struct {
name string
pr *PullRequest
runErr error
runOutput string
viewErr error
viewOutput string
editErr error
editOutput string
createErr error
createOutput string
wantErr bool
wantErrSubstr string
wantArgs []string
wantOutput string
}{
{
name: "creates PR with generated title and body",
name: "creates PR when none exists",
pr: &PullRequest{
Title: "Add gh pr command",
Body: "## Summary\n- Add routing and PR creation",
},
runOutput: "https://github.com/example/repo/pull/123",
wantArgs: []string{"pr", "create", "--title", "Add gh pr command", "--body", "## Summary\n- Add routing and PR creation"},
wantOutput: "https://github.com/example/repo/pull/123",
viewErr: errors.New("no PR found"),
createOutput: "https://github.com/example/repo/pull/123",
wantOutput: "https://github.com/example/repo/pull/123",
},
{
name: "updates PR when one exists",
pr: &PullRequest{
Title: "Update gh pr command",
Body: "## Summary\n- Updated PR description",
},
viewOutput: "https://github.com/example/repo/pull/123",
editOutput: "",
wantOutput: "Pull request updated successfully",
},
{
name: "fails when PR content is nil",
Expand All @@ -340,26 +353,58 @@ func TestCreatePullRequest(t *testing.T) {
wantErrSubstr: "pull request body is required",
},
{
name: "returns gh command error",
name: "returns gh edit error when PR exists",
pr: &PullRequest{
Title: "title",
Body: "body",
},
viewOutput: "https://github.com/example/repo/pull/123",
editErr: errors.New("gh pr edit failed"),
wantErr: true,
wantErrSubstr: "gh pr edit failed",
},
{
name: "returns gh create error when PR does not exist",
pr: &PullRequest{
Title: "title",
Body: "body",
},
runErr: errors.New("gh pr create failed"),
viewErr: errors.New("no PR found"),
createErr: errors.New("gh pr create failed"),
wantErr: true,
wantErrSubstr: "gh pr create failed",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var gotArgs []string
callCount := 0
runGHCommand = func(args ...string) (string, error) {
gotArgs = args
if tt.runErr != nil {
return "", tt.runErr
callCount++

// First call is always "pr view"
if callCount == 1 {
if tt.viewErr != nil {
return "", tt.viewErr
}
return tt.viewOutput, nil
}

// Second call is either "pr edit" or "pr create"
if len(args) > 1 && args[1] == "edit" {
if tt.editErr != nil {
return "", tt.editErr
}
return tt.editOutput, nil
}
return tt.runOutput, nil
if len(args) > 1 && args[1] == "create" {
if tt.createErr != nil {
return "", tt.createErr
}
return tt.createOutput, nil
}

return "", errors.New("unexpected command")
}

output, err := CreatePullRequest(tt.pr)
Expand All @@ -375,10 +420,6 @@ func TestCreatePullRequest(t *testing.T) {
return
}

if !reflect.DeepEqual(gotArgs, tt.wantArgs) {
t.Errorf("CreatePullRequest() args = %v, want %v", gotArgs, tt.wantArgs)
}

if output != tt.wantOutput {
t.Errorf("CreatePullRequest() output = %q, want %q", output, tt.wantOutput)
}
Expand Down
Loading