Module: ZendeskAppsTools::Deploy

Includes:
APIConnection, Common
Included in:
CommandHelpers
Defined in:
lib/zendesk_apps_tools/deploy.rb

Constant Summary

Constants included from APIConnection

APIConnection::DEFAULT_URL_TEMPLATE, APIConnection::EMAIL_ERROR_MSG, APIConnection::EMAIL_REGEX, APIConnection::PROMPT_FOR_URL, APIConnection::SUBDOMAIN_VALIDATION_PATTERN, APIConnection::URL_ERROR_MSG, APIConnection::ZENDESK_URL_VALIDATION_PATTERN

Instance Method Summary collapse

Methods included from APIConnection

#get_connection, #prepare_api_auth

Methods included from Common

#get_password_from_stdin, #get_value_from_stdin, included, #json_or_die, #say_error, #say_error_and_exit

Instance Method Details

#app_exists?(app_id) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
# File 'lib/zendesk_apps_tools/deploy.rb', line 26

def app_exists?(app_id)
  url = "/api/v2/apps/#{app_id}.json"
  response = cached_connection.send(:get) do |req|
    req.url url
  end

  %w(200 201 202).include? response.status.to_s
end

#check_job(job_id) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/zendesk_apps_tools/deploy.rb', line 103

def check_job(job_id)
  loop do
    request  = cached_connection.get("/api/v2/apps/job_statuses/#{job_id}")
    response = json_or_die(request.body)
    status   = response['status']

    if %w(completed failed).include? status
      case status
      when 'completed'
        cache.save zat_contents(response)
        say_status @command, 'OK'
      when 'failed'
        say_status @command, response['message'], :red
        exit 1
      end
      break
    end

    say_status 'Status', status
    sleep 3
  end
rescue Faraday::Error::ClientError => e
  say_error_and_exit e.message
end

#check_status(response, poll_job = true) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/zendesk_apps_tools/deploy.rb', line 92

def check_status(response, poll_job = true)
  job_response = json_or_die(response.body)

  say_error_and_exit job_response['error'] if job_response['error']

  if poll_job
    job_id = job_response['job_id'] || job_response['pending_job_id']
    check_job job_id
  end
end

#deploy_app(connection_method, url, body) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/zendesk_apps_tools/deploy.rb', line 10

def deploy_app(connection_method, url, body)
  body[:upload_id] = upload(options[:path]).to_s
  sleep 2 # Because the DB needs time to replicate

  response = cached_connection.send(connection_method) do |req|
    req.url url
    req.headers[:content_type] = 'application/json'
    req.body = JSON.generate body
  end

  check_status response

rescue Faraday::Error::ClientError, JSON::ParserError => e
  say_error_and_exit e.message
end

#find_app_id(product_name = 'v2') ⇒ Object

use the v2 endpoint if no product name is provided



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
# File 'lib/zendesk_apps_tools/deploy.rb', line 65

def find_app_id(product_name = 'v2') # use the v2 endpoint if no product name is provided
  say_status 'Update', 'app ID is missing, searching...'
  app_name = get_value_from_stdin('Enter the name of the app:')

  response = cached_connection.get("/api/#{product_name}/apps/owned.json")
  owned_apps_json = json_or_die(response.body)

  unless response.success? && owned_apps_json.has_key?('apps')
    say_error_and_exit "Unable to retrieve apps. Please check your credentials and internet connection."
  else
    app = owned_apps_json['apps'].find {
      |app| app['name'] == app_name
    }
  end

  unless app
    say_error_and_exit "App not found. Please check that your app name is correct."
  end

  app_id = app['id']
  cache.save 'app_id' => app_id
  app_id

rescue Faraday::Error::ClientError => e
  say_error_and_exit e.message
end

#install_app(poll_job, product_name, installation) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/zendesk_apps_tools/deploy.rb', line 35

def install_app(poll_job, product_name, installation)
  response = cached_connection.post do |req|
    req.url "api/#{product_name}/apps/installations.json"
    req.headers[:content_type] = 'application/json'
    req.body = JSON.generate(installation)
  end
  check_status(response, poll_job)
end

#upload(path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/zendesk_apps_tools/deploy.rb', line 44

def upload(path)
  zipfile_path = options[:zipfile]

  if zipfile_path
    package_path = zipfile_path
  else
    package
    package_path = Dir[File.join path, '/tmp/*.zip'].sort.last
  end

  payload = {
    uploaded_data: Faraday::UploadIO.new(package_path, 'application/zip')
  }

  response = cached_connection(:multipart).post('/api/v2/apps/uploads.json', payload)
  json_or_die(response.body)['id']

rescue Faraday::Error::ClientError => e
  say_error_and_exit e.message
end