Class: Dependabot::Gradle::FileUpdater::LockfileUpdater

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/gradle/file_updater/lockfile_updater.rb

Constant Summary collapse

INIT_SCRIPT_TASK_NAME =
"dependabotResolveAll"
GRADLE_JVMARGS_ATTEMPTS =
[
  "-Xmx1536m -Dfile.encoding=UTF-8",
  "-Xmx2048m -Dfile.encoding=UTF-8"
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(dependency_files:, repo_contents_path: nil) ⇒ LockfileUpdater

Returns a new instance of LockfileUpdater.



29
30
31
32
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 29

def initialize(dependency_files:, repo_contents_path: nil)
  @dependency_files = dependency_files
  @repo_contents_path = repo_contents_path
end

Instance Method Details

#determine_root_dir(build_file:) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 83

def determine_root_dir(build_file:)
  settings_file = find_settings_file(build_file)
  return normalized_directory_path(settings_file) if settings_file

  file_path = normalized_file_path(build_file)
  return normalize_path(File.dirname(file_path, 2)) if file_path.end_with?("/gradle/libs.versions.toml")

  normalized_directory_path(build_file)
end

#lockfiles_for_root(root_dir) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 101

def lockfiles_for_root(root_dir)
  sub_build_roots = sub_build_roots_for(root_dir)

  dependency_files.select do |file|
    next false unless file.name.end_with?(".lockfile")

    file_path = normalized_file_path(file)
    next false unless path_under_root?(file_path, root_dir)

    sub_build_roots.none? { |sub_root| file_path.start_with?("#{sub_root}/") || file_path == sub_root }
  end
end

#normalized_directory_path(file) ⇒ Object



94
95
96
97
98
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 94

def normalized_directory_path(file)
  file_path = normalized_file_path(file)
  dir = File.dirname(file_path)
  dir == "/" ? "/" : normalize_path(dir)
end

#update_lockfiles(build_file, build_files: [build_file]) ⇒ Object



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 40

def update_lockfiles(build_file, build_files: [build_file])
  root_dir = determine_root_dir(build_file: build_file)
  lockfiles = lockfiles_for_root(root_dir)

  return dependency_files unless lockfiles.any?

  updated_files = dependency_files.dup

  SharedHelpers.in_a_temporary_directory do |temp_dir|
    populate_temp_directory(temp_dir)

    cwd = File.join(temp_dir, root_dir == "/" ? "" : root_dir.delete_prefix("/"))
    FileUtils.mkdir_p(cwd)

    properties_file_path = File.join(cwd, "gradle.properties")

    init_script_path = File.join(cwd, "dependabot-locking.init.gradle")
    write_init_script(init_script_path)

    command_parts = [
      gradle_executable_for(cwd: cwd, workspace_root: temp_dir.to_s),
      "--init-script", init_script_path,
      *lockfile_tasks_for(build_files),
      "--write-locks",
      "--no-daemon"
    ]
    command = Shellwords.join(command_parts)
    run_lockfile_update_with_retry(
      command: command,
      cwd: cwd,
      properties_file_path: properties_file_path
    )

    update_lockfiles_content(temp_dir, lockfiles, updated_files)
  rescue SharedHelpers::HelperSubprocessFailed => e
    Dependabot.logger.error("Failed to update lockfiles: #{e.message}")
    return updated_files
  end

  updated_files
end

#update_lockfiles_content(temp_dir, lockfiles, updated_lockfiles) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 121

def update_lockfiles_content(temp_dir, lockfiles, updated_lockfiles)
  lockfiles.each do |file|
    # Handle "/" directory as root - File.join treats "/" as absolute path and ignores prior components
    relative_dir = file.directory == "/" ? "" : file.directory
    lockfile_path = File.join(temp_dir, relative_dir, file.name)

    unless File.exist?(lockfile_path)
      Dependabot.logger.warn(
        "Lockfile #{file.name} was not regenerated by Gradle after a successful lockfile update run. " \
        "Preserving existing lockfile."
      )
      next
    end

    content = File.read(lockfile_path)
    next if content == file.content

    tmp_file = file.dup
    tmp_file.content = content

    index = updated_lockfiles.find_index { |f| f.name == file.name }
    if index
      updated_lockfiles[index] = tmp_file
    else
      updated_lockfiles << tmp_file
    end
  end
end