Skip to content

Commit

Permalink
handle missing env var for codespace
Browse files Browse the repository at this point in the history
  • Loading branch information
rongxin-liu committed Feb 1, 2024
1 parent 3af5e29 commit f85c427
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions lib50/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: ")

Expand All @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit f85c427

Please sign in to comment.