class Dependabot::GithubActions::FileUpdater

Public Class Methods

updated_files_regex() click to toggle source
# File lib/dependabot/github_actions/file_updater.rb, line 10
def self.updated_files_regex
  [%r{\.github/workflows/.+\.ya?ml$}]
end

Public Instance Methods

updated_dependency_files() click to toggle source
# File lib/dependabot/github_actions/file_updater.rb, line 14
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_workflow_file_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/github_actions/file_updater.rb, line 40
def check_required_files
  # Just check if there are any files at all.
  return if dependency_files.any?

  raise "No workflow files!"
end
dependency() click to toggle source
# File lib/dependabot/github_actions/file_updater.rb, line 35
def dependency
  # GitHub Actions will only ever be updating a single dependency
  dependencies.first
end
updated_workflow_file_content(file) click to toggle source
# File lib/dependabot/github_actions/file_updater.rb, line 47
def updated_workflow_file_content(file)
  updated_requirement_pairs =
    dependency.requirements.zip(dependency.previous_requirements).
    reject do |new_req, old_req|
      next true if new_req[:file] != file.name

      new_req[:source] == old_req[:source]
    end

  updated_content = file.content

  updated_requirement_pairs.each do |new_req, old_req|
    # TODO: Support updating Docker sources
    next unless new_req.fetch(:source).fetch(:type) == "git"

    old_declaration = old_req.fetch(:metadata).fetch(:declaration_string)
    new_declaration =
      old_declaration.
      gsub(/@.*+/, "@#{new_req.fetch(:source).fetch(:ref)}")

    # Replace the old declaration that's preceded by a non-word character
    # and followed by a whitespace character (comments) or EOL
    updated_content =
      updated_content.
      gsub(
        /(?<=\W|"|')#{Regexp.escape(old_declaration)}(?=\s|"|'|$)/,
        new_declaration
      )
  end

  updated_content
end