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

Include directory when serializing dependencies #10002

Merged
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
21 changes: 21 additions & 0 deletions updater/lib/dependabot/dependency_change.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def initialize(job:, updated_dependencies:, updated_dependency_files:, dependenc
@dependency_group = dependency_group

@pr_message = T.let(nil, T.nilable(Dependabot::PullRequestCreator::Message))
ensure_dependencies_have_directories
end

sig { returns(Dependabot::PullRequestCreator::Message) }
Expand Down Expand Up @@ -180,10 +181,30 @@ def updated_dependencies_set
{
"dependency-name" => dep.name,
"dependency-version" => dep.version,
"directory" => should_consider_directory? ? dep.directory : nil,
"dependency-removed" => dep.removed? ? true : nil
}.compact
end
)
end

sig { returns(T::Array[Dependabot::Dependency]) }
def ensure_dependencies_have_directories
updated_dependencies.each do |dep|
dep.directory = directory
end
end

sig { returns(String) }
def directory
return "" if updated_dependency_files.empty?

T.must(updated_dependency_files.first).directory
end

sig { returns(T::Boolean) }
def should_consider_directory?
grouped_update? && Dependabot::Experiments.enabled?("dependency_has_directory")
Copy link
Member

Choose a reason for hiding this comment

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

I know this is targeted to fix a grouping bug, but any reason not to do it consistently for any kind of update instead of just grouped updates?

Copy link
Member Author

Choose a reason for hiding this comment

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

In my testing, the logic relying on this method would eagerly result in closing existing/creating new pull requests. Focusing on grouped updates seemed like a lighter touch with a lower likelihood of generating unwanted churn. It'd be great if we could get to a place of setting directory reliably so that we can consider all types of changes 😄

end
end
end
122 changes: 122 additions & 0 deletions updater/spec/dependabot/dependency_change_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,126 @@
end
end
end

describe "#matches_existing_pr?" do
before do
Dependabot::Experiments.register("dependency_has_directory", true)
end

after do
Dependabot::Experiments.reset!
end

context "when no existing pull requests are found" do
let(:job) do
instance_double(Dependabot::Job,
dependencies: updated_dependencies.map(&:name),
existing_pull_requests: [])
end
let(:dependency_change) do
described_class.new(
job: job,
updated_dependencies: updated_dependencies,
updated_dependency_files: updated_dependency_files
)
end

it "returns false" do
expect(dependency_change.matches_existing_pr?).to be false
end
end

context "when updating a pull request with the same dependencies" do
let(:job) do
instance_double(Dependabot::Job,
dependencies: updated_dependencies.map(&:name),
existing_pull_requests: existing_pull_requests)
end
let(:existing_pull_requests) do
[
updated_dependencies.map do |dep|
{ "dependency-name" => dep.name,
"dependency-version" => dep.version }
end
]
end
let(:dependency_change) do
described_class.new(
job: job,
updated_dependencies: updated_dependencies,
updated_dependency_files: updated_dependency_files
)
end

it "returns true" do
expect(dependency_change.matches_existing_pr?).to be true
end
end

context "when updating a grouped pull request with the same dependencies" do
let(:job) do
instance_double(Dependabot::Job,
dependencies: updated_dependencies.map(&:name),
existing_group_pull_requests: existing_group_pull_requests)
end
let(:existing_group_pull_requests) do
[
{ "dependency-group-name" => "foo",
"dependencies" => [
updated_dependencies.map do |dep|
{ "dependency-name" => dep.name.to_s,
"dependency-version" => dep.version.to_s,
"directory" => dep.directory.to_s }
end
] }
]
end

let(:dependency_change) do
described_class.new(
job: job,
updated_dependencies: updated_dependencies,
updated_dependency_files: updated_dependency_files,
dependency_group: Dependabot::DependencyGroup.new(name: "foo", rules: { patterns: ["*"] })
)
end

it "returns true" do
expect(dependency_change.matches_existing_pr?).to be false
end
end

context "when updating a grouped pull request with the same dependencies, but in different directory" do
let(:job) do
instance_double(Dependabot::Job,
dependencies: updated_dependencies.map(&:name),
existing_group_pull_requests: existing_group_pull_requests)
end
let(:existing_group_pull_requests) do
[
{ "dependency-group-name" => "foo",
"dependencies" => [
updated_dependencies.map do |dep|
{ "dependency-name" => dep.name.to_s,
"dependency-version" => dep.version.to_s,
"directory" => "/foo" }
end
] }
]
end

let(:dependency_change) do
described_class.new(
job: job,
updated_dependencies: updated_dependencies,
updated_dependency_files: updated_dependency_files,
dependency_group: Dependabot::DependencyGroup.new(name: "foo", rules: { patterns: ["*"] })
)
end

it "returns false" do
expect(dependency_change.matches_existing_pr?).to be false
end
end
end
end
4 changes: 2 additions & 2 deletions updater/spec/dependabot/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

let(:dependency_files) do
[
{ name: "Gemfile", content: "some gems" }
Dependabot::DependencyFile.new(name: "Gemfile", content: "some gems")
]
end

Expand Down Expand Up @@ -138,7 +138,7 @@

let(:dependency_files) do
[
{ name: "Gemfile", content: "some gems" }
Dependabot::DependencyFile.new(name: "Gemfile", content: "some gems")
]
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@

before do
stub_rubygems_calls
Dependabot::Experiments.register("dependency_has_directory", true)
end

after do
Dependabot::Experiments.reset!
end

it "updates the existing pull request without errors" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ job:
dependencies:
- dependency-name: dummy-pkg-b
dependency-version: 1.2.0
directory: "/"
updating-a-pull-request: true
lockfile-only: false
update-subdependencies: false
Expand Down
Loading