diff --git a/services/github/github_manager.py b/services/github/github_manager.py index 3a820dad..69dbfd34 100644 --- a/services/github/github_manager.py +++ b/services/github/github_manager.py @@ -370,6 +370,24 @@ def get_latest_remote_commit_sha( ) +def get_organization_members(owner_name: str, token: str): + """Get members of an organization https://docs.github.com/en/rest/orgs/members?apiVersion=2022-11-28#list-organization-members""" + try: + response = requests.get( + url=f"https://api.github.com/orgs/{owner_name}/members", + headers=create_headers(token=token), + timeout=TIMEOUT_IN_SECONDS, + ) + response.raise_for_status() + return response.json() + except requests.exceptions.HTTPError as e: + logging.error( + msg=f"get_organization_members HTTP Error: {e.response.status_code} - {e.response.text}" + ) + except Exception as e: + logging.error(msg=f"get_organization_members Error: {e}") + + def get_remote_file_content( file_path: str, # Ex) 'src/main.py' owner: str, @@ -423,6 +441,24 @@ def get_remote_file_tree( ) +def get_user_email(user_name: str, token: str): + """Get the email of a GitHub user. https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#get-a-user""" + try: + response: requests.Response = requests.get( + url=f"{GITHUB_API_URL}/user/{user_name}", + headers=create_headers(token=token), + timeout=TIMEOUT_IN_SECONDS, + ) + response.raise_for_status() + return response.json() + except requests.exceptions.HTTPError as e: + logging.error( + msg=f"get_user_email HTTP Error: {e.response.status_code} - {e.response.text}" + ) + except Exception as e: + logging.error(msg=f"get_user_email Error: {e}") + + async def verify_webhook_signature(request: Request, secret: str) -> None: """Verify the webhook signature for security""" signature: str | None = request.headers.get("X-Hub-Signature-256") diff --git a/services/supabase/gitauto_manager.py b/services/supabase/gitauto_manager.py index 4d7cdbfa..83b4e301 100644 --- a/services/supabase/gitauto_manager.py +++ b/services/supabase/gitauto_manager.py @@ -3,6 +3,7 @@ import datetime import logging from supabase import Client +from services.github.github_manager import get_installation_access_token, get_organization_members, get_user_email from services.stripe.customer import create_stripe_customer, subscribe_to_free_plan @@ -80,6 +81,15 @@ def create_installation( } ).execute() + self.create_users_on_installation( + installation_id=installation_id, + owner_id=owner_id, + owner_type=owner_type, + owner_name=owner_name, + user_id=user_id, + user_name=user_name, + ) + # Create User, and set is_selected to True if user has no selected account is_selected = True data, _ = ( @@ -107,6 +117,40 @@ def create_installation( # Raise as installation flow was not successful raise RuntimeError("Installation flow was not successful") + def create_users_on_installation(self, + installation_id: int, + owner_id: int, + owner_type: str, + owner_name: str, + user_id: int, + user_name: str, + ): + token = get_installation_access_token(installation_id=installation_id) + """Create users and get their emails depending on the owner type(user or organization)""" + if(owner_type === "Organization"): + members = get_organization_members(owner_name=owner_name, token=token) + for member in members: + email = get_user_email(user_name=user_name, token=token) + is_selected = user_id == member["id"] + self.client.table(table_name="users").insert( + json={ + "user_id": member["id"], + "user_name": member["login"], + "installation_id": installation_id, + "is_selected": is_selected, + } + ).execute() + # Is just a user + else: + email = get_user_email(user_name=user_name, token=token) + self.client.table(table_name="users").insert( + json={ + "user_id": member["id"], + "user_name": member["login"], + "installation_id": installation_id, + "is_selected": True, + } + def create_user_request( self, user_id: int, installation_id: int, unique_issue_id: str ) -> int: