class Dependabot::GoModules::FileUpdater

Public Class Methods

new(dependencies:, dependency_files:, repo_contents_path: nil, credentials:, options: {}) click to toggle source
Calls superclass method
# File lib/dependabot/go_modules/file_updater.rb, line 13
def initialize(dependencies:, dependency_files:, repo_contents_path: nil,
               credentials:, options: {})
  super

  use_repo_contents_stub if repo_contents_path.nil?
end
updated_files_regex() click to toggle source
# File lib/dependabot/go_modules/file_updater.rb, line 20
def self.updated_files_regex
  [
    /^go\.mod$/,
    /^go\.sum$/
  ]
end

Public Instance Methods

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

  if go_mod && file_changed?(go_mod)
    updated_files <<
      updated_file(
        file: go_mod,
        content: file_updater.updated_go_mod_content
      )

    if go_sum && go_sum.content != file_updater.updated_go_sum_content
      updated_files <<
        updated_file(
          file: go_sum,
          content: file_updater.updated_go_sum_content
        )
    end

    vendor_updater.updated_vendor_cache_files(base_directory: directory).
      each do |file|
      updated_files << file
    end
  end

  raise "No files changed!" if updated_files.none?

  updated_files
end

Private Instance Methods

check_required_files() click to toggle source
# File lib/dependabot/go_modules/file_updater.rb, line 58
def check_required_files
  return if go_mod

  raise "No go.mod!"
end
directory() click to toggle source
# File lib/dependabot/go_modules/file_updater.rb, line 95
def directory
  dependency_files.first.directory
end
file_updater() click to toggle source
# File lib/dependabot/go_modules/file_updater.rb, line 110
def file_updater
  @file_updater ||=
    GoModUpdater.new(
      dependencies: dependencies,
      credentials: credentials,
      repo_contents_path: repo_contents_path,
      directory: directory,
      options: { tidy: tidy?, vendor: vendor? }
    )
end
go_mod() click to toggle source
# File lib/dependabot/go_modules/file_updater.rb, line 87
def go_mod
  @go_mod ||= get_original_file("go.mod")
end
go_sum() click to toggle source
# File lib/dependabot/go_modules/file_updater.rb, line 91
def go_sum
  @go_sum ||= get_original_file("go.sum")
end
tidy?() click to toggle source
# File lib/dependabot/go_modules/file_updater.rb, line 121
def tidy?
  !@repo_contents_stub
end
use_repo_contents_stub() click to toggle source
# File lib/dependabot/go_modules/file_updater.rb, line 64
def use_repo_contents_stub
  @repo_contents_stub = true
  @repo_contents_path = Dir.mktmpdir

  Dir.chdir(@repo_contents_path) do
    dependency_files.each do |file|
      path = File.join(@repo_contents_path, directory, file.name)
      path = Pathname.new(path).expand_path
      FileUtils.mkdir_p(path.dirname) unless Dir.exist?(path.dirname)
      File.write(path, file.content)
    end

    # Only used to create a backup git config that's reset
    SharedHelpers.with_git_configured(credentials: []) do
      `git config --global user.email "no-reply@github.com"`
      `git config --global user.name "Dependabot"`
      `git init .`
      `git add .`
      `git commit -m'fake repo_contents_path'`
    end
  end
end
vendor?() click to toggle source
# File lib/dependabot/go_modules/file_updater.rb, line 125
def vendor?
  File.exist?(File.join(vendor_dir, "modules.txt"))
end
vendor_dir() click to toggle source
# File lib/dependabot/go_modules/file_updater.rb, line 99
def vendor_dir
  File.join(repo_contents_path, directory, "vendor")
end
vendor_updater() click to toggle source
# File lib/dependabot/go_modules/file_updater.rb, line 103
def vendor_updater
  Dependabot::FileUpdaters::VendorUpdater.new(
    repo_contents_path: repo_contents_path,
    vendor_dir: vendor_dir
  )
end