Skip to content

Commit

Permalink
Merge pull request #38 from ramazanoacar/fix/update-user-repositories
Browse files Browse the repository at this point in the history
Fix/update user repositories
  • Loading branch information
berkingurcan authored Oct 16, 2024
2 parents 746675a + 237a052 commit a0ae549
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
47 changes: 29 additions & 18 deletions github_tracker_bot/mongo_data_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import copy
import config

from datetime import datetime
from dataclasses import dataclass, field, asdict
from typing import List, Dict, Any, Optional, Union
Expand Down Expand Up @@ -303,6 +302,8 @@ def update_all_contribution_datas_from_ai_decisions(
self, user_handle
) -> Optional[User]:
"""Updates all contribution data by calculating ai decisions with helper functions"""
from leader_bot.sheet_functions import get_repositories_from_user

try:
user = self.get_user(user_handle)
ai_decisions = user.ai_decisions
Expand All @@ -329,6 +330,10 @@ def update_all_contribution_datas_from_ai_decisions(
user.qualified_daily_contribution_dates
)

repositories = user.repositories.copy()
repositories.extend(get_repositories_from_user(user_handle))
user.repositories = list(set(repositories))

updated_user = self.update_user(user_handle, user)
return updated_user

Expand Down Expand Up @@ -549,20 +554,26 @@ def set_qualified_daily_contribution_streak(
raise

def update_ai_decisions(self, user: User, new_decisions: List[AIDecision]) -> None:
for new_decision in new_decisions:
for user_ai_decision in user.ai_decisions[0]:
if (
user_ai_decision.repository == new_decision.repository
and user_ai_decision.date == new_decision.date
):
user_ai_decision.response = new_decision.response
for commit in new_decision.commit_hashes:
if commit not in user_ai_decision.commit_hashes:
user_commit_hashes = user_ai_decision.commit_hashes
if type(user_commit_hashes) != list:
user_commit_hashes = user_commit_hashes.split(",")
user_commit_hashes.extend(commit)
user_ai_decision.commit_hashes = user_commit_hashes
break
else:
user.ai_decisions[0].extend([new_decision])
logger.info(f"Updating AI decisions for user {user.user_handle}")
logger.info(f"New decisions: {new_decisions}")
logger.info(f"Old decisions: {user.ai_decisions}")
if len(user.ai_decisions) == 0:
user.ai_decisions = [new_decisions]
else:
for new_decision in new_decisions:
for user_ai_decision in user.ai_decisions[0]:
if (
user_ai_decision.repository == new_decision.repository
and user_ai_decision.date == new_decision.date
):
user_ai_decision.response = new_decision.response
for commit in new_decision.commit_hashes:
if commit not in user_ai_decision.commit_hashes:
user_commit_hashes = user_ai_decision.commit_hashes
if type(user_commit_hashes) != list:
user_commit_hashes = user_commit_hashes.split(",")
user_commit_hashes.extend(commit)
user_ai_decision.commit_hashes = user_commit_hashes
break
else:
user.ai_decisions[0].extend([new_decision])
27 changes: 25 additions & 2 deletions leader_bot/sheet_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
from log_config import get_logger
from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build
from db_functions import fetch_db_get_users, get_ai_decisions_by_user_and_timeframe
from github_tracker_bot.mongo_data_handler import AIDecision
from helpers import get_monthly_user_data_from_ai_decisions, get_since_until_y_m_d
from config import GOOGLE_CREDENTIALS

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
Expand Down Expand Up @@ -129,6 +127,8 @@ def create_leaderboard_sheet(


def fill_created_spreadsheet_with_users_except_ai_decisions(spreadsheed_id):
from db_functions import fetch_db_get_users

try:
column_names = [
[
Expand Down Expand Up @@ -169,6 +169,8 @@ def fill_created_spreadsheet_with_users_except_ai_decisions(spreadsheed_id):


def write_users_to_csv(file_path):
from db_functions import fetch_db_get_users

try:
column_names = [
"User Handle",
Expand Down Expand Up @@ -210,6 +212,8 @@ def write_users_to_csv(file_path):


def write_users_to_csv_monthly(file_path, month):
from db_functions import fetch_db_get_users

try:
users = fetch_db_get_users()
filtered_users = []
Expand Down Expand Up @@ -274,6 +278,8 @@ def write_ai_decisions_to_csv(


def update_created_spreadsheet_with_users_except_ai_decisions(spreadsheed_id):
from db_functions import fetch_db_get_users

try:
users = fetch_db_get_users()
data = []
Expand Down Expand Up @@ -479,6 +485,9 @@ def delete_user(discord_handle: str):


def write_all_data_of_user_to_csv_by_month(file_path: str, username: str, date: str):
from db_functions import get_ai_decisions_by_user_and_timeframe
from helpers import get_monthly_user_data_from_ai_decisions, get_since_until_y_m_d

try:
since, until = get_since_until_y_m_d(date)
ai_decisions = get_ai_decisions_by_user_and_timeframe(username, since, until)
Expand Down Expand Up @@ -509,3 +518,17 @@ def write_all_data_of_user_to_csv_by_month(file_path: str, username: str, date:
except Exception as e:
logger.error(f"Failed to write to CSV: {e}")
return f"Failed to write to CSV: {e}"


def get_repositories_from_user(username: str):
data = read_sheet(config.SPREADSHEET_ID)
if not data:
logger.error("No data found in the spreadsheet.")
return

for row in data:
if row[0] == username:
return row[2].split(", ")

logger.error(f"User with Discord handle {username} not found.")
return None

0 comments on commit a0ae549

Please sign in to comment.