From 085f6bbbb796d88eb9056de3cf938ccf56953315 Mon Sep 17 00:00:00 2001 From: montero Date: Fri, 26 Sep 2025 22:01:54 +0300 Subject: [PATCH] refactor(gitea): extract clone URL construction into a reusable function --- packages/server/src/utils/providers/gitea.ts | 34 ++++++++++++-------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/server/src/utils/providers/gitea.ts b/packages/server/src/utils/providers/gitea.ts index 874e2679e..c64d0472a 100644 --- a/packages/server/src/utils/providers/gitea.ts +++ b/packages/server/src/utils/providers/gitea.ts @@ -99,6 +99,19 @@ export const refreshGiteaToken = async (giteaProviderId: string) => { } }; +const buildGiteaCloneUrl = ( + giteaUrl: string, + accessToken: string, + owner: string, + repository: string +) => { + const protocol = giteaUrl.startsWith("http://") ? "http" : "https"; + const baseUrl = giteaUrl.replace(/^https?:\/\//, ""); + const repoClone = `${owner}/${repository}.git`; + const cloneUrl = `${protocol}://oauth2:${accessToken}@${baseUrl}/${repoClone}`; + return cloneUrl; +}; + export type ApplicationWithGitea = InferResultType< "applications", { gitea: true } @@ -148,9 +161,8 @@ export const getGiteaCloneCommand = async ( const basePath = isCompose ? COMPOSE_PATH : APPLICATIONS_PATH; const outputPath = join(basePath, appName, "code"); - const baseUrl = gitea?.giteaUrl.replace(/^https?:\/\//, ""); - const repoClone = `${giteaOwner}/${giteaRepository}.git`; - const cloneUrl = `https://oauth2:${gitea?.accessToken}@${baseUrl}/${repoClone}`; + const repoClone = `${giteaOwner}/${giteaRepository}.git`; + const cloneUrl = buildGiteaCloneUrl(gitea?.giteaUrl!, gitea?.accessToken!, giteaOwner!, giteaRepository!); const cloneCommand = ` rm -rf ${outputPath}; @@ -204,9 +216,8 @@ export const cloneGiteaRepository = async ( const outputPath = join(basePath, appName, "code"); await recreateDirectory(outputPath); - const repoClone = `${giteaOwner}/${giteaRepository}.git`; - const baseUrl = giteaProvider.giteaUrl.replace(/^https?:\/\//, ""); - const cloneUrl = `https://oauth2:${giteaProvider.accessToken}@${baseUrl}/${repoClone}`; + const repoClone = `${giteaOwner}/${giteaRepository}.git`; + const cloneUrl = buildGiteaCloneUrl(giteaProvider.giteaUrl, giteaProvider.accessToken!, giteaOwner!, giteaRepository!); writeStream.write(`\nCloning Repo ${repoClone} to ${outputPath}...\n`); @@ -269,9 +280,7 @@ export const cloneRawGiteaRepository = async (entity: Compose) => { const outputPath = join(basePath, appName, "code"); await recreateDirectory(outputPath); - const repoClone = `${giteaOwner}/${giteaRepository}.git`; - const baseUrl = giteaProvider.giteaUrl.replace(/^https?:\/\//, ""); - const cloneUrl = `https://oauth2:${giteaProvider.accessToken}@${baseUrl}/${repoClone}`; + const cloneUrl = buildGiteaCloneUrl(giteaProvider.giteaUrl, giteaProvider.accessToken!, giteaOwner!, giteaRepository!); try { await spawnAsync("git", [ @@ -317,10 +326,9 @@ export const cloneRawGiteaRepositoryRemote = async (compose: Compose) => { const giteaProvider = await findGiteaById(giteaId); const basePath = COMPOSE_PATH; const outputPath = join(basePath, appName, "code"); - const repoClone = `${giteaOwner}/${giteaRepository}.git`; - const baseUrl = giteaProvider.giteaUrl.replace(/^https?:\/\//, ""); - const cloneUrl = `https://oauth2:${giteaProvider.accessToken}@${baseUrl}/${repoClone}`; - try { + const cloneUrl = buildGiteaCloneUrl(giteaProvider.giteaUrl, giteaProvider.accessToken!, giteaOwner!, giteaRepository!); + + try { const command = ` rm -rf ${outputPath}; git clone --branch ${giteaBranch} --depth 1 ${enableSubmodules ? "--recurse-submodules" : ""} ${cloneUrl} ${outputPath}