From dca4520301f9b792e9c3501a6d205c3b7470093a Mon Sep 17 00:00:00 2001 From: yingying Date: Thu, 12 Dec 2024 17:16:44 +0800 Subject: [PATCH 1/4] feat: enable org --- client/app/hooks/useBot.ts | 6 ++-- server/bot/router.py | 42 +++++++++++++++++--------- server/core/dao/repositoryConfigDAO.py | 16 +++++++++- server/github_app/utils.py | 13 -------- 4 files changed, 46 insertions(+), 31 deletions(-) diff --git a/client/app/hooks/useBot.ts b/client/app/hooks/useBot.ts index 9232e939..bb986c80 100644 --- a/client/app/hooks/useBot.ts +++ b/client/app/hooks/useBot.ts @@ -34,7 +34,7 @@ export const useBotDetail = (id: string) => { }); }; -export const useGetBotBoundRepos =(id:string)=>{ +export const useGetBotBoundRepos = (id: string) => { return useQuery({ queryKey: [`bot.boundRepos.${id}`, id], queryFn: async () => getBotBoundRepos(id), @@ -42,7 +42,7 @@ export const useGetBotBoundRepos =(id:string)=>{ enabled: !!id, retry: false, }); -} +}; export const useBotConfig = (id: string, enabled: boolean) => { return useQuery({ @@ -153,7 +153,7 @@ export const useGetBotRagTask = ( queryKey: [`rag.task`, repoName], queryFn: async () => getRagTask(repoName), select: (data) => data, - enabled:!!repoName, + enabled: !!repoName, retry: true, refetchInterval: refetchInterval ? 3 * 1000 : undefined, }); diff --git a/server/bot/router.py b/server/bot/router.py index dad0793b..4518f28e 100644 --- a/server/bot/router.py +++ b/server/bot/router.py @@ -30,6 +30,7 @@ def get_bot_list( ), name: Optional[str] = Query(None, description="Filter bots by name"), user_id: Annotated[str | None, Depends(get_user_id)] = None, + user: Annotated[User | None, Depends(get_user)] = None, ): try: supabase = get_client() @@ -39,21 +40,35 @@ def get_bot_list( if personal == "true": if not user_id: return {"data": [], "personal": personal} - query = query.eq("uid", user_id).order("updated_at", desc=True) - if name: + + auth = Auth.Token(token=user.access_token) + g = Github(auth=auth) + github_user = g.get_user() + orgs = github_user.get_orgs() + repository_config_dao = RepositoryConfigDAO() + bots = repository_config_dao.query_by_owners( + [org.id for org in orgs] + [github_user.id] + ) + bot_ids = [bot["robot_id"] for bot in bots] + bot_ids_str = ",".join(map(str, bot_ids)) # 将 bots ID 列表转换为字符串 + or_clause = f"uid.eq.{user_id},id.in.({bot_ids_str})" + + # 添加过滤条件 query = ( - supabase.table("bots") - .select( - "id, created_at, updated_at, avatar, description, name, public, starters, uid, repo_name" - ) + query.or_(or_clause).order("updated_at", desc=True) + if not name + else query.or_(or_clause) .filter("name", "like", f"%{name}%") + .order("updated_at", desc=True) + ) + else: + query = ( + query.eq("public", True).order("updated_at", desc=True) + if not name + else query.eq("public", True) + .filter("name", "like", f"%{name}%") + .order("updated_at", desc=True) ) - - query = ( - query.eq("public", True).order("updated_at", desc=True) - if not personal or personal != "true" - else query - ) data = query.execute() if not data or not data.data: @@ -186,6 +201,7 @@ async def bot_generator( content={"success": False, "errorMessage": str(e)}, status_code=500 ) + @router.get("/git/avatar", status_code=200) async def get_git_avatar( repo_name: str, @@ -201,8 +217,6 @@ async def get_git_avatar( ) - - @router.put("/update/{id}", status_code=200) def update_bot( id: str, diff --git a/server/core/dao/repositoryConfigDAO.py b/server/core/dao/repositoryConfigDAO.py index b9c410fe..128a63f4 100644 --- a/server/core/dao/repositoryConfigDAO.py +++ b/server/core/dao/repositoryConfigDAO.py @@ -74,8 +74,22 @@ def query_by_owners(self, orgs: List[str]): try: response = ( self.client.table("github_repo_config") - .select("*") + .select("robot_id") + .filter("owner_id", "in", f"({','.join(map(str, orgs))})") + .execute() + ) + return response.data + except Exception as e: + print(f"Error: {e}") + return None + + def query_bot_id_by_owners(self, orgs: List[str]): + try: + response = ( + self.client.table("github_repo_config") + .select("robot_id") .filter("owner_id", "in", f"({','.join(map(str, orgs))})") + .filter("robot_id", "isnot", None) .execute() ) return response.data diff --git a/server/github_app/utils.py b/server/github_app/utils.py index 98e825eb..2b727fed 100644 --- a/server/github_app/utils.py +++ b/server/github_app/utils.py @@ -58,16 +58,3 @@ def get_installation_repositories(access_token: str): }, ) return resp.json() - - -def get_user_orgs(username, access_token: str): - url = f"https://api.github.com/users/{username}/orgs" - resp = requests.get( - url, - headers={ - "X-GitHub-Api-Version": "2022-11-28", - "Accept": "application/vnd.github+json", - "Authorization": f"Bearer {access_token}", - }, - ) - return resp.json() From c40bba1da6504a4890e3bc8a4abad3ec71597f2a Mon Sep 17 00:00:00 2001 From: yingying Date: Thu, 12 Dec 2024 17:23:22 +0800 Subject: [PATCH 2/4] chore: fix ci --- server/auth/router.py | 1 - 1 file changed, 1 deletion(-) diff --git a/server/auth/router.py b/server/auth/router.py index 7a7a3e8b..3d4e7305 100644 --- a/server/auth/router.py +++ b/server/auth/router.py @@ -1,4 +1,3 @@ -import json from github import Github from core.dao.profilesDAO import ProfilesDAO from fastapi import APIRouter, Request, HTTPException, status, Depends From 8b2bd31e45670ac249deffab27eadb98bdca4bff Mon Sep 17 00:00:00 2001 From: yingying Date: Thu, 12 Dec 2024 18:17:38 +0800 Subject: [PATCH 3/4] chore: fix cr --- server/bot/router.py | 16 ++++++++++------ server/bot/util.ts | 0 server/core/dao/repositoryConfigDAO.py | 3 +-- 3 files changed, 11 insertions(+), 8 deletions(-) delete mode 100644 server/bot/util.ts diff --git a/server/bot/router.py b/server/bot/router.py index 4518f28e..14fa3162 100644 --- a/server/bot/router.py +++ b/server/bot/router.py @@ -29,7 +29,6 @@ def get_bot_list( None, description="Filter bots by personal category" ), name: Optional[str] = Query(None, description="Filter bots by name"), - user_id: Annotated[str | None, Depends(get_user_id)] = None, user: Annotated[User | None, Depends(get_user)] = None, ): try: @@ -38,20 +37,25 @@ def get_bot_list( "id, created_at, updated_at, avatar, description, name, public, starters, uid, repo_name" ) if personal == "true": - if not user_id: + if not user or not user.access_token: return {"data": [], "personal": personal} - + user_id = user.id auth = Auth.Token(token=user.access_token) g = Github(auth=auth) github_user = g.get_user() orgs = github_user.get_orgs() repository_config_dao = RepositoryConfigDAO() - bots = repository_config_dao.query_by_owners( + bots = repository_config_dao.query_bot_id_by_owners( [org.id for org in orgs] + [github_user.id] ) - bot_ids = [bot["robot_id"] for bot in bots] + bot_ids = [bot["robot_id"] for bot in bots if bot["robot_id"]] bot_ids_str = ",".join(map(str, bot_ids)) # 将 bots ID 列表转换为字符串 - or_clause = f"uid.eq.{user_id},id.in.({bot_ids_str})" + + or_clause = ( + f"uid.eq.{user_id},id.in.({bot_ids_str})" + if bot_ids_str + else f"uid.eq.{user_id}" + ) # 添加过滤条件 query = ( diff --git a/server/bot/util.ts b/server/bot/util.ts deleted file mode 100644 index e69de29b..00000000 diff --git a/server/core/dao/repositoryConfigDAO.py b/server/core/dao/repositoryConfigDAO.py index 128a63f4..e1f8f6d1 100644 --- a/server/core/dao/repositoryConfigDAO.py +++ b/server/core/dao/repositoryConfigDAO.py @@ -74,7 +74,7 @@ def query_by_owners(self, orgs: List[str]): try: response = ( self.client.table("github_repo_config") - .select("robot_id") + .select("*") .filter("owner_id", "in", f"({','.join(map(str, orgs))})") .execute() ) @@ -89,7 +89,6 @@ def query_bot_id_by_owners(self, orgs: List[str]): self.client.table("github_repo_config") .select("robot_id") .filter("owner_id", "in", f"({','.join(map(str, orgs))})") - .filter("robot_id", "isnot", None) .execute() ) return response.data From a1f80f67e8edb9e7d90bf033f8f4b2619c52bf32 Mon Sep 17 00:00:00 2001 From: yingying Date: Fri, 13 Dec 2024 16:20:09 +0800 Subject: [PATCH 4/4] refactor: fix cr --- server/bot/router.py | 48 ++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/server/bot/router.py b/server/bot/router.py index 14fa3162..00775b9e 100644 --- a/server/bot/router.py +++ b/server/bot/router.py @@ -36,48 +36,38 @@ def get_bot_list( query = supabase.table("bots").select( "id, created_at, updated_at, avatar, description, name, public, starters, uid, repo_name" ) + if personal == "true": if not user or not user.access_token: return {"data": [], "personal": personal} - user_id = user.id + auth = Auth.Token(token=user.access_token) - g = Github(auth=auth) - github_user = g.get_user() - orgs = github_user.get_orgs() + github_user = Github(auth=auth).get_user() + orgs_ids = [org.id for org in github_user.get_orgs()] + bot_ids = [] + repository_config_dao = RepositoryConfigDAO() bots = repository_config_dao.query_bot_id_by_owners( - [org.id for org in orgs] + [github_user.id] + orgs_ids + [github_user.id] ) - bot_ids = [bot["robot_id"] for bot in bots if bot["robot_id"]] - bot_ids_str = ",".join(map(str, bot_ids)) # 将 bots ID 列表转换为字符串 - or_clause = ( - f"uid.eq.{user_id},id.in.({bot_ids_str})" - if bot_ids_str - else f"uid.eq.{user_id}" - ) + if bots: + bot_ids = [bot["robot_id"] for bot in bots if bot["robot_id"]] - # 添加过滤条件 - query = ( - query.or_(or_clause).order("updated_at", desc=True) - if not name - else query.or_(or_clause) - .filter("name", "like", f"%{name}%") - .order("updated_at", desc=True) + or_clause = f"uid.eq.{user.id}" + ( + f",id.in.({','.join(map(str, bot_ids))})" if bot_ids else "" ) + query = query.or_(or_clause) else: - query = ( - query.eq("public", True).order("updated_at", desc=True) - if not name - else query.eq("public", True) - .filter("name", "like", f"%{name}%") - .order("updated_at", desc=True) - ) + query = query.eq("public", True) + if name: + query = query.filter("name", "like", f"%{name}%") + + query = query.order("updated_at", desc=True) data = query.execute() - if not data or not data.data: - return {"data": [], "personal": personal} - return {"data": data.data, "personal": personal} + + return {"data": data.data if data and data.data else [], "personal": personal} except Exception as e: return JSONResponse(