Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only open a PR if one doesn't already exist #39

Merged
merged 1 commit into from
Jun 19, 2024
Merged
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
45 changes: 14 additions & 31 deletions pkg/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,28 +205,6 @@ func (fa *FrizbeeAction) commitChanges(path, content string) error {
func (fa *FrizbeeAction) createPR(ctx context.Context) error {
// Create a new branch for the PR
branchName := "frizbee-action-patch"
var existingPR *github.PullRequest

// Check if a PR already exists for the branch and return if it does
openPrs, _, err := fa.Client.PullRequests.List(ctx, fa.RepoOwner, fa.RepoName, &github.PullRequestListOptions{
Head: branchName,
State: "all",
})
if err != nil {
return err
}
for _, pr := range openPrs {
if pr.GetHead().GetRef() == branchName {
existingPR = pr
break
}
}

if existingPR != nil && existingPR.GetState() == "closed" &&
branchExists(ctx, fa.Client, fa.RepoOwner, fa.RepoName, branchName) {
log.Printf("PR %d is closed. Won't do anything as long as branch %s exists", existingPR.GetNumber(), branchName)
return nil
}

headRef, err := fa.Repo.Head()
if err != nil {
Expand Down Expand Up @@ -255,11 +233,21 @@ func (fa *FrizbeeAction) createPR(ctx context.Context) error {

log.Printf("Branch %s pushed successfully\n", branchName)

if existingPR != nil && existingPR.GetState() == "open" {
log.Printf("PR %d already exists. Won't create a new one\n", existingPR.GetNumber())
return nil
// Check if a PR already exists for the branch and return if it does
openPrs, _, err := fa.Client.PullRequests.List(ctx, fa.RepoOwner, fa.RepoName, &github.PullRequestListOptions{
Head: branchName,
State: "open",
})
if err != nil {
log.Fatalf("failed to list PRs: %v", err)
}

for _, pr := range openPrs {
if pr.GetHead().GetRef() == branchName {
log.Printf("PR %d already exists\n", pr.GetNumber())
return nil
}
}
// either the PR doesn't exist or was merged and it's time for another one

// Get defaultBranch
repository, _, err := fa.Client.Repositories.Get(ctx, fa.RepoOwner, fa.RepoName)
Expand Down Expand Up @@ -292,8 +280,3 @@ func (fa *FrizbeeAction) createPR(ctx context.Context) error {
log.Printf("PR %d created successfully\n", pr.GetNumber())
return nil
}

func branchExists(ctx context.Context, cli *github.Client, owner, repo, branch string) bool {
_, _, err := cli.Repositories.GetBranch(ctx, owner, repo, branch, 64)
return err == nil
}