Skip to content
Open
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
44 changes: 42 additions & 2 deletions gitops/git/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,47 @@ var (
githubEnterpriseHost = flag.String("github_enterprise_host", "", "The host name of the private enterprise github, e.g. git.corp.adobe.com")
)

func updateExistingPR(ctx context.Context, gh *github.Client, owner, repo, from, to, title, body string) error {
log.Println("PR already exists, updating title and body...")

// List PRs to find the existing one
listOpts := &github.PullRequestListOptions{
State: "open",
Head: owner + ":" + from,
Base: to,
}

prs, _, err := gh.PullRequests.List(ctx, owner, repo, listOpts)
if err != nil {
log.Println("Error listing PRs:", err)
return err
}

if len(prs) == 0 {
return errors.New("could not find open PR matching the head and base branches")
}

// Update the first matching PR
existingPR := prs[0]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic in updateExistingPR always updates the first PR returned (prs[0]). If multiple open PRs match the criteria, this may result in updating the wrong PR. Consider iterating through all matching PRs or adding additional checks to ensure the correct PR is updated.

updatePR := &github.PullRequest{
Title: &title,
Body: &body,
}

updatedPR, _, err := gh.PullRequests.Edit(ctx, owner, repo, *existingPR.Number, updatePR)
if err != nil {
log.Println("Error updating PR:", err)
return err
}

if updatedPR.URL != nil {
log.Println("Updated existing PR:", *updatedPR.URL)
} else {
log.Println("Updated existing PR #", *existingPR.Number)
}
return nil
}

func CreatePR(from, to, title, body string) error {
if *repoOwner == "" {
return errors.New("github_repo_owner must be set")
Expand Down Expand Up @@ -68,8 +109,7 @@ func CreatePR(from, to, title, body string) error {

if resp.StatusCode == http.StatusUnprocessableEntity {
// Handle the case: "Create PR" request fails because it already exists
log.Println("Reusing existing PR")
return nil
return updateExistingPR(ctx, gh, *repoOwner, *repo, from, to, title, body)
}

// All other github responses
Expand Down