fix: add tests for git url converts

This commit is contained in:
Andras Bacsai
2024-11-12 11:32:18 +01:00
parent 4a2846fcd0
commit 327b4308d4
5 changed files with 273 additions and 203 deletions

View File

@@ -906,50 +906,7 @@ 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) {
$port = $matches[0];
$gitHost = str($this->git_repository)->before(':');
$gitRepo = str($this->git_repository)->after('/');
$repository = "$gitHost:$gitRepo";
}
return [
'repository' => $repository,
'port' => $port,
];
return convertGitUrl($this->git_repository, $this->deploymentType(), $this->source);
}
public function generateBaseDir(string $uuid)