From dfe64190ca191a26979d8b172d8b00d280fe8221 Mon Sep 17 00:00:00 2001 From: Greg V Date: Fri, 13 Sep 2024 15:36:31 -0700 Subject: [PATCH] [Profile] GitHub History! --- api/messages/messages_service.py | 14 ++++++++++++-- api/messages/messages_views.py | 7 ++++++- common/utils/firebase.py | 22 ++++++++++++++++++++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/api/messages/messages_service.py b/api/messages/messages_service.py index 439c5e5..a5b385b 100644 --- a/api/messages/messages_service.py +++ b/api/messages/messages_service.py @@ -1,6 +1,6 @@ from common.utils import safe_get_env_var from common.utils.slack import send_slack_audit, create_slack_channel, send_slack, invite_user_to_channel -from common.utils.firebase import get_hackathon_by_event_id, upsert_news +from common.utils.firebase import get_hackathon_by_event_id, upsert_news, get_github_contributions_for_user from common.utils.openai_api import generate_and_save_image_to_cdn from common.utils.github import create_github_repo from api.messages.message import Message @@ -30,7 +30,7 @@ logger = logging.getLogger("myapp") -logger.setLevel(logging.INFO) +logger.setLevel(logging.DEBUG) google_recaptcha_key = safe_get_env_var("GOOGLE_CAPTCHA_SECRET_KEY") @@ -1713,6 +1713,16 @@ def get_problem_statement_list_old(): logger.debug(results) return { "problem_statements": results } +@cached(cache=TTLCache(maxsize=100, ttl=600)) +@limits(calls=100, period=ONE_MINUTE) +def get_github_profile(github_username): + logger.debug(f"Getting Github Profile for {github_username}") + + return { + "github_history": get_github_contributions_for_user(github_username) + } + + # -------------------- User functions to be deleted ---------------------------------------- # # 10 minute cache for 100 objects LRU diff --git a/api/messages/messages_views.py b/api/messages/messages_views.py index 2136bf9..ad8dbfe 100644 --- a/api/messages/messages_views.py +++ b/api/messages/messages_views.py @@ -42,7 +42,8 @@ get_all_profiles, save_npo_application, get_npo_applications, - update_npo_application + update_npo_application, + get_github_profile ) @@ -316,6 +317,10 @@ def save_profile(): else: return None +@bp.route("/profile/github/", methods=["GET"]) +def get_github_profile_api(username): + return get_github_profile(username) + # Get user profile by user id @bp.route("/profile/", methods=["GET"]) def get_profile_by_id(id): diff --git a/common/utils/firebase.py b/common/utils/firebase.py index 03739f0..039a665 100644 --- a/common/utils/firebase.py +++ b/common/utils/firebase.py @@ -16,9 +16,9 @@ # add logger import logging -logger = logging.getLogger(__name__) +logger = logging.getLogger("myapp") # set log level -logger.setLevel(logging.INFO) +logger.setLevel(logging.DEBUG) def get_db(): @@ -88,6 +88,24 @@ def get_user_by_email(email_address): return adict +def get_github_contributions_for_user(login): + logger.info(f"Getting github contributions for user {login}") + + db = get_db() # this connects to our Firestore database + docs = db.collection('github_contributors').where("login", "==", login).stream() + + logger.info(f"Found {docs} contributions for user {login}") + + github_history = [] + for doc in docs: + adict = doc.to_dict() + adict["id"] = doc.id + github_history.append(adict) + + return github_history + + + def create_user(name, email_address, slack_id): db = get_db() # this connects to our Firestore database logger.info(f"Creating user {name} {email_address}")