From e4ec98fb1c006c884b31ad7c1221219e8aa1dc64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BC=A8=E7=BC=A8?= Date: Wed, 4 Dec 2024 17:08:02 +0800 Subject: [PATCH] fix: get the GitHub avatar from the api instead of the cache (#561) * feat: add the api to get the github avatar * fix: close the issue #554 --- .../factory/edit/components/BotCreateForm.tsx | 5 +++-- client/app/hooks/useBot.ts | 12 +++++++++++- client/app/services/BotsController.ts | 4 ++++ server/bot/router.py | 16 ++++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/client/app/factory/edit/components/BotCreateForm.tsx b/client/app/factory/edit/components/BotCreateForm.tsx index 5389dc45..2033cd04 100644 --- a/client/app/factory/edit/components/BotCreateForm.tsx +++ b/client/app/factory/edit/components/BotCreateForm.tsx @@ -24,7 +24,7 @@ import InputList from './InputList'; import BulbIcon from '@/public/icons/BulbIcon'; import GitHubIcon from '@/public/icons/GitHubIcon'; import { random } from 'lodash'; -import { useBotDelete } from '@/app/hooks/useBot'; +import { useBotDelete, useGetGitAvatar } from '@/app/hooks/useBot'; import { ToastContainer, toast } from 'react-toastify'; import { useBot } from '@/app/contexts/BotContext'; @@ -37,6 +37,7 @@ import CreateButton from '@/app/user/tokens/components/CreateButton'; const BotCreateFrom = () => { const { botProfile, setBotProfile } = useBot(); + const { data: gitAvatar } = useGetGitAvatar(botProfile?.repoName); const router = useRouter(); const { isOpen, onOpen, onOpenChange, onClose } = useDisclosure(); const { data: availableLLMs = [] } = useAvailableLLMs(); @@ -128,7 +129,7 @@ const BotCreateFrom = () => { aria-label={I18N.components.BotCreateFrom.gITHU} onClick={() => { setBotProfile((draft: BotProfile) => { - draft.avatar = botProfile?.gitAvatar; + draft.avatar = gitAvatar; }); }} > diff --git a/client/app/hooks/useBot.ts b/client/app/hooks/useBot.ts index 83684e9e..beca50da 100644 --- a/client/app/hooks/useBot.ts +++ b/client/app/hooks/useBot.ts @@ -9,6 +9,7 @@ import { getBotInfoByRepoName, getBotList, getChunkList, + getGitAvatarByRepoName, getRagTask, getUserPeterCatAppRepos, publicBot, @@ -191,7 +192,16 @@ export const useGetUserPeterCatAppRepos = (enabled: boolean = true) => { queryKey: ['github.user.app.repos'], queryFn: async () => getUserPeterCatAppRepos(), select: (data) => data.data, - enabled + enabled, + }); +}; + +export const useGetGitAvatar = (repoName?: string) => { + return useQuery({ + queryKey: ['github.repo.name', repoName], + queryFn: async () => getGitAvatarByRepoName(repoName!), + select: (data) => data.data.data, + enabled: !!repoName, }); }; diff --git a/client/app/services/BotsController.ts b/client/app/services/BotsController.ts index 4908337c..eff1896b 100644 --- a/client/app/services/BotsController.ts +++ b/client/app/services/BotsController.ts @@ -67,6 +67,10 @@ export async function getBotInfoByRepoName(params: { return axios.post(`${apiDomain}/api/bot/config/generator`, params); } +export async function getGitAvatarByRepoName(repo_name: string) { + return axios.get(`${apiDomain}/api/bot/git/avatar?repo_name=${repo_name}`); +} + export async function getChunkList( repo_name: string, page_size: number, diff --git a/server/bot/router.py b/server/bot/router.py index c01380ec..6608367a 100644 --- a/server/bot/router.py +++ b/server/bot/router.py @@ -173,6 +173,22 @@ 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, +): + try: + g = Github() + repo = g.get_repo(repo_name) + avatar = repo.organization.avatar_url if repo.organization else None + return JSONResponse(content={"success": True, "data": avatar}) + except Exception as e: + return JSONResponse( + content={"success": False, "errorMessage": str(e)}, status_code=500 + ) + + + @router.put("/update/{id}", status_code=200) def update_bot(