Skip to content

Commit

Permalink
Adds conditional for no pull request for existing branch (#10963)
Browse files Browse the repository at this point in the history
Adds conditional for no pull request for existing branch (#10963)
  • Loading branch information
sachin-sandhu authored Nov 19, 2024
1 parent 3ef842c commit 557906b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
19 changes: 12 additions & 7 deletions common/lib/dependabot/pull_request_creator/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ def create
"Initiating Github pull request."
)

if experiment_duplicate_branch? && branch_exists?(branch_name)
if experiment_duplicate_branch? && branch_exists?(branch_name) && no_pull_request_exists?
Dependabot.logger.info(
"Existing branch \"#{branch_name}\" found. Pull request not created."
)
raise BranchAlreadyExists, "Duplicate branch #{branch_name} already exists"
end

if branch_exists?(branch_name) && unmerged_pull_request_exists?
raise UnmergedPRExists, "PR ##{unmerged_pull_requests.first.number} already exists"
if branch_exists?(branch_name) && open_pull_request_exists?
raise UnmergedPRExists, "PR ##{open_pull_requests.first.number} already exists"
end
if require_up_to_date_base? && !base_commit_is_up_to_date?
raise BaseCommitNotUpToDate, "HEAD #{head_commit} does not match base #{base_commit}"
Expand Down Expand Up @@ -164,13 +164,18 @@ def branch_exists?(name)
# rubocop:enable Metrics/PerceivedComplexity

sig { returns(T::Boolean) }
def unmerged_pull_request_exists?
unmerged_pull_requests.any?
def no_pull_request_exists?
pull_requests_for_branch.none?
end

sig { returns(T::Boolean) }
def open_pull_request_exists?
open_pull_requests.any?
end

sig { returns(T::Array[T.untyped]) }
def unmerged_pull_requests
pull_requests_for_branch.reject(&:merged)
def open_pull_requests
pull_requests_for_branch.reject(&:closed).reject(&:merged)
end

sig { returns(T::Array[T.untyped]) }
Expand Down
4 changes: 2 additions & 2 deletions common/spec/dependabot/pull_request_creator/github_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@
"&state=all"
stub_request(:get, url).to_return(
status: 200,
body: "[{ \"merged\": true }]",
body: "[{ \"closed\": true }]",
headers: json_header
)
stub_request(
Expand Down Expand Up @@ -613,7 +613,7 @@
end

context "when the branch already exists" do
let(:service_pack_response) { fixture("git", "upload_packs", "existing-branch") }
let(:service_pack_response) { fixture("git", "upload_packs", "existing-branch-with-no-pr") }

before do
Dependabot::Experiments.register(:dedup_branch_names, true)
Expand Down
Binary file not shown.

0 comments on commit 557906b

Please sign in to comment.