Class: Fastlane::Helper::ShuttleHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb

Class Method Summary collapse

Class Method Details

.abort(endpoint, body, response) ⇒ Object



184
185
186
187
188
189
190
191
192
193
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 184

def self.abort(endpoint, body, response)
  reqBody = JSON.parse body
  errorBody = JSON.parse response.body
  case endpoint
  when "/releases"
    UI.abort_with_message!("💥 Can't create release for #{reqBody["data"]["attributes"]["title"]}: #{errorBody["errors"][0]["detail"]}")
  else
    UI.abort_with_message!("Error #{response.status.to_s} occured while calling endpoint #{endpoint} with body #{body} => #{errorBody["errors"][0]["detail"]}")
  end
end

.app_from_json(json_app) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 118

def self.app_from_json(json_app)
  json_app_attrb = json_app["attributes"]
  ShuttleApp.new(
    json_app["id"],
    json_app_attrb["name"],
    json_app_attrb["platform_id"],
    json_app_attrb["path"]
  )
end

.connection(shuttle_instance, endpoint, is_multipart = false) ⇒ Object



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

def self.connection(shuttle_instance, endpoint, is_multipart = false)
  return Faraday.new(url: "#{shuttle_instance.base_url}/api#{endpoint}") do |builder|
    # builder.response :logger, Logger.new(STDOUT), bodies: true
    builder.headers["Authorization"] = "Bearer #{shuttle_instance.access_token}"
    builder.headers["Accept"] = "application/vnd.api+json"
    builder.options.timeout = 120
    if is_multipart
      builder.request :multipart
    else
      builder.headers["Content-Type"] = "application/vnd.api+json"
    end
    builder.adapter :net_http
  end
end

.create_app(shuttle_instance, app_name, app_platform) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 139

def self.create_app(shuttle_instance, app_name, app_platform)
  app_path = "#{app_name.downcase}-#{app_platform}"
  body = JSON.generate({
    data: {
      type: "apps",
      attributes: {
        name: app_name,
        path: app_path,
        platform_id: app_platform
      }
    }
  })
  json_app = self.post(shuttle_instance, "/apps", body)
  self.app_from_json(json_app)
end

.create_environment(shuttle_instance, name, versioning_id, app_id, package_id) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 94

def self.create_environment(shuttle_instance, name, versioning_id, app_id, package_id)
  body = JSON.generate({
    data: {
      type: "environments",
      attributes: {
        name: name,
        path: name.downcase,
        package_id: package_id,
        versioning_id: versioning_id
      },
      relationships: {
        app: {
          data: {
            id: app_id,
            type: "apps"
          }
        }
      }
    }
  })
  json_env = self.post(shuttle_instance, "/environments", body)
  self.environment_from_json(json_env)
end

.create_release(shuttle_instance, release) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 204

def self.create_release(shuttle_instance, release)
  body = JSON.generate({
    data: {
      type: "releases",
      attributes: {
        title: release.name,
        notes: release.notes,
        commit_id: release.commit_id
      },
      relationships: {
        build: {
          data: {
            id: release.build.id,
            type: "builds"
          }
        },
        environment: {
          data: {
            id: release.environment.id,
            type: "environments"
          }
        }
      }
    }
  })
  json_release = self.post(shuttle_instance, "/releases", body)
end

.download_url(shuttle_instance, app_environment, package_info) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 244

def self.download_url(shuttle_instance, app_environment, package_info)
  app = app_environment.shuttle_app
  env = app_environment.shuttle_environment
  url_path = File.join(
                app.path, 
                env.path,
                package_info.release_version)
  url_path = File.join(url_path, package_info.build_version) if env.versioning_id == "version_and_build"
  return URI.join(
    shuttle_instance.base_url, url_path).to_s
end

.environment_from_json(json_env) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 65

def self.environment_from_json(json_env)
  attrb = json_env["attributes"]
  ShuttleEnvironment.new(
    json_env["id"],
    attrb["name"],
    attrb["package_id"],
    json_env["relationships"]["app"]["data"]["id"],
    attrb["versioning_id"],
    attrb["path"]
  )
end

.get(shuttle_instance, endpoint, debug = false) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 51

def self.get(shuttle_instance, endpoint, debug=false) 
  connection = self.connection(shuttle_instance, endpoint)
  response = connection.get()
  case response.status
  when 200...300
    data = JSON.parse(response.body)
    UI.message("Debug: #{JSON.pretty_generate(data["data"])}\n") if debug == true
    data["data"]
  else 
    UI.abort_with_message!("Error #{response.status.to_s} occured while calling endpoint #{endpoint}")
    nil
  end
end

.get_app(shuttle_instance, app_id) ⇒ Object



134
135
136
137
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 134

def self.get_app(shuttle_instance, app_id)
  json_app = self.get(shuttle_instance, "/apps/#{app_id}")
  self.app_from_json(json_app)
end

.get_app_environments(shuttle_instance, environments) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 155

def self.get_app_environments(shuttle_instance, environments)
  apps = environments.map do |env| 
    self.get_app(shuttle_instance, env.app_id)
  end

  apps.zip(environments).map do |app_env| 
    AppEnvironment.new(
      app_env[0],
      app_env[1]
    )
  end
end

.get_app_info(params) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 19

def self.get_app_info(params)
  package_path = params[:package_path] unless params[:package_path].to_s.empty?
  package_path = Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::IPA_OUTPUT_PATH] if package_path.to_s.empty?
  package_path = Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::GRADLE_APK_OUTPUT_PATH] if package_path.to_s.empty?
  UI.abort_with_message!("No Package file found") if package_path.to_s.empty?
  UI.abort_with_message!("Package at path #{package_path} does not exist") unless File.exist?(package_path)
  app_info = ::AppInfo.parse(package_path)
  PackageInfo.new(app_info.identifier, app_info.name, package_path, app_info.os.downcase, app_info.release_version, app_info.build_version)
end

.get_apps(shuttle_instance) ⇒ Object



128
129
130
131
132
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 128

def self.get_apps(shuttle_instance)
  self.get(shuttle_instance, "/apps/").map do |json_app|
    self.app_from_json(json_app)
  end
end

.get_environment(shuttle_instance, env_id) ⇒ Object



83
84
85
86
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 83

def self.get_environment(shuttle_instance, env_id)
  json_env = self.get(shuttle_instance, "/environments/#{env_id}")
  self.environment_from_json(json_env)
end

.get_environments(shuttle_instance) ⇒ Object



77
78
79
80
81
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 77

def self.get_environments(shuttle_instance)
  self.get(shuttle_instance, '/environments').map do |json_env|
    self.environment_from_json(json_env)
  end
end

.get_environments_for_app(shuttle_instance, app) ⇒ Object



88
89
90
91
92
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 88

def self.get_environments_for_app(shuttle_instance, app)
  self.get(shuttle_instance, "/apps/#{app.id}/environments").map do |json_env|
    self.environment_from_json(json_env)
  end
end

.get_release_info(params, app_environment, package_info) ⇒ Object



29
30
31
32
33
34
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 29

def self.get_release_info(params, app_environment, package_info) 
  name = params[:release_name]
  notes = params[:release_notes]
  commit_id = Helper.backticks("git show --format='%H' --quiet").chomp
  ReleaseInfo.new(name, notes, nil, app_environment.shuttle_environment, commit_id)
end

.get_shuttle_instance(params) ⇒ Object

class methods that you define here become available in your action as ‘Helper::ShuttleHelper.your_method`



13
14
15
16
17
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 13

def self.get_shuttle_instance(params) 
  shuttle_base_url = params[:base_url]
  shuttle_access_token = params[:access_token]
  ShuttleInstance.new(shuttle_base_url, shuttle_access_token)
end

.post(shuttle_instance, endpoint, body, is_multipart: false, debug: false) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 168

def self.post(shuttle_instance, endpoint, body, is_multipart: false, debug: false)
  connection = self.connection(shuttle_instance, endpoint, is_multipart)
  response = connection.post do |req|
    req.body = body
  end
  case response.status
  when 200...300
    data = JSON.parse response.body
    UI.message(JSON.pretty_generate(data)) if debug == true
    data["data"]
  else
    self.abort(endpoint, body, response)
    nil
  end
end


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
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 256

def self.print_summary_table(shuttle_instance, app_environment, package_info, release)
  rows = [
    'Shuttle Base URL', 
    'Shuttle app name', 
    'Shuttle env name', 
    'Shuttle env ID', 
    'Package path', 
    'Platform', 
    'Package Id',
    'Release name',
    'Release version',
    'Build version',
    'Release notes',
    'Commit hash',
    'Shuttle release URL'
  ].zip([
      shuttle_instance.base_url, 
      app_environment.shuttle_app.name,
      app_environment.shuttle_environment.name, 
      app_environment.shuttle_environment.id, 
      package_info.path, 
      package_info.platform_id, 
      package_info.id,
      release.name,
      package_info.release_version,
      package_info.build_version,
      release.notes,
      release.commit_id,
      self.download_url(shuttle_instance, app_environment, package_info)
  ])
  table = Terminal::Table.new :rows => rows, :title => "Shuttle upload info summary".green
  puts
  puts table
  puts
end

.prompt_choices(question, options, nonInteractiveErrorMessage) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 232

def self.prompt_choices(question, options, nonInteractiveErrorMessage) 
  UI.abort_with_message!(nonInteractiveErrorMessage) unless UI.interactive?
    abort_option = "None match, abort"
    user_choice = UI.select question, options << abort_option
    case user_choice
    when abort_option
      UI.user_error!("Aborting…")
    else
      choice_index = options.find_index(user_choice)
    end
end

.upload_build(shuttle_instance, package_info, app_id) ⇒ Object



195
196
197
198
199
200
201
202
# File 'lib/fastlane/plugin/shuttle/helper/shuttle_helper.rb', line 195

def self.upload_build(shuttle_instance, package_info, app_id)
  body = {
    "build[app_id]": app_id,
    "build[package]": Faraday::UploadIO.new(package_info.path, 'application/octet-stream')
  }
  json_build = self.post(shuttle_instance, '/builds', body, is_multipart: true)
  ShuttleBuild.new(json_build["id"])
end