Class: Dependabot::Terraform::RequirementsUpdater

Inherits:
Object
  • Object
show all
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: “`ruby {

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.

Parameters:


51
52
53
54
55
56
57
58
59
# File 'lib/dependabot/terraform/requirements_updater.rb', line 51

def initialize(requirements:, latest_version:, tag_for_latest_version:)
  @requirements = requirements
  @tag_for_latest_version = tag_for_latest_version

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

  @latest_version = version_class.new(latest_version)
end

Instance Method Details

#updated_requirementsObject


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dependabot/terraform/requirements_updater.rb', line 66

def updated_requirements
  return requirements unless latest_version

  # 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