class Dependabot::Docker::FileUpdater

Constants

FROM_REGEX

Public Class Methods

updated_files_regex() click to toggle source
# File lib/dependabot/docker/file_updater.rb, line 12
def self.updated_files_regex
  [/dockerfile/i]
end

Public Instance Methods

updated_dependency_files() click to toggle source
# File lib/dependabot/docker/file_updater.rb, line 16
def updated_dependency_files
  updated_files = []

  dependency_files.each do |file|
    next unless requirement_changed?(file, dependency)

    updated_files <<
      updated_file(
        file: file,
        content: updated_dockerfile_content(file)
      )
  end

  updated_files.reject! { |f| dependency_files.include?(f) }
  raise "No files changed!" if updated_files.none?

  updated_files
end

Private Instance Methods

check_required_files() click to toggle source
# File lib/dependabot/docker/file_updater.rb, line 42
def check_required_files
  # Just check if there are any files at all.
  return if dependency_files.any?

  raise "No Dockerfile!"
end
dependency() click to toggle source
# File lib/dependabot/docker/file_updater.rb, line 37
def dependency
  # Dockerfiles will only ever be updating a single dependency
  dependencies.first
end
new_digest(file) click to toggle source
# File lib/dependabot/docker/file_updater.rb, line 106
def new_digest(file)
  return unless specified_with_digest?(file)

  dependency.requirements.
    find { |r| r[:file] == file.name }.
    fetch(:source).fetch(:digest)
end
new_tag(file) click to toggle source
# File lib/dependabot/docker/file_updater.rb, line 122
def new_tag(file)
  dependency.requirements.
    find { |r| r[:file] == file.name }.
    fetch(:source)[:tag]
end
old_digest(file) click to toggle source
# File lib/dependabot/docker/file_updater.rb, line 114
def old_digest(file)
  return unless specified_with_digest?(file)

  dependency.previous_requirements.
    find { |r| r[:file] == file.name }.
    fetch(:source).fetch(:digest)
end
old_tags(file) click to toggle source
# File lib/dependabot/docker/file_updater.rb, line 128
def old_tags(file)
  dependency.
    previous_requirements.
    select { |r| r[:file] == file.name }.
    map { |r| r.fetch(:source)[:tag] }
end
private_registry_url(file) click to toggle source
# File lib/dependabot/docker/file_updater.rb, line 135
def private_registry_url(file)
  dependency.requirements.
    find { |r| r[:file] == file.name }.
    fetch(:source)[:registry]
end
specified_with_digest?(file) click to toggle source
# File lib/dependabot/docker/file_updater.rb, line 99
def specified_with_digest?(file)
  dependency.
    requirements.
    find { |r| r[:file] == file.name }.
    fetch(:source)[:digest]
end
update_digest_and_tag(file) click to toggle source
# File lib/dependabot/docker/file_updater.rb, line 62
def update_digest_and_tag(file)
  old_declaration_regex = /^#{FROM_REGEX}\s+.*@#{old_digest(file)}/

  file.content.gsub(old_declaration_regex) do |old_dec|
    old_dec.
      gsub("@#{old_digest(file)}", "@#{new_digest(file)}").
      gsub(":#{dependency.previous_version}",
           ":#{dependency.version}")
  end
end
update_tag(file) click to toggle source
# File lib/dependabot/docker/file_updater.rb, line 73
def update_tag(file)
  old_tags = old_tags(file)
  return if old_tags.empty?

  modified_content = file.content

  old_tags.each do |old_tag|
    old_declaration =
      if private_registry_url(file) then "#{private_registry_url(file)}/"
      else ""
      end
    old_declaration += "#{dependency.name}:#{old_tag}"
    escaped_declaration = Regexp.escape(old_declaration)

    old_declaration_regex =
      %r{^#{FROM_REGEX}\s+(docker\.io/)?#{escaped_declaration}(?=\s|$)}

    modified_content = modified_content.
                       gsub(old_declaration_regex) do |old_dec|
      old_dec.gsub(":#{old_tag}", ":#{new_tag(file)}")
    end
  end

  modified_content
end
updated_dockerfile_content(file) click to toggle source
# File lib/dependabot/docker/file_updater.rb, line 49
def updated_dockerfile_content(file)
  updated_content =
    if specified_with_digest?(file)
      update_digest_and_tag(file)
    else
      update_tag(file)
    end

  raise "Expected content to change!" if updated_content == file.content

  updated_content
end