Skip to content

Commit 15fe20c

Browse files
committed
main: Ask for login when required
Signed-off-by: Mathieu Dubois-Briand <[email protected]>
1 parent 79723a4 commit 15fe20c

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

swattool/main.py

+21-5
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,28 @@ def parse_filters(kwargs) -> dict[str, Any]:
6464

6565
@click.group()
6666
@click.option('-v', '--verbose', count=True, help="Increase verbosity")
67-
def main(verbose: int):
67+
def maingroup(verbose: int):
6868
"""Handle triage of Yocto autobuilder failures."""
6969
utils.setup_logging(verbose)
7070

7171

72-
@main.command()
72+
def main():
73+
"""Handle triage of Yocto autobuilder failures."""
74+
try:
75+
maingroup() # pylint: disable=no-value-for-parameter
76+
except utils.LoginRequiredException as e:
77+
if e.service == "swatbot":
78+
logger.warning("Login required to swatbot server")
79+
user = click.prompt('swatbot user')
80+
password = click.prompt('swatbot password', hide_input=True)
81+
success = swatbotrest.login(user, password)
82+
if success:
83+
maingroup() # pylint: disable=no-value-for-parameter
84+
else:
85+
raise
86+
87+
88+
@maingroup.command()
7389
@click.option('--user', '-u', prompt=True)
7490
@click.option('--password', '-p', prompt=True, hide_input=True)
7591
def login(user: str, password: str):
@@ -145,7 +161,7 @@ def format_field(build, userinfo, field):
145161
return (table, headers)
146162

147163

148-
@main.command()
164+
@maingroup.command()
149165
@_add_options(failures_list_options)
150166
@click.option('--open-url', '-u', is_flag=True,
151167
help="Open the autobuilder url in web browser")
@@ -194,7 +210,7 @@ def show_pending_failures(refresh: str, open_url: str,
194210
if b.status == swatbuild.Status.CANCELLED]))
195211

196212

197-
@main.command()
213+
@maingroup.command()
198214
@_add_options(failures_list_options)
199215
@click.option('--open-autobuilder-url', '-u', is_flag=True,
200216
help="Open the autobuilder url in web browser")
@@ -235,7 +251,7 @@ def review_pending_failures(refresh: str, open_autobuilder_url: bool,
235251
userinfos.save()
236252

237253

238-
@main.command()
254+
@maingroup.command()
239255
@click.option('--dry-run', '-n', is_flag=True,
240256
help="Only shows what would be done")
241257
def publish_new_reviews(dry_run: bool):

swattool/swatbotrest.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def _get_csrftoken() -> str:
100100
return Session().session.cookies['csrftoken']
101101

102102

103-
def login(user: str, password: str):
103+
def login(user: str, password: str) -> bool:
104104
"""Login to the swatbot Django interface."""
105105
session = Session()
106106

@@ -120,11 +120,13 @@ def login(user: str, password: str):
120120
raise error
121121
else:
122122
logger.warning("Unexpected reply, login probably failed")
123-
return
123+
return False
124124

125125
session.save_cookies()
126126
logger.info("Logging success")
127127

128+
return True
129+
128130

129131
def _get_json(path: str, max_cache_age: int = -1):
130132
data = Session().get(f"{REST_BASE_URL}{path}", max_cache_age)
@@ -133,7 +135,8 @@ def _get_json(path: str, max_cache_age: int = -1):
133135
except json.decoder.JSONDecodeError as err:
134136
Session().invalidate_cache(f"{REST_BASE_URL}{path}")
135137
if "Please login to see this page." in data:
136-
raise utils.SwattoolException("Not logged in swatbot") from err
138+
raise utils.LoginRequiredException("Not logged in swatbot",
139+
"swatbot") from err
137140
raise utils.SwattoolException("Failed to parse server reply") from err
138141
return json_data
139142

swattool/utils.py

+9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ class SwattoolException(Exception):
6060
"""A generic swattool error."""
6161

6262

63+
class LoginRequiredException(SwattoolException):
64+
"""An exception for operations requiring login."""
65+
66+
def __init__(self, message, service):
67+
super().__init__(message)
68+
69+
self.service = service
70+
71+
6372
class _LogFormatter(logging.Formatter):
6473
colors = {
6574
logging.DEBUG: Color.WHITE,

0 commit comments

Comments
 (0)