Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion api/users/users_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
return result


@bp.route("/github/<github_username>", 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
3 changes: 3 additions & 0 deletions db/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,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)
Expand Down
18 changes: 16 additions & 2 deletions db/firestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,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):
Expand Down
21 changes: 19 additions & 2 deletions services/users_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_user_by_propel_id, fetch_user_by_email, 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_user_by_propel_id, fetch_user_by_email, 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
Expand Down Expand Up @@ -398,6 +398,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)

Expand Down
Loading