knife-github 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NDViMjMxNGQzYTkxODBmMzM2MTI2ZjVkYmFkNmRmNjMyNjEzZjE2MA==
4
+ MjM5M2RiM2MwOWRlY2U1YmUxMjMwYjk5NTgwOWNiZGQ3ODk3N2JmOA==
5
5
  data.tar.gz: !binary |-
6
- NDk3NTRkOTc1MjYxMzZiZDhlNWJmZThlM2ZlNDNiZDg2MzcxZDBkMw==
6
+ MWQyMjFiNTliYWNkZTcyYjFhY2QzOWVjM2QyNjlkMGE0ZDI5NjgzMw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- N2ZkMTM5OGIxNTRiNDkyMGY2YWMyY2E4NjYxYmU3ZGMyN2FkZWI5YThhYmE3
10
- MGE4YjhiODIzYTg4ZWE4OTM0NGIzOGY1MGI0NWQ0MjY1MzkwZWM4N2NmMjFk
11
- YWIzY2Y3ODY2ZjhhNDNlMjQzMzRmYjc3Mzg4ZGU3ZGRiODEwMzI=
9
+ ZDE3NDk2NTBkODgzNzRlZTIxNzI1ZjI5YzRjYjhiYTQxZDQ2NWZiMWU3OGY2
10
+ ZWU0N2Y4YTE0NGNkYTE2ZDY1Y2M2NjdiODM1MzlmMjgxMzliMjlkYTUyMThj
11
+ MDM5NDQ1ZTk1NzA5YmMzOTdjN2FmNTM0OGVhYjJlNzNlNzQ4MzE=
12
12
  data.tar.gz: !binary |-
13
- ODllMmI5OTRhNDlhYTNkM2MwNmQ5ODIwNmJjZGZiYjE5MThmY2EwMmRkMDIy
14
- NzJjNDVkNDk3MGM5YjIzYjZmN2I3ZTdiMjQ4N2JmNjIwMTdhMjliZTllZjFm
15
- Y2EwZDNkMDJhMzk4NTQ2MWI4YjdjMGRiMTcyNjMwY2JkOWVkOTY=
13
+ MmRhYTdiZTA3ODAzNWUzMDA4NjRmZDE4MDhmYjFjN2Y2YjlhNGYzOGYyMGM1
14
+ OWVlMmRiZDliMDlkMzdmYmMwMjNmZDhmYWEwNTc0ZjE0MzA5ZWIyZWQ3NDlm
15
+ OWUxODRlODNkNWYyMDE4MjhmZjVhNzg5MjQ5NTM0Mjk1MGM1OWY=
data/README.md CHANGED
@@ -52,6 +52,8 @@ Currently the options are: <tt>verify_peer</tt> <tt>verify_none</tt>
52
52
  Token information is required when creating and deleting github repositories.
53
53
  With the command <tt>knife github token create</tt> you are able to create token information.
54
54
 
55
+ ###### github_proxy \<optional\>
56
+ Used for HTTP communication over a proxy.
55
57
 
56
58
  Other
57
59
  =====
data/knife-github.gemspec CHANGED
@@ -21,5 +21,5 @@ Gem::Specification.new do |s|
21
21
  s.add_dependency 'mixlib-versioning', '>= 1.0.0'
22
22
  s.add_dependency 'highline', '>= 1.6.19'
23
23
  s.add_dependency 'rb-readline', '~> 0.5.1'
24
- s.add_dependency 'chef', '>= 10.0.0'
24
+ s.add_dependency 'chef', '~> 11.0'
25
25
  end
@@ -21,6 +21,7 @@ require 'knife-github/config'
21
21
  require 'knife-github/version'
22
22
  require 'knife-github/connection'
23
23
  require 'mixlib/versioning'
24
+ require 'chef/mixin/shell_out'
24
25
 
25
26
  class Chef
26
27
  class Knife
@@ -59,6 +60,15 @@ class Chef
59
60
  :long => "--github_no_update",
60
61
  :description => "Turn github update checking off",
61
62
  :boolean => true
63
+
64
+ option :github_proxy,
65
+ :long => "--github_proxy",
66
+ :description => "Enable proxy configuration for github api"
67
+
68
+ option :github_token,
69
+ :short => "-t",
70
+ :long => "--github_token",
71
+ :description => "Your github token for OAuth authentication"
62
72
 
63
73
  def validate_base_options
64
74
  unless locate_config_value('github_url')
@@ -69,22 +79,37 @@ class Chef
69
79
  ui.error "Github organization(s) not specified"
70
80
  exit 1
71
81
  end
72
- unless locate_config_value('github_no_update')
73
- check_gem_version
74
- end
75
82
 
76
83
  @github_url = locate_config_value("github_url")
77
84
  @github_organizations = locate_config_value("github_organizations")
78
85
  @github_link = locate_config_value("github_link") || 'ssh'
79
86
  @github_api_version = locate_config_value("github_api_version") || 'v3'
80
87
  @github_ssl_verify_mode = locate_config_value("github_ssl_verify_mode") || 'verify_peer'
88
+ @github_proxy = locate_config_value("github_proxy")
89
+ @github_token = locate_config_value("github_token")
81
90
  @github_tmp = locate_config_value("github_tmp") || '/var/tmp/gitdiff'
82
91
  @github_tmp = "#{@github_tmp}#{Process.pid}"
92
+
93
+ unless locate_config_value('github_no_update')
94
+ check_gem_version
95
+ end
83
96
  end
84
97
 
85
98
  def check_gem_version
86
- url = 'http://rubygems.org/api/v1/gems/knife-github.json'
87
- result = `curl -L -s #{url}`
99
+ extend Chef::Mixin::ShellOut
100
+ url = 'http://rubygems.org/api/v1/gems/knife-github.json'
101
+ proxy = @github_proxy
102
+ if proxy.nil?
103
+ result = `curl -L -s #{url}`
104
+ Chef::Log.debug("removing proxy in glogal git config")
105
+ shell_out!("git config --global --unset http.proxy")
106
+ shell_out!("git config --global --unset https.proxy")
107
+ else
108
+ Chef::Log.debug("Putting proxy in glogal git config")
109
+ shell_out!("git config --global http.proxy #{proxy}")
110
+ shell_out!("git config --global https.proxy #{proxy}")
111
+ result = `curl --proxy #{proxy} -L -s #{url}`
112
+ end
88
113
  begin
89
114
  json = JSON.parse(result)
90
115
  webversion = Mixlib::Versioning.parse(json['version'])
@@ -110,6 +135,8 @@ class Chef
110
135
  Chef::Log.debug("github_api : " + @github_api_version.to_s)
111
136
  Chef::Log.debug("github_link : " + @github_link.to_s)
112
137
  Chef::Log.debug("github_ssl_mode : " + @github_ssl_verify_mode.to_s)
138
+ Chef::Log.debug("github_proxy : " + @github_proxy.to_s)
139
+ Chef::Log.debug("github_token : " + @github_token.to_s)
113
140
  end
114
141
 
115
142
  def locate_config_value(key)
@@ -199,12 +226,16 @@ class Chef
199
226
  end
200
227
 
201
228
  def get_github_repo_data(org)
229
+ params = {}
202
230
  arr = []
203
- page = 1
204
231
  url = @github_url + "/api/" + @github_api_version + "/orgs/" + org + "/repos"
232
+ params[:token] = @github_token
233
+ params[:action] = "GET"
234
+ params[:url] = url
235
+ page = 1
205
236
  while true
206
- params = {'response' => 'json', 'page' => page }
207
- result = connection.send_get_request(url, params)
237
+ params[:request_uri] = "?response=json&page=#{page}"
238
+ result = connection.request(params)
208
239
  break if result.nil? || result.count < 1
209
240
  result.each { |key| arr << Github::Repo.new(key) }
210
241
  page = page + 1
@@ -280,6 +311,23 @@ class Chef
280
311
  version
281
312
  end
282
313
 
314
+ # Get the OAuth authentication token from config or command line
315
+ # @param nil
316
+ def get_github_token()
317
+ token = locate_config_value('github_token')
318
+ if token.nil? || token.empty?
319
+ return nil
320
+ end
321
+ token
322
+ end
323
+
324
+ # Create the json body with repo config for POST information
325
+ # @param name [String] cookbook name
326
+ def get_body_json()
327
+ body = {
328
+ "scopes" => ["public_repo"]
329
+ }.to_json
330
+ end
283
331
  end
284
332
  end
285
333
  end
@@ -82,7 +82,7 @@ module KnifeGithubCleanup
82
82
 
83
83
  def repo_cleanup(repo)
84
84
  cookbook_path = config[:cookbook_path] || Chef::Config[:cookbook_path]
85
- cookbook = File.join(cookbook_path.first,repo)
85
+ cookbook = File.join(cookbook_path,repo)
86
86
  if File.exists?(cookbook)
87
87
  if repo_status_clean?(repo, cookbook)
88
88
  # delete the repo
@@ -47,11 +47,6 @@ module KnifeGithubRepoCreate
47
47
  banner "knife github repo create <name> <description> (options)"
48
48
  category "github"
49
49
 
50
- option :github_token,
51
- :short => "-t",
52
- :long => "--github_token",
53
- :description => "Your github token for OAuth authentication"
54
-
55
50
  option :github_user_repo,
56
51
  :short => "-U",
57
52
  :long => "--github_user_repo",
@@ -60,6 +55,7 @@ module KnifeGithubRepoCreate
60
55
 
61
56
  def run
62
57
  extend Chef::Mixin::ShellOut
58
+ params = {}
63
59
 
64
60
  # validate base options from base module.
65
61
  validate_base_options
@@ -83,31 +79,29 @@ module KnifeGithubRepoCreate
83
79
  type = get_repo_type
84
80
 
85
81
  if config[:github_user_repo]
86
- url = @github_url + "/api/" + @github_api_version + "/user/repos"
82
+ params[:url] = @github_url + "/api/" + @github_api_version + "/user/repos"
87
83
  Chef::Log.debug("Creating repository in user environment")
88
84
  else
89
- url = @github_url + "/api/" + @github_api_version + "/orgs/#{org}/repos"
85
+ params[:url] = @github_url + "/api/" + @github_api_version + "/orgs/#{org}/repos"
90
86
  Chef::Log.debug("Creating repository in organization: #{org}")
91
87
  end
92
88
 
93
89
  @github_tmp = locate_config_value("github_tmp") || '/var/tmp/gitcreate'
94
90
  @github_tmp = "#{@github_tmp}#{Process.pid}"
95
91
 
96
- # Get token information
97
- token = get_github_token()
98
-
99
- # Get body data for post
100
- body = get_body_json(name, desc)
101
92
 
102
93
  # Creating the local repository or using existing one.
103
94
  cookbook_dir = get_cookbook_path(name) || ""
104
95
 
96
+ params[:body] = lget_body_json(name, desc)
97
+ params[:token] = get_github_token()
98
+ params[:action] = "POST"
105
99
  if File.exists?(cookbook_dir)
106
100
  Chef::Log.debug("Using local repository from #{cookbook_dir}")
107
101
 
108
102
  # Creating the github repository
109
103
  Chef::Log.debug("Creating the github repository")
110
- repo = post_request(url, body, token)
104
+ repo = connection.request(params)
111
105
  github_ssh_url = repo['ssh_url']
112
106
 
113
107
  Chef::Log.debug("Commit and push local repository")
@@ -126,7 +120,7 @@ module KnifeGithubRepoCreate
126
120
 
127
121
  # Creating the github repository
128
122
  Chef::Log.debug("Creating the github repository")
129
- repo = post_request(url, body, token)
123
+ repo = connection.request(params)
130
124
  github_ssh_url = repo['ssh_url']
131
125
 
132
126
  Chef::Log.debug("Commit and push local repository")
@@ -271,7 +265,7 @@ module KnifeGithubRepoCreate
271
265
 
272
266
  # Create the json body with repo config for POST information
273
267
  # @param name [String] cookbook name
274
- def get_body_json(cookbook_name, description="Please fill in the description.")
268
+ def lget_body_json(cookbook_name, description="Please fill in the description.")
275
269
  body = {
276
270
  "name" => cookbook_name,
277
271
  "description" => description,
@@ -282,61 +276,5 @@ module KnifeGithubRepoCreate
282
276
  }.to_json
283
277
  end
284
278
 
285
- # Get the OAuth authentication token from config or command line
286
- # @param nil
287
- def get_github_token()
288
- token = locate_config_value('github_token')
289
- if token.nil? || token.empty?
290
- Chef::Log.error("Please specify a github token")
291
- exit 1
292
- end
293
- token
294
- end
295
-
296
- # Post Get the OAuth authentication token from config or command line
297
- # @param url [String] target url (organization or user)
298
- # body [JSON] json data with repo configuration
299
- # token [String] token sring
300
- def post_request(url, body, token)
301
-
302
- if @github_ssl_verify_mode == "verify_none"
303
- config[:ssl_verify_mode] = :verify_none
304
- elsif @github_ssl_verify_mode == "verify_peer"
305
- config[:ssl_verify_mode] = :verify_peer
306
- end
307
-
308
- Chef::Log.debug("URL: " + url.to_s)
309
-
310
- uri = URI.parse(url)
311
- http = Net::HTTP.new(uri.host,uri.port)
312
- if uri.scheme == "https"
313
- http.use_ssl = true
314
- if @github_ssl_verify_mode == "verify_none"
315
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
316
- else
317
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
318
- end
319
- end
320
-
321
- req = Net::HTTP::Post.new(uri.path, initheader = {"Authorization" => "token #{token}"})
322
- req.body = body
323
- response = http.request(req)
324
-
325
- unless response.code == "201" then
326
- puts "Error #{response.code}: #{response.message}"
327
- puts JSON.pretty_generate(JSON.parse(response.body))
328
- puts "URL: #{url}"
329
- exit 1
330
- end
331
-
332
- begin
333
- json = JSON.parse(response.body)
334
- rescue
335
- ui.warn "The result on the RESTRequest is not in json format"
336
- ui.warn "Output: " + response.body
337
- exit 1
338
- end
339
- json
340
- end
341
279
  end
342
280
  end
@@ -106,7 +106,11 @@ module KnifeGithubRepoDestroy
106
106
  # update_metadata(cookbook_path)
107
107
 
108
108
  # Creating the github repository
109
- repo = delete_request(url, token)
109
+ params = {}
110
+ params[:url] = url
111
+ params[:token] = token
112
+ params[:action] = "DELETE"
113
+ repo = connection.request(params)
110
114
  puts "Repo: #{name} is deleted" if repo.nil?
111
115
 
112
116
  # github_ssh_url = repo['ssh_url']
@@ -202,72 +206,5 @@ module KnifeGithubRepoDestroy
202
206
  create.run
203
207
  end
204
208
 
205
- # Create the json body with repo config for POST information
206
- # @param name [String] cookbook name
207
- def get_body_json()
208
- body = {
209
- "scopes" => ["public_repo"]
210
- }.to_json
211
- end
212
-
213
- # Get the OAuth authentication token from config or command line
214
- # @param nil
215
- def get_github_token()
216
- token = locate_config_value('github_token')
217
- if token.nil? || token.empty?
218
- Chef::Log.error("Cannot find any token information!")
219
- Chef::Log.error("Please use: knife github token create")
220
- exit 1
221
- end
222
- token
223
- end
224
-
225
- # Send DELETE command to API OAuth authentication token from config or command line
226
- # @param url [String] target url (organization or user)
227
- # body [JSON] json data with repo configuration
228
- # token [String] token sring
229
- def delete_request(url, token)
230
-
231
- # if @github_ssl_verify_mode == "verify_none"
232
- # config[:ssl_verify_mode] = :verify_none
233
- # elsif @github_ssl_verify_mode == "verify_peer"
234
- # config[:ssl_verify_mode] = :verify_peer
235
- # end
236
-
237
- Chef::Log.debug("URL: " + url.to_s)
238
-
239
- uri = URI.parse(url)
240
- http = Net::HTTP.new(uri.host,uri.port)
241
- if uri.scheme == "https"
242
- http.use_ssl = true
243
- if @github_ssl_verify_mode == "verify_none"
244
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
245
- else
246
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
247
- end
248
- end
249
-
250
- req = Net::HTTP::Delete.new(uri.path, initheader = {"Authorization" => "token #{token}"})
251
- req.body = get_body_json()
252
- response = http.request(req)
253
-
254
- return nil if response.code == "204"
255
-
256
- unless response.code == "201" then
257
- puts "Error #{response.code}: #{response.message}"
258
- puts JSON.pretty_generate(JSON.parse(response.body))
259
- puts "URL: #{url}"
260
- exit 1
261
- end
262
-
263
- begin
264
- json = JSON.parse(response.body)
265
- rescue
266
- ui.warn "The result on the RESTRequest is not in json format"
267
- ui.warn "Output: " + response.body
268
- exit 1
269
- end
270
- json
271
- end
272
209
  end
273
210
  end
@@ -29,9 +29,6 @@ module KnifeGithubRepoFork
29
29
  # Fork a new cookbook:
30
30
  # knife github repo fork <name>
31
31
  #
32
- # === Options
33
- # -t --github_token Authentication token for the github.
34
- #
35
32
 
36
33
  deps do
37
34
  require 'chef/knife/github_base'
@@ -41,13 +38,7 @@ module KnifeGithubRepoFork
41
38
  banner "knife github repo fork <name> [owner] [target] (options)"
42
39
  category "github"
43
40
 
44
- option :github_token,
45
- :short => "-t",
46
- :long => "--github_token",
47
- :description => "Your github token for OAuth authentication"
48
-
49
41
  def run
50
- params = {}
51
42
  # validate base options from base module.
52
43
  validate_base_options
53
44
 
@@ -65,15 +56,17 @@ module KnifeGithubRepoFork
65
56
  end
66
57
 
67
58
  # Set params for the rest request
68
- params['url'] = @github_url + "/api/" + @github_api_version + "/repos/#{owner}/#{name}/forks"
69
- params['body'] = get_body_json(target) unless target.nil?
70
- params['token'] = get_github_token()
71
- params['response_code'] = 202
59
+ params = {}
60
+ params[:url] = @github_url + "/api/" + @github_api_version + "/repos/#{owner}/#{name}/forks"
61
+ params[:body] = get_body_json(target) unless target.nil?
62
+ params[:token] = get_github_token()
63
+ #params[:response_code] = 202
64
+ params[:action] = "POST"
72
65
 
73
66
  # Execute the rest request
74
67
  username = ENV['USER']
75
68
 
76
- rest_request(params)
69
+ connection.request(params)
77
70
  if target
78
71
  puts "Fork of #{name} is created in #{target}"
79
72
  else
@@ -81,74 +74,5 @@ module KnifeGithubRepoFork
81
74
  end
82
75
  end
83
76
 
84
- # Create the json body with repo config for POST information
85
- # @param target [String] oragnization target name
86
- def get_body_json(target)
87
- body = {
88
- "organization" => target,
89
- }.to_json
90
- end
91
-
92
- # Get the OAuth authentication token from config or command line
93
- # @param nil
94
- def get_github_token()
95
- token = locate_config_value('github_token')
96
- if token.nil? || token.empty?
97
- Chef::Log.error("Please specify a github token")
98
- exit 1
99
- end
100
- token
101
- end
102
-
103
- # Post Get the OAuth authentication token from config or command line
104
- # @param url [String] target url (organization or user)
105
- # body [JSON] json data with repo configuration
106
- # token [String] token sring
107
- def rest_request(params = {})
108
- url = params['url'].to_s
109
- token = params['token'].to_s
110
- code = params['response_code'] || 200
111
- body = params['body'] || nil
112
- action = params['action'] || 'POST'
113
-
114
- if @github_ssl_verify_mode == "verify_none"
115
- config[:ssl_verify_mode] = :verify_none
116
- elsif @github_ssl_verify_mode == "verify_peer"
117
- config[:ssl_verify_mode] = :verify_peer
118
- end
119
-
120
- Chef::Log.debug("URL: " + url)
121
-
122
- uri = URI.parse(url)
123
- http = Net::HTTP.new(uri.host,uri.port)
124
- if uri.scheme == "https"
125
- http.use_ssl = true
126
- if @github_ssl_verify_mode == "verify_none"
127
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
128
- else
129
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
130
- end
131
- end
132
-
133
- req = Net::HTTP::Post.new(uri.path, initheader = {"Authorization" => "token #{token}"})
134
- req.body = body unless body.nil?
135
- response = http.request(req)
136
-
137
- unless response.code.to_s == code.to_s then
138
- puts "Error #{response.code}: #{response.message}"
139
- puts JSON.pretty_generate(JSON.parse(response.body))
140
- puts "URL: #{url}"
141
- exit 1
142
- end
143
-
144
- begin
145
- json = JSON.parse(response.body)
146
- rescue
147
- ui.warn "The result on the RESTRequest is not in json format"
148
- ui.warn "Output: " + response.body
149
- exit 1
150
- end
151
- json
152
- end
153
77
  end
154
78
  end
@@ -71,16 +71,22 @@ module KnifeGithubSearch
71
71
  end
72
72
  end
73
73
 
74
- def github_search_repos(query, params ={})
74
+ def github_search_repos(query, request = {})
75
75
  # once the new search function is available, we can use these params
76
- params['q'] = query
77
- params['sort'] = 'stars'
78
- params['order'] = 'desc'
79
- params['response'] = 'json'
80
-
81
- url = @github_url + "/api/" + @github_api_version + "/legacy/repos/search/" + query
82
- Chef::Log.debug("URL: #{url}")
83
- connection.send_get_request(url, params = {})
76
+ request['sort'] = 'stars'
77
+ request['order'] = 'desc'
78
+ request['response'] = 'json'
79
+ request_arr = []
80
+ request.sort.each { |elem|
81
+ request_arr << elem[0].to_s + '=' + CGI.escape(elem[1].to_s).gsub('+', '%20').gsub(' ','%20')
82
+ }
83
+ data = request_arr.join('&')
84
+ params = {}
85
+ params[:url] = @github_url + "/api/" + @github_api_version + "/legacy/repos/search/" + query
86
+ params[:action] = "GET"
87
+ params[:token] = get_github_token
88
+ params[:request_uri] = "?#{data}"
89
+ connection.request(params)
84
90
  end
85
91
 
86
92
  end
@@ -107,17 +107,6 @@ module KnifeGithubTokenCreate
107
107
  return true
108
108
  end
109
109
 
110
- # Get the OAuth authentication token from config or command line
111
- # @param none
112
- def get_github_token()
113
- token = locate_config_value('github_token')
114
- if token.nil? || token.empty?
115
- return nil
116
- else
117
- return token
118
- end
119
- end
120
-
121
110
  # Validate the OAuth authentication token for the knife-github application.
122
111
  # @param username [String] validates the token for specific user. (default is ENV['USER'])
123
112
  #
@@ -134,7 +123,7 @@ module KnifeGithubTokenCreate
134
123
 
135
124
  token_key = nil
136
125
 
137
- result = send_request(params)
126
+ result = connection.request(params)
138
127
  result.each do |token|
139
128
  if token['app'] && token['app']['name'] == "knife-github (API)"
140
129
  if token['scopes'].include?("delete_repo")
@@ -167,73 +156,15 @@ module KnifeGithubTokenCreate
167
156
  params[:url] = @github_url + "/api/" + @github_api_version + "/authorizations"
168
157
  params[:body] = '{"note":"knife-github","scopes":["delete_repo", "user", "public_repo", "repo", "gist"]"}'
169
158
  params[:action] = "POST"
170
- send_request(params)
159
+ connection.request(params)
171
160
  end
172
161
 
173
162
  def delete_github_token(params)
174
163
  Chef::Log.debug("Deleting token id: #{params[':id']}")
175
164
  params[:url] = @github_url + "/api/" + @github_api_version + "/authorizations/#{params[:id]}"
176
165
  params[:action] = "DELETE"
177
- send_request(params)
166
+ connection.request(params)
178
167
  end
179
168
 
180
- # Post Get the OAuth authentication token from config or command line
181
- # @param params [Hash] Hash containing all options
182
- # params[:url] [String] Url to target
183
- # params[:body] [JSON] json data for the request
184
- # params[:token] [String] OAuth token
185
- # params[:username] [String] Username if no token specified
186
- # params[:password] [String] Password if no token specified
187
- #
188
- def send_request(params)
189
- url = params[:url]
190
-
191
- Chef::Log.debug("URL: " + url.to_s)
192
-
193
- uri = URI.parse(url)
194
- http = Net::HTTP.new(uri.host,uri.port)
195
- if uri.scheme == "https"
196
- http.use_ssl = true
197
- if @github_ssl_verify_mode == "verify_none"
198
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
199
- else
200
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
201
- end
202
- end
203
-
204
- if params[:action] == "GET"
205
- req = Net::HTTP::Get.new(uri.path)
206
- elsif params[:action] == "POST"
207
- req = Net::HTTP::Post.new(uri.path)
208
- elsif params[:action] == "DELETE"
209
- req = Net::HTTP::Delete.new(uri.path)
210
- end
211
-
212
- if params[:token].nil?
213
- req.basic_auth params[:username], params[:password]
214
- else
215
- req.initheader = ({"Authorization" => "token #{params[:token]}"})
216
- end
217
- req.body = params[:body] if params[:body]
218
- response = http.request(req)
219
-
220
- unless response.code =~ /^2../ then
221
- puts "Error #{response.code}: #{response.message}"
222
- puts JSON.pretty_generate(JSON.parse(response.body))
223
- puts "URL: #{url}"
224
- exit 1
225
- end
226
-
227
- return nil if response.body.nil? || response.body.empty?
228
-
229
- begin
230
- json = JSON.parse(response.body)
231
- rescue
232
- ui.warn "The result on the REST Request is not in json format"
233
- ui.warn "Output: " + response.body
234
- exit 1
235
- end
236
- json
237
- end
238
169
  end
239
170
  end
@@ -8,6 +8,7 @@ module Github
8
8
  configurable :github_link
9
9
  configurable :github_api_version
10
10
  configurable :github_ssl_verify_mode
11
+ configurable :github_proxy
11
12
  end
12
13
  end
13
14
  end
@@ -6,39 +6,80 @@ require 'net/http'
6
6
 
7
7
  module GithubClient
8
8
  class Connection
9
- def send_get_request(url, params = {})
10
- unless params.empty?
11
- params_arr = []
12
- params.sort.each { |elem|
13
- params_arr << elem[0].to_s + '=' + CGI.escape(elem[1].to_s).gsub('+', '%20').gsub(' ','%20')
14
- }
15
- data = params_arr.join('&')
16
- url = "#{url}?#{data}"
17
- end
18
9
 
19
- #if @github_ssl_verify_mode == "verify_none"
20
- # config[:ssl_verify_mode] = :verify_none
21
- #elsif @github_ssl_verify_mode == "verify_peer"
22
- # config[:ssl_verify_mode] = :verify_peer
23
- #end
10
+ def request(params)
11
+ ssl_verify_mode = Chef::Config[:knife][:github_ssl_verify_mode]
12
+ # @param params [Hash] Hash containing all options
13
+ # params[:url] [String] Url to target
14
+ # params[:body] [JSON] json data for the request
15
+ # params[:token] [String] OAuth token
16
+ # params[:username] [String] Username if no token specified
17
+ # params[:password] [String] Password if no token specified
18
+ # params[:request_uri] [String] Some request, only need an URI....
19
+ # params[:action] [String] The HTTP action
20
+ #
21
+ url = params[:url]
22
+ action = params[:action]
23
+ token = params[:token]
24
+ username = params[:username]
25
+ password = params[:password]
26
+ body = params[:body]
27
+ request_uri = params[:request_uri] || ''
28
+
29
+ unless url || action then
30
+ puts "URL and ACTION not defined!"
31
+ exit 1
32
+ end
24
33
 
25
34
  Chef::Log.debug("URL: " + url.to_s)
26
35
 
36
+ url = "#{url}#{request_uri}"
27
37
  uri = URI.parse(url)
28
- http = http_client_builder.new(uri.host, uri.port)
29
-
38
+ http = http_builder.new(uri.host,uri.port)
30
39
  if uri.scheme == "https"
31
40
  http.use_ssl = true
32
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
41
+ if @ssl_verify_mode == "verify_none"
42
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
43
+ else
44
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
45
+ end
46
+ end
47
+
48
+ initheader = {}
49
+ if token
50
+ initheader = {"Authorization" => "token #{token}"}
51
+ Chef::Log.debug("Using token: #{token} for action: #{action} on URL: #{url}")
33
52
  end
34
-
35
- request = Net::HTTP::Get.new(uri.request_uri)
36
- response = http.request(request)
37
53
 
38
- unless response.is_a?(Net::HTTPOK) then
54
+ case action
55
+ when "GET"
56
+ if uri.request_uri.nil?
57
+ req = Net::HTTP::Get.new(uri.path, initheader)
58
+ else
59
+ req = Net::HTTP::Get.new(uri.request_uri, initheader)
60
+ end
61
+ when "POST"
62
+ req = Net::HTTP::Post.new(uri.path, initheader)
63
+ when "DELETE"
64
+ req = Net::HTTP::Delete.new(uri.path, initheader)
65
+ else
66
+ puts "Error, undefined action #{action}"
67
+ exit 1
68
+ end
69
+ if username && password
70
+ req.basic_auth username, password
71
+ Chef::Log.debug("Using basic_auth #{username}, #{password} for action: #{action} on URL: #{url}")
72
+ end
73
+
74
+ req.body = body if body
75
+ response = http.request(req)
76
+ validate = response_validator(response)
77
+ end
78
+
79
+ def response_validator(response)
80
+ unless response.code =~ /^2../ then
39
81
  puts "Error #{response.code}: #{response.message}"
40
82
  puts JSON.pretty_generate(JSON.parse(response.body))
41
- puts "URL: #{url}"
42
83
  exit 1
43
84
  end
44
85
 
@@ -52,11 +93,12 @@ module GithubClient
52
93
  return json
53
94
  end
54
95
 
55
- def http_client_builder
56
- http_proxy = proxy_uri
57
- if http_proxy.nil?
96
+ def http_builder
97
+ proxy = Chef::Config[:knife][:github_proxy]
98
+ if proxy.nil?
58
99
  Net::HTTP
59
100
  else
101
+ http_proxy = URI.parse(proxy)
60
102
  Chef::Log.debug("Using #{http_proxy.host}:#{http_proxy.port} for proxy")
61
103
  user = http_proxy.user if http_proxy.user
62
104
  pass = http_proxy.password if http_proxy.password
@@ -64,11 +106,6 @@ module GithubClient
64
106
  end
65
107
  end
66
108
 
67
- def proxy_uri
68
- return nil if @api_proxy.nil?
69
- result = URI.parse(@api_proxy)
70
- return result unless result.host.nil? || result.host.empty?
71
- nil
72
- end
73
109
  end
74
110
  end
111
+
@@ -76,7 +76,11 @@ module Github
76
76
 
77
77
  def get_tags(url)
78
78
  tags = []
79
- result = connection.send_get_request(url)
79
+ params = {}
80
+ params[:url] = url
81
+ params[:action] = "GET"
82
+ params[:token] = Chef::Config[:knife][:github_token]
83
+ result = connection.request(params)
80
84
  result.each { |tag| tags.push(tag['name']) if tag['name'] =~ /^(\d*)\.(\d*)\.(\d*)$/ }
81
85
  tags || nil
82
86
  end
@@ -1,6 +1,6 @@
1
1
  module Knife
2
2
  module Github
3
- VERSION = "0.1.4"
3
+ VERSION = "0.1.6"
4
4
  MAJOR, MINOR, TINY = VERSION.split('.')
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-github
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sander Botman
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-03 00:00:00.000000000 Z
12
+ date: 2014-12-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mixlib-versioning
@@ -57,16 +57,16 @@ dependencies:
57
57
  name: chef
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ! '>='
60
+ - - ~>
61
61
  - !ruby/object:Gem::Version
62
- version: 10.0.0
62
+ version: '11.0'
63
63
  type: :runtime
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ! '>='
67
+ - - ~>
68
68
  - !ruby/object:Gem::Version
69
- version: 10.0.0
69
+ version: '11.0'
70
70
  description: Github interaction support for chef's knife cli tool
71
71
  email:
72
72
  - sbotman@schubergphilis.com