diff --git a/src/utils/request.ts b/src/utils/request.ts index a9f1e3e5c8b63..667e1bc9f8550 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -2,6 +2,16 @@ export const getRequestBaseUrl = (): string => { return import.meta.env.VITE_APP_API_BASE_URL || 'http://localhost:8000'; }; +export const getRequestBaseUrlWs = (): string => { + if (import.meta.env.VITE_APP_API_BASE_URL.startsWith('http://')) { + return ( + import.meta.env.VITE_APP_API_BASE_URL.replace(/https?/, 'ws') || + 'ws://localhost:8000' + ); + } + return `ws://${window.location.hostname}${import.meta.env.VITE_APP_API_BASE_URL}`; +}; + export const getEmptyResponseWithListData = < T = any, >(): ResponseWithListData => { diff --git a/src/views/git/detail/useGitDetail.ts b/src/views/git/detail/useGitDetail.ts index 4c2c9df1a715c..9b563552494b3 100644 --- a/src/views/git/detail/useGitDetail.ts +++ b/src/views/git/detail/useGitDetail.ts @@ -22,6 +22,7 @@ import { TAB_NAME_LOGS, TAB_NAME_OVERVIEW, } from '@/constants'; +import { getRequestBaseUrlWs } from '@/utils'; // i18n const t = translate; @@ -160,26 +161,36 @@ const useGitDetail = () => { }; const onPull = async () => { - pullLoading.value = true; - try { - const res = await store.dispatch(`${ns}/pull`, { - id: id.value, - }); - if (res.data) { - ElMessage.info(res.data); + const ws = new WebSocket( + getRequestBaseUrlWs() + `/gits/${id.value}/pull/ws` + ); + ws.onopen = () => { + pullLoading.value = true; + }; + ws.onmessage = event => { + console.debug(event.data); + }; + ws.onerror = error => { + pullLoading.value = false; + ElMessage.error(error); + }; + ws.onclose = event => { + pullLoading.value = false; + if (event.code === 1000) { + if (event.reason) { + ElMessage.info(event.reason); + } else { + ElMessage.success(t('components.git.common.message.success.pull')); + if (activeTabName.value === TAB_NAME_FILES) { + store.dispatch(`${ns}/listDir`, { id: id.value }); + } else if (activeTabName.value === TAB_NAME_LOGS) { + store.dispatch(`${ns}/getLogs`, { id: id.value }); + } + } } else { - ElMessage.success(t('components.git.common.message.success.pull')); - } - if (activeTabName.value === TAB_NAME_FILES) { - await store.dispatch(`${ns}/listDir`, { id: id.value }); - } else if (activeTabName.value === TAB_NAME_LOGS) { - await store.dispatch(`${ns}/getLogs`, { id: id.value }); + ElMessage.error(event.reason); } - } catch (e: any) { - ElMessage.error(e.message); - } finally { - pullLoading.value = false; - } + }; }; const onPush = async () => {