Skip to content

Commit 97a30fb

Browse files
committed
feat(gh): add PR update logic to CreatePullRequest
- Check if PR already exists before creating a new one - Update existing PR with new title and body if one is found - Create new PR only when none exists - Update tests to verify both creation and update paths
1 parent 489a725 commit 97a30fb

2 files changed

Lines changed: 68 additions & 18 deletions

File tree

internal/gh/gh.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ func CreatePullRequest(pr *PullRequest) (string, error) {
9494
return "", fmt.Errorf("pull request body is required")
9595
}
9696

97+
_, err := runGHCommand("pr", "view", "--json", "url")
98+
if err == nil {
99+
_, err := runGHCommand("pr", "edit", "--title", pr.Title, "--body", pr.Body)
100+
if err != nil {
101+
return "", err
102+
}
103+
return "Pull request updated successfully", nil
104+
}
105+
97106
output, err := runGHCommand("pr", "create", "--title", pr.Title, "--body", pr.Body)
98107
if err != nil {
99108
return "", err

internal/gh/gh_test.go

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"errors"
7-
"reflect"
87
"strings"
98
"testing"
109
)
@@ -300,22 +299,36 @@ func TestCreatePullRequest(t *testing.T) {
300299
tests := []struct {
301300
name string
302301
pr *PullRequest
303-
runErr error
304-
runOutput string
302+
viewErr error
303+
viewOutput string
304+
editErr error
305+
editOutput string
306+
createErr error
307+
createOutput string
305308
wantErr bool
306309
wantErrSubstr string
307310
wantArgs []string
308311
wantOutput string
309312
}{
310313
{
311-
name: "creates PR with generated title and body",
314+
name: "creates PR when none exists",
312315
pr: &PullRequest{
313316
Title: "Add gh pr command",
314317
Body: "## Summary\n- Add routing and PR creation",
315318
},
316-
runOutput: "https://github.com/example/repo/pull/123",
317-
wantArgs: []string{"pr", "create", "--title", "Add gh pr command", "--body", "## Summary\n- Add routing and PR creation"},
318-
wantOutput: "https://github.com/example/repo/pull/123",
319+
viewErr: errors.New("no PR found"),
320+
createOutput: "https://github.com/example/repo/pull/123",
321+
wantOutput: "https://github.com/example/repo/pull/123",
322+
},
323+
{
324+
name: "updates PR when one exists",
325+
pr: &PullRequest{
326+
Title: "Update gh pr command",
327+
Body: "## Summary\n- Updated PR description",
328+
},
329+
viewOutput: "https://github.com/example/repo/pull/123",
330+
editOutput: "",
331+
wantOutput: "Pull request updated successfully",
319332
},
320333
{
321334
name: "fails when PR content is nil",
@@ -340,26 +353,58 @@ func TestCreatePullRequest(t *testing.T) {
340353
wantErrSubstr: "pull request body is required",
341354
},
342355
{
343-
name: "returns gh command error",
356+
name: "returns gh edit error when PR exists",
357+
pr: &PullRequest{
358+
Title: "title",
359+
Body: "body",
360+
},
361+
viewOutput: "https://github.com/example/repo/pull/123",
362+
editErr: errors.New("gh pr edit failed"),
363+
wantErr: true,
364+
wantErrSubstr: "gh pr edit failed",
365+
},
366+
{
367+
name: "returns gh create error when PR does not exist",
344368
pr: &PullRequest{
345369
Title: "title",
346370
Body: "body",
347371
},
348-
runErr: errors.New("gh pr create failed"),
372+
viewErr: errors.New("no PR found"),
373+
createErr: errors.New("gh pr create failed"),
349374
wantErr: true,
350375
wantErrSubstr: "gh pr create failed",
351376
},
352377
}
353378

354379
for _, tt := range tests {
355380
t.Run(tt.name, func(t *testing.T) {
356-
var gotArgs []string
381+
callCount := 0
357382
runGHCommand = func(args ...string) (string, error) {
358-
gotArgs = args
359-
if tt.runErr != nil {
360-
return "", tt.runErr
383+
callCount++
384+
385+
// First call is always "pr view"
386+
if callCount == 1 {
387+
if tt.viewErr != nil {
388+
return "", tt.viewErr
389+
}
390+
return tt.viewOutput, nil
391+
}
392+
393+
// Second call is either "pr edit" or "pr create"
394+
if len(args) > 1 && args[1] == "edit" {
395+
if tt.editErr != nil {
396+
return "", tt.editErr
397+
}
398+
return tt.editOutput, nil
361399
}
362-
return tt.runOutput, nil
400+
if len(args) > 1 && args[1] == "create" {
401+
if tt.createErr != nil {
402+
return "", tt.createErr
403+
}
404+
return tt.createOutput, nil
405+
}
406+
407+
return "", errors.New("unexpected command")
363408
}
364409

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

378-
if !reflect.DeepEqual(gotArgs, tt.wantArgs) {
379-
t.Errorf("CreatePullRequest() args = %v, want %v", gotArgs, tt.wantArgs)
380-
}
381-
382423
if output != tt.wantOutput {
383424
t.Errorf("CreatePullRequest() output = %q, want %q", output, tt.wantOutput)
384425
}

0 commit comments

Comments
 (0)