Skip to content

Commit

Permalink
Get correct username from gtihub
Browse files Browse the repository at this point in the history
  • Loading branch information
avgupta456 committed Nov 29, 2023
1 parent e7c9286 commit 094af5b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
13 changes: 6 additions & 7 deletions backend/src/aggregation/layer1/auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import timedelta
from typing import List, Tuple
from typing import List, Optional, Tuple

from src.constants import OWNER, REPO
from src.data.github.rest import (
Expand All @@ -14,17 +14,16 @@
from src.utils import alru_cache


async def get_valid_github_user(user_id: str) -> bool:
async def get_valid_github_user(user_id: str) -> Optional[str]:
access_token = get_access_token()
try:
github_get_user(user_id, access_token)
return True
except RESTErrorNotFound:
return github_get_user(user_id, access_token)["login"]
except (RESTErrorNotFound, KeyError):
# User does not exist
return False
return None
except RESTError:
# Rate limited, so assume user exists
return True
return user_id


async def get_valid_db_user(user_id: str) -> bool:
Expand Down
18 changes: 9 additions & 9 deletions backend/src/aggregation/layer2/auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import timedelta
from typing import Tuple
from typing import Optional, Tuple

from src.aggregation.layer1.auth import (
get_repo_stargazers,
Expand All @@ -12,7 +12,7 @@
from src.utils import alru_cache


async def check_github_user_exists(user_id: str) -> bool:
async def check_github_user_exists(user_id: str) -> Optional[str]:
return await get_valid_github_user(user_id)


Expand All @@ -38,16 +38,16 @@ async def check_user_starred_repo(

@alru_cache(ttl=timedelta(hours=1))
async def get_is_valid_user(user_id: str) -> Tuple[bool, str]:
if user_id in USER_WHITELIST:
return (True, "Valid user")
if user_id.lower() in USER_WHITELIST:
return (True, f"Valid user {user_id.lower()}")

valid_github_user = await check_github_user_exists(user_id)
if not valid_github_user:
valid_user_id = await check_github_user_exists(user_id)
if valid_user_id is None:
return (False, "GitHub user not found")

valid_db_user = await check_db_user_exists(user_id)
user_starred = await check_user_starred_repo(user_id)
valid_db_user = await check_db_user_exists(valid_user_id)
user_starred = await check_user_starred_repo(valid_user_id)
if not (user_starred or valid_db_user):
return (False, "Repo not starred")

return (True, "Valid user")
return (True, f"Valid user {valid_user_id}")
2 changes: 1 addition & 1 deletion backend/src/routers/wrapped.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def get_wrapped_user(
response: Response, user_id: str, year: int = 2022, no_cache: bool = False
) -> Optional[WrappedPackage]:
valid_user = await get_is_valid_user(user_id)
if valid_user != "Valid user":
if "Valid user" not in valid_user:
return WrappedPackage.empty()

return await query_wrapped_user(user_id, year, no_cache=no_cache)
2 changes: 1 addition & 1 deletion frontend/src/pages/Demo/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const DemoScreen = () => {

const handleSubmit = async () => {
const validUser = await getValidUser(userName);
if (validUser === 'Valid user' || validUser === 'Repo not starred') {
if (validUser.includes('Valid user') || validUser === 'Repo not starred') {
setLoading(true);
setSelectedUserName(userName);
setLoading(false);
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/pages/Wrapped/SelectUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ const SelectUserScreen = () => {
const handleSubmit = async () => {
setIsLoading(true);
const validUser = await getValidUser(userName);
if (validUser === 'Valid user') {
await sleep(100);
navigate(`/${userName}`);
if (validUser.includes('Valid user')) {
const newUserName = validUser.split(' ')[2];
await sleep(10);
navigate(`/${newUserName}`);
} else if (validUser === 'GitHub user not found') {
setError('GitHub user not found. Check your spelling and try again.');
} else if (validUser === 'Repo not starred') {
Expand Down

0 comments on commit 094af5b

Please sign in to comment.