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

Introduce feature flag to raise exception on same branch exists #10878

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 2 additions & 6 deletions common/lib/dependabot/pull_request_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class NoHistoryInCommon < StandardError; end

class UnmergedPRExists < StandardError; end

class BranchAlreadyExists < StandardError; end

class BaseCommitNotUpToDate < StandardError; end

class UnexpectedError < StandardError; end
Expand Down Expand Up @@ -396,12 +398,6 @@ def message

sig { returns(Dependabot::PullRequestCreator::BranchNamer) }
def branch_namer
if Dependabot::Experiments.enabled?(:dedup_branch_names) && existing_branches
Dependabot.logger.debug(
"Dependabot::PullRequestCreator::branch_namer : #{existing_branches}"
)
end

@branch_namer ||= T.let(
BranchNamer.new(
dependencies: dependencies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,7 @@ def sanitize_branch_name(ref_name)
sanitized_name[[T.must(max_length) - sha.size, 0].max..] = sha
end

if Dependabot::Experiments.enabled?(:dedup_branch_names)
dedup_existing_branches(sanitized_name)
else
sanitized_name
end
end

sig { params(ref: String).returns(String) }
def dedup_existing_branches(ref)
Dependabot.logger.debug(
"Dependabot::PullRequestCreator::dedup_existing_branches::ref : #{ref}"
)
return ref unless existing_branches.include?(ref)

i = 1
new_ref = "#{ref}-#{i}"
while existing_branches.include?(new_ref)
i += 1
new_ref = "#{ref}-#{i}"
end

Dependabot.logger.debug(
"Dependabot::PullRequestCreator::dedup_existing_branches::new_ref : #{new_ref}"
)

new_ref
sanitized_name
end

sig { params(ref: String).returns(String) }
Expand Down
17 changes: 17 additions & 0 deletions common/lib/dependabot/pull_request_creator/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ def initialize(source:, branch_name:, base_commit:, credentials:,

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

Copy link
Member

Choose a reason for hiding this comment

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

If we are raising when a branch already exists then we can get rid of the checks for when an unmerged pull request exists. But it might be better to do that in a follow-up PR since this one is already a lot of changes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Nishnha , ill do it next release

if branch_exists?(branch_name) && unmerged_pull_request_exists?
raise UnmergedPRExists, "PR ##{unmerged_pull_requests.first.number} already exists"
end
Expand All @@ -132,6 +139,11 @@ def require_up_to_date_base?
# rubocop:disable Metrics/PerceivedComplexity
sig { params(name: String).returns(T::Boolean) }
def branch_exists?(name)
Dependabot.logger.debug(
"Dependabot::PullRequestCreator::Github:branch_exists?. " \
"Name : #{name}. IsDuplicate: #{git_metadata_fetcher.ref_names.include?(name)}"
)

git_metadata_fetcher.ref_names.include?(name)
rescue Dependabot::GitDependenciesNotReachable => e
raise T.must(e.cause) if e.cause&.message&.include?("is disabled")
Expand Down Expand Up @@ -580,6 +592,11 @@ def raise_custom_error(base_err, type, message)
raise type, message
end
end

sig { returns(T::Boolean) }
def experiment_duplicate_branch?
Dependabot::Experiments.enabled?(:dedup_branch_names)
end
end
# rubocop:enable Metrics/ClassLength
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,6 @@
it { is_expected.to eq("myapp/dummy/business-1.5.0") }
end

context "with a branch name that already exists" do
before do
Dependabot::Experiments.register(:dedup_branch_names, true)
end

let!(:existing_branches) do
[
"dependabot/dummy/business-1.5.0",
"dependabot/dummy/business-1.5.0-1"
]
end
let(:namer) do
described_class.new(
dependencies: dependencies,
files: files,
target_branch: target_branch,
existing_branches: existing_branches
)
end

it { is_expected.to eq("dependabot/dummy/business-1.5.0-2") }
end

context "with a target branch" do
let(:target_branch) { "my-branch" }

Expand Down
35 changes: 35 additions & 0 deletions common/spec/dependabot/pull_request_creator/github_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,41 @@
end
end

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

before do
Dependabot::Experiments.register(:dedup_branch_names, true)
end

after do
Dependabot::Experiments.register(:dedup_branch_names, false)
end

context "when the branch already exists" do
before do
url = "#{repo_api_url}/pulls?head=gocardless:#{branch_name}" \
"&state=all"
stub_request(:get, url)
.to_return(status: 200, body: "[]", headers: json_header)
stub_request(
:patch,
"#{repo_api_url}/git/refs/heads/#{branch_name}"
).to_return(
status: 200,
body: fixture("github", "update_ref.json"),
headers: json_header
)
end

it "returns a suitable exception" do
expect { creator.create }
.to raise_error(Dependabot::PullRequestCreator::BranchAlreadyExists,
"Duplicate branch #{branch_name} already exists")
end
end
end

context "when the PR doesn't have history in common with the base branch" do
before do
stub_request(:post, "#{repo_api_url}/pulls")
Expand Down
Binary file not shown.
Loading