Changeset View
Changeset View
Standalone View
Standalone View
src/applications/diffusion/ssh/DiffusionSSHWorkflow.php
| <?php | <?php | ||||
| abstract class DiffusionSSHWorkflow extends PhabricatorSSHWorkflow { | abstract class DiffusionSSHWorkflow extends PhabricatorSSHWorkflow { | ||||
| private $args; | private $args; | ||||
| private $repository; | private $repository; | ||||
| private $hasWriteAccess; | private $hasWriteAccess; | ||||
| public function getRepository() { | public function getRepository() { | ||||
| if (!$this->repository) { | if (!$this->repository) { | ||||
| throw new Exception('Call loadRepository() before getRepository()!'); | throw new Exception(pht('Repository is not available yet!')); | ||||
| } | } | ||||
| return $this->repository; | return $this->repository; | ||||
| } | } | ||||
| private function setRepository(PhabricatorRepository $repository) { | |||||
| $this->repository = $repository; | |||||
| return $this; | |||||
| } | |||||
| public function getArgs() { | public function getArgs() { | ||||
| return $this->args; | return $this->args; | ||||
| } | } | ||||
| public function getEnvironment() { | public function getEnvironment() { | ||||
| $env = array( | $env = array( | ||||
| DiffusionCommitHookEngine::ENV_USER => $this->getUser()->getUsername(), | DiffusionCommitHookEngine::ENV_USER => $this->getUser()->getUsername(), | ||||
| DiffusionCommitHookEngine::ENV_REMOTE_PROTOCOL => 'ssh', | DiffusionCommitHookEngine::ENV_REMOTE_PROTOCOL => 'ssh', | ||||
| ); | ); | ||||
| $ssh_client = getenv('SSH_CLIENT'); | $ssh_client = getenv('SSH_CLIENT'); | ||||
| if ($ssh_client) { | if ($ssh_client) { | ||||
| // This has the format "<ip> <remote-port> <local-port>". Grab the IP. | // This has the format "<ip> <remote-port> <local-port>". Grab the IP. | ||||
| $remote_address = head(explode(' ', $ssh_client)); | $remote_address = head(explode(' ', $ssh_client)); | ||||
| $env[DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS] = $remote_address; | $env[DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS] = $remote_address; | ||||
| } | } | ||||
| return $env; | return $env; | ||||
| } | } | ||||
| /** | |||||
| * Identify and load the affected repository. | |||||
| */ | |||||
| abstract protected function identifyRepository(); | |||||
| abstract protected function executeRepositoryOperations(); | abstract protected function executeRepositoryOperations(); | ||||
| protected function writeError($message) { | protected function writeError($message) { | ||||
| $this->getErrorChannel()->write($message); | $this->getErrorChannel()->write($message); | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| final public function execute(PhutilArgumentParser $args) { | final public function execute(PhutilArgumentParser $args) { | ||||
| $this->args = $args; | $this->args = $args; | ||||
| $repository = $this->identifyRepository(); | |||||
| $this->setRepository($repository); | |||||
| // TODO: Here, we would make a proxying decision, had I implemented | |||||
| // proxying yet. | |||||
| try { | try { | ||||
| return $this->executeRepositoryOperations(); | return $this->executeRepositoryOperations(); | ||||
| } catch (Exception $ex) { | } catch (Exception $ex) { | ||||
| $this->writeError(get_class($ex).': '.$ex->getMessage()); | $this->writeError(get_class($ex).': '.$ex->getMessage()); | ||||
| return 1; | return 1; | ||||
| } | } | ||||
| } | } | ||||
| protected function loadRepository($path) { | protected function loadRepositoryWithPath($path) { | ||||
| $viewer = $this->getUser(); | $viewer = $this->getUser(); | ||||
| $regex = '@^/?diffusion/(?P<callsign>[A-Z]+)(?:/|\z)@'; | $regex = '@^/?diffusion/(?P<callsign>[A-Z]+)(?:/|\z)@'; | ||||
| $matches = null; | $matches = null; | ||||
| if (!preg_match($regex, $path, $matches)) { | if (!preg_match($regex, $path, $matches)) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht( | pht( | ||||
| 'Unrecognized repository path "%s". Expected a path like '. | 'Unrecognized repository path "%s". Expected a path like '. | ||||
| Show All 20 Lines | switch ($repository->getServeOverSSH()) { | ||||
| // check write access when the user actually issues a write command. | // check write access when the user actually issues a write command. | ||||
| break; | break; | ||||
| case PhabricatorRepository::SERVE_OFF: | case PhabricatorRepository::SERVE_OFF: | ||||
| default: | default: | ||||
| throw new Exception( | throw new Exception( | ||||
| pht('This repository is not available over SSH.')); | pht('This repository is not available over SSH.')); | ||||
| } | } | ||||
| $this->repository = $repository; | |||||
| return $repository; | return $repository; | ||||
| } | } | ||||
| protected function requireWriteAccess($protocol_command = null) { | protected function requireWriteAccess($protocol_command = null) { | ||||
| if ($this->hasWriteAccess === true) { | if ($this->hasWriteAccess === true) { | ||||
| return; | return; | ||||
| } | } | ||||
| Show All 39 Lines | |||||