Skip to content

Commit 8fe02ac

Browse files
committed
Merged in vpereirabr/ruby-bugzilla (pull request tagoh#6)
refactored some repeated code into own function
2 parents 2588fd7 + 9346aaa commit 8fe02ac

File tree

1 file changed

+98
-98
lines changed

1 file changed

+98
-98
lines changed

bin/bzconsole

Lines changed: 98 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,29 @@ rescue LoadError
4141
require 'bugzilla/bug'
4242
end
4343

44+
module BzConsole
45+
module Utils
46+
def get_xmlrpc(conf = {},opts = {})
47+
info = conf
48+
uri = URI.parse(info[:URL])
49+
host = uri.host
50+
port = uri.port
51+
path = uri.path.empty? ? nil : uri.path
52+
proxy_host, proxy_port = get_proxy(info)
53+
timeout = opts[:timeout].nil? ? 60 : opts[:timeout]
54+
yield host if block_given? # if you want to run some pre hook
55+
xmlrpc = Bugzilla::XMLRPC.new(host, port, path, proxy_host, proxy_port, timeout, uri.user, uri.password)
56+
[xmlrpc,host]
57+
end
58+
end
59+
end
60+
4461
module BzConsole
4562

4663
class CommandTemplate
4764

65+
include Utils
66+
4867
def initialize(plugin)
4968
@n_args = 0
5069
@defaultyamlfile = File.join(ENV['HOME'], ".bzconsole.yml")
@@ -112,7 +131,6 @@ module BzConsole
112131

113132
def initialize(plugin)
114133
super
115-
116134
@n_args = 0
117135
end # def initialize
118136

@@ -190,7 +208,6 @@ module BzConsole
190208

191209
def initialize(plugin)
192210
super
193-
194211
@n_args = 1
195212
end # def initialize
196213

@@ -217,17 +234,12 @@ module BzConsole
217234
end
218235

219236
info = conf[prefix]
220-
uri = URI.parse(info[:URL])
221-
host = uri.host
222-
port = uri.port
223-
path = uri.path.empty? ? nil : uri.path
224237
login = info[:User].nil? ? ask("Bugzilla ID: ") : info[:User]
225238
pass = info[:Password].nil? ? ask("Bugzilla password: ") {|q| q.echo = false} : info[:Password]
226-
proxy_host, proxy_port = get_proxy(info)
227-
timeout = opts[:timeout].nil? ? 60 : opts[:timeout]
228239

229-
xmlrpc = Bugzilla::XMLRPC.new(host, port, path, proxy_host, proxy_port, timeout, uri.user, uri.password)
240+
xmlrpc,host = get_xmlrpc(conf[prefix],opts)
230241
user = Bugzilla::User.new(xmlrpc)
242+
231243
begin
232244
result = user.login({'login'=>login, 'password'=>pass, 'remember'=>true})
233245
cconf[host] = xmlrpc.cookie
@@ -321,18 +333,13 @@ module BzConsole
321333
end
322334

323335
info = conf[prefix]
324-
uri = URI.parse(info[:URL])
325-
host = uri.host
326-
port = uri.port
327-
path = uri.path.empty? ? nil : uri.path
328-
login = opts[:command][:anonymous] == true ? nil : info[:User]
329-
pass = opts[:command][:anonymous] == true ? nil : info[:Password]
330-
proxy_host, proxy_port = get_proxy(info)
331-
timeout = opts[:timeout].nil? ? 60 : opts[:timeout]
336+
login = info[:User].nil? ? ask("Bugzilla ID: ") : info[:User]
337+
pass = info[:Password].nil? ? ask("Bugzilla password: ") {|q| q.echo = false} : info[:Password]
332338

333-
@plugin.run(:pre, host, :getbug, opts)
339+
xmlrpc,host = get_xmlrpc(conf[prefix],opts) do |h|
340+
@plugin.run(:pre, h, :getbug, opts)
341+
end
334342

335-
xmlrpc = Bugzilla::XMLRPC.new(host, port, path, proxy_host, proxy_port, timeout, uri.user, uri.password)
336343
user = Bugzilla::User.new(xmlrpc)
337344
user.session(login, pass) do
338345
bug = Bugzilla::Bug.new(xmlrpc)
@@ -449,19 +456,19 @@ module BzConsole
449456
xmlrpc = Bugzilla::XMLRPC.new(host, port, path, proxy_host, proxy_port, timeout, uri.user, uri.password)
450457
user = Bugzilla::User.new(xmlrpc)
451458
user.session(login, pass) do
452-
bug = Bugzilla::Bug.new(xmlrpc)
453-
454-
opts[:command][:query][:product].map! {|x| info.include?(:ProductAliases) && info[:ProductAliases].include?(x) ? info[:ProductAliases][x] : x} if opts[:command][:query].include?(:product)
459+
bug = Bugzilla::Bug.new(xmlrpc)
460+
opts[:command][:query][:product].map! { |x| info.include?(:ProductAliases) &&
461+
info[:ProductAliases].include?(x) ? info[:ProductAliases][x] : x } if opts[:command][:query].include?(:product)
455462

456-
result = bug.search(opts[:command][:query])
463+
result = bug.search(opts[:command][:query])
457464

458-
@plugin.run(:post, host, :search, result)
465+
@plugin.run(:post, host, :search, result)
459466

460-
if result.include?('bugs') then
461-
result['bugs'].each do |r|
462-
yield r
467+
if result.include?('bugs') then
468+
result['bugs'].each do |r|
469+
yield r
470+
end
463471
end
464-
end
465472
end
466473
end
467474
end # def real_do
@@ -633,7 +640,7 @@ module BzConsole
633640
timeline = data[:label]
634641
data.delete(:label)
635642
def timeline.to_hash
636-
ret = {}
643+
ret = {}
637644
(0..self.length-1).each do |i|
638645
ret[i] = self[i] unless self[i].nil?
639646
end
@@ -679,16 +686,11 @@ module BzConsole
679686
end
680687

681688
info = conf[prefix]
682-
uri = URI.parse(info[:URL])
683-
host = uri.host
684-
port = uri.port
685-
path = uri.path.empty? ? nil : uri.path
686-
login = opts[:command][:anonymous] == true ? nil : info[:User]
687-
pass = opts[:command][:anonymous] == true ? nil : info[:Password]
688-
proxy_host, proxy_port = get_proxy(info)
689-
timeout = opts[:timeout].nil? ? 60 : opts[:timeout]
689+
login = info[:User].nil? ? ask("Bugzilla ID: ") : info[:User]
690+
pass = info[:Password].nil? ? ask("Bugzilla password: ") {|q| q.echo = false} : info[:Password]
690691

691-
xmlrpc = Bugzilla::XMLRPC.new(host, port, path, proxy_host, proxy_port, timeout, uri.user, uri.password)
692+
693+
xmlrpc,host = get_xmlrpc(conf[prefix],opts)
692694
user = Bugzilla::User.new(xmlrpc)
693695
user.session(login, pass) do
694696
bug = Bugzilla::Bug.new(xmlrpc)
@@ -711,97 +713,95 @@ module BzConsole
711713

712714
@plugin.run(:pre, host, :metrics, searchopts, opts[:metrics])
713715

714-
if searchopts == opts[:command][:query] then
715-
raise ArgumentError, "No method to deal with the query. specify search options."
716-
end
716+
raise NoMethodError, "No method to deal with the query" if searchopts == opts[:command][:query]
717717

718718
while ts < te do
719-
searchopts = opts[:command][:query].clone
720-
721-
# don't rely on the status to deal with NEW bugs.
722-
# unable to estimate the case bugs closed quickly
723-
if opts[:command][:metrics][:x_coordinate] == :weekly then
724-
d = Date.new(ts.year, ts.month, ts.day)
725-
de = Date.commercial(d.year, d.cweek, 7)
726-
drange = [ts, Time.utc(de.year, de.month, de.day, 23, 59, 59)]
727-
else
728-
drange = [ts, Time.utc(ts.year, ts.month + 1, 1) - 1]
729-
end
730-
searchopts[:creation_time] = drange
719+
searchopts = opts[:command][:query].clone
720+
721+
# don't rely on the status to deal with NEW bugs.
722+
# unable to estimate the case bugs closed quickly
723+
if opts[:command][:metrics][:x_coordinate] == :weekly then
724+
d = Date.new(ts.year, ts.month, ts.day)
725+
de = Date.commercial(d.year, d.cweek, 7)
726+
drange = [ts, Time.utc(de.year, de.month, de.day, 23, 59, 59)]
727+
else
728+
drange = [ts, Time.utc(ts.year, ts.month + 1, 1) - 1]
729+
end
731730

732-
@plugin.run(:pre, host, :metrics, searchopts)
731+
searchopts[:creation_time] = drange
733732

734-
result = bug.search(searchopts)
733+
@plugin.run(:pre, host, :metrics, searchopts)
735734

736-
@plugin.run(:post, host, :search, result)
735+
result = bug.search(searchopts)
737736

738-
new = result.include?('bugs') ? result['bugs'].length : 0
737+
@plugin.run(:post, host, :search, result)
739738

740-
# for open bugs
741-
# what we are interested in here would be how many bugs still keeps open.
742-
searchopts = opts[:command][:query].clone
743-
searchopts[:last_change_time] = drange
744-
searchopts[:status] = '__open__'
739+
new = result.include?('bugs') ? result['bugs'].length : 0
745740

746-
@plugin.run(:pre, host, :metrics, searchopts)
741+
# for open bugs
742+
# what we are interested in here would be how many bugs still keeps open.
743+
searchopts = opts[:command][:query].clone
744+
searchopts[:last_change_time] = drange
745+
searchopts[:status] = '__open__'
747746

748-
result = bug.search(searchopts)
747+
@plugin.run(:pre, host, :metrics, searchopts)
749748

750-
@plugin.run(:post, host, :search, result)
749+
result = bug.search(searchopts)
751750

752-
assigned = result.include?('bugs') ? result['bugs'].map{|x| x['status'] == 'ASSIGNED' ? x : nil}.compact.length : 0
753-
modified = result.include?('bugs') ? result['bugs'].map{|x| x['status'] == 'MODIFIED' ? x : nil}.compact.length : 0
754-
on_qa = result.include?('bugs') ? result['bugs'].map{|x| x['status'] == 'ON_QA' ? x : nil}.compact.length : 0
751+
@plugin.run(:post, host, :search, result)
755752

756-
# send a separate query for closed.
757-
# just counting CLOSED the above is meaningless.
758-
# what we are interested in here would be how much bugs are
759-
# actually closed, but not how many closed bugs one worked on.
760-
searchopts = opts[:command][:query].clone
761-
searchopts[:last_change_time] = drange
762-
searchopts[:status] = 'CLOSED'
753+
assigned = result.include?('bugs') ? result['bugs'].map{|x| x['status'] == 'ASSIGNED' ? x : nil}.compact.length : 0
754+
modified = result.include?('bugs') ? result['bugs'].map{|x| x['status'] == 'MODIFIED' ? x : nil}.compact.length : 0
755+
on_qa = result.include?('bugs') ? result['bugs'].map{|x| x['status'] == 'ON_QA' ? x : nil}.compact.length : 0
763756

764-
@plugin.run(:pre, host, :metrics, searchopts)
757+
# send a separate query for closed.
758+
# just counting CLOSED the above is meaningless.
759+
# what we are interested in here would be how much bugs are
760+
# actually closed, but not how many closed bugs one worked on.
761+
searchopts = opts[:command][:query].clone
762+
searchopts[:last_change_time] = drange
763+
searchopts[:status] = 'CLOSED'
765764

766-
result = bug.search(searchopts)
765+
@plugin.run(:pre, host, :metrics, searchopts)
767766

768-
@plugin.run(:post, host, :search, result)
767+
result = bug.search(searchopts)
769768

770-
closed = result.include?('bugs') ? result['bugs'].length : 0
769+
@plugin.run(:post, host, :search, result)
771770

772-
# obtain the information for open bugs that closed now
773-
searchopts = opts[:command][:query].clone
774-
searchopts[:status] = 'CLOSED'
775-
searchopts[:metrics_closed_after] = drange[1] + 1
771+
closed = result.include?('bugs') ? result['bugs'].length : 0
776772

777-
@plugin.run(:pre, host, :metrics, searchopts)
773+
# obtain the information for open bugs that closed now
774+
searchopts = opts[:command][:query].clone
775+
searchopts[:status] = 'CLOSED'
776+
searchopts[:metrics_closed_after] = drange[1] + 1
778777

779-
result = bug.search(searchopts)
778+
@plugin.run(:pre, host, :metrics, searchopts)
780779

781-
@plugin.run(:post, host, :search, result)
780+
result = bug.search(searchopts)
782781

783-
open_bugs = result.include?('bugs') ? result['bugs'].length : 0
782+
@plugin.run(:post, host, :search, result)
784783

785-
# obtain the information for open bugs
786-
searchopts = opts[:command][:query].clone
787-
searchopts[:metrics_not_closed] = drange[1]
784+
open_bugs = result.include?('bugs') ? result['bugs'].length : 0
788785

789-
@plugin.run(:pre, host, :metrics, searchopts)
786+
# obtain the information for open bugs
787+
searchopts = opts[:command][:query].clone
788+
searchopts[:metrics_not_closed] = drange[1]
790789

791-
result = bug.search(searchopts)
790+
@plugin.run(:pre, host, :metrics, searchopts)
792791

793-
@plugin.run(:post, host, :search, result)
792+
result = bug.search(searchopts)
794793

795-
open_bugs += result.include?('bugs') ? result['bugs'].length : 0
794+
@plugin.run(:post, host, :search, result)
796795

797-
yield ts, new, assigned, modified, on_qa, closed, open_bugs
796+
open_bugs += result.include?('bugs') ? result['bugs'].length : 0
798797

799-
ts = drange[1] + 1
800-
end
798+
yield ts, new, assigned, modified, on_qa, closed, open_bugs
799+
800+
ts = drange[1] + 1
801+
end #while
801802
end
802803
end
803804
end # def real_do
804-
805805
end # class Metrics
806806

807807
class Newbug < CommandTemplate

0 commit comments

Comments
 (0)