Skip to content

Commit e980e3b

Browse files
committed
Fixing oauth decline error messages and wrong jinja warning
1 parent 62eac83 commit e980e3b

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

src/hermes/commands/init/base.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -553,15 +553,13 @@ def configure_ci_template(self, ci_file_path) -> None:
553553
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(""),
554554
block_start_string="{%%", block_end_string="%%}",
555555
variable_start_string="{%", variable_end_string="%}")
556-
source_text = Path(ci_file_path).read_text(encoding="utf-8")
557-
used_params = jinja2.meta.find_undeclared_variables(jinja_env.parse(source_text))
558556
template = jinja_env.get_template(str(ci_file_path))
559-
missing = [p for p in used_params if p not in self.ci_parameters]
560-
if missing:
561-
sc.echo(f"CI Template has missing parameters: {missing}", formatting=sc.Formats.WARNING)
562557
rendered = template.render(self.ci_parameters)
563558
with open(ci_file_path, 'w') as file:
564559
file.write(rendered)
560+
undeclared_params = jinja2.meta.find_undeclared_variables(jinja_env.parse(rendered))
561+
if undeclared_params:
562+
sc.echo(f"CI Template has missing parameters: {undeclared_params}", formatting=sc.Formats.WARNING)
565563

566564
def create_zenodo_token(self) -> None:
567565
"""Makes the user create a zenodo token and saves it in self.deposit_platform.token."""

src/hermes/commands/init/util/oauth_process.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import requests
1919
import requests_oauthlib
20+
from oauthlib.oauth2.rfc6749.errors import CustomOAuth2Error
2021

2122
from . import slim_click as sc
2223

@@ -74,6 +75,7 @@ def __init__(self, name: str, client_id: str = "", client_secret: str = "", auth
7475
self.tokens = {}
7576
self.device_code_url = device_code_url
7677
self.server: HTTPServer = None
78+
self.error_description: str = ""
7779

7880
def create_handler_constructor(self):
7981
def handler(*args, **kwargs):
@@ -158,7 +160,10 @@ def get_tokens_from_device_flow(self) -> dict[str: str]:
158160
if "access_token" in token_response_data:
159161
return token_response_data
160162
elif "error" in token_response_data and token_response_data["error"] != "authorization_pending":
161-
sc.echo(f"Error: {token_response_data['error']}")
163+
sc.echo(f"Error: {token_response_data['error']}", sc.Formats.WARNING)
164+
return {}
165+
elif self.error_description:
166+
sc.echo(f"Error: {self.error_description}", sc.Formats.WARNING)
162167
return {}
163168

164169
time.sleep(interval)
@@ -167,7 +172,7 @@ def get_tokens_from_oauth(self) -> dict[str: str]:
167172
if self.authorize_url == "":
168173
sc.echo(f"OAuth is not available for {self.name}")
169174
return {}
170-
sc.echo(f"Opening browser to log into your {self.name} account...")
175+
# sc.echo(f"Opening browser to log into your {self.name} account...")
171176
self.tokens = {}
172177
server_thread = threading.Thread(target=self.start_server, daemon=True)
173178
server_thread.start()
@@ -220,9 +225,12 @@ def callback(self):
220225
parsed = urlparse(self.path)
221226
auth_code = parse_qs(parsed.query)["code"][0]
222227

223-
tokens = self.oauth_process.get_tokens_from_auth_code(auth_code)
224-
self.oauth_process.tokens = tokens
225-
self.oauth_process.shutdown_event.set()
228+
try:
229+
tokens = self.oauth_process.get_tokens_from_auth_code(auth_code)
230+
self.oauth_process.tokens = tokens
231+
self.oauth_process.shutdown_event.set()
232+
except CustomOAuth2Error as e:
233+
self.oauth_process.error_description = e.description
226234

227235
self.end_headers()
228236
self.wfile.write(b"You can close this window now.")

0 commit comments

Comments
 (0)