Class: Dependabot::FileUpdaters::ArtifactUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/file_updaters/artifact_updater.rb

Direct Known Subclasses

VendorUpdater

Instance Method Summary collapse

Constructor Details

#initialize(repo_contents_path:, target_directory:) ⇒ ArtifactUpdater

Returns a new instance of ArtifactUpdater.

Parameters:

  • repo_contents_path (String, nil)

    the path we cloned the repository into

  • target_directory (String, nil)

    the path within a project directory we should inspect for changes



14
15
16
17
# File 'lib/dependabot/file_updaters/artifact_updater.rb', line 14

def initialize(repo_contents_path:, target_directory:)
  @repo_contents_path = repo_contents_path
  @target_directory = target_directory
end

Instance Method Details

#updated_files(base_directory:, only_paths: nil) ⇒ Array<Dependabot::DependencyFile>

Returns any files that have changed within the path composed from:

:repo_contents_path/:base_directory/:target_directory

Parameters:

  • base_directory (String)

    Update config base directory

  • only_paths (Array<String>, nil) (defaults to: nil)

    An optional list of specific paths to check, if this is nil we will return every change we find within the ‘base_directory`

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dependabot/file_updaters/artifact_updater.rb', line 26

def updated_files(base_directory:, only_paths: nil)
  return [] unless repo_contents_path && target_directory

  Dir.chdir(repo_contents_path) do
    # rubocop:disable Performance/DeletePrefix
    relative_dir = Pathname.new(base_directory).sub(%r{\A/}, "").join(target_directory)
    # rubocop:enable Performance/DeletePrefix

    status = SharedHelpers.run_shell_command(
      "git status --untracked-files all --porcelain v1 #{relative_dir}",
      fingerprint: "git status --untracked-files all --porcelain v1 <relative_dir>"
    )
    changed_paths = status.split("\n").map(&:split)
    changed_paths.filter_map do |type, path|
      project_root = Pathname.new(File.expand_path(File.join(Dir.pwd, base_directory)))
      file_path = Pathname.new(path).expand_path.relative_path_from(project_root)

      # Skip this file if we are looking for specific paths and this isn't on the list
      next if only_paths && !only_paths.include?(file_path.to_s)

      # The following types are possible to be returned:
      # M = Modified = Default for DependencyFile
      # D = Deleted
      # ?? = Untracked = Created
      operation = Dependabot::DependencyFile::Operation::UPDATE
      operation = Dependabot::DependencyFile::Operation::DELETE if type == "D"
      operation = Dependabot::DependencyFile::Operation::CREATE if type == "??"

      encoded_content, encoding = get_encoded_file_contents(path, operation)

      create_dependency_file(
        name: file_path.to_s,
        content: encoded_content,
        directory: base_directory,
        operation: operation,
        content_encoding: encoding
      )
    end
  end
end