Class: Match::Storage::GitStorage

Inherits:
Interface
  • Object
show all
Defined in:
match/lib/match/storage/git_storage.rb

Overview

Store the code signing identities in a git repo

Constant Summary

Constants inherited from Interface

Interface::MATCH_VERSION_FILE_NAME

Instance Attribute Summary collapse

Attributes inherited from Interface

#working_directory

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Interface

#clear_changes, #configure, #save_changes!

Constructor Details

#initialize(type: nil, platform: nil, git_url: nil, shallow_clone: nil, skip_docs: false, branch: "master", git_full_name: nil, git_user_email: nil, clone_branch_directly: false, git_basic_authorization: nil, git_bearer_authorization: nil, git_private_key: nil) ⇒ GitStorage

Returns a new instance of GitStorage.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'match/lib/match/storage/git_storage.rb', line 41

def initialize(type: nil,
               platform: nil,
               git_url: nil,
               shallow_clone: nil,
               skip_docs: false,
               branch: "master",
               git_full_name: nil,
               git_user_email: nil,
               clone_branch_directly: false,
               git_basic_authorization: nil,
               git_bearer_authorization: nil,
               git_private_key: nil)
  self.git_url = git_url
  self.shallow_clone = shallow_clone
  self.skip_docs = skip_docs
  self.branch = branch
  self.git_full_name = git_full_name
  self.git_user_email = git_user_email
  self.clone_branch_directly = clone_branch_directly
  self.git_basic_authorization = git_basic_authorization
  self.git_bearer_authorization = git_bearer_authorization
  self.git_private_key = convert_private_key_path_to_absolute(git_private_key)

  self.type = type if type
  self.platform = platform if platform
end

Instance Attribute Details

#branchObject

Returns the value of attribute branch.



14
15
16
# File 'match/lib/match/storage/git_storage.rb', line 14

def branch
  @branch
end

#clone_branch_directlyObject

Returns the value of attribute clone_branch_directly.



17
18
19
# File 'match/lib/match/storage/git_storage.rb', line 17

def clone_branch_directly
  @clone_branch_directly
end

#git_basic_authorizationObject

Returns the value of attribute git_basic_authorization.



20
21
22
# File 'match/lib/match/storage/git_storage.rb', line 20

def git_basic_authorization
  @git_basic_authorization
end

#git_bearer_authorizationObject

Returns the value of attribute git_bearer_authorization.



21
22
23
# File 'match/lib/match/storage/git_storage.rb', line 21

def git_bearer_authorization
  @git_bearer_authorization
end

#git_full_nameObject

Returns the value of attribute git_full_name.



15
16
17
# File 'match/lib/match/storage/git_storage.rb', line 15

def git_full_name
  @git_full_name
end

#git_private_keyObject

Returns the value of attribute git_private_key.



22
23
24
# File 'match/lib/match/storage/git_storage.rb', line 22

def git_private_key
  @git_private_key
end

#git_urlObject

User provided values



11
12
13
# File 'match/lib/match/storage/git_storage.rb', line 11

def git_url
  @git_url
end

#git_user_emailObject

Returns the value of attribute git_user_email.



16
17
18
# File 'match/lib/match/storage/git_storage.rb', line 16

def git_user_email
  @git_user_email
end

#platformObject

Returns the value of attribute platform.



19
20
21
# File 'match/lib/match/storage/git_storage.rb', line 19

def platform
  @platform
end

#shallow_cloneObject

Returns the value of attribute shallow_clone.



12
13
14
# File 'match/lib/match/storage/git_storage.rb', line 12

def shallow_clone
  @shallow_clone
end

#skip_docsObject

Returns the value of attribute skip_docs.



13
14
15
# File 'match/lib/match/storage/git_storage.rb', line 13

def skip_docs
  @skip_docs
end

#typeObject

Returns the value of attribute type.



18
19
20
# File 'match/lib/match/storage/git_storage.rb', line 18

def type
  @type
end

Class Method Details

.configure(params) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'match/lib/match/storage/git_storage.rb', line 24

def self.configure(params)
  return self.new(
    type: params[:type].to_s,
    platform: params[:platform].to_s,
    git_url: params[:git_url],
    shallow_clone: params[:shallow_clone],
    skip_docs: params[:skip_docs],
    branch: params[:git_branch],
    git_full_name: params[:git_full_name],
    git_user_email: params[:git_user_email],
    clone_branch_directly: params[:clone_branch_directly],
    git_basic_authorization: params[:git_basic_authorization],
    git_bearer_authorization: params[:git_bearer_authorization],
    git_private_key: params[:git_private_key]
  )
end

Instance Method Details

#command_from_private_key(command) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'match/lib/match/storage/git_storage.rb', line 179

def command_from_private_key(command)
  if File.file?(self.git_private_key)
    ssh_add = File.expand_path(self.git_private_key).shellescape.to_s
  else
    UI.message("Private key file does not exist, will continue by using it as a raw key.")
    ssh_add = "- <<< \"#{self.git_private_key}\""
  end
  return "ssh-agent bash -c 'ssh-add #{ssh_add}; #{command}'"
end

#convert_private_key_path_to_absolute(git_private_key) ⇒ Object



68
69
70
71
72
73
74
# File 'match/lib/match/storage/git_storage.rb', line 68

def convert_private_key_path_to_absolute(git_private_key)
  if !git_private_key.nil? && File.file?(File.expand_path(git_private_key))
    File.expand_path(git_private_key).shellescape.to_s
  else
    git_private_key
  end
end

#delete_files(files_to_delete: [], custom_message: nil) ⇒ Object



142
143
144
145
146
147
# File 'match/lib/match/storage/git_storage.rb', line 142

def delete_files(files_to_delete: [], custom_message: nil)
  if files_to_delete.count > 0
    commands = files_to_delete.map { |filename|  "git rm #{filename.shellescape}" }
    git_push(commands: commands, commit_message: custom_message)
  end
end

#downloadObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'match/lib/match/storage/git_storage.rb', line 80

def download
  # Check if we already have a functional working_directory
  return if @working_directory

  # No existing working directory, creating a new one now
  self.working_directory = Dir.mktmpdir

  command = "git clone #{self.git_url.shellescape} #{self.working_directory.shellescape}"
  # HTTP headers are supposed to be case-insensitive but
  # Bitbucket requires `Authorization: Basic` and `Authorization Bearer` to work
  # https://github.com/fastlane/fastlane/pull/15928
  command << " -c http.extraheader='Authorization: Basic #{self.git_basic_authorization}'" unless self.git_basic_authorization.nil?
  command << " -c http.extraheader='Authorization: Bearer #{self.git_bearer_authorization}'" unless self.git_bearer_authorization.nil?

  if self.shallow_clone
    command << " --depth 1"
  end

  if self.clone_branch_directly
    command += " -b #{self.branch.shellescape} --single-branch"
  elsif self.shallow_clone
    # shallow clone all branches if not cloning branch directly
    command += " --no-single-branch"
  end

  command = command_from_private_key(command) unless self.git_private_key.nil?

  UI.message("Cloning remote git repo...")
  if self.branch && !self.clone_branch_directly
    UI.message("If cloning the repo takes too long, you can use the `clone_branch_directly` option in match.")
  end

  begin
    # GIT_TERMINAL_PROMPT will fail the `git clone` command if user credentials are missing
    Helper.with_env_values('GIT_TERMINAL_PROMPT' => '0') do
      FastlaneCore::CommandExecutor.execute(command: command,
                                          print_all: FastlaneCore::Globals.verbose?,
                                      print_command: FastlaneCore::Globals.verbose?)
    end
  rescue
    UI.error("Error cloning certificates repo, please make sure you have read access to the repository you want to use")
    if self.branch && self.clone_branch_directly
      UI.error("You passed '#{self.branch}' as branch in combination with the `clone_branch_directly` flag. Please remove `clone_branch_directly` flag on the first run for _match_ to create the branch.")
    end
    UI.error("Run the following command manually to make sure you're properly authenticated:")
    UI.command(command)
    UI.user_error!("Error cloning certificates git repo, please make sure you have access to the repository - see instructions above")
  end

  add_user_config(self.git_full_name, self.git_user_email)

  unless File.directory?(self.working_directory)
    UI.user_error!("Error cloning repo, make sure you have access to it '#{self.git_url}'")
  end

  checkout_branch
end

#generate_commit_messageObject

Generate the commit message based on the user’s parameters



158
159
160
161
162
163
164
165
166
# File 'match/lib/match/storage/git_storage.rb', line 158

def generate_commit_message
  [
    "[fastlane]",
    "Updated",
    self.type,
    "and platform",
    self.platform
  ].join(" ")
end

#generate_matchfile_contentObject



168
169
170
171
172
173
# File 'match/lib/match/storage/git_storage.rb', line 168

def generate_matchfile_content
  UI.important("Please create a new, private git repository to store the certificates and profiles there")
  url = UI.input("URL of the Git Repo: ")

  return "git_url(\"#{url}\")"
end

#human_readable_descriptionObject



138
139
140
# File 'match/lib/match/storage/git_storage.rb', line 138

def human_readable_description
  "Git Repo [#{self.git_url}]"
end

#list_files(file_name: "", file_ext: "") ⇒ Object



175
176
177
# File 'match/lib/match/storage/git_storage.rb', line 175

def list_files(file_name: "", file_ext: "")
  Dir[File.join(working_directory, "**", file_name, "*.#{file_ext}")]
end

#prefixed_working_directoryObject



76
77
78
# File 'match/lib/match/storage/git_storage.rb', line 76

def prefixed_working_directory
  return working_directory
end

#upload_files(files_to_upload: [], custom_message: nil) ⇒ Object



149
150
151
152
153
154
155
# File 'match/lib/match/storage/git_storage.rb', line 149

def upload_files(files_to_upload: [], custom_message: nil)
  commands = files_to_upload.map do |current_file|
    "git add #{current_file.shellescape}"
  end

  git_push(commands: commands, commit_message: custom_message)
end