Skip to content

Commit

Permalink
Fix build.gradle files missing some updates (#6624)
Browse files Browse the repository at this point in the history
Within a single file, when multiple configurations include the same dependency, only one would
be updated.
  • Loading branch information
deivid-rodriguez authored Feb 9, 2023
1 parent b637fda commit 74c4607
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
26 changes: 15 additions & 11 deletions gradle/lib/dependabot/gradle/file_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,30 @@ def update_files_for_dep_set_change(buildfiles, old_req, new_req)

def update_version_in_buildfile(dependency, buildfile, previous_req,
requirement)
original_content = buildfile.content.dup

updated_content =
buildfile.content.gsub(
original_buildfile_declaration(dependency, previous_req),
updated_buildfile_declaration(
dependency,
previous_req,
requirement
original_buildfile_declarations(dependency, previous_req).reduce(original_content) do |content, declaration|
content.gsub(
declaration,
updated_buildfile_declaration(
declaration,
previous_req,
requirement
)
)
)
end

raise "Expected content to change!" if updated_content == buildfile.content

updated_file(file: buildfile, content: updated_content)
end

def original_buildfile_declaration(dependency, requirement)
def original_buildfile_declarations(dependency, requirement)
# This implementation is limited to declarations that appear on a
# single line.
buildfile = buildfiles.find { |f| f.name == requirement.fetch(:file) }
buildfile.content.lines.find do |line|
buildfile.content.lines.select do |line|
line = evaluate_properties(line, buildfile)
line = line.gsub(%r{(?<=^|\s)//.*$}, "")

Expand Down Expand Up @@ -171,10 +175,10 @@ def property_value_finder
new(dependency_files: dependency_files)
end

def updated_buildfile_declaration(dependency, previous_req, requirement)
def updated_buildfile_declaration(original_buildfile_declaration, previous_req, requirement)
original_req_string = previous_req.fetch(:requirement)

original_buildfile_declaration(dependency, previous_req).gsub(
original_buildfile_declaration.gsub(
original_req_string,
requirement.fetch(:requirement)
)
Expand Down
36 changes: 36 additions & 0 deletions gradle/spec/dependabot/gradle/file_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,42 @@
end
end

context "with multiple configurations using the same dependency" do
let(:buildfile_fixture_name) { "multiple_configurations.gradle" }

let(:dependencies) do
[
Dependabot::Dependency.new(
name: "org.projectlombok:lombok",
version: "1.18.26",
previous_version: "1.18.24",
requirements: [{
file: "build.gradle",
requirement: "1.18.26",
groups: [],
source: {
type: "maven_repo",
url: "https://repo.maven.apache.org/maven2"
}
}],
previous_requirements: [{
file: "build.gradle",
requirement: "1.18.24",
groups: [],
source: nil
}],
package_manager: "gradle"
)
]
end

it "updates the version in all configurations" do
expect(updated_buildfile.content).
to include("compileOnly 'org.projectlombok:lombok:1.18.26'").
and include("annotationProcessor 'org.projectlombok:lombok:1.18.26'")
end
end

context "with a dependency version defined by a property" do
let(:dependency_files) { [buildfile, subproject_buildfile] }
let(:subproject_buildfile) do
Expand Down
12 changes: 12 additions & 0 deletions gradle/spec/fixtures/buildfiles/multiple_configurations.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
id 'java'
}

repositories {
mavenCentral()
}

dependencies {
compileOnly 'org.projectlombok:lombok:1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.24'
}

0 comments on commit 74c4607

Please sign in to comment.