|
| 1 | +<?php |
| 2 | + |
| 3 | +abstract class HeraldPreCommitAdapter extends HeraldAdapter { |
| 4 | + |
| 5 | + private $log; |
| 6 | + private $hookEngine; |
| 7 | + |
| 8 | + public function setPushLog(PhabricatorRepositoryPushLog $log) { |
| 9 | + $this->log = $log; |
| 10 | + return $this; |
| 11 | + } |
| 12 | + |
| 13 | + public function setHookEngine(DiffusionCommitHookEngine $engine) { |
| 14 | + $this->hookEngine = $engine; |
| 15 | + return $this; |
| 16 | + } |
| 17 | + |
| 18 | + public function getHookEngine() { |
| 19 | + return $this->hookEngine; |
| 20 | + } |
| 21 | + |
| 22 | + public function getAdapterApplicationClass() { |
| 23 | + return 'PhabricatorApplicationDiffusion'; |
| 24 | + } |
| 25 | + |
| 26 | + public function getObject() { |
| 27 | + return $this->log; |
| 28 | + } |
| 29 | + |
| 30 | + public function supportsRuleType($rule_type) { |
| 31 | + switch ($rule_type) { |
| 32 | + case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL: |
| 33 | + case HeraldRuleTypeConfig::RULE_TYPE_OBJECT: |
| 34 | + return true; |
| 35 | + case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL: |
| 36 | + default: |
| 37 | + return false; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public function canTriggerOnObject($object) { |
| 42 | + if ($object instanceof PhabricatorRepository) { |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + if ($object instanceof PhabricatorProject) { |
| 47 | + return true; |
| 48 | + } |
| 49 | + |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + public function explainValidTriggerObjects() { |
| 54 | + return pht( |
| 55 | + 'This rule can trigger for **repositories** or **projects**.'); |
| 56 | + } |
| 57 | + |
| 58 | + public function getTriggerObjectPHIDs() { |
| 59 | + return array_merge( |
| 60 | + array( |
| 61 | + $this->hookEngine->getRepository()->getPHID(), |
| 62 | + $this->getPHID(), |
| 63 | + ), |
| 64 | + $this->hookEngine->getRepository()->getProjectPHIDs()); |
| 65 | + } |
| 66 | + |
| 67 | + public function getActions($rule_type) { |
| 68 | + switch ($rule_type) { |
| 69 | + case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL: |
| 70 | + case HeraldRuleTypeConfig::RULE_TYPE_OBJECT: |
| 71 | + return array( |
| 72 | + self::ACTION_BLOCK, |
| 73 | + self::ACTION_NOTHING |
| 74 | + ); |
| 75 | + case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL: |
| 76 | + return array( |
| 77 | + self::ACTION_NOTHING, |
| 78 | + ); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public function getPHID() { |
| 83 | + return $this->getObject()->getPHID(); |
| 84 | + } |
| 85 | + |
| 86 | + public function applyHeraldEffects(array $effects) { |
| 87 | + assert_instances_of($effects, 'HeraldEffect'); |
| 88 | + |
| 89 | + $result = array(); |
| 90 | + foreach ($effects as $effect) { |
| 91 | + $action = $effect->getAction(); |
| 92 | + switch ($action) { |
| 93 | + case self::ACTION_NOTHING: |
| 94 | + $result[] = new HeraldApplyTranscript( |
| 95 | + $effect, |
| 96 | + true, |
| 97 | + pht('Did nothing.')); |
| 98 | + break; |
| 99 | + case self::ACTION_BLOCK: |
| 100 | + $result[] = new HeraldApplyTranscript( |
| 101 | + $effect, |
| 102 | + true, |
| 103 | + pht('Blocked push.')); |
| 104 | + break; |
| 105 | + default: |
| 106 | + throw new Exception(pht('No rules to handle action "%s"!', $action)); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return $result; |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments