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

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

Instance Method Summary collapse

Instance Method Details

#create_app(pipeline_name, org, repository, branch) ⇒ Object



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
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
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/heroku/review/apps/manager/cli.rb', line 80

def create_app(pipeline_name, org, repository, branch)
  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"])

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

  github_archive_link = octokit.archive_link("#{org}/#{repository}", ref: branch)

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

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

  say "Review app already exists." and return unless app.nil?

  begin
    review_app = platform_api.review_app.create(
      branch: branch,
      pipeline: pipeline_id,
      source_blob: { url: github_archive_link, version: "v1.0.0" },
      pr_number: pull_request[:number]
    )
  rescue Excon::Error::Conflict
    say "Review app already exists." and return
  end

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

    say "Status: #{review_app["status"]}"

    if %w[created errored].include?(review_app["status"])
      last_status = review_app["status"]
      break
    end

    sleep 30
  end

  say "Review app was changed to errored status." and return if last_status == "errored"

  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"],
      db: {
        host: uri.host,
        port: uri.port,
        name: uri.path[1..],
        user: uri.user,
        password: uri.password
      }
    }
    say result.to_json
  else
    print_table([
                  ["URL", "DB Host", "DB Port", "DB Name", "DB User",
                   "DB Password", "DB Scheme"],
                  [app_info["web_url"], uri.host, uri.port, uri.path[1..], uri.user, uri.password,
                   uri.scheme]
                ])

  end
end

#delete_app(pipeline_name, branch) ⇒ Object



45
46
47
48
49
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
# File 'lib/heroku/review/apps/manager/cli.rb', line 45

def delete_app(pipeline_name, branch)
  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 "Pipleline does not exists." and return
  end

  begin
    apps = platform_api.review_app.list(pipeline["id"])
  rescue Excon::Error::NotFound
    say "Review app not exists." and return
  end

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

  say "Review app not exists." and return if app.nil?

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

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

#list_app(pipeline_name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/heroku/review/apps/manager/cli.rb', line 18

def list_app(pipeline_name)
  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 "Pipleline does not exists." and return
  end

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

  if options[:json]
    say result.to_json
  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