[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class HarbormasterBuildable extends HarbormasterDAO 4 implements 5 PhabricatorPolicyInterface, 6 HarbormasterBuildableInterface { 7 8 protected $buildablePHID; 9 protected $containerPHID; 10 protected $buildableStatus; 11 protected $isManualBuildable; 12 13 private $buildableObject = self::ATTACHABLE; 14 private $containerObject = self::ATTACHABLE; 15 private $buildableHandle = self::ATTACHABLE; 16 private $containerHandle = self::ATTACHABLE; 17 private $builds = self::ATTACHABLE; 18 19 const STATUS_BUILDING = 'building'; 20 const STATUS_PASSED = 'passed'; 21 const STATUS_FAILED = 'failed'; 22 23 public static function getBuildableStatusName($status) { 24 switch ($status) { 25 case self::STATUS_BUILDING: 26 return pht('Building'); 27 case self::STATUS_PASSED: 28 return pht('Passed'); 29 case self::STATUS_FAILED: 30 return pht('Failed'); 31 default: 32 return pht('Unknown'); 33 } 34 } 35 36 public static function getBuildableStatusIcon($status) { 37 switch ($status) { 38 case self::STATUS_BUILDING: 39 return PHUIStatusItemView::ICON_RIGHT; 40 case self::STATUS_PASSED: 41 return PHUIStatusItemView::ICON_ACCEPT; 42 case self::STATUS_FAILED: 43 return PHUIStatusItemView::ICON_REJECT; 44 default: 45 return PHUIStatusItemView::ICON_QUESTION; 46 } 47 } 48 49 public static function getBuildableStatusColor($status) { 50 switch ($status) { 51 case self::STATUS_BUILDING: 52 return 'blue'; 53 case self::STATUS_PASSED: 54 return 'green'; 55 case self::STATUS_FAILED: 56 return 'red'; 57 default: 58 return 'bluegrey'; 59 } 60 } 61 62 public static function initializeNewBuildable(PhabricatorUser $actor) { 63 return id(new HarbormasterBuildable()) 64 ->setIsManualBuildable(0) 65 ->setBuildableStatus(self::STATUS_BUILDING); 66 } 67 68 public function getMonogram() { 69 return 'B'.$this->getID(); 70 } 71 72 /** 73 * Returns an existing buildable for the object's PHID or creates a 74 * new buildable implicitly if needed. 75 */ 76 public static function createOrLoadExisting( 77 PhabricatorUser $actor, 78 $buildable_object_phid, 79 $container_object_phid) { 80 81 $buildable = id(new HarbormasterBuildableQuery()) 82 ->setViewer($actor) 83 ->withBuildablePHIDs(array($buildable_object_phid)) 84 ->withManualBuildables(false) 85 ->setLimit(1) 86 ->executeOne(); 87 if ($buildable) { 88 return $buildable; 89 } 90 $buildable = HarbormasterBuildable::initializeNewBuildable($actor) 91 ->setBuildablePHID($buildable_object_phid) 92 ->setContainerPHID($container_object_phid); 93 $buildable->save(); 94 return $buildable; 95 } 96 97 /** 98 * Looks up the plan PHIDs and applies the plans to the specified 99 * object identified by it's PHID. 100 */ 101 public static function applyBuildPlans( 102 $phid, 103 $container_phid, 104 array $plan_phids) { 105 106 if (count($plan_phids) === 0) { 107 return; 108 } 109 110 // Skip all of this logic if the Harbormaster application 111 // isn't currently installed. 112 113 $harbormaster_app = 'PhabricatorHarbormasterApplication'; 114 if (!PhabricatorApplication::isClassInstalled($harbormaster_app)) { 115 return; 116 } 117 118 $buildable = HarbormasterBuildable::createOrLoadExisting( 119 PhabricatorUser::getOmnipotentUser(), 120 $phid, 121 $container_phid); 122 123 $plans = id(new HarbormasterBuildPlanQuery()) 124 ->setViewer(PhabricatorUser::getOmnipotentUser()) 125 ->withPHIDs($plan_phids) 126 ->execute(); 127 foreach ($plans as $plan) { 128 if ($plan->isDisabled()) { 129 // TODO: This should be communicated more clearly -- maybe we should 130 // create the build but set the status to "disabled" or "derelict". 131 continue; 132 } 133 134 $buildable->applyPlan($plan); 135 } 136 } 137 138 public function applyPlan(HarbormasterBuildPlan $plan) { 139 $viewer = PhabricatorUser::getOmnipotentUser(); 140 $build = HarbormasterBuild::initializeNewBuild($viewer) 141 ->setBuildablePHID($this->getPHID()) 142 ->setBuildPlanPHID($plan->getPHID()) 143 ->setBuildStatus(HarbormasterBuild::STATUS_PENDING) 144 ->save(); 145 146 PhabricatorWorker::scheduleTask( 147 'HarbormasterBuildWorker', 148 array( 149 'buildID' => $build->getID(), 150 )); 151 152 return $build; 153 } 154 155 public function getConfiguration() { 156 return array( 157 self::CONFIG_AUX_PHID => true, 158 self::CONFIG_COLUMN_SCHEMA => array( 159 'containerPHID' => 'phid?', 160 'buildableStatus' => 'text32', 161 'isManualBuildable' => 'bool', 162 ), 163 self::CONFIG_KEY_SCHEMA => array( 164 'key_buildable' => array( 165 'columns' => array('buildablePHID'), 166 ), 167 'key_container' => array( 168 'columns' => array('containerPHID'), 169 ), 170 'key_manual' => array( 171 'columns' => array('isManualBuildable'), 172 ), 173 ), 174 ) + parent::getConfiguration(); 175 } 176 177 public function generatePHID() { 178 return PhabricatorPHID::generateNewPHID( 179 HarbormasterBuildablePHIDType::TYPECONST); 180 } 181 182 public function attachBuildableObject($buildable_object) { 183 $this->buildableObject = $buildable_object; 184 return $this; 185 } 186 187 public function getBuildableObject() { 188 return $this->assertAttached($this->buildableObject); 189 } 190 191 public function attachContainerObject($container_object) { 192 $this->containerObject = $container_object; 193 return $this; 194 } 195 196 public function getContainerObject() { 197 return $this->assertAttached($this->containerObject); 198 } 199 200 public function attachContainerHandle($container_handle) { 201 $this->containerHandle = $container_handle; 202 return $this; 203 } 204 205 public function getContainerHandle() { 206 return $this->assertAttached($this->containerHandle); 207 } 208 209 public function attachBuildableHandle($buildable_handle) { 210 $this->buildableHandle = $buildable_handle; 211 return $this; 212 } 213 214 public function getBuildableHandle() { 215 return $this->assertAttached($this->buildableHandle); 216 } 217 218 public function attachBuilds(array $builds) { 219 assert_instances_of($builds, 'HarbormasterBuild'); 220 $this->builds = $builds; 221 return $this; 222 } 223 224 public function getBuilds() { 225 return $this->assertAttached($this->builds); 226 } 227 228 229 /* -( PhabricatorPolicyInterface )----------------------------------------- */ 230 231 232 public function getCapabilities() { 233 return array( 234 PhabricatorPolicyCapability::CAN_VIEW, 235 PhabricatorPolicyCapability::CAN_EDIT, 236 ); 237 } 238 239 public function getPolicy($capability) { 240 return $this->getBuildableObject()->getPolicy($capability); 241 } 242 243 public function hasAutomaticCapability($capability, PhabricatorUser $viewer) { 244 return $this->getBuildableObject()->hasAutomaticCapability( 245 $capability, 246 $viewer); 247 } 248 249 public function describeAutomaticCapability($capability) { 250 return pht('A buildable inherits policies from the underlying object.'); 251 } 252 253 254 255 /* -( HarbormasterBuildableInterface )------------------------------------- */ 256 257 258 public function getHarbormasterBuildablePHID() { 259 // NOTE: This is essentially just for convenience, as it allows you create 260 // a copy of a buildable by specifying `B123` without bothering to go 261 // look up the underlying object. 262 return $this->getBuildablePHID(); 263 } 264 265 public function getHarbormasterContainerPHID() { 266 return $this->getContainerPHID(); 267 } 268 269 public function getBuildVariables() { 270 return array(); 271 } 272 273 public function getAvailableBuildVariables() { 274 return array(); 275 } 276 277 278 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |