Class: Dependabot::Terraform::RequirementsUpdater

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/terraform/requirements_updater.rb

Overview

Takes an array of requirements hashes for a dependency at the old version and a new version, and generates a set of new requirements hashes at the new version.

A requirements hash is a basic description of a dependency at a certain version constraint, and it includes the data that is needed to update the manifest (i.e. the .tf file) with the new version.

A requirements hash looks like this for a registry hosted requirement:

{
  requirement: "~> 0.2.1",
  groups: [],
  file: "main.tf",
  source: {
    type: "registry",
    registry_hostname: "registry.terraform.io",
    module_identifier: "hashicorp/consul/aws"
  }
}

And like this for a git requirement:
```ruby
{
  requirement: nil,
  groups: [],
  file: "main.tf",
  source: {
    type: "git",
    url: "https://github.com/cloudposse/terraform-null-label.git",
    branch: nil,
    ref: nil
  }
}

Instance Method Summary collapse

Constructor Details

#initialize(requirements:, latest_version:, tag_for_latest_version:) ⇒ RequirementsUpdater

Returns a new instance of RequirementsUpdater.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dependabot/terraform/requirements_updater.rb', line 64

def initialize(requirements:, latest_version:, tag_for_latest_version:)
  @requirements = T.let(
    requirements.map { |req| Dependabot::DependencyRequirement.create(req) },
    T::Array[Dependabot::DependencyRequirement]
  )
  @tag_for_latest_version = tag_for_latest_version

  return unless latest_version
  return unless version_class.correct?(latest_version)

  @latest_version = T.let(version_class.new(latest_version), Dependabot::Terraform::Version)
end

Instance Method Details

#updated_requirementsObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dependabot/terraform/requirements_updater.rb', line 83

def updated_requirements
  # NOTE: Order is important here. The FileUpdater needs the updated
  # requirement at index `i` to correspond to the previous requirement
  # at the same index.
  requirements.map do |req|
    case req.dig(:source, :type)
    when "git" then update_git_requirement(req)
    when "registry", "provider" then update_registry_requirement(req)
    else req
    end
  end
end