Module: Dependabot::Nuget::NuGetConfigCredentialHelpers

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

Class Method Summary collapse

Class Method Details

.add_credentials_to_nuget_config(credentials) ⇒ 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
55
56
# File 'lib/dependabot/nuget/nuget_config_credential_helpers.rb', line 23

def self.add_credentials_to_nuget_config(credentials)
  return unless File.exist?(user_nuget_config_path)

  nuget_credentials = credentials.select { |cred| cred["type"] == "nuget_feed" }
  return if nuget_credentials.empty?

  File.rename(user_nuget_config_path, temporary_nuget_config_path)

  package_sources = []
  package_source_credentials = []
  nuget_credentials.each_with_index do |c, i|
    source_name = "nuget_source_#{i + 1}"
    package_sources << "    <add key=\"#{source_name}\" value=\"#{c['url']}\" />"
    next unless c["token"]

    package_source_credentials << "    <#{source_name}>"
    package_source_credentials << "      <add key=\"Username\" value=\"user\" />"
    package_source_credentials << "      <add key=\"ClearTextPassword\" value=\"#{c['token']}\" />"
    package_source_credentials << "    </#{source_name}>"
  end

  nuget_config = <<~NUGET_XML
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
    #{package_sources.join("\n")}
      </packageSources>
      <packageSourceCredentials>
    #{package_source_credentials.join("\n")}
      </packageSourceCredentials>
    </configuration>
  NUGET_XML
  File.write(user_nuget_config_path, nuget_config)
end

.patch_nuget_config_for_action(credentials, &_block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dependabot/nuget/nuget_config_credential_helpers.rb', line 67

def self.patch_nuget_config_for_action(credentials, &_block)
  add_credentials_to_nuget_config(credentials)
  begin
    yield
  rescue DependabotError
    # forward these
    raise
  rescue StandardError => e
    log_message =
      <<~LOG_MESSAGE
        Block argument of NuGetConfigCredentialHelpers::patch_nuget_config_for_action causes an exception #{e}:
        #{e.message}
      LOG_MESSAGE
    Dependabot.logger.error(log_message)
    puts log_message
  ensure
    restore_user_nuget_config
  end
end

.restore_user_nuget_configObject



59
60
61
62
63
64
# File 'lib/dependabot/nuget/nuget_config_credential_helpers.rb', line 59

def self.restore_user_nuget_config
  return unless File.exist?(temporary_nuget_config_path)

  File.delete(user_nuget_config_path)
  File.rename(temporary_nuget_config_path, user_nuget_config_path)
end

.temporary_nuget_config_pathObject



18
19
20
# File 'lib/dependabot/nuget/nuget_config_credential_helpers.rb', line 18

def self.temporary_nuget_config_path
  user_nuget_config_path + "_ORIGINAL"
end

.user_nuget_config_pathObject



12
13
14
15
# File 'lib/dependabot/nuget/nuget_config_credential_helpers.rb', line 12

def self.user_nuget_config_path
  home_directory = Dir.home
  File.join(home_directory, ".nuget", "NuGet", "NuGet.Config")
end