PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/applications/drydock/storage/DrydockRepositoryOperation.php

http://github.com/facebook/phabricator
PHP | 282 lines | 219 code | 56 blank | 7 comment | 3 complexity | 26736d8925369fbed3e7902fb6127b74 MD5 | raw file
Possible License(s): JSON, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause, LGPL-2.0, MIT, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * Represents a request to perform a repository operation like a merge or
  4. * cherry-pick.
  5. */
  6. final class DrydockRepositoryOperation extends DrydockDAO
  7. implements
  8. PhabricatorPolicyInterface {
  9. const STATE_WAIT = 'wait';
  10. const STATE_WORK = 'work';
  11. const STATE_DONE = 'done';
  12. const STATE_FAIL = 'fail';
  13. protected $authorPHID;
  14. protected $objectPHID;
  15. protected $repositoryPHID;
  16. protected $repositoryTarget;
  17. protected $operationType;
  18. protected $operationState;
  19. protected $properties = array();
  20. protected $isDismissed;
  21. private $repository = self::ATTACHABLE;
  22. private $object = self::ATTACHABLE;
  23. private $implementation = self::ATTACHABLE;
  24. private $workingCopyLease = self::ATTACHABLE;
  25. public static function initializeNewOperation(
  26. DrydockRepositoryOperationType $op) {
  27. return id(new DrydockRepositoryOperation())
  28. ->setOperationState(self::STATE_WAIT)
  29. ->setOperationType($op->getOperationConstant())
  30. ->setIsDismissed(0);
  31. }
  32. protected function getConfiguration() {
  33. return array(
  34. self::CONFIG_AUX_PHID => true,
  35. self::CONFIG_SERIALIZATION => array(
  36. 'properties' => self::SERIALIZATION_JSON,
  37. ),
  38. self::CONFIG_COLUMN_SCHEMA => array(
  39. 'repositoryTarget' => 'bytes',
  40. 'operationType' => 'text32',
  41. 'operationState' => 'text32',
  42. 'isDismissed' => 'bool',
  43. ),
  44. self::CONFIG_KEY_SCHEMA => array(
  45. 'key_object' => array(
  46. 'columns' => array('objectPHID'),
  47. ),
  48. 'key_repository' => array(
  49. 'columns' => array('repositoryPHID', 'operationState'),
  50. ),
  51. ),
  52. ) + parent::getConfiguration();
  53. }
  54. public function generatePHID() {
  55. return PhabricatorPHID::generateNewPHID(
  56. DrydockRepositoryOperationPHIDType::TYPECONST);
  57. }
  58. public function attachRepository(PhabricatorRepository $repository) {
  59. $this->repository = $repository;
  60. return $this;
  61. }
  62. public function getRepository() {
  63. return $this->assertAttached($this->repository);
  64. }
  65. public function attachObject($object) {
  66. $this->object = $object;
  67. return $this;
  68. }
  69. public function getObject() {
  70. return $this->assertAttached($this->object);
  71. }
  72. public function attachImplementation(DrydockRepositoryOperationType $impl) {
  73. $this->implementation = $impl;
  74. return $this;
  75. }
  76. public function getImplementation() {
  77. return $this->implementation;
  78. }
  79. public function getWorkingCopyLease() {
  80. return $this->assertAttached($this->workingCopyLease);
  81. }
  82. public function attachWorkingCopyLease(DrydockLease $lease) {
  83. $this->workingCopyLease = $lease;
  84. return $this;
  85. }
  86. public function hasWorkingCopyLease() {
  87. return ($this->workingCopyLease !== self::ATTACHABLE);
  88. }
  89. public function getProperty($key, $default = null) {
  90. return idx($this->properties, $key, $default);
  91. }
  92. public function setProperty($key, $value) {
  93. $this->properties[$key] = $value;
  94. return $this;
  95. }
  96. public static function getOperationStateNameMap() {
  97. return array(
  98. self::STATE_WAIT => pht('Waiting'),
  99. self::STATE_WORK => pht('Working'),
  100. self::STATE_DONE => pht('Done'),
  101. self::STATE_FAIL => pht('Failed'),
  102. );
  103. }
  104. public static function getOperationStateIcon($state) {
  105. $map = array(
  106. self::STATE_WAIT => 'fa-clock-o',
  107. self::STATE_WORK => 'fa-plane ph-spin blue',
  108. self::STATE_DONE => 'fa-check green',
  109. self::STATE_FAIL => 'fa-times red',
  110. );
  111. return idx($map, $state, null);
  112. }
  113. public static function getOperationStateName($state) {
  114. $map = self::getOperationStateNameMap();
  115. return idx($map, $state, pht('<Unknown: %s>', $state));
  116. }
  117. public function scheduleUpdate() {
  118. PhabricatorWorker::scheduleTask(
  119. 'DrydockRepositoryOperationUpdateWorker',
  120. array(
  121. 'operationPHID' => $this->getPHID(),
  122. ),
  123. array(
  124. 'objectPHID' => $this->getPHID(),
  125. 'priority' => PhabricatorWorker::PRIORITY_ALERTS,
  126. ));
  127. }
  128. public function applyOperation(DrydockInterface $interface) {
  129. $impl = $this->getImplementation();
  130. $impl->setInterface($interface);
  131. return $impl->applyOperation($this, $interface);
  132. }
  133. public function getOperationDescription(PhabricatorUser $viewer) {
  134. return $this->getImplementation()->getOperationDescription(
  135. $this,
  136. $viewer);
  137. }
  138. public function getOperationCurrentStatus(PhabricatorUser $viewer) {
  139. return $this->getImplementation()->getOperationCurrentStatus(
  140. $this,
  141. $viewer);
  142. }
  143. public function isUnderway() {
  144. switch ($this->getOperationState()) {
  145. case self::STATE_WAIT:
  146. case self::STATE_WORK:
  147. return true;
  148. }
  149. return false;
  150. }
  151. public function isDone() {
  152. return ($this->getOperationState() === self::STATE_DONE);
  153. }
  154. public function getWorkingCopyMerges() {
  155. return $this->getImplementation()->getWorkingCopyMerges(
  156. $this);
  157. }
  158. public function setWorkingCopyLeasePHID($lease_phid) {
  159. return $this->setProperty('exec.leasePHID', $lease_phid);
  160. }
  161. public function getWorkingCopyLeasePHID() {
  162. return $this->getProperty('exec.leasePHID');
  163. }
  164. public function setCommandError(array $error) {
  165. return $this->setProperty('exec.workingcopy.error', $error);
  166. }
  167. public function getCommandError() {
  168. return $this->getProperty('exec.workingcopy.error');
  169. }
  170. public function logText($text) {
  171. return $this->logEvent(
  172. DrydockTextLogType::LOGCONST,
  173. array(
  174. 'text' => $text,
  175. ));
  176. }
  177. public function logEvent($type, array $data = array()) {
  178. $log = id(new DrydockLog())
  179. ->setEpoch(PhabricatorTime::getNow())
  180. ->setType($type)
  181. ->setData($data);
  182. $log->setOperationPHID($this->getPHID());
  183. if ($this->hasWorkingCopyLease()) {
  184. $lease = $this->getWorkingCopyLease();
  185. $log->setLeasePHID($lease->getPHID());
  186. $resource_phid = $lease->getResourcePHID();
  187. if ($resource_phid) {
  188. $resource = $lease->getResource();
  189. $log->setResourcePHID($resource->getPHID());
  190. $log->setBlueprintPHID($resource->getBlueprintPHID());
  191. }
  192. }
  193. return $log->save();
  194. }
  195. /* -( PhabricatorPolicyInterface )----------------------------------------- */
  196. public function getCapabilities() {
  197. return array(
  198. PhabricatorPolicyCapability::CAN_VIEW,
  199. PhabricatorPolicyCapability::CAN_EDIT,
  200. );
  201. }
  202. public function getPolicy($capability) {
  203. $need_capability = $this->getRequiredRepositoryCapability($capability);
  204. return $this->getRepository()
  205. ->getPolicy($need_capability);
  206. }
  207. public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
  208. $need_capability = $this->getRequiredRepositoryCapability($capability);
  209. return $this->getRepository()
  210. ->hasAutomaticCapability($need_capability, $viewer);
  211. }
  212. public function describeAutomaticCapability($capability) {
  213. return pht(
  214. 'A repository operation inherits the policies of the repository it '.
  215. 'affects.');
  216. }
  217. private function getRequiredRepositoryCapability($capability) {
  218. // To edit a RepositoryOperation, require that the user be able to push
  219. // to the repository.
  220. $map = array(
  221. PhabricatorPolicyCapability::CAN_EDIT =>
  222. DiffusionPushCapability::CAPABILITY,
  223. );
  224. return idx($map, $capability, $capability);
  225. }
  226. }