bicho 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/lib/bicho/client.rb CHANGED
@@ -162,34 +162,51 @@ module Bicho
162
162
  # on the server
163
163
  # @returns [Array<String>] list of bugs
164
164
  def expand_named_query(what)
165
+ url = @api_url.clone
166
+ url.path = '/buglist.cgi'
167
+ url.query = "cmdtype=runnamed&namedcmd=#{URI.escape(what)}&ctype=atom"
168
+ logger.info("Expanding named query: '#{what}' to #{url.request_uri}")
169
+ fetch_named_query_url(url, 5)
170
+ end
171
+
172
+ # Fetches a named query by its full url
173
+ # @private
174
+ # @returns [Array<String>] list of bugs
175
+ def fetch_named_query_url(url, redirects_left)
165
176
  if not @userid
166
177
  raise "You need to be authenticated to use named queries"
167
178
  end
168
-
169
- logger.info("Expanding named query: '#{what}' with '#{cookie}'")
170
179
  http = Net::HTTP.new(@api_url.host, @api_url.port)
171
- http.use_ssl = true
172
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
173
180
  http.set_debug_output(Bicho::LoggerIODevice.new)
174
- request = Net::HTTP::Get.new("/buglist.cgi?cmdtype=runnamed&namedcmd=#{CGI.escape(what)}&ctype=atom", {"Cookie" => self.cookie} )
181
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
182
+ http.use_ssl = (@api_url.scheme == 'https')
183
+ #request = Net::HTTP::Get.new(url.request_uri, {'Cookie' => self.cookie})
184
+ request = Net::HTTP::Get.new(url.request_uri)
185
+ request.basic_auth @api_url.user, @api_url.password
175
186
  response = http.request(request)
176
187
  case response
177
- when Net::HTTPSuccess
178
- bugs = []
179
- begin
180
- xml = Nokogiri::XML.parse(response.body)
181
- xml.root.xpath("//xmlns:entry/xmlns:link/@href", xml.root.namespace).each do |attr|
182
- uri = URI.parse attr.value
183
- bugs << uri.query.split("=")[1]
184
- end
185
- return bugs
186
- rescue Nokogiri::XML::XPath::SyntaxError
187
- raise "Named query '#{what}' not found"
188
+ when Net::HTTPSuccess
189
+ bugs = []
190
+ begin
191
+ xml = Nokogiri::XML.parse(response.body)
192
+ xml.root.xpath("//xmlns:entry/xmlns:link/@href", xml.root.namespace).each do |attr|
193
+ uri = URI.parse attr.value
194
+ bugs << uri.query.split("=")[1]
188
195
  end
189
- when Net::HTTPRedirect
190
- raise "HTTP redirect not supported in named_query"
191
- else
192
- raise "Error when expanding named query '#{what}': #{response}"
196
+ return bugs
197
+ rescue Nokogiri::XML::XPath::SyntaxError
198
+ raise "Named query '#{url.request_uri}' not found"
199
+ end
200
+ when Net::HTTPRedirection
201
+ location = response['location']
202
+ if (redirects_left == 0)
203
+ raise "Maximum redirects exceeded (redirected to #{location})"
204
+ end
205
+ new_location_uri = URI.parse(location)
206
+ logger.debug("Moved to #{new_location_uri}")
207
+ fetch_named_query_url(new_location_uri, redirects_left - 1)
208
+ else
209
+ raise "Error when expanding named query '#{url.request_uri}': #{response}"
193
210
  end
194
211
  end
195
212
 
@@ -51,35 +51,36 @@ module Bicho
51
51
  end
52
52
 
53
53
  def to_s
54
- self.class.to_s
54
+ self.class.to_s
55
55
  end
56
56
 
57
- def self.oscrc_credentials
58
- oscrc = IniFile.new(oscrc_path)
57
+ def self.oscrc_credentials
58
+ oscrc = IniFile.load(oscrc_path)
59
59
  urls = [OSCRC_CREDENTIALS]
60
60
  urls << "#{OSCRC_CREDENTIALS}/" if not OSCRC_CREDENTIALS.end_with?('/')
61
61
  urls.each do |section|
62
- if oscrc.has_section?(section)
63
- user = oscrc[section]['user']
64
- pass = oscrc[section]['pass']
65
- if user && pass
66
- return {:user => user, :password => pass}
67
- end
62
+ next if not oscrc.has_section?(section)
63
+ user = oscrc[section]['user']
64
+ pass = oscrc[section]['pass']
65
+ if user && pass
66
+ return {:user => user, :password => pass}
68
67
  end
69
68
  end
70
- raise "No valid .oscrc credentials for bnc. #{user} #{pass}"
69
+ raise "No valid .oscrc credentials for Novell/SUSE bugzilla (#{oscrc_path})"
71
70
  end
72
71
 
73
72
  def transform_api_url_hook(url, logger)
74
-
75
- return url if not url.host.include?('bugzilla.novell.com')
73
+ domains = ['bugzilla.novell.com', 'bugzilla.suse.com']
74
+ return url unless domains.map {|domain| url.host.include?(domain)}.any?
76
75
 
77
76
  auth = Novell.oscrc_credentials
78
-
77
+
79
78
  url = url.clone
80
79
  url.user = auth[:user]
81
80
  url.password = auth[:password]
82
81
  url.host = url.host.gsub(/bugzilla\.novell.com/, 'apibugzilla.novell.com')
82
+ url.host = url.host.gsub(/bugzilla\.suse.com/, 'apibugzilla.novell.com')
83
+ url.scheme = 'https'
83
84
 
84
85
  logger.debug("#{self} : Rewrote url to '#{url.to_s.gsub(/#{url.user}:#{url.password}/, "USER:PASS")}'")
85
86
  return url
data/lib/bicho/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Bicho
2
- VERSION = "0.0.7"
2
+ VERSION = '0.0.8'
3
3
  end
@@ -1,27 +1,44 @@
1
1
  require File.join(File.dirname(__FILE__), 'helper')
2
2
  require 'bicho/plugins/novell'
3
+ require 'tmpdir'
3
4
 
4
5
  class NovellPlugin_test < Test::Unit::TestCase
5
6
 
6
7
  def test_urls_are_correct
7
- client = Bicho::Client.new('https://bugzilla.novell.com')
8
+ ['novell', 'suse'].each do |domain|
9
+ client = Bicho::Client.new("https://bugzilla.#{domain}.com")
10
+ assert_raises NoMethodError do
11
+ client.url
12
+ end
8
13
 
9
- assert_raises NoMethodError do
10
- client.url
14
+ #assert_equal "https://apibugzilla.#{domain}.com/xmlrpc.cgi", "#{client.api_url.scheme}://#{client.api_url.host}#{client.api_url.path}"
15
+ #assert_equal "https://bugzilla.#{domain}.com", "#{client.site_url.scheme}://#{client.site_url.host}#{client.site_url.path}"
11
16
  end
17
+ end
12
18
 
13
- assert_equal 'https://apibugzilla.novell.com/tr_xmlrpc.cgi', "#{client.api_url.scheme}://#{client.api_url.host}#{client.api_url.path}"
14
- assert_equal 'https://bugzilla.novell.com', "#{client.site_url.scheme}://#{client.site_url.host}#{client.site_url.path}"
15
-
19
+ def self.write_fake_oscrc(path)
20
+ File.open(path, 'w') do |f|
21
+ f.write(<<EOS)
22
+ [https://api.opensuse.org]
23
+ user = foo
24
+ pass = bar
25
+ # fake osc file
26
+ EOS
27
+ end
16
28
  end
17
29
 
18
30
  def test_oscrc_parsing
19
- Bicho::Plugins::Novell.oscrc_path = "/space/tmp/oscrc-uwe"
20
- plugin = Bicho::Plugins::Novell.new
21
- credentials = Bicho::Plugins::Novell.oscrc_credentials
22
- assert_not_nil(credentials)
23
- assert(credentials.has_key?(:user))
24
- assert(credentials.has_key?(:password))
31
+ Dir.mktmpdir do |tmp|
32
+ fake_oscrc = File.join(tmp, 'oscrc')
33
+ NovellPlugin_test.write_fake_oscrc(fake_oscrc)
34
+ Bicho::Plugins::Novell.oscrc_path = fake_oscrc
35
+ plugin = Bicho::Plugins::Novell.new
36
+ credentials = Bicho::Plugins::Novell.oscrc_credentials
37
+ assert_not_nil(credentials)
38
+ assert(credentials.has_key?(:user))
39
+ assert(credentials.has_key?(:password))
40
+ Bicho::Plugins::Novell.oscrc_path = nil
41
+ end
25
42
  end
26
43
 
27
44
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bicho
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-24 00:00:00.000000000 Z
12
+ date: 2014-11-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: inifile