Class: Dependabot::Bundler::UpdateChecker::FilePreparer

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/bundler/update_checker/file_preparer.rb

Overview

This class takes a set of dependency files and sanitizes them for use in UpdateCheckers::Ruby::Bundler. In particular, it:

  • Removes any version requirement on the dependency being updated (in the Gemfile)

  • Sanitizes any provided gemspecs to remove file imports etc. (since Dependabot doesn’t pull down the entire repo). This process is imperfect - an alternative would be to clone the repo

  • Sets the ruby version in the Gemfile to be the lowest possible version allowed by the gemspec, if the gemspec has a required ruby version range

Constant Summary collapse

VERSION_REGEX =
/[0-9]+(?:\.[A-Za-z0-9\-_]+)*/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(dependency_files:, dependency:, remove_git_source: false, unlock_requirement: true, replacement_git_pin: nil, latest_allowable_version: nil, lock_ruby_version: true) ⇒ FilePreparer

Returns a new instance of FilePreparer.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dependabot/bundler/update_checker/file_preparer.rb', line 38

def initialize(dependency_files:, dependency:,
               remove_git_source: false,
               unlock_requirement: true,
               replacement_git_pin: nil,
               latest_allowable_version: nil,
               lock_ruby_version: true)
  @dependency_files         = dependency_files
  @dependency               = dependency
  @remove_git_source        = remove_git_source
  @unlock_requirement       = unlock_requirement
  @replacement_git_pin      = replacement_git_pin
  @latest_allowable_version = latest_allowable_version
  @lock_ruby_version        = lock_ruby_version
end

Instance Method Details

#gemspec_sourcesObject

Can’t be a constant because some of these don’t exist in bundler 1.15, which Heroku uses, which causes an exception on boot.



31
32
33
34
35
36
# File 'lib/dependabot/bundler/update_checker/file_preparer.rb', line 31

def gemspec_sources
  [
    ::Bundler::Source::Path,
    ::Bundler::Source::Gemspec
  ]
end

#prepared_dependency_filesObject

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dependabot/bundler/update_checker/file_preparer.rb', line 55

def prepared_dependency_files
  files = []

  if gemfile
    files << DependencyFile.new(
      name: gemfile.name,
      content: gemfile_content_for_update_check(gemfile),
      directory: gemfile.directory
    )
  end

  top_level_gemspecs.each do |gemspec|
    files << DependencyFile.new(
      name: gemspec.name,
      content: gemspec_content_for_update_check(gemspec),
      directory: gemspec.directory
    )
  end

  path_gemspecs.each do |file|
    files << DependencyFile.new(
      name: file.name,
      content: sanitize_gemspec_content(file.content),
      directory: file.directory,
      support_file: file.support_file?
    )
  end

  evaled_gemfiles.each do |file|
    files << DependencyFile.new(
      name: file.name,
      content: gemfile_content_for_update_check(file),
      directory: file.directory
    )
  end

  # No editing required for lockfile or Ruby version file
  files += [
    lockfile,
    ruby_version_file,
    *imported_ruby_files,
    *specification_files
  ].compact
end