knife-github 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,7 +17,6 @@
17
17
  #
18
18
 
19
19
  # require 'chef/knife'
20
- require "knife-github/version"
21
20
 
22
21
  class Chef
23
22
  class Knife
@@ -29,7 +28,10 @@ class Chef
29
28
  deps do
30
29
  require 'chef/mixin/shell_out'
31
30
  require 'mixlib/versioning'
32
- require 'chef/knife/github_config'
31
+ require 'knife-github/repo'
32
+ require 'knife-github/config'
33
+ require 'knife-github/version'
34
+ require 'knife-github/connection'
33
35
  end
34
36
 
35
37
  option :github_url,
@@ -38,7 +40,7 @@ class Chef
38
40
 
39
41
  option :github_organizations,
40
42
  :long => "--github_org ORG:ORG",
41
- :description => "Lookup chef cookbooks in this colon-separated list of organizations",
43
+ :description => "Lookup repositories in this colon-separated list of organizations",
42
44
  :proc => lambda { |o| o.split(":") }
43
45
 
44
46
  option :github_link,
@@ -148,144 +150,77 @@ class Chef
148
150
  # Parse every org and merge all into one hash
149
151
  repos = {}
150
152
  orgs.each do |org|
151
- get_repos(org).each { |repo| name = repo['name'] ; repos["#{name}"] = repo }
153
+ get_org_data(org).each { |repo|
154
+ name = repo['name']
155
+ repos["#{name}"] = repo.to_hash
156
+ }
152
157
  end
153
158
  repos
154
159
  end
155
160
 
156
- def get_repos(org)
157
- dns_name = get_dns_name(@github_url)
158
- file_cache = "#{ENV['HOME']}/.chef/.#{dns_name.downcase}_#{org.downcase}"
159
-
160
- if File.exists?(file_cache + ".json")
161
- json = JSON.parse(File.read(file_cache + ".json"))
162
- json_updated = Time.parse(json['updated_at'])
163
- Chef::Log.info("#{org} - cache created at : " + json_updated.to_s)
164
- repo_updated = get_org_updated_time(org)
165
- Chef::Log.info("#{org} - repos updated at : " + repo_updated.to_s)
166
-
167
- unless json_updated >= repo_updated
168
- # update cache file
169
- create_cache_file(file_cache + ".cache", org)
170
- create_cache_json(file_cache + ".json", org)
161
+ def get_org_data(org)
162
+ dns_name = get_dns_name(@github_url)
163
+ file = ENV['HOME'] + "/.chef/.#{dns_name}_#{org.downcase}.cache"
164
+
165
+ cache_repo_data = get_cache_data(file)
166
+ github_repo_data = get_github_repo_data(org)
167
+
168
+ github_repoList = Github::RepoList.new
169
+
170
+ github_repo_data.each do |repo|
171
+ github_repoList.push(repo)
172
+ cache_repo = cache_repo_data.find { |k| k['name'] == repo.name }
173
+ if cache_repo
174
+ # found cache repo, update tags if latest_update is not equal
175
+ if repo.updated_at == cache_repo['updated_at']
176
+ github_repoList.last.tags_all = cache_repo['tags_all']
177
+ github_repoList.last.tags_last = cache_repo['tags_last']
178
+ else
179
+ github_repoList.last.update_tags!
180
+ end
181
+ else
182
+ # cannot find cache repo data, updating tags
183
+ github_repoList.last.update_tags!
171
184
  end
172
- else
173
- create_cache_file(file_cache + ".cache", org)
174
- create_cache_json(file_cache + ".json", org)
175
185
  end
176
-
177
- # use cache files
178
- JSON.parse(File.read(file_cache + ".cache"))
186
+ write_cache_data(file, github_repoList)
187
+ get_cache_data(file)
179
188
  end
180
-
181
- def create_cache_json(file, org)
182
- Chef::Log.debug("Updating the cache file: #{file}")
183
- url = @github_url + "/api/" + @github_api_version + "/orgs/" + org
184
- params = {'response' => 'json'}
185
- result = send_request(url, params)
186
- File.open(file, 'w') { |f| f.write(JSON.pretty_generate(result)) }
189
+
190
+ def connection
191
+ @connection ||= GithubClient::Connection.new()
187
192
  end
188
193
 
189
- def create_cache_file(file, org)
190
- Chef::Log.debug("Updating the cache file: #{file}")
191
- result = get_repos_github(org)
192
- File.open(file, 'w') { |f| f.write(JSON.pretty_generate(result)) }
194
+ def write_cache_data(file, json)
195
+ File.open(file, 'w') { |f| f.write(json.to_pretty_json) }
193
196
  end
194
-
195
- def get_org_updated_time(org)
196
- url = @github_url + "/api/" + @github_api_version + "/orgs/" + org
197
- params = {'response' => 'json'}
198
- result = send_request(url, params)
199
- Time.parse(result['updated_at'])
197
+
198
+ def get_cache_data(file)
199
+ if File.exists?(file)
200
+ return JSON.parse(File.read(file))
201
+ else
202
+ return JSON.parse("{}")
203
+ end
200
204
  end
201
-
202
- def get_repos_github(org)
203
- # Get all repo's from cache file
204
-
205
- # Get all repo's for the org from github
205
+
206
+ def get_github_repo_data(org)
206
207
  arr = []
207
208
  page = 1
208
- url = @github_url + "/api/" + @github_api_version + "/orgs/" + org + "/repos"
209
+ url = @github_url + "/api/" + @github_api_version + "/orgs/" + org + "/repos"
209
210
  while true
210
211
  params = {'response' => 'json', 'page' => page }
211
- result = send_request(url, params)
212
+ result = connection.send_get_request(url, params)
212
213
  break if result.nil? || result.count < 1
213
- result.each { |key| arr << key }
214
+ result.each { |key| arr << Github::Repo.new(key) }
214
215
  page = page + 1
215
216
  end
216
- # WE WILL REMOVE THIS, GETTING TAGS FOR EVERY REPO IS VERY SLOW!
217
- # if key['tags_url']
218
- # tags = get_tags(key)
219
- # key['tags'] = tags unless tags.nil? || tags.empty?
220
- # key['latest_tag'] = get_latest_tag(tags)
221
- # arr << key
222
- # else
223
- # arr << key
224
- # end
225
- #}
226
- arr
217
+ return arr
227
218
  end
228
-
229
- def get_tags(repo)
230
- params = {'response' => 'json'}
231
- send_request(repo['tags_url'], params)
232
- end
233
-
234
- def get_latest_tag(tags)
235
- return "" if tags.nil? || tags.empty?
236
- tags_arr =[]
237
- tags.each do |tag|
238
- tags_arr.push(Mixlib::Versioning.parse(tag['name'])) if tag['name'] =~ /^(\d*)\.(\d*)\.(\d*)$/
239
- end
240
- return "" if tags_arr.nil? || tags_arr.empty?
241
- return tags_arr.sort.last.to_s
242
- end
243
-
219
+
244
220
  def get_dns_name(url)
245
221
  url = url.downcase.gsub("http://","") if url.downcase.start_with?("http://")
246
222
  url = url.downcase.gsub("https://","") if url.downcase.start_with?("https://")
247
- url
248
- end
249
-
250
- def send_request(url, params = {})
251
- unless params.empty?
252
- params_arr = []
253
- params.sort.each { |elem|
254
- params_arr << elem[0].to_s + '=' + CGI.escape(elem[1].to_s).gsub('+', '%20').gsub(' ','%20')
255
- }
256
- data = params_arr.join('&')
257
- url = "#{url}?#{data}"
258
- end
259
-
260
- if @github_ssl_verify_mode == "verify_none"
261
- config[:ssl_verify_mode] = :verify_none
262
- elsif @github_ssl_verify_mode == "verify_peer"
263
- config[:ssl_verify_mode] = :verify_peer
264
- end
265
-
266
- Chef::Log.debug("URL: " + url.to_s)
267
-
268
- uri = URI.parse(url)
269
- req_body = Net::HTTP::Get.new(uri.request_uri)
270
- request = Chef::REST::RESTRequest.new("GET", uri, req_body, headers={})
271
-
272
- response = request.call
273
-
274
- unless response.is_a?(Net::HTTPOK) then
275
- puts "Error #{response.code}: #{response.message}"
276
- puts JSON.pretty_generate(JSON.parse(response.body))
277
- puts "URL: #{url}"
278
- exit 1
279
- end
280
-
281
- begin
282
- json = JSON.parse(response.body)
283
- rescue
284
- ui.warn "The result on the RESTRequest is not in json format"
285
- ui.warn "Output: " + response.body
286
- exit 1
287
- end
288
- return json
223
+ url.downcase
289
224
  end
290
225
 
291
226
  def get_clone(url, cookbook)
@@ -16,9 +16,6 @@
16
16
  # limitations under the License.
17
17
  #
18
18
 
19
-
20
- require 'chef/knife'
21
-
22
19
  class Chef
23
20
  class Knife
24
21
  module GithubBaseList
@@ -53,7 +53,7 @@ class Chef
53
53
 
54
54
  #Get the github link
55
55
  git_link = get_repo_clone_link
56
-
56
+
57
57
  # Filter all repo information based on the tags that we can find
58
58
  if config[:fields] || config[:fieldlist]
59
59
  all_repos = get_all_repos
@@ -68,7 +68,7 @@ class Chef
68
68
  else
69
69
  cookbooks.each { |k,v|
70
70
  get_all_repos[k].nil? || get_all_repos[k][git_link].nil? ? gh_url = ui.color("Cannot find cookbook!", :red) : gh_url = get_all_repos[k][git_link]
71
- get_all_repos[k].nil? || get_all_repos[k]['latest_tag'].nil? || get_all_repos[k]['latest_tag'].empty? ? gh_tag = ui.color("No tags!", :red) : gh_tag = get_all_repos[k]['latest_tag']
71
+ get_all_repos[k].nil? || get_all_repos[k]['tags_last'].nil? || get_all_repos[k]['tags_last'].empty? ? gh_tag = ui.color("No tags!", :red) : gh_tag = get_all_repos[k]['tags_last']
72
72
  all_repos[k] = { 'name' => k, 'latest_cb_tag' => v['versions'][0]['version'], 'git_url' => gh_url, 'latest_gh_tag' => gh_tag }
73
73
  }
74
74
  end
@@ -92,7 +92,6 @@ class Chef
92
92
  end
93
93
 
94
94
  end
95
-
96
95
  end
97
96
  end
98
97
  end
@@ -0,0 +1,74 @@
1
+ require 'json'
2
+ require 'uri'
3
+ require 'cgi'
4
+ require 'openssl'
5
+ require 'net/http'
6
+
7
+ module GithubClient
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
+
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
24
+
25
+ Chef::Log.debug("URL: " + url.to_s)
26
+
27
+ uri = URI.parse(url)
28
+ http = http_client_builder.new(uri.host, uri.port)
29
+
30
+ if uri.scheme == "https"
31
+ http.use_ssl = true
32
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
33
+ end
34
+
35
+ request = Net::HTTP::Get.new(uri.request_uri)
36
+ response = http.request(request)
37
+
38
+ unless response.is_a?(Net::HTTPOK) then
39
+ puts "Error #{response.code}: #{response.message}"
40
+ puts JSON.pretty_generate(JSON.parse(response.body))
41
+ puts "URL: #{url}"
42
+ exit 1
43
+ end
44
+
45
+ begin
46
+ json = JSON.parse(response.body)
47
+ rescue
48
+ ui.warn "The result on the RESTRequest is not in json format"
49
+ ui.warn "Output: " + response.body
50
+ exit 1
51
+ end
52
+ return json
53
+ end
54
+
55
+ def http_client_builder
56
+ http_proxy = proxy_uri
57
+ if http_proxy.nil?
58
+ Net::HTTP
59
+ else
60
+ Chef::Log.debug("Using #{http_proxy.host}:#{http_proxy.port} for proxy")
61
+ user = http_proxy.user if http_proxy.user
62
+ pass = http_proxy.password if http_proxy.password
63
+ Net::HTTP.Proxy(http_proxy.host, http_proxy.port, user, pass)
64
+ end
65
+ end
66
+
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
+ end
74
+ end
@@ -0,0 +1,162 @@
1
+ require 'mixlib/versioning'
2
+ require 'knife-github/connection'
3
+
4
+ module Github
5
+ class Repo
6
+
7
+ def initialize(repo)
8
+ # Instance variables
9
+ @id = repo['id']
10
+ @name = repo['name']
11
+ @description = repo['description']
12
+ @full_name = repo['full_name']
13
+ @private = repo['private']
14
+ @homapage = repo['homepage']
15
+ @created_at = repo['created_at']
16
+ @updated_at = repo['updated_at']
17
+ @pushed_at = repo['pushed_at']
18
+ @html_url = repo['html_url']
19
+ @ssh_url = repo['ssh_url']
20
+ @git_url = repo['git_url']
21
+ @svn_url = repo['svn_url']
22
+ @clone_url = repo['clone_url']
23
+ @tags_url = repo['tags_url']
24
+ @tags_all = repo['tags_all']
25
+ @tags_last = repo['tags_last']
26
+ end
27
+
28
+ attr_reader :name, :id, :updated_at
29
+ attr_writer :tags_all, :tags_last
30
+
31
+ def last_tag?
32
+ get_last_tag(@tags_all)
33
+ end
34
+
35
+ def to_s
36
+ @name
37
+ end
38
+
39
+ def update_tags!
40
+ if @tags_url
41
+ @tags_all = get_tags(@tags_url)
42
+ @tags_last = get_last_tag(@tags_all)
43
+ end
44
+ self
45
+ end
46
+
47
+ def to_hash
48
+ {
49
+ 'id' => @id,
50
+ 'name' => @name,
51
+ 'description' => @description,
52
+ 'full_name' => @full_name,
53
+ 'private' => @private,
54
+ 'homepage' => @homepage,
55
+ 'created_at' => @created_at,
56
+ 'updated_at' => @updated_at,
57
+ 'pushed_at' => @pushed_at,
58
+ 'html_url' => @html_url,
59
+ 'ssh_url' => @ssh_url,
60
+ 'git_url' => @git_url,
61
+ 'svn_url' => @svn_url,
62
+ 'clone_url' => @clone_url,
63
+ 'tags_url' => @tags_url,
64
+ 'tags_all' => @tags_all,
65
+ 'tags_last' => @tags_last
66
+ }
67
+ end
68
+
69
+ def get_repo_data(orgs)
70
+ orgs.each do |org|
71
+ get_org_data(org)
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ def get_tags(url)
78
+ tags = []
79
+ result = connection.send_get_request(url)
80
+ result.each { |tag| tags.push(tag['name']) if tag['name'] =~ /^(\d*)\.(\d*)\.(\d*)$/ }
81
+ tags || nil
82
+ end
83
+
84
+ def get_last_tag(tags)
85
+ return nil if tags.nil? || tags.empty?
86
+ base, last = "0.0.0", nil
87
+ tags.each do |tag|
88
+ if Mixlib::Versioning.parse(tag) >= Mixlib::Versioning.parse(base)
89
+ last = tag
90
+ base = last
91
+ end
92
+ end
93
+ last
94
+ end
95
+
96
+ def connection
97
+ @connection ||= GithubClient::Connection.new()
98
+ end
99
+ end
100
+
101
+ class RepoList
102
+ def initialize
103
+ # Instance variables
104
+ @repos = Array.new
105
+ end
106
+
107
+ def push(aRepo)
108
+ pos = self.find_index(aRepo.name)
109
+ if pos
110
+ @repos[pos] = aRepo
111
+ else
112
+ @repos.push(aRepo)
113
+ end
114
+ self
115
+ end
116
+
117
+ def shift
118
+ @repos.shift
119
+ end
120
+
121
+ def pop
122
+ @repos.pop
123
+ end
124
+
125
+ def count
126
+ @repos.count
127
+ end
128
+
129
+ def delete(key)
130
+ @repos.delete(self.find(key))
131
+ self
132
+ end
133
+
134
+ def last
135
+ @repos.last
136
+ end
137
+
138
+ def find(key)
139
+ @repos.find { |aRepo| aRepo.name == key }
140
+ end
141
+
142
+ def find_index(key)
143
+ @repos.find_index { |aRepo| aRepo.name == key }
144
+ end
145
+
146
+ def [](key)
147
+ return @repos[key] if key.kind_of?(Integer)
148
+ return @repos.find { |aRepo| aRepo.name == key }
149
+ nil
150
+ end
151
+
152
+ def to_pretty_json
153
+ json = []
154
+ @repos.each do |repo|
155
+ json << repo.to_hash
156
+ end
157
+ JSON.pretty_generate(json)
158
+ end
159
+
160
+ end
161
+ end
162
+
@@ -1,6 +1,6 @@
1
1
  module Knife
2
2
  module Github
3
- VERSION = "0.0.8"
3
+ VERSION = "0.0.9"
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.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-11-17 00:00:00.000000000 Z
13
+ date: 2013-11-18 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mixlib-versioning
@@ -91,13 +91,15 @@ files:
91
91
  - lib/chef/knife/github_cleanup.rb
92
92
  - lib/chef/knife/github_clone.rb
93
93
  - lib/chef/knife/github_compare.rb
94
- - lib/chef/knife/github_config.rb
95
94
  - lib/chef/knife/github_create.rb
96
95
  - lib/chef/knife/github_deploy.rb
97
96
  - lib/chef/knife/github_diff.rb
98
97
  - lib/chef/knife/github_list.rb
99
98
  - lib/chef/knife/github_pin.rb
100
99
  - lib/chef/knife/github_search.rb
100
+ - lib/knife-github/config.rb
101
+ - lib/knife-github/connection.rb
102
+ - lib/knife-github/repo.rb
101
103
  - lib/knife-github/version.rb
102
104
  homepage: https://github.com/schubergphilis/knife-github
103
105
  licenses: