class Dependabot::Terraform::FileFetcher

Constants

LOCAL_PATH_SOURCE

www.terraform.io/docs/language/modules/sources.html#local-paths

Public Class Methods

required_files_in?(filenames) click to toggle source
# File lib/dependabot/terraform/file_fetcher.rb, line 15
def self.required_files_in?(filenames)
  filenames.any? { |f| f.end_with?(".tf", ".hcl") }
end
required_files_message() click to toggle source
# File lib/dependabot/terraform/file_fetcher.rb, line 19
def self.required_files_message
  "Repo must contain a Terraform configuration file."
end

Private Instance Methods

fetch_files() click to toggle source
# File lib/dependabot/terraform/file_fetcher.rb, line 25
def fetch_files
  fetched_files = []
  fetched_files += terraform_files
  fetched_files += terragrunt_files
  fetched_files += local_path_module_files(terraform_files)
  fetched_files += [lock_file] if lock_file

  return fetched_files if fetched_files.any?

  raise(
    Dependabot::DependencyFileNotFound,
    File.join(directory, "<anything>.tf")
  )
end
local_path_module_files(files, dir: ".") click to toggle source
# File lib/dependabot/terraform/file_fetcher.rb, line 54
def local_path_module_files(files, dir: ".")
  terraform_files = []

  files.each do |file|
    terraform_file_local_module_details(file).each do |path|
      base_path = Pathname.new(File.join(dir, path)).cleanpath.to_path
      nested_terraform_files =
        repo_contents(dir: base_path).
        select { |f| f.type == "file" && f.name.end_with?(".tf") }.
        map { |f| fetch_file_from_host(File.join(base_path, f.name)) }
      terraform_files += nested_terraform_files
      terraform_files += local_path_module_files(nested_terraform_files, dir: path)
    end
  end

  # NOTE: The `support_file` attribute is not used but we set this to
  # match what we do in other ecosystems
  terraform_files.tap { |fs| fs.each { |f| f.support_file = true } }
end
lock_file() click to toggle source
# File lib/dependabot/terraform/file_fetcher.rb, line 83
def lock_file
  @lock_file ||= fetch_file_if_present(".terraform.lock.hcl")
end
terraform_file_local_module_details(file) click to toggle source
# File lib/dependabot/terraform/file_fetcher.rb, line 74
def terraform_file_local_module_details(file)
  return [] unless file.name.end_with?(".tf")
  return [] unless file.content.match?(LOCAL_PATH_SOURCE)

  file.content.scan(LOCAL_PATH_SOURCE).flatten.map do |path|
    Pathname.new(path).cleanpath.to_path
  end
end
terraform_files() click to toggle source
# File lib/dependabot/terraform/file_fetcher.rb, line 40
def terraform_files
  @terraform_files ||=
    repo_contents(raise_errors: false).
    select { |f| f.type == "file" && f.name.end_with?(".tf") }.
    map { |f| fetch_file_from_host(f.name) }
end
terragrunt_files() click to toggle source
# File lib/dependabot/terraform/file_fetcher.rb, line 47
def terragrunt_files
  @terragrunt_files ||=
    repo_contents(raise_errors: false).
    select { |f| f.type == "file" && terragrunt_file?(f.name) }.
    map { |f| fetch_file_from_host(f.name) }
end