Skip to content

Commit f1b279a

Browse files
committed
doc: Add examples/
Bunch of code examples for common operations Resolves #17
1 parent b59bde5 commit f1b279a

5 files changed

Lines changed: 194 additions & 1 deletion

File tree

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ include bugzilla.1
33
include xmlrpc-api-notes.txt
44
include python-bugzilla.spec
55
include *requirements.txt
6+
recursive-include examples *.py
67
recursive-include tests *.py *.txt *.cfg

examples/getbug.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
# getbug.py: Simple demonstration of connecting to bugzilla, fetching
10+
# a bug, and printing some details.
11+
12+
from __future__ import print_function
13+
14+
import pprint
15+
16+
import bugzilla
17+
18+
# public test instance of bugzilla.redhat.com. It's okay to make changes
19+
URL = "partner-bugzilla.redhat.com"
20+
21+
bzapi = bugzilla.Bugzilla(URL)
22+
23+
# getbug() is just a simple wrapper around getbugs(), which takes a list
24+
# IDs, if you need to fetch multiple
25+
#
26+
# Example bug: https://partner-bugzilla.redhat.com/show_bug.cgi?id=427301
27+
bug = bzapi.getbug(427301)
28+
print("Fetched bug #%s:" % bug.id)
29+
print(" Product = %s" % bug.product)
30+
print(" Component = %s" % bug.component)
31+
print(" Status = %s" % bug.status)
32+
print(" Resolution= %s" % bug.resolution)
33+
print(" Summary = %s" % bug.summary)
34+
35+
# Just check dir(bug) for other attributes, or check upstream bugzilla
36+
# Bug.get docs for field names:
37+
# https://bugzilla.readthedocs.io/en/latest/api/core/v1/bug.html#get-bug
38+
39+
# comments must be fetched separately on stock bugzilla. this just returns
40+
# a raw dict with all the info.
41+
comments = bug.getcomments()
42+
print("\nLast comment data:\n%s" % pprint.pformat(comments[-1]))
43+
44+
# getcomments is just a wrapper around bzapi.get_comments(), which can be
45+
# used for bulk comments fetching

examples/query.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

examples/update.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
# update.py: Make changes to an existing bug
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+
bzapi = bugzilla.Bugzilla(URL)
20+
if not bzapi.logged_in:
21+
print("This example requires cached login credentials for %s" % URL)
22+
bzapi.interactive_login()
23+
24+
25+
# Similar to build_query, build_update is a helper function that handles
26+
# some bugzilla version incompatibility issues. All it does is return a
27+
# properly formatted dict(), and provide friendly parameter names.
28+
# The param names map to those accepted by XMLRPC Bug.update:
29+
# https://bugzilla.readthedocs.io/en/latest/api/core/v1/bug.html#update-bug
30+
#
31+
# Example bug: https://partner-bugzilla.redhat.com/show_bug.cgi?id=427301
32+
# Don't worry, changing things here is fine, and won't send any email to
33+
# users or anything. It's what partner-bugzilla.redhat.com is for!
34+
bug = bzapi.getbug(427301)
35+
print("Bug id=%s original summary=%s" % (bug.id, bug.summary))
36+
37+
update = bzapi.build_update(summary="new example summary %s" % time.time())
38+
bzapi.update_bugs([bug.id], update)
39+
40+
# Call bug.refresh() to update its cached state
41+
bug.refresh()
42+
print("Bug id=%s new summary=%s" % (bug.id, bug.summary))
43+
44+
45+
# Now let's add a comment
46+
comments = bug.getcomments()
47+
print("Bug originally has %d comments" % len(comments))
48+
49+
update = bzapi.build_update(comment="new example comment %s" % time.time())
50+
bzapi.update_bugs([bug.id], update)
51+
52+
# refresh() actually isn't required here because comments are fetched
53+
# on demand
54+
comments = bug.getcomments()
55+
print("Bug now has %d comments. Last comment=%s" % (len(comments),
56+
comments[-1]["text"]))
57+
58+
59+
# The 'bug' object actually has some old convenience APIs for specific
60+
# actions like commenting, and closing. However these aren't recommended:
61+
# they encourage splitting up bug edits when really batching should be done
62+
# as much as possible, not only to make your code quicker and save strain
63+
# on the bugzilla instance, but also to avoid spamming bugzilla users with
64+
# redundant email from two modifications that could have been batched.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def finalize_options(self):
126126
pass
127127

128128
def _run(self):
129-
files = ["bugzilla/", "bin-bugzilla", "tests/*.py"]
129+
files = ["bugzilla/", "bin-bugzilla", "examples/*.py", "tests/*.py"]
130130
output_format = sys.stdout.isatty() and "colorized" or "text"
131131

132132
cmd = "pylint "

0 commit comments

Comments
 (0)