Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

fix: convert HTTP to SSH source when using deploy key on GitHub

Matheus Prattae5da464980de
diff --git a/app/Models/Application.php b/app/Models/Application.php
index 6e55f6626c..3fb7da9035 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 [