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

[View source]

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

def self.ensure_no_errors(json)
  error_type = T.let(json.fetch("ErrorType", nil), T.nilable(String))
  error_details = T.let(json.fetch("ErrorDetails", nil), T.nilable(String))
  case error_type
  when "None", nil
    # no issue
  when "AuthenticationFailure"
    raise PrivateSourceAuthenticationFailure, error_details
  when "MissingFile"
    raise DependencyFileNotFound, error_details
  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

[View source]

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
144
145
146
147
148
149
# File 'lib/dependabot/nuget/native_helpers.rb', line 117

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,
    "--verbose"
  ].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>",
    "--verbose"
  ].compact.join(" ")

  [command, fingerprint]
end

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

[View source]

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

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,
    "--verbose"
  ].compact

  command = Shellwords.join(command_parts)

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

  [command, fingerprint]
end

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

[View source]

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
213
214
215
216
217
218
219
220
# File 'lib/dependabot/nuget/native_helpers.rb', line 177

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,
    "--verbose"
  ].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>",
    "--verbose"
  ].compact.join(" ")

  [command, fingerprint]
end

.native_helpers_rootObject

[View source]

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

[View source]

157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/dependabot/nuget/native_helpers.rb', line 157

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

[View source]

100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dependabot/nuget/native_helpers.rb', line 100

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

[View source]

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
55
56
# 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,
    "--verbose"
  ]
  command = Shellwords.join(command_parts)

  fingerprint = [
    exe_path,
    "framework-check",
    "--project-tfms",
    "<project-tfms>",
    "--package-tfms",
    "<package-tfms>",
    "--verbose"
  ].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

[View source]

237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/dependabot/nuget/native_helpers.rb', line 237

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
    env = {}
    env["UseNewNugetPackageResolver"] = "true" if Dependabot::Experiments.enabled?(:nuget_dependency_solver)
    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

[View source]

224
225
226
# File 'lib/dependabot/nuget/native_helpers.rb', line 224

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