Class: Dependabot::FileFetchers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/file_fetchers/base.rb

Direct Known Subclasses

Config::FileFetcher

Constant Summary collapse

CLIENT_NOT_FOUND_ERRORS =
[
  Octokit::NotFound,
  Gitlab::Error::NotFound,
  Dependabot::Clients::Azure::NotFound,
  Dependabot::Clients::Bitbucket::NotFound,
  Dependabot::Clients::CodeCommit::NotFound
].freeze
GIT_SUBMODULE_INACCESSIBLE_ERROR =
/^fatal: unable to access '(?<url>.*)': The requested URL returned error: (?<code>\d+)$/
GIT_SUBMODULE_CLONE_ERROR =
/^fatal: clone of '(?<url>.*)' into submodule path '.*' failed$/
GIT_SUBMODULE_ERROR_REGEX =
/(#{GIT_SUBMODULE_INACCESSIBLE_ERROR})|(#{GIT_SUBMODULE_CLONE_ERROR})/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, credentials:, repo_contents_path: nil, options: {}) ⇒ Base

Creates a new FileFetcher for retrieving ‘DependencyFile`s.

Files are typically grabbed individually via the source’s API. repo_contents_path is an optional empty directory that will be used to clone the entire source repository on first read.

If provided, file data will be loaded from the clone. Submodules and directory listings are not currently supported by repo_contents_path and still use an API trip.

options supports custom feature enablement



54
55
56
57
58
59
60
# File 'lib/dependabot/file_fetchers/base.rb', line 54

def initialize(source:, credentials:, repo_contents_path: nil, options: {})
  @source = source
  @credentials = credentials
  @repo_contents_path = repo_contents_path
  @linked_paths = {}
  @options = options
end

Instance Attribute Details

#credentialsObject (readonly)

Returns the value of attribute credentials.



19
20
21
# File 'lib/dependabot/file_fetchers/base.rb', line 19

def credentials
  @credentials
end

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/dependabot/file_fetchers/base.rb', line 19

def options
  @options
end

#repo_contents_pathObject (readonly)

Returns the value of attribute repo_contents_path.



19
20
21
# File 'lib/dependabot/file_fetchers/base.rb', line 19

def repo_contents_path
  @repo_contents_path
end

#sourceObject (readonly)

Returns the value of attribute source.



19
20
21
# File 'lib/dependabot/file_fetchers/base.rb', line 19

def source
  @source
end

Class Method Details

.required_files_in?(_filename_array) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


35
36
37
# File 'lib/dependabot/file_fetchers/base.rb', line 35

def self.required_files_in?(_filename_array)
  raise NotImplementedError
end

.required_files_messageObject

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/dependabot/file_fetchers/base.rb', line 39

def self.required_files_message
  raise NotImplementedError
end

Instance Method Details

#clone_repo_contentsObject

Returns the path to the cloned repo



92
93
94
95
96
97
98
99
100
101
# File 'lib/dependabot/file_fetchers/base.rb', line 92

def clone_repo_contents
  @clone_repo_contents ||=
    _clone_repo_contents(target_directory: repo_contents_path)
rescue Dependabot::SharedHelpers::HelperSubprocessFailed => e
  if e.message.include?("fatal: Remote branch #{target_branch} not found in upstream origin")
    raise Dependabot::BranchNotFound, target_branch
  end

  raise Dependabot::RepoNotFound, source
end

#commitObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dependabot/file_fetchers/base.rb', line 78

def commit
  return cloned_commit if cloned_commit
  return source.commit if source.commit

  branch = target_branch || default_branch_for_repo

  @commit ||= client_for_provider.fetch_commit(repo, branch)
rescue *CLIENT_NOT_FOUND_ERRORS
  raise Dependabot::BranchNotFound, branch
rescue Octokit::Conflict => e
  raise unless e.message.include?("Repository is empty")
end

#directoryObject



66
67
68
# File 'lib/dependabot/file_fetchers/base.rb', line 66

def directory
  Pathname.new(source.directory || "/").cleanpath.to_path
end

#filesObject



74
75
76
# File 'lib/dependabot/file_fetchers/base.rb', line 74

def files
  @files ||= fetch_files
end

#repoObject



62
63
64
# File 'lib/dependabot/file_fetchers/base.rb', line 62

def repo
  source.repo
end

#target_branchObject



70
71
72
# File 'lib/dependabot/file_fetchers/base.rb', line 70

def target_branch
  source.branch
end