Skip to content

Commit

Permalink
Merge pull request #5786 from szemek/private-modules-with-different-v…
Browse files Browse the repository at this point in the history
…ersions

Update all versions of the same private module in single terraform file
  • Loading branch information
Nishnha authored Sep 30, 2022
2 parents b0516da + 0ad52d0 commit 8fe2c6a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
26 changes: 25 additions & 1 deletion terraform/lib/dependabot/terraform/file_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ def updated_dependency_files

private

# Terraform allows to use a module from the same source multiple times
# To detect any changes in dependencies we need to overwrite an implementation from the base class
#
# Example (for simplicity other parameters are skipped):
# previous_requirements = [{requirement: "0.9.1"}, {requirement: "0.11.0"}]
# requirements = [{requirement: "0.11.0"}, {requirement: "0.11.0"}]
#
# Simple difference between arrays gives:
# requirements - previous_requirements
# => []
# which loses an information that one of our requirements has changed.
#
# By using symmetric difference:
# (requirements - previous_requirements) | (previous_requirements - requirements)
# => [{requirement: "0.9.1"}]
# we can detect that change.
def requirement_changed?(file, dependency)
changed_requirements =
(dependency.requirements - dependency.previous_requirements) |
(dependency.previous_requirements - dependency.requirements)

changed_requirements.any? { |f| f[:file] == file.name }
end

def updated_terraform_file_content(file)
content = file.content.dup

Expand Down Expand Up @@ -89,7 +113,7 @@ def update_git_declaration(new_req, old_req, updated_content, filename)

def update_registry_declaration(new_req, old_req, updated_content)
regex = new_req[:source][:type] == "provider" ? provider_declaration_regex : registry_declaration_regex
updated_content.sub!(regex) do |regex_match|
updated_content.gsub!(regex) do |regex_match|
regex_match.sub(/^\s*version\s*=.*/) do |req_line_match|
req_line_match.sub(old_req[:requirement], new_req[:requirement])
end
Expand Down
69 changes: 69 additions & 0 deletions terraform/spec/dependabot/terraform/file_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,75 @@ module "s3-webapp" {
end
end

context "with private modules with different versions" do
let(:project_name) { "private_modules_with_different_versions" }

let(:dependencies) do
[
Dependabot::Dependency.new(
name: "example-org-5d3190/s3-webapp/aws",
version: "0.11.0",
previous_version: "0.9.1",
requirements: [{
requirement: "0.11.0",
groups: [],
file: "main.tf",
source: {
type: "registry",
registry_hostname: "app.terraform.io",
module_identifier: "example-org-5d3190/s3-webapp/aws"
}
}, {
requirement: "0.11.0",
groups: [],
file: "main.tf",
source: {
type: "registry",
registry_hostname: "app.terraform.io",
module_identifier: "example-org-5d3190/s3-webapp/aws"
}
}],
previous_requirements: [{
requirement: "0.9.1",
groups: [],
file: "main.tf",
source: {
type: "registry",
registry_hostname: "app.terraform.io",
module_identifier: "example-org-5d3190/s3-webapp/aws"
}
}, {
requirement: "0.11.0",
groups: [],
file: "main.tf",
source: {
type: "registry",
registry_hostname: "app.terraform.io",
module_identifier: "example-org-5d3190/s3-webapp/aws"
}
}],
package_manager: "terraform"
)
]
end

it "updates all private modules versions" do
updated_file = subject.find { |file| file.name == "main.tf" }

expect(updated_file.content).to include(<<~HCL)
module "s3-webapp-first" {
source = "app.terraform.io/example-org-5d3190/s3-webapp/aws"
version = "0.11.0"
}
module "s3-webapp-second" {
source = "app.terraform.io/example-org-5d3190/s3-webapp/aws"
version = "0.11.0"
}
HCL
end
end

context "with a private provider" do
let(:project_name) { "private_provider" }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module "s3-webapp-first" {
source = "app.terraform.io/example-org-5d3190/s3-webapp/aws"
version = "0.11.0"
}

module "s3-webapp-second" {
source = "app.terraform.io/example-org-5d3190/s3-webapp/aws"
version = "0.9.1"
}

0 comments on commit 8fe2c6a

Please sign in to comment.