Skip to content

Commit 5ab15b5

Browse files
committed
Herald - tighten up rule display
Summary: we were bad at displaying phid-based values nicely. Now we are good at it. Test Plan: made a herald rule where if the author was a or b, the task should be assigned to c and have projects x, y, z added to it. this displayed nicely. Reviewers: epriestley Reviewed By: epriestley CC: Korvin, aran Differential Revision: https://secure.phabricator.com/D7158
1 parent ed75039 commit 5ab15b5

File tree

2 files changed

+86
-21
lines changed

2 files changed

+86
-21
lines changed

src/applications/herald/adapter/HeraldAdapter.php

Lines changed: 84 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,9 @@ public static function getEnabledAdapterMap() {
710710
}
711711

712712

713-
public function renderRuleAsText(HeraldRule $rule) {
713+
public function renderRuleAsText(HeraldRule $rule, array $handles) {
714+
assert_instances_of($handles, 'PhabricatorObjectHandle');
715+
714716
$out = array();
715717

716718
if ($rule->getMustMatchAll()) {
@@ -721,7 +723,7 @@ public function renderRuleAsText(HeraldRule $rule) {
721723

722724
$out[] = null;
723725
foreach ($rule->getConditions() as $condition) {
724-
$out[] = " ".$this->renderConditionAsText($condition);
726+
$out[] = $this->renderConditionAsText($condition, $handles);
725727
}
726728
$out[] = null;
727729

@@ -733,53 +735,116 @@ public function renderRuleAsText(HeraldRule $rule) {
733735

734736
$out[] = null;
735737
foreach ($rule->getActions() as $action) {
736-
$out[] = " ".$this->renderActionAsText($action);
738+
$out[] = $this->renderActionAsText($action, $handles);
737739
}
738740

739-
return implode("\n", $out);
741+
return phutil_implode_html("\n", $out);
740742
}
741743

742-
private function renderConditionAsText(HeraldCondition $condition) {
744+
private function renderConditionAsText(
745+
HeraldCondition $condition,
746+
array $handles) {
743747
$field_type = $condition->getFieldName();
744748
$field_name = idx($this->getFieldNameMap(), $field_type);
745749

746750
$condition_type = $condition->getFieldCondition();
747751
$condition_name = idx($this->getConditionNameMap(), $condition_type);
748752

749-
$value = $this->renderConditionValueAsText($condition);
753+
$value = $this->renderConditionValueAsText($condition, $handles);
750754

751-
return "{$field_name} {$condition_name} {$value}";
755+
return hsprintf(' %s %s %s', $field_name, $condition_name, $value);
752756
}
753757

754-
private function renderActionAsText(HeraldAction $action) {
758+
private function renderActionAsText(
759+
HeraldAction $action,
760+
array $handles) {
755761
$rule_global = HeraldRuleTypeConfig::RULE_TYPE_GLOBAL;
756762

757763
$action_type = $action->getAction();
758764
$action_name = idx($this->getActionNameMap($rule_global), $action_type);
759765

760-
$target = $this->renderActionTargetAsText($action);
766+
$target = $this->renderActionTargetAsText($action, $handles);
761767

762-
return "{$action_name} {$target}";
768+
return hsprintf(' %s %s', $action_name, $target);
763769
}
764770

765-
private function renderConditionValueAsText(HeraldCondition $condition) {
766-
// TODO: This produces sketchy results for many conditions.
771+
private function renderConditionValueAsText(
772+
HeraldCondition $condition,
773+
array $handles) {
774+
767775
$value = $condition->getValue();
768-
if (is_array($value)) {
769-
$value = implode(', ', $value);
776+
if (!is_array($value)) {
777+
$value = array($value);
770778
}
779+
foreach ($value as $index => $val) {
780+
$handle = idx($handles, $val);
781+
if ($handle) {
782+
$value[$index] = $handle->renderLink();
783+
}
784+
}
785+
$value = phutil_implode_html(', ', $value);
771786
return $value;
772787
}
773788

774-
private function renderActionTargetAsText(HeraldAction $action) {
775-
// TODO: This produces sketchy results for Flags and PHIDs.
789+
private function renderActionTargetAsText(
790+
HeraldAction $action,
791+
array $handles) {
792+
776793
$target = $action->getTarget();
777-
if (is_array($target)) {
778-
$target = implode(', ', $target);
794+
if (!is_array($target)) {
795+
$target = array($target);
779796
}
780-
797+
foreach ($target as $index => $val) {
798+
$handle = idx($handles, $val);
799+
if ($handle) {
800+
$target[$index] = $handle->renderLink();
801+
}
802+
}
803+
$target = phutil_implode_html(', ', $target);
781804
return $target;
782805
}
783806

807+
/**
808+
* Given a @{class:HeraldRule}, this function extracts all the phids that
809+
* we'll want to load as handles later.
810+
*
811+
* This function performs a somewhat hacky approach to figuring out what
812+
* is and is not a phid - try to get the phid type and if the type is
813+
* *not* unknown assume its a valid phid.
814+
*
815+
* Don't try this at home. Use more strongly typed data at home.
816+
*
817+
* Think of the children.
818+
*/
819+
public static function getHandlePHIDs(HeraldRule $rule) {
820+
$phids = array($rule->getAuthorPHID());
821+
foreach ($rule->getConditions() as $condition) {
822+
$value = $condition->getValue();
823+
if (!is_array($value)) {
824+
$value = array($value);
825+
}
826+
foreach ($value as $val) {
827+
if (phid_get_type($val) !=
828+
PhabricatorPHIDConstants::PHID_TYPE_UNKNOWN) {
829+
$phids[] = $val;
830+
}
831+
}
832+
}
833+
834+
foreach ($rule->getActions() as $action) {
835+
$target = $action->getTarget();
836+
if (!is_array($target)) {
837+
$target = array($target);
838+
}
839+
foreach ($target as $val) {
840+
if (phid_get_type($val) !=
841+
PhabricatorPHIDConstants::PHID_TYPE_UNKNOWN) {
842+
$phids[] = $val;
843+
}
844+
}
845+
}
846+
return $phids;
847+
}
848+
784849
}
785850

src/applications/herald/controller/HeraldRuleViewController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private function buildActionView(HeraldRule $rule) {
7373
private function buildPropertyView(HeraldRule $rule) {
7474
$viewer = $this->getRequest()->getUser();
7575

76-
$this->loadHandles(array($rule->getAuthorPHID()));
76+
$this->loadHandles(HeraldAdapter::getHandlePHIDs($rule));
7777

7878
$view = id(new PhabricatorPropertyListView())
7979
->setUser($viewer)
@@ -104,7 +104,7 @@ private function buildPropertyView(HeraldRule $rule) {
104104
array(
105105
'style' => 'white-space: pre-wrap;',
106106
),
107-
$adapter->renderRuleAsText($rule)));
107+
$adapter->renderRuleAsText($rule, $this->getLoadedHandles())));
108108
}
109109

110110
return $view;

0 commit comments

Comments
 (0)