|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# This program is free software; you can redistribute it and/or modify it |
| 4 | +# under the terms of the GNU General Public License as published by the |
| 5 | +# Free Software Foundation; either version 2 of the License, or (at your |
| 6 | +# option) any later version. See http://www.gnu.org/copyleft/gpl.html for |
| 7 | +# the full text of the license. |
| 8 | + |
| 9 | +# query.py: Perform a few varieties of queries |
| 10 | + |
| 11 | +from __future__ import print_function |
| 12 | + |
| 13 | +import time |
| 14 | + |
| 15 | +import bugzilla |
| 16 | + |
| 17 | +# public test instance of bugzilla.redhat.com. It's okay to make changes |
| 18 | +URL = "partner-bugzilla.redhat.com" |
| 19 | + |
| 20 | +bzapi = bugzilla.Bugzilla(URL) |
| 21 | + |
| 22 | + |
| 23 | +# build_query is a helper function that handles some bugzilla version |
| 24 | +# incompatibility issues. All it does is return a properly formatted |
| 25 | +# dict(), and provide friendly parameter names. The param names map |
| 26 | +# to those accepted by XMLRPC Bug.search: |
| 27 | +# https://bugzilla.readthedocs.io/en/latest/api/core/v1/bug.html#search-bugs |
| 28 | +query = bzapi.build_query( |
| 29 | + product="Fedora", |
| 30 | + component="python-bugzilla") |
| 31 | + |
| 32 | +# query() is what actually performs the query. it's a wrapper around Bug.search |
| 33 | +t1 = time.time() |
| 34 | +bugs = bzapi.query(query) |
| 35 | +t2 = time.time() |
| 36 | +print("Found %d bugs with our query" % len(bugs)) |
| 37 | +print("Query processing time: %s" % (t2 - t1)) |
| 38 | + |
| 39 | + |
| 40 | +# Depending on the size of your query, you can massively speed things up |
| 41 | +# by telling bugzilla to only return the fields you care about, since a |
| 42 | +# large chunk of the return time is transmitting the extra bug data. You |
| 43 | +# tweak this with include_fields: |
| 44 | +# https://wiki.mozilla.org/Bugzilla:BzAPI#Field_Control |
| 45 | +# Bugzilla will only return those fields listed in include_fields. |
| 46 | +query = bzapi.build_query( |
| 47 | + product="Fedora", |
| 48 | + component="python-bugzilla", |
| 49 | + include_fields=["id", "summary"]) |
| 50 | +t1 = time.time() |
| 51 | +bugs = bzapi.query(query) |
| 52 | +t2 = time.time() |
| 53 | +print("Quicker query processing time: %s" % (t2 - t1)) |
| 54 | + |
| 55 | + |
| 56 | +# bugzilla.redhat.com, and bugzilla >= 5.0 support queries using the same |
| 57 | +# format as is used for 'advanced' search URLs via the Web UI. For example, |
| 58 | +# I go to partner-bugzilla.redhat.com -> Search -> Advanced Search, select |
| 59 | +# Classification=Fedora |
| 60 | +# Product=Fedora |
| 61 | +# Component=python-bugzilla |
| 62 | +# Unselect all bug statuses (so, all status values) |
| 63 | +# Under Custom Search |
| 64 | +# Creation date -- is less than or equal to -- 2010-01-01 |
| 65 | +# |
| 66 | +# Run that, copy the URL and bring it here, pass it to url_to_query to |
| 67 | +# convert it to a dict(), and query as usual |
| 68 | +query = bzapi.url_to_query("https://partner-bugzilla.redhat.com/" |
| 69 | + "buglist.cgi?classification=Fedora&component=python-bugzilla&" |
| 70 | + "f1=creation_ts&o1=lessthaneq&order=Importance&product=Fedora&" |
| 71 | + "query_format=advanced&v1=2010-01-01") |
| 72 | +query["include_fields"] = ["id", "summary"] |
| 73 | +bugs = bzapi.query(query) |
| 74 | +print("The URL query returned 22 bugs... " |
| 75 | + "I know that without even checking because it shouldn't change!... " |
| 76 | + "(count is %d)" % len(bugs)) |
| 77 | + |
| 78 | + |
| 79 | +# One note about querying... you can get subtley different results if |
| 80 | +# you are not logged in. Depending on your bugzilla setup it may not matter, |
| 81 | +# but if you are dealing with private bugs, check bzapi.logged_in setting |
| 82 | +# to ensure your cached credentials are up to date. See update.py for |
| 83 | +# an example usage |
0 commit comments