Class: Heroku::Review::Apps::Manager::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/heroku/review/apps/manager/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/heroku/review/apps/manager/cli.rb', line 18

def exit_on_failure?
  true
end

Instance Method Details

#create_appObject



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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/heroku/review/apps/manager/cli.rb', line 156

def create_app
  Whirly.configure(spinner: "dots", stream: $stderr)

  platform_api = PlatformAPI.connect_oauth(ENV["HEROKU_REVIEW_APPS_MANAGER_HEROKU_API_KEY"])
  octokit = Octokit::Client.new(access_token: ENV["HEROKU_REVIEW_APPS_MANAGER_GITHUB_TOKEN"])
  branch = options[:branch]
  pipeline_name = options[:pipeline]
  repository = options[:repository]
  org = repository.split("/").first

  pipeline = platform_api.pipeline.info(pipeline_name)
  pipeline_id = pipeline["id"]

  pull_requests = octokit.pull_requests(
    repository,
    state: "open",
    head: "#{org}:#{branch}"
  )
  pull_request = pull_requests.first

  say_error "Pull Request does not exist.", Thor::Shell::Color::YELLOW and return if pull_request.nil?

  apps = platform_api.review_app.list(pipeline_id)
  app = apps.filter { |app| app["branch"] == branch }.first

  say_error "Review app already exists.", Thor::Shell::Color::YELLOW and return unless app.nil?

  github_archive_link = octokit.archive_link(repository, ref: branch)

  begin
    review_app = platform_api.review_app.create(
      branch: branch,
      pipeline: pipeline_id,
      source_blob: { url: github_archive_link, version: pull_request[:head][:sha] },
      pr_number: pull_request[:number]
    )
  rescue Excon::Error::Conflict
    say_error "Review app already exists.", Thor::Shell::Color::YELLOW and return
  end

  last_status = nil
  Whirly.start
  begin
    loop do
      review_app = platform_api.review_app.get_review_app(review_app["id"])

      if %w[created errored].include?(review_app["status"])
        last_status = review_app["status"]
        Whirly.status = "Status: #{review_app["status"]}"
        break
      else
        Whirly.status = "Status: #{review_app["status"]}"
      end

      sleep 30
    end
  ensure
    Whirly.stop
  end

  if last_status == "errored"
    say_error "Review app was changed to errored status.",
              Thor::Shell::Color::RED and return
  end

  app_id = review_app["app"]["id"]
  app_info = platform_api.app.info(app_id)

  config_vars = platform_api.config_var.info_for_app(app_id)
  database_url = config_vars["DATABASE_URL"]
  uri = URI.parse(database_url)

  if options[:json]
    result = {
      url: app_info["web_url"],
      name: app_info["name"],
      db: {
        url: database_url,
        host: uri.host,
        port: uri.port,
        name: uri.path[1..],
        user: uri.user,
        password: uri.password
      }
    }
    output_as_json(result)
  else
    print_table([
                  ["URL", "Name", "DB URL", "DB Host", "DB Port", "DB Name", "DB User",
                   "DB Password", "DB Scheme"],
                  [
                    app_info["web_url"], app_info["name"], database_url,
                    uri.host, uri.port, uri.path[1..], uri.user,
                    uri.password, uri.scheme
                  ]
                ])

  end
end

#delete_appObject



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
145
146
147
148
# File 'lib/heroku/review/apps/manager/cli.rb', line 115

def delete_app
  branch = options[:branch]
  pipeline_name = options[:pipeline]
  platform_api = PlatformAPI.connect_oauth(ENV["HEROKU_REVIEW_APPS_MANAGER_HEROKU_API_KEY"])

  begin
    pipeline = platform_api.pipeline.info(pipeline_name)
  rescue Excon::Error::NotFound
    say_error "Pipleline does not exists.", Thor::Shell::Color::RED and return
  end

  begin
    apps = platform_api.review_app.list(pipeline["id"])
  rescue Excon::Error::NotFound
    say_error "Review app not exists.", Thor::Shell::Color::RED and return
  end

  app = apps.filter { |app| app["branch"] == branch }.first

  say_error "Review app not exists.", Thor::Shell::Color::RED and return if app.nil?

  result = platform_api.review_app.delete(app["id"])

  if options[:json]
    output_as_json(result)
  else
    headers = %w[ID PR Branch]
    body = [result["id"], "##{result["pr_number"]}", result["branch"]]
    print_table([
                  headers,
                  body
                ])
  end
end

#list_appObject



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
# File 'lib/heroku/review/apps/manager/cli.rb', line 26

def list_app
  pipeline_name = options[:pipeline]
  platform_api = PlatformAPI.connect_oauth(ENV["HEROKU_REVIEW_APPS_MANAGER_HEROKU_API_KEY"])

  begin
    pipeline = platform_api.pipeline.info(pipeline_name)
  rescue Excon::Error::NotFound
    say_error "Pipleline does not exists.", Thor::Shell::Color::RED and return
  end

  result = platform_api.review_app.list(pipeline["id"])

  if options[:json]
    output_as_json(result)
  else
    headers = %w[ID PR Branch Status]
    body = result.map do |app|
      [app["id"], "##{app["pr_number"]}", app["branch"], app["status"]]
    end
    print_table([
                  headers,
                  *body
                ])
  end
end

#list_formationObject



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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/heroku/review/apps/manager/cli.rb', line 260

def list_formation
  branch = options[:branch]
  pipeline_name = options[:pipeline]
  platform_api = PlatformAPI.connect_oauth(ENV["HEROKU_REVIEW_APPS_MANAGER_HEROKU_API_KEY"])

  begin
    pipeline = platform_api.pipeline.info(pipeline_name)
  rescue Excon::Error::NotFound
    say_error "Pipleline does not exists.", Thor::Shell::Color::RED and return
  end

  begin
    apps = platform_api.review_app.list(pipeline["id"])
  rescue Excon::Error::NotFound
    say_error "Review app not exists.", Thor::Shell::Color::RED and return
  end

  app = apps.filter { |app| app["branch"] == branch }.first
  say_error "Review app not exists.", Thor::Shell::Color::RED and return if app.nil?

  app_id = app.dig("app", "id")
  say_error "Review app not exists.", Thor::Shell::Color::RED and return if app_id.nil?

  begin
    formations = platform_api.formation.list(app_id)
  rescue Excon::Error::NotFound
    say_error "Formation not exists.", Thor::Shell::Color::RED and return
  end

  if options[:json]
    output_as_json(formations)
  else
    headers = %w[ID Type Size Quantity State]
    body = formations.map do |formation|
      [
        formation["id"],
        formation["type"],
        formation["size"],
        formation["quantity"],
        formation["state"]
      ]
    end
    print_table([
                  headers,
                  *body
                ])
  end
end

#show_appObject



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/heroku/review/apps/manager/cli.rb', line 56

def show_app
  branch = options[:branch]
  pipeline_name = options[:pipeline]
  platform_api = PlatformAPI.connect_oauth(ENV["HEROKU_REVIEW_APPS_MANAGER_HEROKU_API_KEY"])

  begin
    pipeline = platform_api.pipeline.info(pipeline_name)
  rescue Excon::Error::NotFound
    say_error "Pipleline does not exists.", Thor::Shell::Color::RED and return
  end

  begin
    apps = platform_api.review_app.list(pipeline["id"])
  rescue Excon::Error::NotFound
    say_error "Review app not exists.", Thor::Shell::Color::RED and return
  end

  app = apps.filter { |app| app["branch"] == branch }.first

  say_error "Review app not exists.", Thor::Shell::Color::RED and return if app.nil?

  app_id = app["app"]["id"]
  app_info = platform_api.app.info(app_id)

  config_vars = platform_api.config_var.info_for_app(app_id)
  database_url = config_vars["DATABASE_URL"]
  uri = URI.parse(database_url)

  if options[:json]
    result = {
      url: app_info["web_url"],
      name: app_info["name"],
      db: {
        url: database_url,
        host: uri.host,
        port: uri.port,
        name: uri.path[1..],
        user: uri.user,
        password: uri.password
      }
    }
    output_as_json(result)
  else
    print_table([
                  ["URL", "Name", "DB URL", "DB Host", "DB Port", "DB Name", "DB User",
                   "DB Password", "DB Scheme"],
                  [
                    app_info["web_url"], app_info["name"], database_url,
                    uri.host, uri.port, uri.path[1..], uri.user,
                    uri.password, uri.scheme
                  ]
                ])
  end
end

#update_formationObject



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/heroku/review/apps/manager/cli.rb', line 315

def update_formation
  branch = options[:branch]
  quantity = options[:quantity]
  pipeline_name = options[:pipeline]
  platform_api = PlatformAPI.connect_oauth(ENV["HEROKU_REVIEW_APPS_MANAGER_HEROKU_API_KEY"])

  begin
    pipeline = platform_api.pipeline.info(pipeline_name)
  rescue Excon::Error::NotFound
    say_error "Pipleline does not exists.", Thor::Shell::Color::RED and return
  end

  begin
    apps = platform_api.review_app.list(pipeline["id"])
  rescue Excon::Error::NotFound
    say_error "Review app not exists.", Thor::Shell::Color::RED and return
  end

  app = apps.filter { |app| app["branch"] == branch }.first
  say_error "Review app not exists.", Thor::Shell::Color::RED and return if app.nil?

  app_id = app.dig("app", "id")
  say_error "Review app not exists.", Thor::Shell::Color::RED and return if app_id.nil?

  begin
    formation = platform_api.formation.update(app_id, options[:formation_type], { quantity: quantity.to_i })
  rescue Excon::Error::NotFound
    say_error "Formation not exists.", Thor::Shell::Color::RED and return
  end

  if options[:json]
    output_as_json(formation)
  else
    headers = %w[ID Type Size Quantity State]
    body = [
      formation["id"],
      formation["type"],
      formation["size"],
      formation["quantity"],
      formation["state"]
    ]
    print_table([
                  headers,
                  body
                ])
  end
end