Skip to content

Commit

Permalink
Merge pull request dependabot#4191 from declan-fitzpatrick/main
Browse files Browse the repository at this point in the history
handle terraform module versions with a 'v' prefix
  • Loading branch information
Nishnha authored Sep 7, 2021
2 parents 2b55890 + b8ee465 commit afedf0c
Show file tree
Hide file tree
Showing 15 changed files with 345 additions and 1 deletion.
7 changes: 7 additions & 0 deletions terraform/lib/dependabot/terraform/requirement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
module Dependabot
module Terraform
class Requirement < Gem::Requirement
# Override regex PATTERN from Gem::Requirement to add support for the
# optional 'v' prefix to release tag names, which Terraform supports.
# https://www.terraform.io/docs/registry/modules/publish.html#requirements
OPERATORS = OPS.keys.map { |key| Regexp.quote(key) }.join("|").freeze
PATTERN_RAW = "\\s*(#{OPERATORS})?\\s*v?(#{Gem::Version::VERSION_PATTERN})\\s*"
PATTERN = /\A#{PATTERN_RAW}\z/.freeze

def self.parse(obj)
return ["=", Version.new(obj.to_s)] if obj.is_a?(Gem::Version)

Expand Down
234 changes: 233 additions & 1 deletion terraform/spec/dependabot/terraform/file_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,52 @@ module "s3-webapp" {
end
end

context "with a private module with v prefix" do
let(:project_name) { "private_module_with_v_prefix" }

let(:dependencies) do
[
Dependabot::Dependency.new(
name: "example-org-5d3190/s3-webapp/aws",
version: "2.0.0",
previous_version: "v1.0.0",
requirements: [{
requirement: "2.0.0",
groups: [],
file: "main.tf",
source: {
type: "registry",
registry_hostname: "app.terraform.io",
module_identifier: "example-org-5d3190/s3-webapp/aws"
}
}],
previous_requirements: [{
requirement: "v1.0.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 the private module version and drops the v prefix" do
updated_file = subject.find { |file| file.name == "main.tf" }

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

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

Expand Down Expand Up @@ -346,6 +392,52 @@ module "consul" {
end
end

context "with a legacy registry dependency with v prefix" do
let(:project_name) { "registry_with_v_prefix" }
let(:dependencies) do
[
Dependabot::Dependency.new(
name: "hashicorp/consul/aws",
version: "0.3.1",
previous_version: "v0.1.0",
requirements: [{
requirement: "0.3.1",
groups: [],
file: "main.tf",
source: {
type: "registry",
registry_hostname: "registry.terraform.io",
module_identifier: "hashicorp/consul/aws"
}
}],
previous_requirements: [{
requirement: "v0.1.0",
groups: [],
file: "main.tf",
source: {
type: "registry",
registry_hostname: "registry.terraform.io",
module_identifier: "hashicorp/consul/aws"
}
}],
package_manager: "terraform"
)
]
end

it "updates the requirement and drops the v prefix" do
updated_file = subject.find { |file| file.name == "main.tf" }

expect(updated_file.content).to include(
<<~DEP
module "consul" {
source = "hashicorp/consul/aws"
version = "0.3.1"
DEP
)
end
end

context "with an hcl2-based registry dependency" do
let(:project_name) { "registry_012" }
let(:dependencies) do
Expand Down Expand Up @@ -393,6 +485,52 @@ module "consul" {
end
end

context "with an hcl2-based registry dependency with a v prefix" do
let(:project_name) { "registry_012_with_v_prefix" }
let(:dependencies) do
[
Dependabot::Dependency.new(
name: "hashicorp/consul/aws",
version: "0.3.1",
previous_version: "v0.1.0",
requirements: [{
requirement: "0.3.1",
groups: [],
file: "main.tf",
source: {
type: "registry",
registry_hostname: "registry.terraform.io",
module_identifier: "hashicorp/consul/aws"
}
}],
previous_requirements: [{
requirement: "v0.1.0",
groups: [],
file: "main.tf",
source: {
type: "registry",
registry_hostname: "registry.terraform.io",
module_identifier: "hashicorp/consul/aws"
}
}],
package_manager: "terraform"
)
]
end

it "updates the requirement and drops the v prefix" do
updated_file = subject.find { |file| file.name == "main.tf" }

expect(updated_file.content).to include(
<<~DEP
module "consul" {
source = "hashicorp/consul/aws"
version = "0.3.1"
DEP
)
end
end

context "with an hcl-based terragrunt file" do
let(:project_name) { "terragrunt_hcl" }

Expand Down Expand Up @@ -872,6 +1010,53 @@ module "github_terraform" {
end
end

describe "for a nested module with a v prefix" do
let(:project_name) { "nested_modules_with_v_prefix" }
let(:dependencies) do
[
Dependabot::Dependency.new(
name: "terraform-aws-modules/iam/aws",
version: "4.1.0",
previous_version: "v4.0.0",
requirements: [{
requirement: "4.1.0",
groups: [],
file: "main.tf",
source: {
type: "registry",
registry_hostname: "registry.terraform.io",
module_identifier: "iam/aws"
}
}],
previous_requirements: [{
requirement: "v4.0.0",
groups: [],
file: "main.tf",
source: {
type: "registry",
registry_hostname: "registry.terraform.io",
module_identifier: "iam/aws"
}
}],
package_manager: "terraform"
)
]
end

it "updates the requirement and drops the v prefix" do
updated_file = subject.find { |file| file.name == "main.tf" }

expect(updated_file.content).to include(
<<~DEP
module "github_terraform" {
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role"
version = "4.1.0"
}
DEP
)
end
end

describe "with a lockfile and modules that need to be installed" do
let(:project_name) { "lockfile_with_modules" }
let(:dependencies) do
Expand Down Expand Up @@ -965,8 +1150,55 @@ module "caf" {
end
end

describe "when updating a module with a v prefix in a project with a provider lockfile" do
let(:project_name) { "lockfile_with_modules_with_v_prefix" }
let(:dependencies) do
[
Dependabot::Dependency.new(
name: "aztfmod/caf/azurerm",
version: "5.3.10",
previous_version: "v5.1.0",
requirements: [{
requirement: "5.3.10",
groups: [],
file: "caf_module.tf",
source: {
type: "registry",
registry_hostname: "registry.terraform.io",
module_identifier: "aztfmod/caf/azurerm"
}
}],
previous_requirements: [{
requirement: "v5.1.0",
groups: [],
file: "caf_module.tf",
source: {
type: "registry",
registry_hostname: "registry.terraform.io",
module_identifier: "aztfmod/caf/azurerm"
}
}],
package_manager: "terraform"
)
]
end

it "updates the module version and drops the v prefix" do
module_file = subject.find { |file| file.name == "caf_module.tf" }

expect(module_file.content).to include(
<<~DEP
module "caf" {
source = "aztfmod/caf/azurerm"
version = "5.3.10"
}
DEP
)
end
end

describe "when updating a provider with local path modules" do
let(:project_name) { "provider_with_local_path_moudules" }
let(:project_name) { "provider_with_local_path_modules" }
let(:dependencies) do
[
Dependabot::Dependency.new(
Expand Down
4 changes: 4 additions & 0 deletions terraform/spec/dependabot/terraform/requirement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
let(:requirement_string) { "~> 4.2.5, >= 4.2.5.1" }
it { is_expected.to eq(described_class.new("~> 4.2.5", ">= 4.2.5.1")) }
end
context "with a comma-separated string" do
let(:requirement_string) { "~> v4.2.5, >= v4.2.5.1" }
it { is_expected.to eq(described_class.new("~> 4.2.5", ">= 4.2.5.1")) }
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module "caf" {
source = "aztfmod/caf/azurerm"
version = "v5.1.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
github = {
source = "integrations/github"
version = "~> 4.4, <= 4.12.0"
}
}
required_version = ">= v0.14"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module "github_terraform" {
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role"
version = "v4.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module "s3-webapp" {
source = "app.terraform.io/example-org-5d3190/s3-webapp/aws"
version = "v1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module "consul" {
source = "hashicorp/consul/aws"
version = "v0.1.0"
}

module "vpc" {
source = "app.terraform.io/example_corp/vpc/aws"
version = "v0.9.3"
}

module "rds" {
source = "terraform-aws-modules/rds/aws"
version = "~> v1.0.0"
}

module "members-github" {
source = "devops-workflow/members/github"
}

module "merged" {
source = "mongodb/ecs-task-definition/aws//modules/merge"

container_definitions = [
var.web_container_definition,
module.xray.container_definitions,
module.reverse_proxy.container_definitions,
module.datadog.container_definitions,
]
}
Loading

0 comments on commit afedf0c

Please sign in to comment.