diff --git a/src/ShinyDeploy/Action/WsDataAction/AddRepository.php b/src/ShinyDeploy/Action/WsDataAction/AddRepository.php index cfc1858..d84844c 100644 --- a/src/ShinyDeploy/Action/WsDataAction/AddRepository.php +++ b/src/ShinyDeploy/Action/WsDataAction/AddRepository.php @@ -3,6 +3,7 @@ use ShinyDeploy\Domain\Database\Auth; use ShinyDeploy\Domain\Database\Repositories; +use ShinyDeploy\Domain\Git; use ShinyDeploy\Exceptions\InvalidPayloadException; use Valitron\Validator; @@ -95,21 +96,13 @@ public function __invoke(array $actionPayload) : bool */ private function checkUrl(string $url, string $username = '', string $password = '') : bool { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_HEADER, true); - curl_setopt($ch, CURLOPT_NOBODY, true); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_MAXREDIRS, 3); - if (!empty($username)) { - curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password); + $credentials = $username ?? ''; + if (!empty($password)) { + $credentials .= ':' . $password; } - $headers = curl_exec($ch); - curl_close($ch); - if (stripos($headers, 'HTTP/1.1 200') !== false) { - return true; - } - return false; + $url = str_replace('://', '://' . $credentials . '@', $url); + + $gitDomain = new Git($this->config, $this->logger); + return $gitDomain->checkRemoteConnectivity($url); } } diff --git a/src/ShinyDeploy/Domain/Git.php b/src/ShinyDeploy/Domain/Git.php index 90f21b6..177f295 100644 --- a/src/ShinyDeploy/Domain/Git.php +++ b/src/ShinyDeploy/Domain/Git.php @@ -366,6 +366,27 @@ public function pruneRemoteBranches(string $repoPath): bool } } + /** + * Check if a remote url is accessible. + * + * @param string $repoUrl + * @return bool + */ + public function checkRemoteConnectivity(string $repoUrl): bool + { + if (empty($repoUrl)) { + return false; + } + $command = 'ls-remote -q -h --exit-code ' . $repoUrl; + try { + $this->exec($command); + return true; + } catch (GitException $e) { + $this->logger->error('Remote Repository URL not accessible.'); + return false; + } + } + /** * Executes a git command and returns response. * @@ -375,7 +396,7 @@ public function pruneRemoteBranches(string $repoPath): bool */ protected function exec(string $command): string { - $command = 'git ' . $command; + $command = 'GIT_TERMINAL_PROMPT=0 git ' . $command; $command = escapeshellcmd($command) . ' 2>&1'; exec($command, $output, $exitCode); $response = implode("\n", $output) ?? ''; diff --git a/src/ShinyDeploy/Domain/Repository.php b/src/ShinyDeploy/Domain/Repository.php index 46ee129..a7f6c96 100644 --- a/src/ShinyDeploy/Domain/Repository.php +++ b/src/ShinyDeploy/Domain/Repository.php @@ -324,30 +324,11 @@ public function checkGit() : bool * Checks if URL responses with status 200. * * @return bool - * @throws \RuntimeException */ public function checkConnectivity() : bool { - if (empty($this->data)) { - throw new \RuntimeException('Repository data not set. Missing inititialization?'); - } - - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $this->data['url']); - curl_setopt($ch, CURLOPT_HEADER, true); - curl_setopt($ch, CURLOPT_NOBODY, true); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_MAXREDIRS, 3); - if (!empty($this->data['username'])) { - curl_setopt($ch, CURLOPT_USERPWD, $this->data['username'].':'.$this->data['password']); - } - $headers = curl_exec($ch); - curl_close($ch); - if (stripos($headers, 'HTTP/1.1 200') !== false) { - return true; - } - return false; + $url = $this->getCredentialsUrl(); + return $this->git->checkRemoteConnectivity($url); } /**