@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Move most Herald field configuration into dynamic Adapters

Summary: Ref T2769. Herald has a giant hard-coded list of fields. Primarily make these dynamic and adapter-based.

Test Plan: Viewed and edited Herald rules.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T2769

Differential Revision: https://secure.phabricator.com/D6657

+127 -65
+50
src/applications/herald/adapter/HeraldAdapter.php
··· 2 2 3 3 abstract class HeraldAdapter { 4 4 5 + const FIELD_TITLE = 'title'; 6 + const FIELD_BODY = 'body'; 7 + const FIELD_AUTHOR = 'author'; 8 + const FIELD_REVIEWER = 'reviewer'; 9 + const FIELD_REVIEWERS = 'reviewers'; 10 + const FIELD_CC = 'cc'; 11 + const FIELD_TAGS = 'tags'; 12 + const FIELD_DIFF_FILE = 'diff-file'; 13 + const FIELD_DIFF_CONTENT = 'diff-content'; 14 + const FIELD_REPOSITORY = 'repository'; 15 + const FIELD_RULE = 'rule'; 16 + const FIELD_AFFECTED_PACKAGE = 'affected-package'; 17 + const FIELD_AFFECTED_PACKAGE_OWNER = 'affected-package-owner'; 18 + 5 19 abstract public function getPHID(); 6 20 abstract public function getHeraldName(); 7 21 abstract public function getHeraldTypeName(); ··· 21 35 } 22 36 23 37 abstract public function getAdapterContentName(); 38 + abstract public function getFields(); 39 + 40 + public function getFieldNameMap() { 41 + return array( 42 + self::FIELD_TITLE => pht('Title'), 43 + self::FIELD_BODY => pht('Body'), 44 + self::FIELD_AUTHOR => pht('Author'), 45 + self::FIELD_REVIEWER => pht('Reviewer'), 46 + self::FIELD_REVIEWERS => pht('Reviewers'), 47 + self::FIELD_CC => pht('CCs'), 48 + self::FIELD_TAGS => pht('Tags'), 49 + self::FIELD_DIFF_FILE => pht('Any changed filename'), 50 + self::FIELD_DIFF_CONTENT => pht('Any changed file content'), 51 + self::FIELD_REPOSITORY => pht('Repository'), 52 + self::FIELD_RULE => pht('Another Herald rule'), 53 + self::FIELD_AFFECTED_PACKAGE => pht('Any affected package'), 54 + self::FIELD_AFFECTED_PACKAGE_OWNER => 55 + pht("Any affected package's owner"), 56 + ); 57 + } 24 58 25 59 public static function applyFlagEffect(HeraldEffect $effect, $phid) { 26 60 $color = $effect->getTarget(); ··· 81 115 } 82 116 return $adapters; 83 117 } 118 + 119 + public static function getAdapterForContentType($content_type) { 120 + $adapters = self::getAllAdapters(); 121 + 122 + foreach ($adapters as $adapter) { 123 + if ($adapter->getAdapterContentType() == $content_type) { 124 + return $adapter; 125 + } 126 + } 127 + 128 + throw new Exception( 129 + pht( 130 + 'No adapter exists for Herald content type "%s".', 131 + $content_type)); 132 + } 133 + 84 134 85 135 86 136 }
+33
src/applications/herald/adapter/HeraldCommitAdapter.php
··· 2 2 3 3 final class HeraldCommitAdapter extends HeraldAdapter { 4 4 5 + const FIELD_NEED_AUDIT_FOR_PACKAGE = 'need-audit-for-package'; 6 + const FIELD_DIFFERENTIAL_REVISION = 'differential-revision'; 7 + const FIELD_DIFFERENTIAL_REVIEWERS = 'differential-reviewers'; 8 + const FIELD_DIFFERENTIAL_CCS = 'differential-ccs'; 9 + 5 10 protected $diff; 6 11 protected $revision; 7 12 ··· 28 33 29 34 public function getAdapterContentName() { 30 35 return pht('Commits'); 36 + } 37 + 38 + public function getFieldNameMap() { 39 + return array( 40 + self::FIELD_NEED_AUDIT_FOR_PACKAGE => 41 + pht('Affected packages that need audit'), 42 + self::FIELD_DIFFERENTIAL_REVISION => pht('Differential revision'), 43 + self::FIELD_DIFFERENTIAL_REVIEWERS => pht('Differential reviewers'), 44 + self::FIELD_DIFFERENTIAL_CCS => pht('Differential CCs'), 45 + ) + parent::getFieldNameMap(); 46 + } 47 + 48 + public function getFields() { 49 + return array( 50 + self::FIELD_BODY, 51 + self::FIELD_AUTHOR, 52 + self::FIELD_REVIEWER, 53 + self::FIELD_REPOSITORY, 54 + self::FIELD_DIFF_FILE, 55 + self::FIELD_DIFF_CONTENT, 56 + self::FIELD_RULE, 57 + self::FIELD_AFFECTED_PACKAGE, 58 + self::FIELD_AFFECTED_PACKAGE_OWNER, 59 + self::FIELD_NEED_AUDIT_FOR_PACKAGE, 60 + self::FIELD_DIFFERENTIAL_REVISION, 61 + self::FIELD_DIFFERENTIAL_REVIEWERS, 62 + self::FIELD_DIFFERENTIAL_CCS, 63 + ); 31 64 } 32 65 33 66 public static function newLegacyAdapter(
+17
src/applications/herald/adapter/HeraldDifferentialRevisionAdapter.php
··· 30 30 return pht('Differential Revisions'); 31 31 } 32 32 33 + public function getFields() { 34 + return array( 35 + self::FIELD_TITLE, 36 + self::FIELD_BODY, 37 + self::FIELD_AUTHOR, 38 + self::FIELD_REVIEWERS, 39 + self::FIELD_CC, 40 + self::FIELD_REPOSITORY, 41 + self::FIELD_DIFF_FILE, 42 + self::FIELD_DIFF_CONTENT, 43 + self::FIELD_RULE, 44 + self::FIELD_AFFECTED_PACKAGE, 45 + self::FIELD_AFFECTED_PACKAGE_OWNER, 46 + ); 47 + } 48 + 49 + 33 50 public static function newLegacyAdapter( 34 51 DifferentialRevision $revision, 35 52 DifferentialDiff $diff) {
+4
src/applications/herald/adapter/HeraldDryRunAdapter.php
··· 26 26 return null; 27 27 } 28 28 29 + public function getFields() { 30 + return array(); 31 + } 32 + 29 33 public function applyHeraldEffects(array $effects) { 30 34 assert_instances_of($effects, 'HeraldEffect'); 31 35 $results = array();
-1
src/applications/herald/config/HeraldConditionConfig.php
··· 61 61 case HeraldFieldConfig::FIELD_AUTHOR: 62 62 case HeraldFieldConfig::FIELD_REPOSITORY: 63 63 case HeraldFieldConfig::FIELD_REVIEWER: 64 - case HeraldFieldConfig::FIELD_MERGE_REQUESTER: 65 64 return array_select_keys( 66 65 $map, 67 66 array(
+2 -45
src/applications/herald/config/HeraldFieldConfig.php
··· 2 2 3 3 final class HeraldFieldConfig { 4 4 5 + // TODO: Remove; still required by conditions, etc. 5 6 const FIELD_TITLE = 'title'; 6 7 const FIELD_BODY = 'body'; 7 8 const FIELD_AUTHOR = 'author'; ··· 19 20 const FIELD_DIFFERENTIAL_REVISION = 'differential-revision'; 20 21 const FIELD_DIFFERENTIAL_REVIEWERS = 'differential-reviewers'; 21 22 const FIELD_DIFFERENTIAL_CCS = 'differential-ccs'; 22 - const FIELD_MERGE_REQUESTER = 'merge-requester'; 23 23 24 + // TODO: Remove; still required by transcripts. 24 25 public static function getFieldMap() { 25 26 $map = array( 26 27 self::FIELD_TITLE => pht('Title'), ··· 42 43 self::FIELD_DIFFERENTIAL_REVISION => pht('Differential revision'), 43 44 self::FIELD_DIFFERENTIAL_REVIEWERS => pht('Differential reviewers'), 44 45 self::FIELD_DIFFERENTIAL_CCS => pht('Differential CCs'), 45 - self::FIELD_MERGE_REQUESTER => pht('Merge requester') 46 46 ); 47 47 48 48 return $map; 49 - } 50 - 51 - public static function getFieldMapForContentType($type) { 52 - $map = self::getFieldMap(); 53 - 54 - switch ($type) { 55 - case HeraldContentTypeConfig::CONTENT_TYPE_DIFFERENTIAL: 56 - return array_select_keys( 57 - $map, 58 - array( 59 - self::FIELD_TITLE, 60 - self::FIELD_BODY, 61 - self::FIELD_AUTHOR, 62 - self::FIELD_REVIEWERS, 63 - self::FIELD_CC, 64 - self::FIELD_REPOSITORY, 65 - self::FIELD_DIFF_FILE, 66 - self::FIELD_DIFF_CONTENT, 67 - self::FIELD_RULE, 68 - self::FIELD_AFFECTED_PACKAGE, 69 - self::FIELD_AFFECTED_PACKAGE_OWNER, 70 - )); 71 - case HeraldContentTypeConfig::CONTENT_TYPE_COMMIT: 72 - return array_select_keys( 73 - $map, 74 - array( 75 - self::FIELD_BODY, 76 - self::FIELD_AUTHOR, 77 - self::FIELD_REVIEWER, 78 - self::FIELD_REPOSITORY, 79 - self::FIELD_DIFF_FILE, 80 - self::FIELD_DIFF_CONTENT, 81 - self::FIELD_RULE, 82 - self::FIELD_AFFECTED_PACKAGE, 83 - self::FIELD_AFFECTED_PACKAGE_OWNER, 84 - self::FIELD_NEED_AUDIT_FOR_PACKAGE, 85 - self::FIELD_DIFFERENTIAL_REVISION, 86 - self::FIELD_DIFFERENTIAL_REVIEWERS, 87 - self::FIELD_DIFFERENTIAL_CCS, 88 - )); 89 - default: 90 - throw new Exception("Unknown content type."); 91 - } 92 49 } 93 50 94 51 }
+21 -18
src/applications/herald/controller/HeraldRuleController.php
··· 18 18 $rule_type_map = HeraldRuleTypeConfig::getRuleTypeMap(); 19 19 20 20 if ($this->id) { 21 - $rule = id(new HeraldRule())->load($this->id); 21 + $rule = id(new HeraldRuleQuery()) 22 + ->setViewer($user) 23 + ->withIDs(array($this->id)) 24 + ->requireCapabilities( 25 + array( 26 + PhabricatorPolicyCapability::CAN_VIEW, 27 + PhabricatorPolicyCapability::CAN_EDIT, 28 + )) 29 + ->executeOne(); 22 30 if (!$rule) { 23 31 return new Aphront404Response(); 24 - } 25 - if (!$this->canEditRule($rule, $user)) { 26 - throw new Exception("You don't own this rule and can't edit it."); 27 32 } 28 33 } else { 29 34 $rule = new HeraldRule(); ··· 31 36 $rule->setMustMatchAll(true); 32 37 33 38 $content_type = $request->getStr('content_type'); 34 - if (!isset($content_type_map[$content_type])) { 35 - $content_type = HeraldContentTypeConfig::CONTENT_TYPE_DIFFERENTIAL; 36 - } 37 39 $rule->setContentType($content_type); 38 40 39 41 $rule_type = $request->getStr('rule_type'); ··· 42 44 } 43 45 $rule->setRuleType($rule_type); 44 46 } 47 + 48 + $adapter = HeraldAdapter::getAdapterForContentType($rule->getContentType()); 45 49 46 50 $local_version = id(new HeraldRule())->getConfigVersion(); 47 51 if ($rule->getConfigVersion() > $local_version) { ··· 169 173 ->setValue(pht('Save Rule')) 170 174 ->addCancelButton('/herald/view/'.$rule->getContentType().'/')); 171 175 172 - $this->setupEditorBehavior($rule, $handles); 176 + $this->setupEditorBehavior($rule, $handles, $adapter); 173 177 174 178 $title = $rule->getID() 175 179 ? pht('Edit Herald Rule') ··· 194 198 )); 195 199 } 196 200 197 - private function canEditRule($rule, $user) { 198 - return 199 - ($user->getIsAdmin()) || 200 - ($rule->getRuleType() == HeraldRuleTypeConfig::RULE_TYPE_GLOBAL) || 201 - ($rule->getAuthorPHID() == $user->getPHID()); 202 - } 203 - 204 201 private function saveRule($rule, $request) { 205 202 $rule->setName($request->getStr('name')); 206 203 $rule->setMustMatchAll(($request->getStr('must_match') == 'all')); ··· 329 326 return array($e_name, $errors); 330 327 } 331 328 332 - private function setupEditorBehavior($rule, $handles) { 329 + private function setupEditorBehavior( 330 + HeraldRule $rule, 331 + array $handles, 332 + HeraldAdapter $adapter) { 333 + 333 334 $serial_conditions = array( 334 335 array('default', 'default', ''), 335 336 ); ··· 386 387 $all_rules = mpull($all_rules, 'getName', 'getID'); 387 388 asort($all_rules); 388 389 390 + $fields = $adapter->getFields(); 391 + $field_map = array_select_keys($adapter->getFieldNameMap(), $fields); 392 + 389 393 $config_info = array(); 390 - $config_info['fields'] 391 - = HeraldFieldConfig::getFieldMapForContentType($rule->getContentType()); 394 + $config_info['fields'] = $field_map; 392 395 $config_info['conditions'] = HeraldConditionConfig::getConditionMap(); 393 396 foreach ($config_info['fields'] as $field => $name) { 394 397 $config_info['conditionMap'][$field] = array_keys(
-1
src/applications/herald/engine/HeraldEngine.php
··· 446 446 break; 447 447 case HeraldFieldConfig::FIELD_AUTHOR: 448 448 case HeraldFieldConfig::FIELD_REPOSITORY: 449 - case HeraldFieldConfig::FIELD_MERGE_REQUESTER: 450 449 // TODO: Type should be PHID. 451 450 $result = $this->object->getHeraldField($field); 452 451 break;