Skip to content

Commit 7fd3328

Browse files
committed
bugzilla: Switch bug_autorefresh default to False
The bug_autorefresh behavior is not a good idea as a default. It's been this way forever but it's time to change. While it makes writing the code slightly easier, it's really easy to make scripts incredibly non-performant scripts, which has come up in many different instances. Extend the error message for missing attributes to try and hint at the solution as well. https://bugzilla.redhat.com/show_bug.cgi?id=1297637
1 parent ce8a123 commit 7fd3328

4 files changed

Lines changed: 20 additions & 5 deletions

File tree

bugzilla/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def __init__(self, url=-1, user=None, password=None, cookiefile=-1,
244244
self._cookiejar = None
245245
self._sslverify = sslverify
246246
self._cache = _BugzillaAPICache()
247-
self._bug_autorefresh = True
247+
self._bug_autorefresh = False
248248

249249
self._field_aliases = []
250250
self._init_field_aliases()

bugzilla/bug.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Bug(object):
2525
you can read any attributes or make modifications to this
2626
bug.
2727
'''
28-
def __init__(self, bugzilla, bug_id=None, dict=None, autorefresh=True):
28+
def __init__(self, bugzilla, bug_id=None, dict=None, autorefresh=False):
2929
# pylint: disable=redefined-builtin
3030
# API had pre-existing issue that we can't change ('dict' usage)
3131

@@ -102,7 +102,12 @@ def __getattr__(self, name):
102102
self.refresh(extra_fields=[name])
103103
refreshed = True
104104

105-
raise AttributeError("Bug object has no attribute '%s'" % name)
105+
msg = ("Bug object has no attribute '%s'." % name)
106+
if not self.autorefresh:
107+
msg += ("\nIf '%s' is a bugzilla attribute, it may not have "
108+
"been cached when the bug was fetched. You may want "
109+
"to adjust your include_fields for getbug/query." % name)
110+
raise AttributeError(msg)
106111

107112
def refresh(self, include_fields=None, exclude_fields=None,
108113
extra_fields=None):

tests/ro_functional.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,23 @@ def testBugFields(self):
283283
def testBugAutoRefresh(self):
284284
bz = self.bzclass(self.url, use_creds=False)
285285

286+
bz.bug_autorefresh = True
287+
286288
bug = bz.query(bz.build_query(bug_id=720773,
287289
include_fields=["summary"]))[0]
288290
self.assertTrue(hasattr(bug, "component"))
291+
self.assertTrue(bool(bug.component))
289292

290293
bz.bug_autorefresh = False
291294

292295
bug = bz.query(bz.build_query(bug_id=720773,
293296
include_fields=["summary"]))[0]
294297
self.assertFalse(hasattr(bug, "component"))
298+
try:
299+
self.assertFalse(bool(bug.component))
300+
except:
301+
e = sys.exc_info()[1]
302+
self.assertTrue("adjust your include_fields" in str(e))
295303

296304
def testExtraFields(self):
297305
bz = self.bzclass(self.url, cookiefile=None, tokenfile=None)

tests/rw_functional.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def test04NewBugAllFields(self):
158158
self.assertTrue(len(newout.splitlines()) == 3)
159159

160160
bugid = int(newout.splitlines()[2])
161-
bug = bz.getbug(bugid)
161+
bug = bz.getbug(bugid, extra_fields=["sub_components"])
162162
print("\nCreated bugid: %s" % bugid)
163163

164164
self.assertEquals(bug.summary, summary)
@@ -515,7 +515,7 @@ def _test8Attachments(self):
515515
testfile = "../tests/data/bz-attach-get1.txt"
516516

517517
# Add attachment as CLI option
518-
setbug = bz.getbug(setbugid)
518+
setbug = bz.getbug(setbugid, extra_fields=["attachments"])
519519
orignumattach = len(setbug.attachments)
520520

521521
# Add attachment from CLI with mime guessing
@@ -571,6 +571,7 @@ def _test8Attachments(self):
571571

572572
# Get all attachments
573573
getbug = bz.getbug(getallbugid)
574+
getbug.autorefresh = True
574575
numattach = len(getbug.attachments)
575576
out = tests.clicomm(cmd + "--getall %s" % getallbugid, bz).splitlines()
576577

@@ -856,6 +857,7 @@ def test13SubComponents(self):
856857
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
857858
# Long closed RHEL5 lvm2 bug. This component has sub_components
858859
bug = bz.getbug("185526")
860+
bug.autorefresh = True
859861
self.assertEquals(bug.component, "lvm2")
860862

861863
bz.update_bugs(bug.id, bz.build_update(

0 commit comments

Comments
 (0)