Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) Populate users' email #89

Closed
wants to merge 1 commit into from
Closed
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
36 changes: 36 additions & 0 deletions services/github/github_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down
44 changes: 44 additions & 0 deletions services/supabase/gitauto_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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, _ = (
Expand Down Expand Up @@ -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:
Expand Down
Loading