Path: blob/master/src/applications/differential/editor/DifferentialDiffEditor.php
12256 views
<?php12final class DifferentialDiffEditor3extends PhabricatorApplicationTransactionEditor {45private $diffDataDict;6private $lookupRepository = true;78public function setLookupRepository($bool) {9$this->lookupRepository = $bool;10return $this;11}1213public function getEditorApplicationClass() {14return 'PhabricatorDifferentialApplication';15}1617public function getEditorObjectsDescription() {18return pht('Differential Diffs');19}2021public function getTransactionTypes() {22$types = parent::getTransactionTypes();2324$types[] = PhabricatorTransactions::TYPE_VIEW_POLICY;25$types[] = DifferentialDiffTransaction::TYPE_DIFF_CREATE;2627return $types;28}2930protected function getCustomTransactionOldValue(31PhabricatorLiskDAO $object,32PhabricatorApplicationTransaction $xaction) {3334switch ($xaction->getTransactionType()) {35case DifferentialDiffTransaction::TYPE_DIFF_CREATE:36return null;37}3839return parent::getCustomTransactionOldValue($object, $xaction);40}4142protected function getCustomTransactionNewValue(43PhabricatorLiskDAO $object,44PhabricatorApplicationTransaction $xaction) {4546switch ($xaction->getTransactionType()) {47case DifferentialDiffTransaction::TYPE_DIFF_CREATE:48$this->diffDataDict = $xaction->getNewValue();49return true;50}5152return parent::getCustomTransactionNewValue($object, $xaction);53}5455protected function applyCustomInternalTransaction(56PhabricatorLiskDAO $object,57PhabricatorApplicationTransaction $xaction) {5859switch ($xaction->getTransactionType()) {60case DifferentialDiffTransaction::TYPE_DIFF_CREATE:61$dict = $this->diffDataDict;62$this->updateDiffFromDict($object, $dict);63return;64}6566return parent::applyCustomInternalTransaction($object, $xaction);67}6869protected function applyCustomExternalTransaction(70PhabricatorLiskDAO $object,71PhabricatorApplicationTransaction $xaction) {7273switch ($xaction->getTransactionType()) {74case DifferentialDiffTransaction::TYPE_DIFF_CREATE:75return;76}7778return parent::applyCustomExternalTransaction($object, $xaction);79}8081protected function applyFinalEffects(82PhabricatorLiskDAO $object,83array $xactions) {8485// If we didn't get an explicit `repositoryPHID` (which means the client86// is old, or couldn't figure out which repository the working copy87// belongs to), apply heuristics to try to figure it out.8889if ($this->lookupRepository && !$object->getRepositoryPHID()) {90$repository = id(new DifferentialRepositoryLookup())91->setDiff($object)92->setViewer($this->getActor())93->lookupRepository();94if ($repository) {95$object->setRepositoryPHID($repository->getPHID());96$object->setRepositoryUUID($repository->getUUID());97$object->save();98}99}100101return $xactions;102}103104/**105* We run Herald as part of transaction validation because Herald can106* block diff creation for Differential diffs. Its important to do this107* separately so no Herald logs are saved; these logs could expose108* information the Herald rules are intended to block.109*/110protected function validateTransaction(111PhabricatorLiskDAO $object,112$type,113array $xactions) {114115$errors = parent::validateTransaction($object, $type, $xactions);116117foreach ($xactions as $xaction) {118switch ($type) {119case DifferentialDiffTransaction::TYPE_DIFF_CREATE:120$diff = clone $object;121$diff = $this->updateDiffFromDict($diff, $xaction->getNewValue());122123$adapter = $this->buildHeraldAdapter($diff, $xactions);124$adapter->setContentSource($this->getContentSource());125$adapter->setIsNewObject($this->getIsNewObject());126127$engine = new HeraldEngine();128129$rules = $engine->loadRulesForAdapter($adapter);130$rules = mpull($rules, null, 'getID');131132$effects = $engine->applyRules($rules, $adapter);133$action_block = DifferentialBlockHeraldAction::ACTIONCONST;134135$blocking_effect = null;136foreach ($effects as $effect) {137if ($effect->getAction() == $action_block) {138$blocking_effect = $effect;139break;140}141}142143if ($blocking_effect) {144$rule = $blocking_effect->getRule();145146$message = $effect->getTarget();147if (!strlen($message)) {148$message = pht('(None.)');149}150151$errors[] = new PhabricatorApplicationTransactionValidationError(152$type,153pht('Rejected by Herald'),154pht(155"Creation of this diff was rejected by Herald rule %s.\n".156" Rule: %s\n".157"Reason: %s",158$rule->getMonogram(),159$rule->getName(),160$message));161}162break;163}164}165166return $errors;167}168169170protected function shouldPublishFeedStory(171PhabricatorLiskDAO $object,172array $xactions) {173return false;174}175176protected function shouldSendMail(177PhabricatorLiskDAO $object,178array $xactions) {179return false;180}181182protected function supportsSearch() {183return false;184}185186/* -( Herald Integration )------------------------------------------------- */187188/**189* See @{method:validateTransaction}. The only Herald action is to block190* the creation of Diffs. We thus have to be careful not to save any191* data and do this validation very early.192*/193protected function shouldApplyHeraldRules(194PhabricatorLiskDAO $object,195array $xactions) {196197return false;198}199200protected function buildHeraldAdapter(201PhabricatorLiskDAO $object,202array $xactions) {203204$adapter = id(new HeraldDifferentialDiffAdapter())205->setDiff($object);206207return $adapter;208}209210private function updateDiffFromDict(DifferentialDiff $diff, $dict) {211$diff212->setSourcePath(idx($dict, 'sourcePath'))213->setSourceMachine(idx($dict, 'sourceMachine'))214->setBranch(idx($dict, 'branch'))215->setCreationMethod(idx($dict, 'creationMethod'))216->setAuthorPHID(idx($dict, 'authorPHID', $this->getActor()))217->setBookmark(idx($dict, 'bookmark'))218->setRepositoryPHID(idx($dict, 'repositoryPHID'))219->setRepositoryUUID(idx($dict, 'repositoryUUID'))220->setSourceControlSystem(idx($dict, 'sourceControlSystem'))221->setSourceControlPath(idx($dict, 'sourceControlPath'))222->setSourceControlBaseRevision(idx($dict, 'sourceControlBaseRevision'))223->setLintStatus(idx($dict, 'lintStatus'))224->setUnitStatus(idx($dict, 'unitStatus'));225226return $diff;227}228}229230231