Class: Fastlane::FirebaseTestLab::FirebaseTestLabService

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/firebase_test_lab/helper/ftl_service.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credential) ⇒ FirebaseTestLabService

Returns a new instance of FirebaseTestLabService.



31
32
33
34
# File 'lib/fastlane/plugin/firebase_test_lab/helper/ftl_service.rb', line 31

def initialize(credential)
  @auth = credential.get_google_credential(TESTLAB_OAUTH_SCOPES)
  @default_bucket = nil
end

Class Method Details

.map_device_to_proto(device) ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/fastlane/plugin/firebase_test_lab/helper/ftl_service.rb', line 196

def self.map_device_to_proto(device)
  {
    iosModelId: device[:ios_model_id],
    iosVersionId: device[:ios_version_id],
    locale: device[:locale],
    orientation: device[:orientation]
  }
end

Instance Method Details

#get_default_bucket(gcp_project) ⇒ Object



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
# File 'lib/fastlane/plugin/firebase_test_lab/helper/ftl_service.rb', line 50

def get_default_bucket(gcp_project)
  return @default_bucket unless @default_bucket.nil?

  init_default_bucket(gcp_project)
  conn = Faraday.new(APIARY_ENDPOINT)
  begin
    resp = conn.get(TOOLRESULTS_GET_SETTINGS_API_V3.gsub("{project}", gcp_project)) do |req|
      req.headers = @auth.apply(req.headers)
      req.options.timeout = 15
      req.options.open_timeout = 5
    end
  rescue Faraday::Error => ex
    UI.abort_with_message!("Network error when obtaining Firebase Test Lab default GCS bucket, " \
      "type: #{ex.class}, message: #{ex.message}")
  end

  if resp.status != 200
    FastlaneCore::UI.error("Failed to obtain default bucket for Firebase Test Lab.")
    summarized_error = ErrorHelper.summarize_google_error(resp.body)
    if summarized_error.include?("Not Authorized for project")
      FastlaneCore::UI.error("Please make sure that the account associated with your Google credential is the " \
                             "project editor or owner. You can do this at the Google Developer Console " \
                             "https://console.cloud.google.com/iam-admin/iam?project=#{gcp_project}")
    end
    FastlaneCore::UI.abort_with_message!(summarized_error)
    return nil
  else
    response_json = JSON.parse(resp.body)
    @default_bucket = response_json["defaultBucket"]
    return @default_bucket
  end
end

#get_execution_steps(gcp_project, history_id, execution_id) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/fastlane/plugin/firebase_test_lab/helper/ftl_service.rb', line 172

def get_execution_steps(gcp_project, history_id, execution_id)
  conn = Faraday.new(APIARY_ENDPOINT)
  url = TOOLRESULTS_LIST_EXECUTION_STEP_API_V3
        .gsub("{project}", gcp_project)
        .gsub("{history_id}", history_id)
        .gsub("{execution_id}", execution_id)
  begin
    resp = conn.get(url) do |req|
      req.headers = @auth.apply(req.headers)
      req.options.timeout = 15
      req.options.open_timeout = 5
    end
  rescue Faraday::Error => ex
    UI.abort_with_message!("Failed to obtain the metadata of test artifacts, " \
      "type: #{ex.class}, message: #{ex.message}")
  end

  if resp.status != 200
    FastlaneCore::UI.error("Failed to obtain the metadata of test artifacts.")
    FastlaneCore::UI.abort_with_message!(ErrorHelper.summarize_google_error(resp.body))
  end
  return JSON.parse(resp.body)
end

#get_matrix_results(gcp_project, matrix_id) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/fastlane/plugin/firebase_test_lab/helper/ftl_service.rb', line 146

def get_matrix_results(gcp_project, matrix_id)
  url = FTL_RESULTS_API
        .gsub("{project}", gcp_project)
        .gsub("{matrix}", matrix_id)

  conn = Faraday.new(FIREBASE_TEST_LAB_ENDPOINT)
  begin
    resp = conn.get(url) do |req|
      req.headers = @auth.apply(req.headers)
      req.options.timeout = 15
      req.options.open_timeout = 5
    end
  rescue Faraday::Error => ex
    UI.abort_with_message!("Network error when attempting to get test results, " \
      "type: #{ex.class}, message: #{ex.message}")
  end

  if resp.status != 200
    FastlaneCore::UI.error("Failed to obtain test results.")
    FastlaneCore::UI.abort_with_message!(ErrorHelper.summarize_google_error(resp.body))
    return nil
  else
    return JSON.parse(resp.body)
  end
end

#init_default_bucket(gcp_project) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fastlane/plugin/firebase_test_lab/helper/ftl_service.rb', line 36

def init_default_bucket(gcp_project)
  conn = Faraday.new(APIARY_ENDPOINT)
  begin
    conn.post(TOOLRESULTS_INITIALIZE_SETTINGS_API_V3.gsub("{project}", gcp_project)) do |req|
      req.headers = @auth.apply(req.headers)
      req.options.timeout = 15
      req.options.open_timeout = 5
    end
  rescue Faraday::Error => ex
    UI.abort_with_message!("Network error when initializing Firebase Test Lab, " \
      "type: #{ex.class}, message: #{ex.message}")
  end
end

#start_job(gcp_project, app_path, result_path, devices, timeout_sec, additional_client_info) ⇒ Object



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
137
138
139
140
141
142
143
144
# File 'lib/fastlane/plugin/firebase_test_lab/helper/ftl_service.rb', line 83

def start_job(gcp_project, app_path, result_path, devices, timeout_sec, additional_client_info)
  if additional_client_info.nil? 
    additional_client_info = { version: VERSION }
  else
    additional_client_info["version"] = VERSION
  end
  additional_client_info = additional_client_info.map { |k,v| { key: k, value: v } }

  body = {
    projectId: gcp_project,
    testSpecification: {
      testTimeout: {
        seconds: timeout_sec
      },
      iosTestSetup: {},
      iosXcTest: {
        testsZip: {
          gcsPath: app_path
        }
      }
    },
    environmentMatrix: {
      iosDeviceList: {
        iosDevices: devices.map(&FirebaseTestLabService.method(:map_device_to_proto))
      }
    },
    resultStorage: {
      googleCloudStorage: {
        gcsPath: result_path
      }
    },
    clientInfo: {
      name: PLUGIN_NAME,
      clientInfoDetails: [
        additional_client_info
      ]
    }
  }

  conn = Faraday.new(FIREBASE_TEST_LAB_ENDPOINT)
  begin
    resp = conn.post(FTL_CREATE_API.gsub("{project}", gcp_project)) do |req|
      req.headers = @auth.apply(req.headers)
      req.headers["Content-Type"] = "application/json"
      req.headers["X-Goog-User-Project"] = gcp_project
      req.body = body.to_json
      req.options.timeout = 15
      req.options.open_timeout = 5
    end
  rescue Faraday::Error => ex
    UI.abort_with_message!("Network error when initializing Firebase Test Lab, " \
      "type: #{ex.class}, message: #{ex.message}")
  end

  if resp.status != 200
    FastlaneCore::UI.error("Failed to start Firebase Test Lab jobs.")
    FastlaneCore::UI.abort_with_message!(ErrorHelper.summarize_google_error(resp.body))
  else
    response_json = JSON.parse(resp.body)
    return response_json["testMatrixId"]
  end
end