Module: TDUploadService

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

Class Method Summary collapse

Class Method Details

.create_distribution_profile(name:, auth_token:) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 68

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



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 162

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 30

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 122

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



140
141
142
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 140

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 49

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



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
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 89

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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 9

def self.upload_artifact(token:, message:, app:, dist_profile_id:)
  url = "https://api.appcircle.io/distribution/v2/profiles/#{dist_profile_id}/app-versions"
  headers = {
    Authorization: "Bearer #{token}"
  }
  payload = {
    Message: message,
    File: File.new(app, 'rb'),
    multipart: true # Force multipart encoding for RestClient
  }

  begin
    response = RestClient.post(url, payload, headers)
    JSON.parse(response.body) rescue response.body
  rescue RestClient::ExceptionWithResponse => e
    raise e
  rescue StandardError => e
    raise e
  end
end