Module: TDUploadService
- Defined in:
- lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb
Constant Summary collapse
- UI =
FastlaneCore::UI
Class Method Summary collapse
- .create_distribution_profile(name:, auth_token:, api_endpoint: BASE_URL) ⇒ Object
- .create_profile(authToken, profileName, profileAuthType, profileUsername, profilePassword, profileTestingGroupNames, api_endpoint = BASE_URL) ⇒ Object
- .get_distribution_profiles(auth_token:, api_endpoint: BASE_URL) ⇒ Object
- .get_profile_id(authToken, profileName, api_endpoint = BASE_URL) ⇒ Object
- .get_testing_group_ids(authToken, testingGroupNames, api_endpoint = BASE_URL) ⇒ Object
- .get_testing_groups(auth_token:, api_endpoint: BASE_URL) ⇒ Object
- .put_with_retry(url, body, headers, max_retries: 5) ⇒ Object
- .update_distribution_profile(profile_id:, auth_type:, username:, password:, testing_group_ids:, auth_token:, api_endpoint: BASE_URL) ⇒ Object
- .upload_artifact(token:, message:, app:, dist_profile_id:, api_endpoint: BASE_URL) ⇒ Object
Class Method Details
.create_distribution_profile(name:, auth_token:, api_endpoint: BASE_URL) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 162 def self.create_distribution_profile(name:, auth_token:, api_endpoint: BASE_URL) url = "#{api_endpoint}/distribution/v2/profiles" headers = { Authorization: "Bearer #{auth_token}", content_type: :json, accept: 'application/json' } payload = { name: name }.to_json begin response = RestClient.post(url, payload, headers) JSON.parse(response.body) rescue RestClient::ExceptionWithResponse => e raise e rescue StandardError => e raise e end end |
.create_profile(authToken, profileName, profileAuthType, profileUsername, profilePassword, profileTestingGroupNames, api_endpoint = BASE_URL) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 256 def self.create_profile(authToken, profileName, profileAuthType, profileUsername, profilePassword, profileTestingGroupNames, api_endpoint = BASE_URL) # Get testing group IDs if !profileTestingGroupNames&.empty? profileTestingGroupIds = TDUploadService.get_testing_group_ids(authToken, profileTestingGroupNames, api_endpoint) end # Create begin new_profile = TDUploadService.create_distribution_profile( name: profileName, auth_token: authToken, api_endpoint: api_endpoint ) if new_profile.nil? raise "Error: The new profile could not be created." end profileId = new_profile['id'] rescue => e raise "Something went wrong while creating a new profile: #{e.}." end # Configure begin Fastlane::UI.("Configuring the profile...") configured_profile = TDUploadService.update_distribution_profile( profile_id: profileId, auth_type: profileAuthType, username: profileUsername, password: profilePassword, testing_group_ids: profileTestingGroupIds, auth_token: authToken, api_endpoint: api_endpoint ) if configured_profile.nil? raise "Error: The new profile could not be configured." end profileId = configured_profile['id'] # Should be the same as before rescue => e raise "Something went wrong while configuring the new profile: #{e.}." end return profileId end |
.get_distribution_profiles(auth_token:, api_endpoint: BASE_URL) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 124 def self.get_distribution_profiles(auth_token:, api_endpoint: BASE_URL) url = "#{api_endpoint}/distribution/v2/profiles" # Set up the headers with authentication headers = { Authorization: "Bearer #{auth_token}", accept: 'application/json' } begin response = RestClient.get(url, headers) JSON.parse(response.body) rescue RestClient::ExceptionWithResponse => e raise e rescue StandardError => e raise e end end |
.get_profile_id(authToken, profileName, api_endpoint = BASE_URL) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 216 def self.get_profile_id(authToken, profileName, api_endpoint = BASE_URL) profileId = nil begin profiles = TDUploadService.get_distribution_profiles(auth_token: authToken, api_endpoint: api_endpoint) profiles.each do |profile| if profile["name"] == profileName profileId = profile['id'] break end end rescue => e raise "Something went wrong while fetching profiles: #{e.}." end return profileId end |
.get_testing_group_ids(authToken, testingGroupNames, api_endpoint = BASE_URL) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 234 def self.get_testing_group_ids(authToken, testingGroupNames, api_endpoint = BASE_URL) testingGroupIds = [] remainingGroupNames = Set.new(testingGroupNames) begin groups = TDUploadService.get_testing_groups(auth_token: authToken, api_endpoint: api_endpoint) groups.each do |group| if remainingGroupNames.include?(group["name"]) testingGroupIds.push(group['id']) remainingGroupNames.delete(group["name"]) end end rescue => e raise "Something went wrong while fetching testing groups: #{e.}." end raise "Following testing groups couldn't be found: '#{remainingGroupNames.to_a.join(', ')}'. Aborting profile creation..." unless remainingGroupNames.empty? return testingGroupIds end |
.get_testing_groups(auth_token:, api_endpoint: BASE_URL) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 143 def self.get_testing_groups(auth_token:, api_endpoint: BASE_URL) url = "#{api_endpoint}/distribution/v2/testing-groups" # Set up the headers with authentication headers = { Authorization: "Bearer #{auth_token}", accept: 'application/json' } begin response = RestClient.get(url, headers) JSON.parse(response.body) rescue RestClient::ExceptionWithResponse => e raise e rescue StandardError => e raise e end end |
.put_with_retry(url, body, headers, max_retries: 5) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 11 def self.put_with_retry(url, body, headers, max_retries: 5) attempt = 0 delay = 1.0 begin RestClient.put(url, body, headers) rescue => e status = e.respond_to?(:http_code) ? e.http_code : nil retryable = status == 503 || e.is_a?(RestClient::ServerBrokeConnection) || e.is_a?(Errno::ECONNRESET) || (defined?(RestClient::Exceptions::OpenTimeout) && e.is_a?(RestClient::Exceptions::OpenTimeout)) || (defined?(RestClient::Exceptions::ReadTimeout) && e.is_a?(RestClient::Exceptions::ReadTimeout)) raise e if !retryable || attempt >= max_retries attempt += 1 sleep(delay + rand(0.3)) delay *= 2 retry end end |
.update_distribution_profile(profile_id:, auth_type:, username:, password:, testing_group_ids:, auth_token:, api_endpoint: BASE_URL) ⇒ Object
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 |
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 183 def self.update_distribution_profile(profile_id:, auth_type:, username:, password:, testing_group_ids:, auth_token:, api_endpoint: BASE_URL) url = "#{api_endpoint}/distribution/v2/profiles/#{profile_id}" headers = { Authorization: "Bearer #{auth_token}", content_type: :json, accept: 'application/json-patch+json' } ### Construct the payload payload = {} settings_payload = { authenticationType: auth_type, username: username, password: password }.compact payload[:settings] = settings_payload unless settings_payload.empty? payload[:testingGroupIds] = testing_group_ids unless testing_group_ids&.empty? payload = payload.compact.to_json ### begin response = RestClient.patch(url, payload, headers) JSON.parse(response.body) rescue RestClient::ExceptionWithResponse => e raise e rescue StandardError => e raise e end end |
.upload_artifact(token:, message:, app:, dist_profile_id:, api_endpoint: BASE_URL) ⇒ Object
34 35 36 37 38 39 40 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 67 68 69 70 71 72 73 74 75 76 77 78 79 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 |
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 34 def self.upload_artifact(token:, message:, app:, dist_profile_id:, api_endpoint: BASE_URL) file_path = app file_name = File.basename(file_path) file_size = File.size(file_path) upload_info_url = "#{api_endpoint}/distribution/v1/profiles/#{dist_profile_id}/app-versions" headers = { Authorization: "Bearer #{token}", accept: 'application/json' } uri = URI(upload_info_url) uri.query = URI.encode_www_form({ action: 'uploadInformation', fileName: file_name, fileSize: file_size }) begin UI.("Getting file upload information...") response = RestClient.get(uri.to_s, headers) upload_info = JSON.parse(response.body) if response.code.between?(200, 299) UI.success("File upload information retrieved successfully with status code: #{response.code}") else UI.error("Failed to retrieve file upload information with status code: #{response.code}") raise "Failed to retrieve file upload information." end file_id = upload_info['fileId'] upload_url = upload_info['uploadUrl'] configuration = upload_info['configuration'] http_method = (configuration && configuration['httpMethod']) ? configuration['httpMethod'].to_s.upcase : 'PUT' UI.("Uploading file to Appcircle...") if http_method == 'POST' sign_parameters = configuration['signParameters'] || {} payload = {} sign_parameters.each { |key, value| payload[key] = value } payload['file'] = File.new(file_path, 'rb') response = RestClient.post(upload_url, payload) else file_content = File.binread(file_path) response = put_with_retry( upload_url, file_content, { content_type: 'application/octet-stream' } ) end if response.code.between?(200, 299) UI.success("File upload finished successfully with status code: #{response.code}") else UI.error("File upload failed with status code: #{response.code}") raise "File upload failed." end commit_url = "#{api_endpoint}/distribution/v1/profiles/#{dist_profile_id}/app-versions" uri = URI(commit_url) uri.query = URI.encode_www_form({ action: 'commitFileUpload' }) commit_payload = { fileId: file_id, fileName: file_name, message: }.to_json commit_headers = { Authorization: "Bearer #{token}", content_type: :json, accept: 'application/json' } UI.("Committing file upload...") commit_response = RestClient.post(uri.to_s, commit_payload, commit_headers) if commit_response.code.between?(200, 299) result = JSON.parse(commit_response.body) UI.success("Commit successful with status code: #{commit_response.code}") else UI.error("Commit failed with status code: #{commit_response.code}") raise "Commit failed with status code: #{commit_response.code}" end return result rescue RestClient::ExceptionWithResponse => e raise e rescue StandardError => e raise e end end |