Module: Dependabot::Nuget::NativeHelpers

Extended by:
T::Sig
Defined in:
lib/dependabot/nuget/native_helpers.rb

Class Method Summary collapse

Class Method Details

.ensure_no_errors(json) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/dependabot/nuget/native_helpers.rb', line 256

def self.ensure_no_errors(json)
  error_type = T.let(json.fetch("ErrorType", nil), T.nilable(String))
  error_details = json.fetch("ErrorDetails", nil)
  case error_type
  when "None", nil
    # no issue
  when "AuthenticationFailure"
    raise PrivateSourceAuthenticationFailure, T.let(error_details, T.nilable(String))
  when "MissingFile"
    raise DependencyFileNotFound, T.let(error_details, T.nilable(String))
  when "UpdateNotPossible"
    raise UpdateNotPossible, T.let(error_details, T::Array[String])
  when "Unknown"
    raise DependabotError, T.let(error_details, String)
  else
    raise "Unexpected error type from native tool: #{error_type}: #{error_details}"
  end
end

.get_nuget_analyze_tool_command(repo_root:, discovery_file_path:, dependency_file_path:, analysis_folder_path:) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dependabot/nuget/native_helpers.rb', line 113

def self.get_nuget_analyze_tool_command(repo_root:, discovery_file_path:, dependency_file_path:,
                                        analysis_folder_path:)
  exe_path = File.join(native_helpers_root, "NuGetUpdater", "NuGetUpdater.Cli")
  command_parts = [
    exe_path,
    "analyze",
    "--repo-root",
    repo_root,
    "--discovery-file-path",
    discovery_file_path,
    "--dependency-file-path",
    dependency_file_path,
    "--analysis-folder-path",
    analysis_folder_path
  ].compact

  command = Shellwords.join(command_parts)

  fingerprint = [
    exe_path,
    "analyze",
    "--discovery-file-path",
    "<discovery-file-path>",
    "--dependency-file-path",
    "<dependency-file-path>",
    "--analysis-folder-path",
    "<analysis_folder_path>"
  ].compact.join(" ")

  [command, fingerprint]
end

.get_nuget_discover_tool_command(repo_root:, workspace_path:, output_path:) ⇒ Object



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
# File 'lib/dependabot/nuget/native_helpers.rb', line 59

def self.get_nuget_discover_tool_command(repo_root:, workspace_path:, output_path:)
  exe_path = File.join(native_helpers_root, "NuGetUpdater", "NuGetUpdater.Cli")
  command_parts = [
    exe_path,
    "discover",
    "--repo-root",
    repo_root,
    "--workspace",
    workspace_path,
    "--output",
    output_path
  ].compact

  command = Shellwords.join(command_parts)

  fingerprint = [
    exe_path,
    "discover",
    "--repo-root",
    "<repo-root>",
    "--workspace",
    "<path-to-workspace>",
    "--output",
    "<path-to-output>"
  ].compact.join(" ")

  [command, fingerprint]
end

.get_nuget_updater_tool_command(repo_root:, proj_path:, dependency:, is_transitive:, result_output_path:) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/dependabot/nuget/native_helpers.rb', line 171

def self.get_nuget_updater_tool_command(repo_root:, proj_path:, dependency:, is_transitive:, result_output_path:)
  exe_path = File.join(native_helpers_root, "NuGetUpdater", "NuGetUpdater.Cli")
  command_parts = [
    exe_path,
    "update",
    "--repo-root",
    repo_root,
    "--solution-or-project",
    proj_path,
    "--dependency",
    dependency.name,
    "--new-version",
    dependency.version,
    "--previous-version",
    dependency.previous_version,
    is_transitive ? "--transitive" : nil,
    "--result-output-path",
    result_output_path
  ].compact

  command = Shellwords.join(command_parts)

  fingerprint = [
    exe_path,
    "update",
    "--repo-root",
    "<repo-root>",
    "--solution-or-project",
    "<path-to-solution-or-project>",
    "--dependency",
    "<dependency-name>",
    "--new-version",
    "<new-version>",
    "--previous-version",
    "<previous-version>",
    is_transitive ? "--transitive" : nil,
    "--result-output-path",
    "<result-output-path>"
  ].compact.join(" ")

  [command, fingerprint]
end

.native_helpers_rootObject



15
16
17
18
19
20
# File 'lib/dependabot/nuget/native_helpers.rb', line 15

def self.native_helpers_root
  helpers_root = ENV.fetch("DEPENDABOT_NATIVE_HELPERS_PATH", nil)
  return File.join(helpers_root, "nuget") unless helpers_root.nil?

  File.expand_path("../../../helpers", __dir__)
end

.run_nuget_analyze_tool(repo_root:, discovery_file_path:, dependency_file_path:, analysis_folder_path:, credentials:) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/dependabot/nuget/native_helpers.rb', line 151

def self.run_nuget_analyze_tool(repo_root:, discovery_file_path:, dependency_file_path:,
                                analysis_folder_path:, credentials:)
  (command, fingerprint) = get_nuget_analyze_tool_command(repo_root: repo_root,
                                                          discovery_file_path: discovery_file_path,
                                                          dependency_file_path: dependency_file_path,
                                                          analysis_folder_path: analysis_folder_path)

  puts "running NuGet analyze:\n" + command

  NuGetConfigCredentialHelpers.patch_nuget_config_for_action(credentials) do
    output = SharedHelpers.run_shell_command(command, allow_unsafe_shell_command: true, fingerprint: fingerprint)
    puts output
  end
end

.run_nuget_discover_tool(repo_root:, workspace_path:, output_path:, credentials:) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dependabot/nuget/native_helpers.rb', line 96

def self.run_nuget_discover_tool(repo_root:, workspace_path:, output_path:, credentials:)
  (command, fingerprint) = get_nuget_discover_tool_command(repo_root: repo_root,
                                                           workspace_path: workspace_path,
                                                           output_path: output_path)

  puts "running NuGet discovery:\n" + command

  NuGetConfigCredentialHelpers.patch_nuget_config_for_action(credentials) do
    output = SharedHelpers.run_shell_command(command, allow_unsafe_shell_command: true, fingerprint: fingerprint)
    puts output
  end
end

.run_nuget_framework_check(project_tfms, package_tfms) ⇒ Object



23
24
25
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
# File 'lib/dependabot/nuget/native_helpers.rb', line 23

def self.run_nuget_framework_check(project_tfms, package_tfms)
  exe_path = File.join(native_helpers_root, "NuGetUpdater", "NuGetUpdater.Cli")
  command_parts = [
    exe_path,
    "framework-check",
    "--project-tfms",
    *project_tfms,
    "--package-tfms",
    *package_tfms
  ]
  command = Shellwords.join(command_parts)

  fingerprint = [
    exe_path,
    "framework-check",
    "--project-tfms",
    "<project-tfms>",
    "--package-tfms",
    "<package-tfms>"
  ].join(" ")

  puts "running NuGet updater:\n" + command

  output = SharedHelpers.run_shell_command(command, allow_unsafe_shell_command: true, fingerprint: fingerprint)
  puts output

  # Exit code == 0 means that all project frameworks are compatible
  true
rescue Dependabot::SharedHelpers::HelperSubprocessFailed
  # Exit code != 0 means that not all project frameworks are compatible
  false
end

.run_nuget_updater_tool(repo_root:, proj_path:, dependency:, is_transitive:, credentials:) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/dependabot/nuget/native_helpers.rb', line 229

def self.run_nuget_updater_tool(repo_root:, proj_path:, dependency:, is_transitive:, credentials:)
  (command, fingerprint) = get_nuget_updater_tool_command(repo_root: repo_root, proj_path: proj_path,
                                                          dependency: dependency, is_transitive: is_transitive,
                                                          result_output_path: update_result_file_path)

  puts "running NuGet updater:\n" + command

  NuGetConfigCredentialHelpers.patch_nuget_config_for_action(credentials) do
    # default to UseNewNugetPackageResolved _unless_ nuget_legacy_dependency_solver is enabled
    env = {}
    unless Dependabot::Experiments.enabled?(:nuget_legacy_dependency_solver)
      env["UseNewNugetPackageResolver"] = "true"
    end
    output = SharedHelpers.run_shell_command(command,
                                             allow_unsafe_shell_command: true,
                                             fingerprint: fingerprint,
                                             env: env)
    puts output

    result_contents = File.read(update_result_file_path)
    Dependabot.logger.info("update result: #{result_contents}")
    result_json = T.let(JSON.parse(result_contents), T::Hash[String, T.untyped])
    ensure_no_errors(result_json)
  end
end

.update_result_file_pathObject



216
217
218
# File 'lib/dependabot/nuget/native_helpers.rb', line 216

def self.update_result_file_path
  File.join(Dir.tmpdir, "update-result.json")
end