Class: Bicho::Client

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/bicho/client.rb

Overview

Client to query bugzilla

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Logging

#logger, logger, logger=

Constructor Details

- (Client) initialize(site_url)

A new instance of Client

Parameters:

  • site_url (String)

    Bugzilla installation site url



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/bicho/client.rb', line 93

def initialize(site_url)
  # Don't modify the original url
  @site_url = site_url.is_a?(URI) ? site_url.clone : URI.parse(site_url) 
  
  @api_url = @site_url.clone
  @api_url.path = '/xmlrpc.cgi'

  # Scan plugins
  plugin_glob = File.join(File.dirname(__FILE__), 'plugins', '*.rb')
  Dir.glob(plugin_glob).each do |plugin|
    logger.debug("Loading file: #{plugin}")
    load plugin
  end

  #instantiate plugins
  ::Bicho::Plugins.constants.each do |cnt|
    pl_class = ::Bicho::Plugins.const_get(cnt)
    pl_instance = pl_class.new
    logger.debug("Loaded: #{pl_instance}")

    # Modify API url
    if pl_instance.respond_to?(:transform_api_url_hook)
      @api_url = pl_instance.transform_api_url_hook(@api_url, logger)
    end
  end

  @client = XMLRPC::Client.new_from_uri(@api_url.to_s, nil, 900)
  @client.set_debug

  # User.login sets the credentials cookie for subsequent calls
  if @client.user && @client.password
    ret = @client.call("User.login", { 'login' => @client.user, 'password' => @client.password, 'remember' => 0 } )
    handle_faults(ret)
    @userid = ret['id']
  end
end

Instance Attribute Details

- (URI) api_url (readonly)

This URL is automatically inferred from the Client#site_url

Plugins can modify the inferred value by providing a transform_api_url_hook(url, logger) method returning the modified value.

Returns:

  • (URI)

    XML-RPC API end-point



74
75
76
# File 'lib/bicho/client.rb', line 74

def api_url
  @api_url
end

- (URI) site_url (readonly)

This value is provided at construction time

Returns:

  • (URI)

    Bugzilla installation website



79
80
81
# File 'lib/bicho/client.rb', line 79

def site_url
  @site_url
end

- (String) userid (readonly)

User id, available after login

Returns:

  • (String)

    user id, available after login



82
83
84
# File 'lib/bicho/client.rb', line 82

def userid
  @userid
end

Instance Method Details



130
131
132
# File 'lib/bicho/client.rb', line 130

def cookie
  @client.cookie
end

- (Object) expand_named_query(what)

Given a named query's name, runs it on the server



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/bicho/client.rb', line 164

def expand_named_query(what)
  if not @userid
    raise "You need to be authenticated to use named queries"
  end

  logger.info("Expanding named query: '#{what}' with '#{cookie}'")
  http = Net::HTTP.new(@api_url.host, @api_url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http.set_debug_output(Bicho::LoggerIODevice.new)
  request = Net::HTTP::Get.new("/buglist.cgi?cmdtype=runnamed&namedcmd=#{CGI.escape(what)}&ctype=atom", {"Cookie" => self.cookie} )
  response = http.request(request)
  case response
    when Net::HTTPSuccess
      bugs = []
      xml = Nokogiri::XML.parse(response.body)
      xml.root.xpath("//xmlns:entry/xmlns:link/@href", xml.root.namespace).each do |attr|
        uri = URI.parse attr.value
        bugs << uri.query.split("=")[1]
      end
      return bugs
    when Net::HTTPRedirect
      raise "HTTP redirect not supported in named_query"
    else
      raise "Error when expanding named query '#{what}': #{response}"
  end
end

- (Object) get_bugs(*ids)

Retrieves one or more bugs by id



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/bicho/client.rb', line 193

def get_bugs(*ids)
  params = Hash.new
  params[:ids] = ids.collect(&:to_s).map do |what|
    if what =~ /^[0-9]+$/
      next what.to_i
    else
      next expand_named_query(what)
    end
  end.flatten

  bugs = []
  ret = @client.call("Bug.get", params)
  handle_faults(ret)
  ret['bugs'].each do |bug_data|
    bugs << Bug.new(self, bug_data)
  end
  bugs
end

- (Object) handle_faults(ret)



134
135
136
137
138
139
140
# File 'lib/bicho/client.rb', line 134

def handle_faults(ret)
  if ret.has_key?('faults')
    ret['faults'].each do |fault|
      logger.error fault
    end
  end
end

- (Object) search_bugs(query)

Search for a bug

query has to be either a Query object or a String that will be searched in the summary of the bugs.



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/bicho/client.rb', line 148

def search_bugs(query)
  # allow plain strings to be passed, interpretting them
  query = Query.new.summary(query) if query.is_a?(String)

  ret = @client.call("Bug.search", query.query_map)
  handle_faults(ret)
  bugs = []
  ret['bugs'].each do |bug_data|
    bugs << Bug.new(self, bug_data)
  end
  bugs
end

- (Object) url

Implemented only to warn users about the replacement APIs

Raises:

  • (NoMethodError)


87
88
89
90
# File 'lib/bicho/client.rb', line 87

def url
  warn "url is deprecated. Use site_url or api_url"
  raise NoMethodError
end