Module: Dependabot::Maven::NativeHelpers

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

Constant Summary collapse

DEPENDENCY_PLUGIN_VERSION =
T.let(version, T.nilable(String))

Class Method Summary collapse

Class Method Details

.handle_tool_error(output) ⇒ Object

Raises:

  • (DependabotError)


45
46
47
48
49
50
51
52
53
# File 'lib/dependabot/maven/native_helpers.rb', line 45

def self.handle_tool_error(output)
  if (match = output.match(
    %r{Could not transfer artifact (?<artifact>[^ ]+) from/to (?<repository_name>[^ ]+) \((?<repository_url>[^ ]+)\): status code: (?<status_code>[0-9]+)} # rubocop:disable Layout/LineLength
  )) && (match[:status_code] == "403" || match[:status_code] == "401")
    raise Dependabot::PrivateSourceAuthenticationFailure, match[:repository_url]
  end

  raise DependabotError, "mvn CLI failed with an unhandled error"
end

.run_mvn_dependency_tree_plugin(file_name) ⇒ Object

Raises:

  • (DependabotError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dependabot/maven/native_helpers.rb', line 28

def self.run_mvn_dependency_tree_plugin(file_name)
  raise DependabotError, "Could not resolve maven-dependency-plugin version" unless DEPENDENCY_PLUGIN_VERSION

  proxy_url = URI.parse(ENV.fetch("HTTPS_PROXY"))
  stdout, _, status = Open3.capture3(
    { "PROXY_HOST" => proxy_url.host },
    "mvn",
    "dependency:#{DEPENDENCY_PLUGIN_VERSION}:tree",
    "-DoutputFile=#{file_name}",
    "-DoutputType=json",
    "-e"
  )
  Dependabot.logger.info("mvn dependency:tree output: STDOUT:#{stdout}")
  handle_tool_error(stdout) unless status.success?
end

.run_mvnw_wrapper(version:, wrapper_plugin_version:, env:, distribution_type:, extra_args: [], cwd: nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dependabot/maven/native_helpers.rb', line 74

def self.run_mvnw_wrapper(version:, wrapper_plugin_version:, env:, distribution_type:, extra_args: [], cwd: nil)
  # Use the fully-qualified plugin goal so the exact plugin version is
  # invoked regardless of the project's plugin group configuration.
  plugin_goal = "org.apache.maven.plugins:maven-wrapper-plugin:" \
                "#{wrapper_plugin_version}:wrapper"

  standard_args = [
    plugin_goal,
    "-Dmaven=#{version}",
    "-Dtype=#{distribution_type}",
    "--no-transfer-progress"
  ] + extra_args

  # Pass the argument vector directly instead of a pre-joined shell string.
  # `run_shell_command` shell-escapes string commands internally, so building
  # the command with `Shellwords.join` here would double-escape arguments
  # (e.g. `-Dmaven=3.6.3` becoming `-Dmaven\=3.6.3`), which Maven then fails
  # to parse. An argument vector is executed without an intermediate shell.
  cmd = ["mvn"] + standard_args
  run_cwd = cwd && cwd != "." ? cwd : nil
  SharedHelpers.run_shell_command(cmd, env: env, cwd: run_cwd)
end