summaryrefslogtreecommitdiff
path: root/qa/qa/git/repository.rb
diff options
context:
space:
mode:
Diffstat (limited to 'qa/qa/git/repository.rb')
-rw-r--r--qa/qa/git/repository.rb46
1 files changed, 32 insertions, 14 deletions
diff --git a/qa/qa/git/repository.rb b/qa/qa/git/repository.rb
index ac8dcbf0d83..0aa94101098 100644
--- a/qa/qa/git/repository.rb
+++ b/qa/qa/git/repository.rb
@@ -5,15 +5,19 @@ require 'uri'
require 'open3'
require 'fileutils'
require 'tmpdir'
+require 'tempfile'
+require 'securerandom'
module QA
module Git
class Repository
include Scenario::Actable
- attr_writer :password, :use_lfs
+ attr_writer :use_lfs
attr_accessor :env_vars
+ InvalidCredentialsError = Class.new(RuntimeError)
+
def initialize
# We set HOME to the current working directory (which is a
# temporary directory created in .perform()) so the temporarily dropped
@@ -28,6 +32,14 @@ module QA
end
end
+ def password=(password)
+ @password = password
+
+ raise InvalidCredentialsError, "Please provide a username when setting a password" unless username
+
+ try_add_credentials_to_netrc
+ end
+
def uri=(address)
@uri = URI(address)
end
@@ -148,16 +160,7 @@ module QA
return unless add_credentials?
return if netrc_already_contains_content?
- # Despite libcurl supporting a custom .netrc location through the
- # CURLOPT_NETRC_FILE environment variable, git does not support it :(
- # Info: https://curl.haxx.se/libcurl/c/CURLOPT_NETRC_FILE.html
- #
- # This will create a .netrc in the correct working directory, which is
- # a temporary directory created in .perform()
- #
- FileUtils.mkdir_p(tmp_home_dir)
- File.open(netrc_file_path, 'a') { |file| file.puts(netrc_content) }
- File.chmod(0600, netrc_file_path)
+ save_netrc_content
end
private
@@ -175,7 +178,6 @@ module QA
def add_credentials?
return false if !username || !password
return true unless ssh_key_set?
- return true if ssh_key_set? && use_lfs?
false
end
@@ -214,6 +216,23 @@ module QA
end
end
+ def read_netrc_content
+ File.exist?(netrc_file_path) ? File.readlines(netrc_file_path) : []
+ end
+
+ def save_netrc_content
+ # Despite libcurl supporting a custom .netrc location through the
+ # CURLOPT_NETRC_FILE environment variable, git does not support it :(
+ # Info: https://curl.haxx.se/libcurl/c/CURLOPT_NETRC_FILE.html
+ #
+ # This will create a .netrc in the correct working directory, which is
+ # a temporary directory created in .perform()
+ #
+ FileUtils.mkdir_p(tmp_home_dir)
+ File.open(netrc_file_path, 'a') { |file| file.puts(netrc_content) }
+ File.chmod(0600, netrc_file_path)
+ end
+
def tmp_home_dir
@tmp_home_dir ||= File.join(Dir.tmpdir, "qa-netrc-credentials", $$.to_s)
end
@@ -227,8 +246,7 @@ module QA
end
def netrc_already_contains_content?
- File.exist?(netrc_file_path) &&
- File.readlines(netrc_file_path).grep(/^#{netrc_content}$/).any?
+ read_netrc_content.grep(/^#{netrc_content}$/).any?
end
end
end