Skip to content

Commit 67fcbc3

Browse files
committed
Merge recent changes from branch 'origin'
Currently it's 13 commits behind branch 'origin' (sync with branch python-bugzilla:main)
2 parents 3d49c17 + a890ad0 commit 67fcbc3

File tree

8 files changed

+55
-15
lines changed

8 files changed

+55
-15
lines changed

bugzilla/_backendbase.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self, url, bugzillasession):
2222
@staticmethod
2323
def probe(url):
2424
try:
25-
requests.head(url).raise_for_status()
25+
requests.head(url, timeout=10).raise_for_status()
2626
return True # pragma: no cover
2727
except Exception as e:
2828
log.debug("Failed to probe url=%s : %s", url, str(e))

bugzilla/_backendrest.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import os
88

99
from ._backendbase import _BackendBase
10-
from .exceptions import BugzillaError
10+
from .exceptions import BugzillaError, BugzillaHTTPError
1111
from ._util import listify
1212

1313

@@ -32,6 +32,23 @@ def __init__(self, url, bugzillasession):
3232
#########################
3333
# Internal REST helpers #
3434
#########################
35+
def _handle_error(self, e):
36+
response = getattr(e, "response", None)
37+
if response is None:
38+
raise e # pragma: no cover
39+
40+
if response.status_code in [400, 401, 404]:
41+
self._handle_error_response(response.text)
42+
raise e
43+
44+
def _handle_error_response(self, text):
45+
try:
46+
result = json.loads(text)
47+
except json.JSONDecodeError:
48+
return
49+
50+
if result.get("error"):
51+
raise BugzillaError(result["message"], code=result["code"])
3552

3653
def _handle_response(self, text):
3754
try:
@@ -40,7 +57,7 @@ def _handle_response(self, text):
4057
log.debug("Failed to parse REST response. Output is:\n%s", text)
4158
raise
4259

43-
if ret.get("error", False):
60+
if ret.get("error", False): # pragma: no cover
4461
raise BugzillaError(ret["message"], code=ret["code"])
4562
return ret
4663

@@ -55,8 +72,13 @@ def _op(self, method, apiurl, paramdict=None):
5572
else:
5673
data = json.dumps(paramdict or {})
5774

58-
response = self._bugzillasession.request(method, fullurl, data=data,
59-
params=authparams)
75+
try:
76+
response = self._bugzillasession.request(
77+
method, fullurl, data=data, params=authparams
78+
)
79+
except BugzillaHTTPError as e:
80+
self._handle_error(e)
81+
6082
return self._handle_response(response.text)
6183

6284
def _get(self, *args, **kwargs):

bugzilla/_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ def _do_modify(bz, parser, opt):
10471047

10481048
for k, v in wbmap.copy().items():
10491049
if not v[0] and not v[1]:
1050-
del(wbmap[k])
1050+
del wbmap[k]
10511051

10521052
if opt.fields:
10531053
_merge_field_opts(update, opt.fields, parser)

bugzilla/_session.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import requests
1111

12+
from .exceptions import BugzillaHTTPError
1213

1314
log = getLogger(__name__)
1415

@@ -30,7 +31,7 @@ def __init__(self, url, user_agent,
3031
self._use_auth_bearer = False
3132

3233
if self._scheme not in ["http", "https"]:
33-
raise Exception("Invalid URL scheme: %s (%s)" % (
34+
raise ValueError("Invalid URL scheme: %s (%s)" % (
3435
self._scheme, url))
3536

3637
self._session = requests_session
@@ -106,9 +107,12 @@ def request(self, *args, **kwargs):
106107

107108
try:
108109
response.raise_for_status()
109-
except Exception as e:
110+
except requests.HTTPError as e:
110111
# Scrape the api key out of the returned exception string
111112
message = str(e).replace(self._api_key or "", "")
112-
raise type(e)(message).with_traceback(sys.exc_info()[2])
113+
response = getattr(e, "response", None)
114+
raise BugzillaHTTPError(message, response=response).with_traceback(
115+
sys.exc_info()[2]
116+
)
113117

114118
return response

bugzilla/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ def add_email(key, value, count):
13021302
# Strip out None elements in the dict
13031303
for k, v in query.copy().items():
13041304
if v is None:
1305-
del(query[k])
1305+
del query[k]
13061306

13071307
self.pre_translation(query)
13081308
return query
@@ -1799,7 +1799,7 @@ def _validate_createbug(self, *args, **kwargs):
17991799

18001800
# Back compat handling for check_args
18011801
if "check_args" in data:
1802-
del(data["check_args"])
1802+
del data["check_args"]
18031803

18041804
return data
18051805

bugzilla/bug.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import copy
88
from logging import getLogger
9+
from urllib.parse import urlparse, urlunparse
910

1011

1112
log = getLogger(__name__)
@@ -39,8 +40,16 @@ def __init__(self, bugzilla, bug_id=None, dict=None, autorefresh=False):
3940
dict["id"] = bug_id
4041

4142
self._update_dict(dict)
42-
self.weburl = bugzilla.url.replace('xmlrpc.cgi',
43-
'show_bug.cgi?id=%i' % self.bug_id)
43+
self.weburl = self._generate_weburl()
44+
45+
def _generate_weburl(self):
46+
"""
47+
Generate the URL to the bug in the web UI
48+
"""
49+
parsed = urlparse(self.bugzilla.url)
50+
return urlunparse((parsed.scheme, parsed.netloc,
51+
'show_bug.cgi', '', 'id=%s' % self.bug_id,
52+
''))
4453

4554
def __str__(self):
4655
"""
@@ -136,7 +145,7 @@ def _translate_dict(self, newdict):
136145
"d[%s]=%s and d[%s]=%s , dropping the value "
137146
"d[%s]", newname, newdict[newname], oldname,
138147
newdict[oldname], oldname)
139-
del(newdict[oldname])
148+
del newdict[oldname]
140149

141150

142151
def _update_dict(self, newdict):

bugzilla/exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# This work is licensed under the GNU GPLv2 or later.
22
# See the COPYING file in the top-level directory.
3+
from requests import HTTPError
34

45

56
class BugzillaError(Exception):
@@ -36,3 +37,7 @@ def __init__(self, message, code=None):
3637
if self.code:
3738
message += " (code=%s)" % self.code
3839
Exception.__init__(self, message)
40+
41+
42+
class BugzillaHTTPError(HTTPError):
43+
"""Error raised in the Bugzilla session"""

docs/bugzilla.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,4 +859,4 @@ SEE ALSO
859859
========
860860

861861
https://bugzilla.readthedocs.io/en/latest/api/index.html
862-
https://bugzilla.redhat.com/docs/en/html/api/Bugzilla/WebService/Bug.html
862+
https://bugzilla.redhat.com/docs/en/html/api/core/v1/bug.html

0 commit comments

Comments
 (0)