From f909a295f76da7585c1443402a1eeab324393a5b Mon Sep 17 00:00:00 2001 From: Bob Trahan Date: Thu, 15 Aug 2013 13:10:45 -0700 Subject: [PATCH] Integrate Pholio with Herald Summary: Ref T2766. Does the integration via ApplicationTransactionsEditor. Only did addCC and Flag for proof of concept. Test Plan: Made a rule to cc, made a rule to flag. They worked! (will attach screens to diff) Reviewers: epriestley Reviewed By: epriestley CC: Korvin, aran Maniphest Tasks: T2766 Differential Revision: https://secure.phabricator.com/D6766 --- src/__phutil_library_map__.php | 2 + .../herald/adapter/HeraldAdapter.php | 3 + .../herald/adapter/HeraldCommitAdapter.php | 3 + .../HeraldDifferentialRevisionAdapter.php | 3 + .../adapter/HeraldPholioMockAdapter.php | 115 ++++++++++++++++++ .../pholio/editor/PholioMockEditor.php | 27 ++++ ...habricatorApplicationTransactionEditor.php | 58 +++++++++ 7 files changed, 211 insertions(+) create mode 100644 src/applications/herald/adapter/HeraldPholioMockAdapter.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 49970784ad..7c6243cd9e 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -613,6 +613,7 @@ phutil_register_library_map(array( 'HeraldNewController' => 'applications/herald/controller/HeraldNewController.php', 'HeraldObjectTranscript' => 'applications/herald/storage/transcript/HeraldObjectTranscript.php', 'HeraldPHIDTypeRule' => 'applications/herald/phid/HeraldPHIDTypeRule.php', + 'HeraldPholioMockAdapter' => 'applications/herald/adapter/HeraldPholioMockAdapter.php', 'HeraldRecursiveConditionsException' => 'applications/herald/engine/exception/HeraldRecursiveConditionsException.php', 'HeraldRepetitionPolicyConfig' => 'applications/herald/config/HeraldRepetitionPolicyConfig.php', 'HeraldRule' => 'applications/herald/storage/HeraldRule.php', @@ -2627,6 +2628,7 @@ phutil_register_library_map(array( 'HeraldInvalidFieldException' => 'Exception', 'HeraldNewController' => 'HeraldController', 'HeraldPHIDTypeRule' => 'PhabricatorPHIDType', + 'HeraldPholioMockAdapter' => 'HeraldAdapter', 'HeraldRecursiveConditionsException' => 'Exception', 'HeraldRule' => array( diff --git a/src/applications/herald/adapter/HeraldAdapter.php b/src/applications/herald/adapter/HeraldAdapter.php index e8450e21d1..eb8f34a565 100644 --- a/src/applications/herald/adapter/HeraldAdapter.php +++ b/src/applications/herald/adapter/HeraldAdapter.php @@ -1,5 +1,8 @@ mock = $mock; + return $this; + } + public function getMock() { + return $this->mock; + } + + private function setCcPHIDs(array $cc_phids) { + $this->ccPHIDs = $cc_phids; + return $this; + } + public function getCcPHIDs() { + return $this->ccPHIDs; + } + + public function getAdapterContentName() { + return pht('Pholio Mocks'); + } + + public function getFields() { + return array( + self::FIELD_TITLE, + self::FIELD_BODY, + self::FIELD_AUTHOR, + self::FIELD_CC, + ); + } + + public function getActions($rule_type) { + switch ($rule_type) { + case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL: + return array( + self::ACTION_ADD_CC, + self::ACTION_NOTHING, + ); + case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL: + return array( + self::ACTION_ADD_CC, + self::ACTION_FLAG, + self::ACTION_NOTHING, + ); + } + } + + public function getPHID() { + return $this->getMock()->getPHID(); + } + + public function getHeraldName() { + return 'M'.$this->getMock()->getID(); + } + + public function getHeraldField($field) { + switch ($field) { + case self::FIELD_TITLE: + return $this->getMock()->getName(); + case self::FIELD_BODY: + return $this->getMock()->getDescription(); + case self::FIELD_AUTHOR: + return $this->getMock()->getAuthorPHID(); + case self::FIELD_CC: + return PhabricatorSubscribersQuery::loadSubscribersForPHID( + $this->getMock()->getPHID()); + } + + return parent::getHeraldField($field); + } + + public function applyHeraldEffects(array $effects) { + assert_instances_of($effects, 'HeraldEffect'); + + $result = array(); + foreach ($effects as $effect) { + $action = $effect->getAction(); + switch ($action) { + case self::ACTION_NOTHING: + $result[] = new HeraldApplyTranscript( + $effect, + true, + pht('Great success at doing nothing.')); + break; + case self::ACTION_ADD_CC: + $add_cc = array(); + foreach ($effect->getTarget() as $phid) { + $add_cc[$phid] = true; + } + $this->setCcPHIDs(array_keys($add_cc)); + $result[] = new HeraldApplyTranscript( + $effect, + true, + pht('Added address to cc list.')); + break; + case self::ACTION_FLAG: + $result[] = parent::applyFlagEffect( + $effect, + $this->getMock()->getPHID()); + break; + default: + throw new Exception("No rules to handle action '{$action}'."); + } + } + return $result; + } +} diff --git a/src/applications/pholio/editor/PholioMockEditor.php b/src/applications/pholio/editor/PholioMockEditor.php index a7e66ed8e3..c82edd3d72 100644 --- a/src/applications/pholio/editor/PholioMockEditor.php +++ b/src/applications/pholio/editor/PholioMockEditor.php @@ -383,6 +383,33 @@ final class PholioMockEditor extends PhabricatorApplicationTransactionEditor { return true; } + protected function supportsHerald() { + return true; + } + + protected function buildHeraldAdapter( + PhabricatorLiskDAO $object, + array $xactions) { + + return id(new HeraldPholioMockAdapter()) + ->setMock($object); + } + + protected function didApplyHeraldRules( + PhabricatorLiskDAO $object, + HeraldAdapter $adapter, + HeraldTranscript $transcript) { + + $cc_phids = $adapter->getCcPHIDs(); + if ($cc_phids) { + id(new PhabricatorSubscriptionsEditor()) + ->setObject($object) + ->setActor($this->requireActor()) + ->subscribeImplicit($cc_phids) + ->save(); + } + } + protected function sortTransactions(array $xactions) { $head = array(); $tail = array(); diff --git a/src/applications/transactions/editor/PhabricatorApplicationTransactionEditor.php b/src/applications/transactions/editor/PhabricatorApplicationTransactionEditor.php index 1eb67c76d3..887a7910db 100644 --- a/src/applications/transactions/editor/PhabricatorApplicationTransactionEditor.php +++ b/src/applications/transactions/editor/PhabricatorApplicationTransactionEditor.php @@ -16,6 +16,8 @@ abstract class PhabricatorApplicationTransactionEditor private $mentionedPHIDs; private $continueOnNoEffect; private $parentMessageID; + private $heraldAdapter; + private $heraldTranscript; private $subscribers; private $isPreview; @@ -439,6 +441,10 @@ abstract class PhabricatorApplicationTransactionEditor $this->applyExternalEffects($object, $xaction); } + if ($this->supportsHerald()) { + $this->applyHeraldRules($object, $xactions); + } + $this->applyFinalEffects($object, $xactions); if ($read_locking) { @@ -1324,6 +1330,58 @@ abstract class PhabricatorApplicationTransactionEditor } +/* -( Herald Integration )-------------------------------------------------- */ + + + protected function supportsHerald() { + return false; + } + + protected function buildHeraldAdapter( + PhabricatorLiskDAO $object, + array $xactions) { + throw new Exception('No herald adapter specified.'); + } + + private function setHeraldAdapter(HeraldAdapter $adapter) { + $this->heraldAdapter = $adapter; + return $this; + } + + protected function getHeraldAdapter() { + return $this->heraldAdapter; + } + + private function setHeraldTranscript(HeraldTranscript $transcript) { + $this->heraldTranscript = $transcript; + return $this; + } + + protected function getHeraldTranscript() { + return $this->heraldTranscript; + } + + private function applyHeraldRules( + PhabricatorLiskDAO $object, + array $xactions) { + + $adapter = $this->buildHeraldAdapter($object, $xactions); + $xscript = HeraldEngine::loadAndApplyRules($adapter); + + $this->setHeraldAdapter($adapter); + $this->setHeraldTranscript($xscript); + + $this->didApplyHeraldRules($object, $adapter, $xscript); + } + + protected function didApplyHeraldRules( + PhabricatorLiskDAO $object, + HeraldAdapter $adapter, + HeraldTranscript $transcript) { + + } + + /* -( Custom Fields )------------------------------------------------------ */