Skip to content

Commit

Permalink
adding email to create_user calls, created upsert email logic on inst…
Browse files Browse the repository at this point in the history
…allation and create_user_request, added on_conflict to create_user, and created variables for user_name and unique_issue_id in test_complete_and_update_usage_record_only_updates_one_record
  • Loading branch information
BigOleHealz committed Oct 22, 2024
1 parent 014b68b commit 6c00947
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
2 changes: 2 additions & 0 deletions services/github/github_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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(
Expand Down
47 changes: 30 additions & 17 deletions services/supabase/gitauto_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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."""
Expand Down Expand Up @@ -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={
Expand All @@ -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
Expand Down Expand Up @@ -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={
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion services/supabase/users_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 7 additions & 4 deletions tests/services/supabase/test_gitauto_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
)

Expand Down Expand Up @@ -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()
Expand All @@ -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,
)

Expand All @@ -88,15 +91,15 @@ 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,
)

usage_record_id = supabase_manager.create_user_request(
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(
Expand Down

0 comments on commit 6c00947

Please sign in to comment.