From e5da464980de312267a7a05b5e6b3f81a8ef6fb7 Mon Sep 17 00:00:00 2001 From: Matheus Pratta Date: Sun, 16 Jun 2024 02:23:29 -0300 Subject: [PATCH] fix: convert HTTP to SSH source when using deploy key on GitHub --- app/Models/Application.php | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/app/Models/Application.php b/app/Models/Application.php index 6e55f6626..3fb7da903 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -662,6 +662,37 @@ class Application extends BaseModel public function customRepository() { + $repository = $this->git_repository; + + // Let's try and parse the string to detect if it's a valid SSH string or not + $sshMatches = []; + preg_match('/((.*?)\:\/\/)?(.*@.*:.*)/', $this->git_repository, $sshMatches); + + if ($this->deploymentType() === 'deploy_key' && empty($sshMatches) && $this->source) { + // If this happens, the user may have provided an HTTP URL when they needed an SSH one + // Let's try and fix that for known Git providers + $providerInfo = [ + 'host' => null, + 'user' => 'git', + 'port' => 22, + 'repository' => $this->git_repository, + ]; + + switch ($this->source->getMorphClass()) { + case \App\Models\GithubApp::class: + $providerInfo['host'] = Url::fromString($this->source->html_url)->getHost(); + $providerInfo['port'] = $this->source->custom_port; + $providerInfo['user'] = $this->source->custom_user; + break; + } + + if (! empty($providerInfo['host'])) { + $repository = ($providerInfo['port'] === 22) + ? "{$providerInfo['user']}@{$providerInfo['host']}:{$providerInfo['repository']}" + : "ssh://{$providerInfo['user']}@{$providerInfo['host']}:{$providerInfo['port']}/{$providerInfo['repository']}"; + } + } + preg_match('/(?<=:)\d+(?=\/)/', $this->git_repository, $matches); $port = 22; if (count($matches) === 1) { @@ -669,8 +700,6 @@ class Application extends BaseModel $gitHost = str($this->git_repository)->before(':'); $gitRepo = str($this->git_repository)->after('/'); $repository = "$gitHost:$gitRepo"; - } else { - $repository = $this->git_repository; } return [