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

This commit is contained in:
Matheus Pratta
2024-06-16 02:23:29 -03:00
parent de7380fb0c
commit e5da464980

View File

@@ -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 [