From 6113a44d23befae583d4c98844c6c071f40cd7d7 Mon Sep 17 00:00:00 2001 From: Greg V Date: Mon, 6 Jul 2026 20:17:09 -0700 Subject: [PATCH] Given a github username, get the Slack ID from the Profile --- api/users/users_views.py | 12 +++++++++++- db/db.py | 3 +++ db/firestore.py | 18 ++++++++++++++++-- services/users_service.py | 21 +++++++++++++++++++-- 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/api/users/users_views.py b/api/users/users_views.py index 8aa70f1..42e7f5f 100644 --- a/api/users/users_views.py +++ b/api/users/users_views.py @@ -181,4 +181,14 @@ def get_received_praises_by_db_id(user_id): result = users_service.get_received_praises_by_db_id(user_id, limit=limit, offset=offset) if result is None: return {"error": "Praises are private for this user"}, 403 - return result \ No newline at end of file + return result + + +@bp.route("/github/", methods=["GET"]) +def get_slack_id_by_github(github_username): + """Map a GitHub username to a Slack user ID.""" + from services.users_service import get_slack_user_id_by_github + slack_id = get_slack_user_id_by_github(github_username) + if slack_id: + return {"slack_user_id": slack_id} + return {"error": "not found"}, 404 \ No newline at end of file diff --git a/db/db.py b/db/db.py index 667aec8..3936f3a 100644 --- a/db/db.py +++ b/db/db.py @@ -57,6 +57,9 @@ def delete_user_by_db_id(id): def fetch_users(): return db.fetch_users() +def fetch_user_by_github(github_username): + return db.fetch_user_by_github(github_username) + # Problem Statements def fetch_problem_statement(id): return db.fetch_problem_statement(id) diff --git a/db/firestore.py b/db/firestore.py index bf90cd3..f352dad 100644 --- a/db/firestore.py +++ b/db/firestore.py @@ -315,10 +315,24 @@ def fetch_users(self): continue results.append(User.deserialize(temp)) - + return results - + def fetch_user_by_github(self, github_username): + debug(logger, "Fetching user by github", github_username=github_username) + db = self.get_db() + for candidate in [github_username, github_username.lower()]: + try: + docs = list(db.collection('users').where('github', '==', candidate).limit(1).stream()) + if docs: + temp = docs[0].to_dict() + temp['id'] = docs[0].reference.id + return User.deserialize(temp) + except Exception as e: + warning(logger, "Error querying github field", github_username=candidate, error=str(e)) + return None + + # ----------------------- Problem Statements -------------------------------------------- def fetch_problem_statements(self): diff --git a/services/users_service.py b/services/users_service.py index 4aa8e33..47bfa69 100644 --- a/services/users_service.py +++ b/services/users_service.py @@ -3,9 +3,9 @@ import threading from ratelimit import limits import requests -from common.utils.slack import send_slack_audit +from common.utils.slack import send_slack_audit, get_slack_user_by_email from model.user import User -from db.db import delete_user_by_db_id, delete_user_by_user_id, fetch_user_by_user_id, fetch_user_by_db_id, fetch_users, insert_user, update_user, get_user_profile_by_db_id, upsert_profile_metadata +from db.db import delete_user_by_db_id, delete_user_by_user_id, fetch_user_by_user_id, fetch_user_by_db_id, fetch_users, insert_user, update_user, get_user_profile_by_db_id, upsert_profile_metadata, fetch_user_by_github import pytz from cachetools import cached, LRUCache, TTLCache from cachetools.keys import hashkey @@ -386,6 +386,23 @@ def save_profile_metadata(propel_id, json): def get_user_by_db_id(id): return fetch_user_by_db_id(id) +def get_slack_user_id_by_github(github_username): + """Look up a Slack user ID given a GitHub username.""" + if not github_username: + return None + user = fetch_user_by_github(github_username) + if user is None: + return None + raw_id = extract_slack_user_id(user.user_id) + import re + if re.match(r'^[UW][A-Z0-9]{5,}$', raw_id): + return raw_id + if user.email_address: + slack_user = get_slack_user_by_email(user.email_address) + if slack_user: + return slack_user.get('id') + return None + def get_user_from_slack_id(user_id): return fetch_user_by_user_id(user_id)