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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDUploadService.rb', line 52

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

.get_distribution_profiles(auth_token:) ⇒ Object



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

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)
    parsed_response = JSON.parse(response.body)

    parsed_response
  rescue RestClient::ExceptionWithResponse => e
    raise e
  rescue StandardError => e
    raise e
  end
end

.get_profile_id(authToken, profileName, createProfileIfNotExists) ⇒ Object



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

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

  begin
    profiles = TDUploadService.get_distribution_profiles(auth_token: authToken)
    profiles.each do |profile|
      if profile["name"] == profileName
        profileId = profile['id']
      end
    end
  rescue => e
    raise "Something went wrong while fetching profiles: #{e.message}"
  end
    
  if profileId.nil? && !createProfileIfNotExists
    raise "Error: The test profile '#{profileName}' could not be found. The option 'createProfileIfNotExists' is set to false, so no new profile was created. To automatically create a new profile if it doesn't exist, set 'createProfileIfNotExists' to true."
  end

  if profileId.nil? && createProfileIfNotExists
    begin
      puts "The test profile '#{profileName}' could not be found. A new profile is being created..."
      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
  end

  return profileId
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
29
# 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}",
    Message: message,
    content_type: :multipart
  }
  payload = {
    Message: message,
    File: File.new(app, 'rb')
  }

  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