diff options
author | Alex Legler <alex@a3li.li> | 2011-03-14 19:10:23 +0100 |
---|---|---|
committer | Alex Legler <alex@a3li.li> | 2011-03-14 19:10:23 +0100 |
commit | 3950d2e809b5086b5e0fd0cfa80753e855712fad (patch) | |
tree | 9e3ad4414524e70b07fb2cd413c7c25b719afafa | |
parent | Add basic release functionality to the GLSA model (diff) | |
download | glsamaker-3950d2e809b5086b5e0fd0cfa80753e855712fad.tar.gz glsamaker-3950d2e809b5086b5e0fd0cfa80753e855712fad.tar.bz2 glsamaker-3950d2e809b5086b5e0fd0cfa80753e855712fad.zip |
lib/bugzilla.rb: Use the XMLRPC API of Bugzilla 4.0
-rw-r--r-- | lib/bugzilla.rb | 253 |
1 files changed, 129 insertions, 124 deletions
diff --git a/lib/bugzilla.rb b/lib/bugzilla.rb index 5c8adea..5425b43 100644 --- a/lib/bugzilla.rb +++ b/lib/bugzilla.rb @@ -12,6 +12,7 @@ require 'nokogiri' require 'fastercsv' require 'fileutils' +require 'xmlrpc/client' module Bugzilla ; end @@ -19,153 +20,157 @@ module Bugzilla ; end # Bugzilla module module Bugzilla - # Looks on bugs.gentoo.org for all bugs that are in the [glsa] state - module_function - def find_glsa_bugs - url="http://bugs.gentoo.org/buglist.cgi?bug_file_loc=&bug_file_loc_type=allwordssubstr&bug_id=&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&bugidtype=include&chfieldfrom=&chfieldto=Now&chfieldvalue=&component=Vulnerabilities&email1=&email2=&emailassigned_to1=1&emailassigned_to2=1&emailcc2=1&emailreporter2=1&emailtype1=substring&emailtype2=substring&field-1-0-0=product&field-1-1-0=component&field-1-2-0=bug_status&field-1-3-0=status_whiteboard&field0-0-0=noop&keywords=&keywords_type=allwords&long_desc=&long_desc_type=substring&product=Gentoo%20Security&query_format=advanced&remaction=&short_desc=&short_desc_type=allwordssubstr&status_whiteboard=%5Bglsa%5D&status_whiteboard_type=substring&type-1-0-0=anyexact&type-1-1-0=anyexact&type-1-2-0=anyexact&type-1-3-0=substring&type0-0-0=noop&value-1-0-0=Gentoo%20Security&value-1-1-0=Vulnerabilities&value-1-2-0=UNCONFIRMED%2CNEW%2CASSIGNED%2CREOPENED&value-1-3-0=%5Bglsa%5D&value0-0-0=&votes=&ctype=csv" - bugs = [] - - FasterCSV.parse(Glsamaker::HTTP.get(url)) do |row| - bugs << { "bug_id" => row.shift, - "severity" => row.shift, - "priority" => row.shift, - "os" => row.shift, - "assignee" => row.shift, - "status" => row.shift, - "resolution" => row.shift, - "summary" => row.shift - } - end - - bugs.shift - bugs - end - + # Adds a comment to a bug. Returns the comment id on success, raises an exception on failure. def add_comment(bug, comment) - Float(bug) - cookie_file = File.join(RAILS_ROOT, 'tmp', 'bugzie-cookies.yaml') - - a = Mechanize.new { |agent| - agent.user_agent = "GLSAMaker/#{GLSAMAKER_VERSION} (http://security.gentoo.org/)" - } - - a.cookie_jar.load(cookie_file) - - a.get("https://bugs.gentoo.org/show_bug.cgi?id=%s" % bug) do |page| - unless page.body.include? '*+*GLSAMAKER-LOGGEDIN*+*' - log_in() - a.cookie_jar.load(cookie_file) + Rails.logger.debug 'Called Bugzilla.add_comment' + did_retry = false + + begin + client = xmlrpc_client + + result = client.call('Bug.add_comment', { + 'id' => bug.to_i, + 'comment' => comment + }) + result['id'] + rescue XMLRPC::FaultException => e + if did_retry + raise "Could not add the comment: #{e.faultString} (code #{e.faultCode})" end - post_result = page.form_with(:name => 'changeform') do |form| - form.comment = comment - end.submit - - raise unless post_result.body.include? "Changes submitted for bug" + # If we need to log in first + if e.faultCode == 410 + Rails.logger.debug "Not logged in, doing that now." + log_in + did_retry = true + retry + else + raise "Could not add the comment: #{e.faultString} (code #{e.faultCode})" + end end + end + # Updates a bug. Returns an array of changes that were done on the bug. def update_bug(bug, changes = {}) - Rails.logger.debug "Called Bugzilla.update_bug" - Float(bug) - cookie_file = File.join(RAILS_ROOT, 'tmp', 'bugzie-cookies.yaml') - - a = Mechanize.new { |agent| - agent.user_agent = "GLSAMaker/#{GLSAMAKER_VERSION} (http://security.gentoo.org/)" - } - - log_in unless File.exist? cookie_file - a.cookie_jar.load(cookie_file) + Rails.logger.debug 'Called Bugzilla.update_bug' + did_retry = false + + begin + client = xmlrpc_client + + rpc_data = {} + rpc_data['ids'] = [bug] + rpc_data['component'] = changes[:component] if changes.has_key?(:component) + rpc_data['product'] = changes[:product] if changes.has_key?(:product) + rpc_data['summary'] = changes[:summary] if changes.has_key?(:summary) + rpc_data['version'] = changes[:version] if changes.has_key?(:version) + rpc_data['comment'] = {'body' => changes[:comment]} if changes.has_key?(:comment) + rpc_data['priority'] = changes[:priority] if changes.has_key?(:priority) + rpc_data['severity'] = changes[:severity] if changes.has_key?(:severity) + rpc_data['alias'] = changes[:alias] if changes.has_key?(:alias) + rpc_data['assigned_to'] = changes[:assignee] if changes.has_key?(:assignee) + #rpc_data['cc'] = changes[:cc].to_a if changes.has_key?(:cc) TODO: add and remove + rpc_data['status'] = changes[:status] if changes.has_key?(:status) + rpc_data['whiteboard'] = changes[:whiteboard] if changes.has_key?(:whiteboard) + rpc_data['url'] = changes[:url] if changes.has_key?(:url) + rpc_data['resolution'] = changes[:resolution] if changes.has_key?(:resolution) + + result = client.call('Bug.update', rpc_data) + result['bugs'].first + rescue XMLRPC::FaultException => e + if did_retry + raise "Could not file the bug: #{e.faultString} (code #{e.faultCode})" + end - a.get("https://bugs.gentoo.org/show_bug.cgi?id=%s" % bug) do |page| - unless page.body.include? '*+*GLSAMAKER-LOGGEDIN*+*' + # If we need to log in first + if e.faultCode == 410 Rails.logger.debug "Not logged in, doing that now." - log_in() - a.cookie_jar.load(cookie_file) + log_in + did_retry = true + retry + else + raise "Could not file the bug: #{e.faultString} (code #{e.faultCode})" end - - post_result = page.form_with(:name => 'changeform') do |form| - form.alias = changes[:alias] if changes.has_key?(:alias) - form.newcc = changes[:newcc] if changes.has_key?(:newcc) - form.bug_file_log = changes[:url] if changes.has_key?(:url) - form.short_desc = changes[:summary] if changes.has_key?(:summary) - form.status_whiteboard = changes[:whiteboard] if changes.has_key?(:whiteboard) - form.keywords = changes[:keywords] if changes.has_key?(:keywords) - form.dependson = changes[:depends] if changes.has_key?(:depends) - form.blocked = changes[:blocks] if changes.has_key?(:blocks) - form.comment = changes[:comment] if changes.has_key?(:comment) - form.knob = changes[:knob] if changes.has_key?(:knob) - form.assigned_to = changes[:assignee] if changes.has_key?(:assignee) - end.submit - - raise unless post_result.body.include? "Changes submitted for bug" end end - + # Files a bug, and returns the id of the filed bug def file_bug(data) - Rails.logger.debug "Called Bugzilla.file_bug" - cookie_file = File.join(RAILS_ROOT, 'tmp', 'bugzie-cookies.yaml') - - a = Mechanize.new { |agent| - agent.user_agent = "GLSAMaker/#{GLSAMAKER_VERSION} (http://security.gentoo.org/)" - } - - log_in unless File.exist? cookie_file - a.cookie_jar.load(cookie_file) + Rails.logger.debug 'Called Bugzilla.file_bug' + did_retry = false + + begin + client = xmlrpc_client + + rpc_data = {} + rpc_data['component'] = data[:component] if data.has_key?(:component) + rpc_data['product'] = data[:product] if data.has_key?(:product) + rpc_data['summary'] = data[:summary] if data.has_key?(:summary) + rpc_data['version'] = data[:version] if data.has_key?(:version) + rpc_data['description'] = data[:comment] if data.has_key?(:comment) + rpc_data['priority'] = data[:priority] if data.has_key?(:priority) + rpc_data['severity'] = data[:severity] if data.has_key?(:severity) + rpc_data['alias'] = data[:alias] if data.has_key?(:alias) + rpc_data['assigned_to'] = data[:assignee] if data.has_key?(:assignee) + rpc_data['cc'] = data[:cc].to_a if data.has_key?(:cc) + rpc_data['status'] = data[:status] if data.has_key?(:status) + + result = client.call('Bug.create', rpc_data) + result['id'] + rescue XMLRPC::FaultException => e + if did_retry + raise "Could not file the bug: #{e.faultString} (code #{e.faultCode})" + end - Rails.logger.debug "URL: https://bugs.gentoo.org/enter_bug.cgi?product=%s" % data[:product] - a.get("https://bugs.gentoo.org/enter_bug.cgi?product=%s" % data[:product]) do |page| - unless page.body.include? '*+*GLSAMAKER-LOGGEDIN*+*' + # If we need to log in first + if e.faultCode == 410 Rails.logger.debug "Not logged in, doing that now." - log_in() - a.cookie_jar.load(cookie_file) + log_in + did_retry = true + retry + else + raise "Could not file the bug: #{e.faultString} (code #{e.faultCode})" end - - post_result = page.form_with(:name => 'Create') do |form| - form.component = data[:component] if data.has_key?(:component) - form.assigned_to = data[:assignee] if data.has_key?(:assignee) - form.cc = data[:cc] if data.has_key?(:cc) - form.alias = data[:alias] if data.has_key?(:alias) - form.bug_file_loc = data[:url] if data.has_key?(:url) - form.short_desc = data[:summary] if data.has_key?(:summary) - form.comment = data[:comment] if data.has_key?(:comment) - form.keywords = data[:keywords] if data.has_key?(:keywords) - form.dependson = data[:depends] if data.has_key?(:depends) - form.blocked = data[:blocks] if data.has_key?(:blocks) - form.bug_severity = data[:severity] if data.has_key?(:severity) - end.submit - - raise unless post_result.body.include? "has been added to the database" - - post_result.body =~ /(\d+) has been added to the database/ - return $1.to_i end - end + end def log_in Rails.logger.debug "Called Bugzilla.log_in" raise unless GLSAMAKER_BUGZIE_USER and GLSAMAKER_BUGZIE_PW - a = Mechanize.new { |agent| - agent.user_agent = "GLSAMaker/#{GLSAMAKER_VERSION} (http://security.gentoo.org/)" - } - - a.get("https://bugs.gentoo.org/index.cgi?GoAheadAndLogIn=1") do |page| - login_result = page.form_with(:name => 'login') do |form| - form['Bugzilla_login'] = GLSAMAKER_BUGZIE_USER - form['Bugzilla_password'] = GLSAMAKER_BUGZIE_PW - end.submit - - if login_result.body.include? '*+*GLSAMAKER-LOGGEDIN*+*' - Rails.logger.debug "Successfully logged in." - cookie_file = File.join(RAILS_ROOT, 'tmp', 'bugzie-cookies.yaml') - FileUtils.touch(cookie_file) - File.chmod(0600, cookie_file) - a.cookie_jar.save_as(cookie_file) - else - Rails.logger.warn "Failure logging in." - end + + client = xmlrpc_client + + begin + result = client.call('User.login', { + 'login' => GLSAMAKER_BUGZIE_USER, + 'password' => GLSAMAKER_BUGZIE_PW + }) + + Rails.logger.debug "Successfully logged in. UID: #{result['id']}" + + cookie_file = File.join(RAILS_ROOT, 'tmp', 'bugzie-cookies.txt') + FileUtils.rm(cookie_file) if File.exist?(cookie_file) + FileUtils.touch(cookie_file) + File.chmod(0600, cookie_file) + File.open(cookie_file, 'w') {|f| f.write client.cookie } + + return true + rescue XMLRPC::FaultException => e + Rails.logger.warn "Failure logging in: #{e.message}" + return false + end + end + + def xmlrpc_client + client = XMLRPC::Client.new('bugs.gentoo.org', '/xmlrpc.cgi', 443, nil, nil, nil, nil, true) + client.http_header_extra = {'User-Agent' => "GLSAMaker/#{GLSAMAKER_VERSION} (http://security.gentoo.org/)"} + + cookie_file = File.join(RAILS_ROOT, 'tmp', 'bugzie-cookies.txt') + if File.readable? cookie_file + client.cookie = File.read(cookie_file) end + client end end |