From f85c427c13991b347b3d28635eb4b634cf2c8505 Mon Sep 17 00:00:00 2001 From: Rongxin Liu Date: Thu, 1 Feb 2024 13:09:30 -0500 Subject: [PATCH] handle missing env var for codespace --- lib50/authentication.py | 21 ++++++++++++++++++--- setup.py | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib50/authentication.py b/lib50/authentication.py index 39c30fa..c9dca7a 100644 --- a/lib50/authentication.py +++ b/lib50/authentication.py @@ -143,9 +143,6 @@ class State(enum.Enum): # while passphrase is needed, prompt and enter while state == State.PASSPHRASE_PROMPT: - # Show a quick reminder to check https://cs50.ly/github if not immediately authenticated - _show_gh_changes_warning() - # Prompt passphrase passphrase = _prompt_password("Enter passphrase for SSH key: ") @@ -172,6 +169,9 @@ class State(enum.Enum): username = child.match.groups()[0] # Failed authentication, nothing to be done else: + if not os.environ.get("CODESPACES"): + # If not in codespaces, show a quick reminder to check https://cs50.ly/github if not immediately authenticated + _show_gh_changes_warning() return None finally: child.close() @@ -193,6 +193,20 @@ def _authenticate_https(org, repo=None): username = os.environ.get("CS50_GH_USER") password = os.environ.get("CS50_TOKEN") + # If in codespaces, check for missing environment variables and prompt user to re-login + if os.environ.get("CODESPACES"): + missing_env_vars = False + for env_var in ("CS50_GH_USER", "CS50_TOKEN"): + if os.environ.get(env_var) is None: + missing_env_vars = True + error = f"Missing environment variable {env_var}" + print(termcolor.colored(error, color="red", attrs=["bold"])) + if missing_env_vars: + prompt = "Please visit https://cs50.dev/restart to restart your codespace." + print(termcolor.colored(prompt, color="yellow", attrs=["bold"])) + logout() + sys.exit(1) + # Otherwise, get credentials from cache if possible if username is None or password is None: try: @@ -228,6 +242,7 @@ def _authenticate_https(org, repo=None): # Prompt for PAT if not in env vars or cache if password is None: + # Show a quick reminder to check https://cs50.ly/github if not immediately authenticated _show_gh_changes_warning() diff --git a/setup.py b/setup.py index 60eb5bc..839d10e 100644 --- a/setup.py +++ b/setup.py @@ -27,6 +27,6 @@ python_requires=">= 3.6", packages=["lib50"], url="https://github.com/cs50/lib50", - version="3.0.10", + version="3.0.11", include_package_data=True )