Skip to content

Commit

Permalink
Optimize connectivity test git repository urls
Browse files Browse the repository at this point in the history
  • Loading branch information
nekudo committed Oct 7, 2018
1 parent bbc60b9 commit ad2d704
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 37 deletions.
23 changes: 8 additions & 15 deletions src/ShinyDeploy/Action/WsDataAction/AddRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
23 changes: 22 additions & 1 deletion src/ShinyDeploy/Domain/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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) ?? '';
Expand Down
23 changes: 2 additions & 21 deletions src/ShinyDeploy/Domain/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit ad2d704

Please sign in to comment.