From c6117e1c22f519f774c0d3de3839b77db9cace3c Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Mon, 14 Apr 2025 22:58:53 +0200 Subject: [PATCH] Minor fixes --- services/actions/githubprcreator.go | 36 ++++++++++++++++++++---- services/actions/githubprcreator_test.go | 9 +++--- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/services/actions/githubprcreator.go b/services/actions/githubprcreator.go index 9c8af7f..909f0e8 100644 --- a/services/actions/githubprcreator.go +++ b/services/actions/githubprcreator.go @@ -3,7 +3,6 @@ package actions import ( "context" "fmt" - "strings" "github.com/google/go-github/v69/github" "github.com/mudler/LocalAGI/core/types" @@ -37,10 +36,10 @@ func (g *GithubPRCreator) createOrUpdateBranch(ctx context.Context, branchName s } // Try to get the branch if it exists - _, _, err = g.client.Git.GetRef(ctx, g.owner, g.repository, "refs/heads/"+branchName) + _, resp, err := g.client.Git.GetRef(ctx, g.owner, g.repository, "refs/heads/"+branchName) if err != nil { // If branch doesn't exist, create it - if strings.Contains(err.Error(), "404 Not Found") { + if resp != nil && resp.StatusCode == 404 { newRef := &github.Reference{ Ref: github.String("refs/heads/" + branchName), Object: &github.GitObject{SHA: ref.Object.SHA}, @@ -132,15 +131,40 @@ func (g *GithubPRCreator) Run(ctx context.Context, params types.ActionParams) (t } } - // Create pull request - pr := &github.NewPullRequest{ + // Check if PR already exists for this branch + prs, _, err := g.client.PullRequests.List(ctx, result.Owner, result.Repository, &github.PullRequestListOptions{ + State: "open", + Head: fmt.Sprintf("%s:%s", result.Owner, result.Branch), + }) + if err != nil { + return types.ActionResult{}, fmt.Errorf("failed to list pull requests: %w", err) + } + + if len(prs) > 0 { + // Update existing PR + pr := prs[0] + update := &github.PullRequest{ + Title: &result.Title, + Body: &result.Body, + } + updatedPR, _, err := g.client.PullRequests.Edit(ctx, result.Owner, result.Repository, pr.GetNumber(), update) + if err != nil { + return types.ActionResult{}, fmt.Errorf("failed to update pull request: %w", err) + } + return types.ActionResult{ + Result: fmt.Sprintf("Updated pull request #%d: %s", updatedPR.GetNumber(), updatedPR.GetHTMLURL()), + }, nil + } + + // Create new pull request + newPR := &github.NewPullRequest{ Title: &result.Title, Body: &result.Body, Head: &result.Branch, Base: &result.BaseBranch, } - createdPR, _, err := g.client.PullRequests.Create(ctx, result.Owner, result.Repository, pr) + createdPR, _, err := g.client.PullRequests.Create(ctx, result.Owner, result.Repository, newPR) if err != nil { return types.ActionResult{}, fmt.Errorf("failed to create pull request: %w", err) } diff --git a/services/actions/githubprcreator_test.go b/services/actions/githubprcreator_test.go index b283cfc..c96c9f6 100644 --- a/services/actions/githubprcreator_test.go +++ b/services/actions/githubprcreator_test.go @@ -56,7 +56,7 @@ var _ = Describe("GithubPRCreator", func() { result, err := action.Run(ctx, params) Expect(err).NotTo(HaveOccurred()) - Expect(result.Result).To(ContainSubstring("Created pull request #")) + Expect(result.Result).To(ContainSubstring("pull request #")) }) It("should handle missing required fields", func() { @@ -104,9 +104,10 @@ var _ = Describe("GithubPRCreator", func() { It("should handle provided repository and owner in config", func() { config := map[string]string{ - "token": "test-token", - "repository": "test-repo", - "owner": "test-owner", + "token": "test-token", + "repository": "test-repo", + "defaultBranch": "main", + "owner": "test-owner", } action := actions.NewGithubPRCreator(config) def := action.Definition()