diff --git a/services/github/github_manager.py b/services/github/github_manager.py index 35e57628..041fb79a 100644 --- a/services/github/github_manager.py +++ b/services/github/github_manager.py @@ -237,6 +237,7 @@ def create_comment_on_issue_with_gitauto_button(payload: GitHubLabeledPayload) - issue_number: int = payload["issue"]["number"] user_id: int = payload["sender"]["id"] user_name: str = payload["sender"]["login"] + email: str = get_user_public_email(username=user_name) supabase_manager = SupabaseManager(url=SUPABASE_URL, key=SUPABASE_SERVICE_ROLE_KEY) @@ -247,6 +248,7 @@ def create_comment_on_issue_with_gitauto_button(payload: GitHubLabeledPayload) - user_id=user_id, user_name=user_name, installation_id=installation_id, + email=email, ) first_issue = True elif supabase_manager.is_users_first_issue( diff --git a/services/supabase/gitauto_manager.py b/services/supabase/gitauto_manager.py index 420d94ad..dfb65530 100644 --- a/services/supabase/gitauto_manager.py +++ b/services/supabase/gitauto_manager.py @@ -2,9 +2,10 @@ from datetime import datetime, timezone from supabase import Client +from postgrest.base_request_builder import APIResponse from services.stripe.customer import create_stripe_customer, subscribe_to_free_plan +from services.supabase.users_manager import UsersManager from utils.handle_exceptions import handle_exceptions -from postgrest.base_request_builder import APIResponse class GitAutoAgentManager: @@ -20,8 +21,6 @@ def complete_and_update_usage_record( token_input: int, token_output: int, total_seconds: int, - user_id: int, - email: str, is_completed: bool = True, ) -> None: """Add agent information to usage record and set is_completed to True.""" @@ -73,6 +72,26 @@ def create_installation( json={"owner_id": owner_id, "stripe_customer_id": customer_id} ).execute() + users_manager = UsersManager(client=self.client) + email_to_store = None if str(email).lower().endswith("@users.noreply.github.com") else email + + response: APIResponse = self.client.table("users").select("email").eq("user_id", user_id).execute() + + if response.data and len(response.data[0]) > 0: + # Update the email if it's different from the stored one + if email_to_store != response.data[0].get('email'): + self.client.table("users").update( + json={"email": email_to_store} + ).eq("user_id", user_id).execute() + else: + # Create the user if it doesn't exist + users_manager.create_user( + user_id=user_id, + user_name=user_name, + installation_id=installation_id, + email=email_to_store, + ) + # Insert installation record self.client.table(table_name="installations").insert( json={ @@ -82,14 +101,6 @@ def create_installation( "owner_id": owner_id, } ).execute() - - if email and not email.lower().endswith("@users.noreply.github.com"): - response: APIResponse = self.client.table("users").select("email").eq("user_id", user_id).execute() - - if response.data and len(response.data[0]) > 0 and email != response.data[0].get('email'): - self.client.table("users").update( - json={"email": email} - ).eq("user_id", user_id).execute() # Create User, and set is_selected to True if user has no selected account for this installation is_selected = True @@ -123,6 +134,7 @@ def create_user_request( .eq(column="unique_id", value=unique_issue_id) .execute() ) + # If no issue exists with that unique_issue_id, create one if not data[1]: self.client.table(table_name="issues").insert( json={ @@ -143,14 +155,15 @@ def create_user_request( .execute() ) - if email and not email.lower().endswith("@users.noreply.github.com"): + if not str(email).lower().endswith("@users.noreply.github.com"): response: APIResponse = self.client.table("users").select("email").eq("user_id", user_id).execute() - if response.data and len(response.data[0]) > 0 and email != response.data[0].get('email'): - self.client.table("users").update( - json={"email": email} - ).eq("user_id", user_id).execute() - + if response.data and len(response.data[0]) > 0: + if email != response.data[0].get('email'): + self.client.table("users").update( + json={"email": email} + ).eq("user_id", user_id).execute() + return data[1][0]["id"] @handle_exceptions(default_return_value=None, raise_on_error=False) diff --git a/services/supabase/users_manager.py b/services/supabase/users_manager.py index dec98c9b..47a07cdb 100644 --- a/services/supabase/users_manager.py +++ b/services/supabase/users_manager.py @@ -30,7 +30,8 @@ def create_user(self, user_id: int, user_name: str, installation_id: int, email: "user_id": user_id, "user_name": user_name, "email": email, - } + }, + on_conflict="user_id" ).execute() self.client.table(table_name="user_installations").insert( diff --git a/tests/services/supabase/test_gitauto_manager.py b/tests/services/supabase/test_gitauto_manager.py index db63ef94..ab3f1272 100644 --- a/tests/services/supabase/test_gitauto_manager.py +++ b/tests/services/supabase/test_gitauto_manager.py @@ -17,6 +17,7 @@ def test_create_update_user_request_works() -> None: # using -1 to not conflict with real data user_id = -1 installation_id = -1 + user_name = "test" # Clean up at the beginning just in case a prior test failed to clean wipe_installation_owner_user_data() @@ -28,7 +29,7 @@ def test_create_update_user_request_works() -> None: owner_name="gitautoai", owner_id=-1, user_id=user_id, - user_name="test", + user_name=user_name, email=EMAIL, ) @@ -67,6 +68,8 @@ def test_complete_and_update_usage_record_only_updates_one_record() -> None: # using -1 to not conflict with real data user_id = -1 installation_id = -1 + user_name = "test" + unique_issue_id = "U/gitautoai/test#01" # Clean up at the beginning just in case a prior test failed to clean wipe_installation_owner_user_data() @@ -78,7 +81,7 @@ def test_complete_and_update_usage_record_only_updates_one_record() -> None: owner_name="gitautoai", owner_id=-1, user_id=user_id, - user_name="test", + user_name=user_name, email=EMAIL, ) @@ -88,7 +91,7 @@ def test_complete_and_update_usage_record_only_updates_one_record() -> None: user_id=user_id, installation_id=installation_id, # fake issue creation - unique_issue_id="U/gitautoai/test#01", + unique_issue_id=unique_issue_id, email=EMAIL, ) @@ -96,7 +99,7 @@ def test_complete_and_update_usage_record_only_updates_one_record() -> None: user_id=user_id, installation_id=installation_id, # fake issue creation - unique_issue_id="U/gitautoai/test#01", + unique_issue_id=unique_issue_id, email=EMAIL, ) assert isinstance(