({
type = "application",
...entity
}: CloneGitRepository)
| 18 | } |
| 19 | |
| 20 | export const cloneGitRepository = async ({ |
| 21 | type = "application", |
| 22 | ...entity |
| 23 | }: CloneGitRepository) => { |
| 24 | let command = "set -e;"; |
| 25 | const { |
| 26 | appName, |
| 27 | customGitUrl, |
| 28 | customGitBranch, |
| 29 | customGitSSHKeyId, |
| 30 | enableSubmodules, |
| 31 | serverId, |
| 32 | outputPathOverride, |
| 33 | } = entity; |
| 34 | const { SSH_PATH, COMPOSE_PATH, APPLICATIONS_PATH } = paths(!!serverId); |
| 35 | |
| 36 | if (!customGitUrl || !customGitBranch) { |
| 37 | command += `echo "Error: ❌ Repository not found"; exit 1;`; |
| 38 | return command; |
| 39 | } |
| 40 | |
| 41 | const temporalKeyPath = path.join("/tmp", "id_rsa"); |
| 42 | |
| 43 | if (customGitSSHKeyId) { |
| 44 | const sshKey = await findSSHKeyById(customGitSSHKeyId); |
| 45 | |
| 46 | command += ` |
| 47 | echo "${sshKey.privateKey}" > ${temporalKeyPath} |
| 48 | chmod 600 ${temporalKeyPath}; |
| 49 | `; |
| 50 | } |
| 51 | const basePath = type === "compose" ? COMPOSE_PATH : APPLICATIONS_PATH; |
| 52 | const outputPath = outputPathOverride ?? join(basePath, appName, "code"); |
| 53 | const knownHostsPath = path.join(SSH_PATH, "known_hosts"); |
| 54 | |
| 55 | if (!isHttpOrHttps(customGitUrl)) { |
| 56 | if (!customGitSSHKeyId) { |
| 57 | command += `echo "Error: ❌ You are trying to clone a ssh repository without a ssh key, please set a ssh key"; exit 1;`; |
| 58 | return command; |
| 59 | } |
| 60 | command += addHostToKnownHostsCommand(customGitUrl); |
| 61 | } |
| 62 | command += `rm -rf ${outputPath};`; |
| 63 | command += `mkdir -p ${outputPath};`; |
| 64 | command += `echo "Cloning Repo Custom ${customGitUrl} to ${outputPath}: ✅";`; |
| 65 | |
| 66 | if (customGitSSHKeyId) { |
| 67 | await updateSSHKeyById({ |
| 68 | sshKeyId: customGitSSHKeyId, |
| 69 | lastUsedAt: new Date().toISOString(), |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | if (customGitSSHKeyId) { |
| 74 | const sshKey = await findSSHKeyById(customGitSSHKeyId); |
| 75 | const { port } = sanitizeRepoPathSSH(customGitUrl); |
| 76 | const gitSshCommand = `ssh -i /tmp/id_rsa${port ? ` -p ${port}` : ""} -o UserKnownHostsFile=${knownHostsPath}`; |
| 77 | command += `echo "${sshKey.privateKey}" > /tmp/id_rsa;`; |
no test coverage detected