Module: TDUploadService

Defined in:
lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb

Constant Summary collapse

UI =
FastlaneCore::UI

Class Method Summary collapse

Class Method Details

.create_distribution_profile(name:, auth_token:) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 128

def self.create_distribution_profile(name:, auth_token:)
  url = "#{BASE_URL}/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) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 222

def self.create_profile(authToken, profileName, profileAuthType, profileUsername, profilePassword, profileTestingGroupNames)
  # Get testing group IDs
  if !profileTestingGroupNames&.empty?
    profileTestingGroupIds = TDUploadService.get_testing_group_ids(authToken, profileTestingGroupNames)
  end
  
  # Create
  begin
    new_profile = TDUploadService.create_distribution_profile(
      name: profileName,
      auth_token: authToken
    )
    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.message}."
  end

  # Configure
  begin
    Fastlane::UI.message("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
    )
    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.message}."
  end

  return profileId
end

.get_distribution_profiles(auth_token:) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 90

def self.get_distribution_profiles(auth_token:)
  url = "#{BASE_URL}/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) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 182

def self.get_profile_id(authToken, profileName)
  profileId = nil

  begin
    profiles = TDUploadService.get_distribution_profiles(auth_token: authToken)
    profiles.each do |profile|
      if profile["name"] == profileName
        profileId = profile['id']
        break
      end
    end
  rescue => e
    raise "Something went wrong while fetching profiles: #{e.message}."
  end

  return profileId
end

.get_testing_group_ids(authToken, testingGroupNames) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 200

def self.get_testing_group_ids(authToken, testingGroupNames)
  testingGroupIds = []
  remainingGroupNames = Set.new(testingGroupNames)

  begin
    groups = TDUploadService.get_testing_groups(auth_token: authToken)

    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.message}."
  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:) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 109

def self.get_testing_groups(auth_token:)
  url = "#{BASE_URL}/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

.update_distribution_profile(profile_id:, auth_type:, username:, password:, testing_group_ids:, auth_token:) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 149

def self.update_distribution_profile(profile_id:, auth_type:, username:, password:, testing_group_ids:, auth_token:)
  url = "#{BASE_URL}/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:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
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
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
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 11

def self.upload_artifact(token:, message:, app:, dist_profile_id:)
  file_path = app
  file_name = File.basename(file_path)
  file_size = File.size(file_path)

  upload_info_url = "#{BASE_URL}/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.message("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']

    file_content = File.binread(file_path)
    UI.message("Uploading file to Appcircle...")
    response = RestClient.put(
      upload_url,
      file_content,
      { content_type: 'application/octet-stream' }
    )
    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 = "#{BASE_URL}/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: message
    }.to_json

    commit_headers = {
      Authorization: "Bearer #{token}",
      content_type: :json,
      accept: 'application/json'
    }

    UI.message("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