zendesk_apps_tools 1.11.0 → 1.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd3a1e249d27b455dc9b32604783ea161468ca26
4
- data.tar.gz: 6f85e644712b7eefcf16b2c01d0823454ea7109b
3
+ metadata.gz: a1ee115dae07f1e761072c56bd6031e32e90b67d
4
+ data.tar.gz: 34da3b3f22b42e3da9b9d570b0c8db87e80dcbce
5
5
  SHA512:
6
- metadata.gz: 71666498cc72bead4b0afa3b443f4ccf4c0399947f07b4bc11d942fbe0d7d42aa51970d7eda8e2b1bd71c58cbf897e350ca10506e62929d0cba4955cf4b06d8e
7
- data.tar.gz: 25b87b26c4f45ca74fb79f7c561ac040aabc706eabec59d5e241884ae0011dfa51f44abdf30efc0162d20dff96d8972782519cf30ea96a5c5cd1f30215a12c1d
6
+ metadata.gz: 62046dd8e89eb2f80f598dfef722a9cebfaa3eface9ccf0955cebd946fab3c7737dba5246a9093de55ebaa80b5d6d604114fcfd0ac8fabe17523bea9adcae153
7
+ data.tar.gz: 8c158f7f1ab9c747d1075b2b866d12f71b43e75b1885e871e45bf0a9a964d5a585e60993b433b86a7bd5232deca3157bb9702cf26cce98488858ef7b24b49b95
@@ -0,0 +1,36 @@
1
+ module ZendeskAppsTools
2
+ module APIConnection
3
+ FULL_URL = /https?:\/\//
4
+ URL_TEMPLATE = 'https://%s.zendesk.com/'
5
+
6
+ def prepare_api_auth
7
+ @subdomain ||= get_cache('subdomain') || get_value_from_stdin('Enter your Zendesk subdomain or full Zendesk URL:')
8
+ @username ||= get_cache('username') || get_value_from_stdin('Enter your username:')
9
+
10
+ unless @password
11
+ print 'Enter your password: '
12
+ @password ||= STDIN.noecho(&:gets).chomp
13
+ puts
14
+
15
+ set_cache 'subdomain' => @subdomain, 'username' => @username
16
+ end
17
+ end
18
+
19
+ def get_full_url
20
+ if FULL_URL =~ @subdomain
21
+ @subdomain
22
+ else
23
+ URL_TEMPLATE % @subdomain
24
+ end
25
+ end
26
+
27
+ def get_connection(encoding = :url_encoded)
28
+ prepare_api_auth
29
+ Faraday.new get_full_url do |f|
30
+ f.request encoding
31
+ f.adapter :net_http
32
+ f.basic_auth @username, @password
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,25 @@
1
+ module ZendeskAppsTools
2
+ module Cache
3
+ CACHE_FILE_NAME = '.zat'
4
+
5
+ def set_cache(hash)
6
+ return if options[:zipfile]
7
+
8
+ @cache = File.exists?(cache_path) ? JSON.parse(File.read(@cache_path)).update(hash) : hash
9
+ File.open(@cache_path, 'w') { |f| f.write JSON.pretty_generate(@cache) }
10
+ end
11
+
12
+ def get_cache(key)
13
+ @cache ||= File.exists?(cache_path) ? JSON.parse(File.read(@cache_path)) : {}
14
+ @cache[key] if @cache
15
+ end
16
+
17
+ def clear_cache
18
+ File.delete cache_path if options[:clean] && File.exists?(cache_path)
19
+ end
20
+
21
+ def cache_path
22
+ @cache_path ||= File.join options[:path], CACHE_FILE_NAME
23
+ end
24
+ end
25
+ end
@@ -3,20 +3,25 @@ require 'zip/zip'
3
3
  require 'pathname'
4
4
  require 'net/http'
5
5
  require 'json'
6
- require 'zendesk_apps_tools/translate'
7
- require 'zendesk_apps_tools/common'
8
- require 'zendesk_apps_tools/settings'
6
+ require 'faraday'
7
+ require 'io/console'
8
+
9
+ require 'zendesk_apps_tools/command_helpers'
9
10
 
10
11
  module ZendeskAppsTools
12
+
11
13
  require 'zendesk_apps_support'
12
14
 
13
15
  class Command < Thor
14
16
 
15
- DEFAULT_ZENDESK_URL = "http://support.zendesk.com"
17
+ SHARED_OPTIONS = {
18
+ :path => './',
19
+ :clean => false
20
+ }
16
21
 
17
22
  include Thor::Actions
18
23
  include ZendeskAppsSupport
19
- include ZendeskAppsTools::Common
24
+ include ZendeskAppsTools::CommandHelpers
20
25
 
21
26
  source_root File.expand_path(File.join(File.dirname(__FILE__), "../.."))
22
27
 
@@ -29,35 +34,15 @@ module ZendeskAppsTools
29
34
  @author_email = get_value_from_stdin("Enter this app author's email:\n", :valid_regex => /^.+@.+\..+$/, :error_msg => "Invalid email, try again:")
30
35
  @app_name = get_value_from_stdin("Enter a name for this new app:\n", :error_msg => "Invalid app name, try again:")
31
36
 
32
- prompt = "Enter a directory name to save the new app (will create the dir if it does not exist, default to current dir):\n"
33
- opts = { :valid_regex => /^(\w|\/|\\)*$/, :allow_empty => true }
34
- while @app_dir = get_value_from_stdin(prompt, opts) do
35
- @app_dir = './' and break if @app_dir.empty?
36
- if !File.exists?(@app_dir)
37
- break
38
- elsif !File.directory?(@app_dir)
39
- puts "Invalid dir, try again:"
40
- else
41
- break
42
- end
43
- end
37
+ get_new_app_directory
44
38
 
45
39
  directory('app_template', @app_dir)
46
40
  end
47
41
 
48
42
  desc "validate", "Validate your app"
49
- method_option :path, :default => './', :required => false
43
+ method_options SHARED_OPTIONS
50
44
  def validate
51
- prompt = "Enter a zendesk URL that you'd like to install the app (for example: 'http://abc.zendesk.com', default to '#{DEFAULT_ZENDESK_URL}'):\n"
52
- zendesk = get_value_from_stdin(prompt, :valid_regex => /^http:\/\/\w+\.\w+|^$/, :error_msg => 'Invalid url, try again:')
53
- zendesk = DEFAULT_ZENDESK_URL if zendesk.empty?
54
- url = URI.parse(zendesk)
55
- response = Net::HTTP.start(url.host, url.port) { |http| http.get('/api/v2/apps/framework_versions.json') }
56
- version = JSON.parse(response.body, :symbolize_names => true)
57
- if ZendeskAppsSupport::AppVersion::CURRENT != version[:current]
58
- puts 'This tool is using an out of date Zendesk App Framework. Please upgrade!'
59
- exit 1
60
- end
45
+ test_framework_version
61
46
 
62
47
  setup_path(options[:path])
63
48
  errors = app_package.validate
@@ -78,27 +63,16 @@ module ZendeskAppsTools
78
63
  end
79
64
 
80
65
  desc "package", "Package your app"
81
- method_option :path, :default => './', :required => false
66
+ method_options SHARED_OPTIONS
82
67
  def package
68
+ return false unless invoke(:validate, [])
69
+
83
70
  setup_path(options[:path])
84
71
  archive_path = File.join(tmp_dir, "app-#{Time.now.strftime('%Y%m%d%H%M%S')}.zip")
85
72
 
86
- return false unless invoke(:validate, [])
87
-
88
73
  archive_rel_path = relative_to_original_destination_root(archive_path)
89
74
 
90
- Zip::ZipFile.open(archive_path, 'w') do |zipfile|
91
- app_package.files.each do |file|
92
- path = file.relative_path
93
- say_status "package", "adding #{path}"
94
-
95
- # resolve symlink to source path
96
- if File.symlink? file.absolute_path
97
- path = File.readlink(file.absolute_path)
98
- end
99
- zipfile.add(file.relative_path, app_dir.join(path).to_s)
100
- end
101
- end
75
+ zip archive_path
102
76
 
103
77
  say_status "package", "created at #{archive_rel_path}"
104
78
  true
@@ -136,26 +110,40 @@ module ZendeskAppsTools
136
110
  end
137
111
  end
138
112
 
139
- protected
113
+ desc "create", "Create app on your account"
114
+ method_options SHARED_OPTIONS
115
+ method_option :zipfile, :default => nil, :required => false, :type => :string
116
+ def create
117
+ clear_cache
118
+ @command = 'Create'
140
119
 
141
- def setup_path(path)
142
- @destination_stack << relative_to_original_destination_root(path) unless @destination_stack.last == path
120
+ if options[:zipfile]
121
+ app_name = get_value_from_stdin('Enter app name:')
122
+ else
123
+ app_name = JSON.parse(File.read(File.join options[:path], 'manifest.json'))['name']
124
+ end
125
+ deploy_app(:post, '/api/v2/apps.json', { :name => app_name })
143
126
  end
144
127
 
145
- def app_dir
146
- @app_dir ||= Pathname.new(destination_root)
147
- end
128
+ desc "update", "Update app on the server"
129
+ method_options SHARED_OPTIONS
130
+ method_option :zipfile, :default => nil, :required => false, :type => :string
131
+ def update
132
+ clear_cache
133
+ @command = 'Update'
148
134
 
149
- def tmp_dir
150
- @tmp_dir ||= Pathname.new(File.join(app_dir, "tmp")).tap do |dir|
151
- FileUtils.mkdir_p(dir)
135
+ app_id = get_cache('app_id') || find_app_id
136
+ unless /\d+/ =~ app_id.to_s
137
+ say_error_and_exit "App id not found\nPlease try running command with --clean or check your internet connection"
152
138
  end
139
+ deploy_app(:put, "/api/v2/apps/#{app_id}.json", {})
153
140
  end
154
141
 
155
- def app_package
156
- @app_package ||= Package.new(self.app_dir.to_s)
142
+ protected
143
+
144
+ def setup_path(path)
145
+ @destination_stack << relative_to_original_destination_root(path) unless @destination_stack.last == path
157
146
  end
158
147
 
159
148
  end
160
149
  end
161
-
@@ -0,0 +1,21 @@
1
+ require 'zendesk_apps_tools/cache'
2
+ require 'zendesk_apps_tools/common'
3
+ require 'zendesk_apps_tools/api_connection'
4
+ require 'zendesk_apps_tools/deploy'
5
+ require 'zendesk_apps_tools/directory'
6
+ require 'zendesk_apps_tools/package_helper'
7
+ require 'zendesk_apps_tools/settings'
8
+ require 'zendesk_apps_tools/translate'
9
+ require 'zendesk_apps_tools/validate_helper'
10
+
11
+ module ZendeskAppsTools
12
+ module CommandHelpers
13
+ include ZendeskAppsTools::Cache
14
+ include ZendeskAppsTools::Common
15
+ include ZendeskAppsTools::APIConnection
16
+ include ZendeskAppsTools::Deploy
17
+ include ZendeskAppsTools::Directory
18
+ include ZendeskAppsTools::PackageHelper
19
+ include ZendeskAppsTools::ValidateHelper
20
+ end
21
+ end
@@ -6,6 +6,11 @@ module ZendeskAppsTools
6
6
  request.get(url)
7
7
  end
8
8
 
9
+ def say_error_and_exit(msg)
10
+ say msg, :red
11
+ exit 1
12
+ end
13
+
9
14
  def get_value_from_stdin(prompt, opts = {})
10
15
  options = {
11
16
  :valid_regex => opts[:allow_empty] ? /^.*$/ : /\S+/,
@@ -0,0 +1,95 @@
1
+ module ZendeskAppsTools
2
+ module Deploy
3
+ def deploy_app(connection_method, url, body)
4
+ body[:upload_id] = upload(options[:path]).to_s
5
+
6
+ connection = get_connection
7
+
8
+ response = connection.send(connection_method) do |req|
9
+ req.url url
10
+ req.headers[:content_type] = 'application/json'
11
+
12
+ req.body = JSON.generate body
13
+ end
14
+
15
+ check_status response
16
+
17
+ rescue Faraday::Error::ClientError => e
18
+ say_error_and_exit e.message
19
+ end
20
+
21
+ def upload(path)
22
+ connection = get_connection :multipart
23
+ zipfile_path = options[:zipfile]
24
+
25
+ if zipfile_path
26
+ package_path = zipfile_path
27
+ else
28
+ package
29
+ package_path = Dir[File.join path, '/tmp/*.zip'].sort.last
30
+ end
31
+
32
+ payload = { :uploaded_data => Faraday::UploadIO.new(package_path, 'application/zip') }
33
+
34
+ response = connection.post('/api/v2/apps/uploads.json', payload)
35
+ JSON.parse(response.body)['id']
36
+
37
+ rescue Faraday::Error::ClientError => e
38
+ say_error_and_exit e.message
39
+ rescue JSON::ParserError => e
40
+ say_error_and_exit e.message
41
+ end
42
+
43
+ def find_app_id
44
+ say_status 'Update', 'app ID is missing, searching...'
45
+ name = get_value_from_stdin('Enter the name of the app:')
46
+
47
+ connection = get_connection
48
+
49
+ all_apps = connection.get('/api/v2/apps.json').body
50
+
51
+ app = JSON.parse(all_apps)['apps'].find { |app| app['name'] == name }
52
+
53
+ set_cache 'app_id' => app['id']
54
+ app['id']
55
+ rescue Faraday::Error::ClientError => e
56
+ say_error_and_exit e.message
57
+ end
58
+
59
+ def check_status(response)
60
+ job = response.body
61
+ job_id = JSON.parse(job)['job_id']
62
+
63
+ check_job job_id
64
+ end
65
+
66
+ def check_job(job_id)
67
+ connection = get_connection
68
+
69
+ loop do
70
+ response = connection.get("/api/v2/apps/job_statuses/#{job_id}")
71
+ info = JSON.parse(response.body)
72
+ status = info['status']
73
+ message = info['message']
74
+ app_id = info['app_id']
75
+
76
+ if ['completed', 'failed'].include? status
77
+ case status
78
+ when 'completed'
79
+ set_cache 'app_id' => app_id
80
+ say_status @command, 'OK'
81
+ when 'failed'
82
+ say_status @command, message
83
+ end
84
+ break
85
+ end
86
+
87
+ say_status 'Status', status
88
+ sleep 3
89
+ end
90
+ rescue Faraday::Error::ClientError => e
91
+ say_error_and_exit e.message
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,30 @@
1
+ module ZendeskAppsTools
2
+ module Directory
3
+
4
+ def app_dir
5
+ @app_dir ||= Pathname.new(destination_root)
6
+ end
7
+
8
+ def tmp_dir
9
+ @tmp_dir ||= Pathname.new(File.join(app_dir, "tmp")).tap do |dir|
10
+ FileUtils.mkdir_p(dir)
11
+ end
12
+ end
13
+
14
+ def get_new_app_directory
15
+ prompt = "Enter a directory name to save the new app (will create the dir if it does not exist, default to current dir):\n"
16
+ opts = { :valid_regex => /^(\w|\/|\\)*$/, :allow_empty => true }
17
+ while @app_dir = get_value_from_stdin(prompt, opts) do
18
+ @app_dir = './' and break if @app_dir.empty?
19
+ if !File.exists?(@app_dir)
20
+ break
21
+ elsif !File.directory?(@app_dir)
22
+ puts "Invalid dir, try again:"
23
+ else
24
+ break
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ module ZendeskAppsTools
2
+
3
+ require 'zendesk_apps_support'
4
+
5
+ module PackageHelper
6
+
7
+ include ZendeskAppsSupport
8
+
9
+ def app_package
10
+ @app_package ||= Package.new(self.app_dir.to_s)
11
+ end
12
+
13
+ def zip(archive_path)
14
+ Zip::ZipFile.open(archive_path, 'w') do |zipfile|
15
+ app_package.files.each do |file|
16
+ path = file.relative_path
17
+ say_status "package", "adding #{path}"
18
+
19
+ # resolve symlink to source path
20
+ if File.symlink? file.absolute_path
21
+ path = File.readlink(file.absolute_path)
22
+ end
23
+ zipfile.add(file.relative_path, app_dir.join(path).to_s)
24
+ end
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ module ZendeskAppsTools
2
+ module ValidateHelper
3
+
4
+ DEFAULT_ZENDESK_URL = 'http://support.zendesk.com'
5
+
6
+ def test_framework_version
7
+ prompt = "Enter a zendesk URL that you'd like to install the app (for example: 'http://abc.zendesk.com', default to '#{DEFAULT_ZENDESK_URL}'):\n"
8
+ zendesk = get_value_from_stdin(prompt, :valid_regex => /^http:\/\/\w+\.\w+|^$/, :error_msg => 'Invalid url, try again:')
9
+ zendesk = DEFAULT_ZENDESK_URL if zendesk.empty?
10
+ url = URI.parse(zendesk)
11
+ response = Net::HTTP.start(url.host, url.port) { |http| http.get('/api/v2/apps/framework_versions.json') }
12
+ version = JSON.parse(response.body, :symbolize_names => true)
13
+
14
+ if ZendeskAppsSupport::AppVersion::CURRENT != version[:current]
15
+ puts 'This tool is using an out of date Zendesk App Framework. Please upgrade!'
16
+ exit 1
17
+ end
18
+ end
19
+
20
+ end
21
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zendesk_apps_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.11.0
4
+ version: 1.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James A. Rosen
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-12-22 00:00:00.000000000 Z
14
+ date: 2014-01-06 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: thor
@@ -139,6 +139,20 @@ dependencies:
139
139
  - - '>='
140
140
  - !ruby/object:Gem::Version
141
141
  version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: webmock
144
+ requirement: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ type: :development
150
+ prerelease: false
151
+ version_requirements: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - '>='
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
142
156
  description: Tools to help you develop Zendesk Apps.
143
157
  email:
144
158
  - dev@zendesk.com
@@ -147,26 +161,18 @@ executables:
147
161
  extensions: []
148
162
  extra_rdoc_files: []
149
163
  files:
150
- - bin/zat
151
- - lib/zendesk_apps_tools/command.rb
152
- - lib/zendesk_apps_tools/common.rb
153
- - lib/zendesk_apps_tools/locale_identifier.rb
154
- - lib/zendesk_apps_tools/server.rb
155
- - lib/zendesk_apps_tools/settings.rb
156
- - lib/zendesk_apps_tools/translate.rb
157
- - lib/zendesk_apps_tools.rb
164
+ - LICENSE
165
+ - README.md
166
+ - app_template/README.md
158
167
  - app_template/app.css
159
168
  - app_template/app.js
160
169
  - app_template/assets/logo-small.png
161
170
  - app_template/assets/logo.png
162
171
  - app_template/assets/promotion-image.png
163
172
  - app_template/manifest.json.tt
164
- - app_template/README.md
165
173
  - app_template/templates/layout.hdbs
166
174
  - app_template/translations/en.json
167
- - templates/translation.erb.tt
168
- - README.md
169
- - LICENSE
175
+ - bin/zat
170
176
  - features/clean.feature
171
177
  - features/fixtures/quote_character_translation.json
172
178
  - features/new.feature
@@ -174,6 +180,21 @@ files:
174
180
  - features/step_definitions/app_steps.rb
175
181
  - features/support/env.rb
176
182
  - features/validate.feature
183
+ - lib/zendesk_apps_tools.rb
184
+ - lib/zendesk_apps_tools/api_connection.rb
185
+ - lib/zendesk_apps_tools/cache.rb
186
+ - lib/zendesk_apps_tools/command.rb
187
+ - lib/zendesk_apps_tools/command_helpers.rb
188
+ - lib/zendesk_apps_tools/common.rb
189
+ - lib/zendesk_apps_tools/deploy.rb
190
+ - lib/zendesk_apps_tools/directory.rb
191
+ - lib/zendesk_apps_tools/locale_identifier.rb
192
+ - lib/zendesk_apps_tools/package_helper.rb
193
+ - lib/zendesk_apps_tools/server.rb
194
+ - lib/zendesk_apps_tools/settings.rb
195
+ - lib/zendesk_apps_tools/translate.rb
196
+ - lib/zendesk_apps_tools/validate_helper.rb
197
+ - templates/translation.erb.tt
177
198
  homepage: http://github.com/zendesk/zendesk_apps_tools
178
199
  licenses:
179
200
  - Apache License Version 2.0
@@ -194,7 +215,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
215
  version: 1.3.6
195
216
  requirements: []
196
217
  rubyforge_project:
197
- rubygems_version: 2.0.14
218
+ rubygems_version: 2.2.0
198
219
  signing_key:
199
220
  specification_version: 4
200
221
  summary: Tools to help you develop Zendesk Apps.