Skip to content

Commit cd955c6

Browse files
committed
base: Cleanups and tweaks with use_api_key
* Break out save_api_key from the class object * Add explicit use_creds tracking * Tweak the output strings we print * Minor other improvements Signed-off-by: Cole Robinson <[email protected]>
1 parent 0191f5f commit cd955c6

2 files changed

Lines changed: 38 additions & 38 deletions

File tree

bugzilla/_cli.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,13 +1080,11 @@ def _handle_login(opt, action, bz):
10801080
use_key = getattr(opt, "api_key", False)
10811081

10821082
try:
1083-
if is_login_command and use_key:
1084-
bz.interactive_login(use_api_key=True,
1085-
restrict_login=opt.restrict_login)
1086-
elif do_interactive_login:
1087-
if bz.url:
1083+
if do_interactive_login or use_key:
1084+
if bz.url and not use_key:
10881085
print("Logging into %s" % urlparse(bz.url)[1])
10891086
bz.interactive_login(username, password,
1087+
use_api_key=use_key,
10901088
restrict_login=opt.restrict_login)
10911089
except bugzilla.BugzillaError as e:
10921090
print(str(e))
@@ -1099,7 +1097,7 @@ def _handle_login(opt, action, bz):
10991097

11001098
if is_login_command:
11011099
msg = "Login successful."
1102-
if bz.cookiefile or bz.tokenfile:
1100+
if (bz.cookiefile or bz.tokenfile) and not use_key:
11031101
msg = "Login successful, token cache updated."
11041102

11051103
print(msg)

bugzilla/base.py

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,32 @@ def _open_bugzillarc(configpaths=-1):
129129
return cfg
130130

131131

132+
def _save_api_key(url, api_key):
133+
"""
134+
Save the API_KEY in the config file.
135+
136+
If tokenfile and cookiefile are undefined, it means that the
137+
API was called with --no-cache-credentials and no change will be
138+
made
139+
"""
140+
config_filename = _default_config_location('bugzillarc')
141+
section = _parse_hostname(url)
142+
143+
cfg = ConfigParser()
144+
cfg.read(config_filename)
145+
146+
if section not in cfg.sections():
147+
cfg.add_section(section)
148+
149+
cfg[section]['api_key'] = api_key.strip()
150+
151+
with open(config_filename, 'w') as configfile:
152+
cfg.write(configfile)
153+
154+
log.info("API key written to %s", config_filename)
155+
print("API key written to %s" % config_filename)
156+
157+
132158
class _FieldAlias(object):
133159
"""
134160
Track API attribute names that differ from what we expose in users.
@@ -311,7 +337,8 @@ def __init__(self, url=-1, user=None, password=None, cookiefile=-1,
311337
self._field_aliases = []
312338
self._init_field_aliases()
313339

314-
if not use_creds:
340+
self._use_creds = use_creds
341+
if not self._use_creds:
315342
cookiefile = None
316343
tokenfile = None
317344
configpaths = []
@@ -646,34 +673,6 @@ def login(self, user=None, password=None, restrict_login=None):
646673
except Fault as e:
647674
raise BugzillaError("Login failed: %s" % str(e.faultString))
648675

649-
def _save_api_key(self):
650-
"""
651-
Save the API_KEY in the config file.
652-
653-
If toklenfile and cookiefile are undefined, it meas that the
654-
API was called with --no-cache-credentials and no change will be
655-
made
656-
"""
657-
if self.tokenfile is None and self.cookiefile is None:
658-
log.info("API Key won't be updated")
659-
return
660-
661-
config_filename = _default_config_location('bugzillarc')
662-
section = _parse_hostname(self.url)
663-
664-
cfg = ConfigParser()
665-
cfg.read(config_filename)
666-
667-
if section not in cfg.sections():
668-
cfg.add_section(section)
669-
670-
cfg[section]['api_key'] = self.api_key.strip()
671-
672-
with open(config_filename, 'w') as configfile:
673-
cfg.write(configfile)
674-
675-
log.info("API Key updated in %s", config_filename)
676-
677676
def interactive_login(self, user=None, password=None, force=False,
678677
restrict_login=None, use_api_key=False):
679678
"""
@@ -683,7 +682,7 @@ def interactive_login(self, user=None, password=None, force=False,
683682
:param password: bugzilla password. If not specified, prompt for it.
684683
:param force: Unused
685684
:param restrict_login: restricts session to IP address
686-
:param use_api_key: True if the login should be done using an api_key
685+
:param use_api_key: If True, prompt for an api_key instead
687686
"""
688687
ignore = force
689688
log.debug('Calling interactive_login')
@@ -701,9 +700,12 @@ def interactive_login(self, user=None, password=None, force=False,
701700

702701
if not self.logged_in:
703702
raise BugzillaError("Login with API_KEY failed")
704-
log.info('API Key acepted')
703+
log.info('API Key accepted')
705704

706-
self._save_api_key()
705+
if self._use_creds:
706+
_save_api_key(self.url, self.api_key)
707+
else:
708+
log.info("API Key won't be updated because use_creds=False")
707709
return
708710

709711
if not user:

0 commit comments

Comments
 (0)