diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -889,6 +889,7 @@ 'HeraldCommitAdapter' => 'applications/herald/adapter/HeraldCommitAdapter.php', 'HeraldCondition' => 'applications/herald/storage/HeraldCondition.php', 'HeraldConditionTranscript' => 'applications/herald/storage/transcript/HeraldConditionTranscript.php', + 'HeraldConduitAPIMethod' => 'applications/herald/conduit/HeraldConduitAPIMethod.php', 'HeraldController' => 'applications/herald/controller/HeraldController.php', 'HeraldCustomAction' => 'applications/herald/extension/HeraldCustomAction.php', 'HeraldDAO' => 'applications/herald/storage/HeraldDAO.php', @@ -909,6 +910,7 @@ 'HeraldPreCommitAdapter' => 'applications/diffusion/herald/HeraldPreCommitAdapter.php', 'HeraldPreCommitContentAdapter' => 'applications/diffusion/herald/HeraldPreCommitContentAdapter.php', 'HeraldPreCommitRefAdapter' => 'applications/diffusion/herald/HeraldPreCommitRefAdapter.php', + 'HeraldQueryRulesConduitAPIMethod' => 'applications/herald/conduit/HeraldQueryRulesConduitAPIMethod.php', 'HeraldRecursiveConditionsException' => 'applications/herald/engine/exception/HeraldRecursiveConditionsException.php', 'HeraldRemarkupRule' => 'applications/herald/remarkup/HeraldRemarkupRule.php', 'HeraldRepetitionPolicyConfig' => 'applications/herald/config/HeraldRepetitionPolicyConfig.php', @@ -4104,6 +4106,7 @@ 'HeraldApplyTranscript' => 'Phobject', 'HeraldCommitAdapter' => 'HeraldAdapter', 'HeraldCondition' => 'HeraldDAO', + 'HeraldConduitAPIMethod' => 'ConduitAPIMethod', 'HeraldController' => 'PhabricatorController', 'HeraldDAO' => 'PhabricatorLiskDAO', 'HeraldDifferentialAdapter' => 'HeraldAdapter', @@ -4120,6 +4123,7 @@ 'HeraldPreCommitAdapter' => 'HeraldAdapter', 'HeraldPreCommitContentAdapter' => 'HeraldPreCommitAdapter', 'HeraldPreCommitRefAdapter' => 'HeraldPreCommitAdapter', + 'HeraldQueryRulesConduitAPIMethod' => 'HeraldConduitAPIMethod', 'HeraldRecursiveConditionsException' => 'Exception', 'HeraldRemarkupRule' => 'PhabricatorObjectRemarkupRule', 'HeraldRule' => array( diff --git a/src/applications/herald/conduit/HeraldConduitAPIMethod.php b/src/applications/herald/conduit/HeraldConduitAPIMethod.php new file mode 100644 --- /dev/null +++ b/src/applications/herald/conduit/HeraldConduitAPIMethod.php @@ -0,0 +1,89 @@ + $plan->getID(), + 'phid' => $plan->getPHID(), + 'name' => $plan->getName(), + 'contentType' => $plan->getContentType(), + 'mustMatchAll' => $plan->getMustMatchAll(), + 'ruleType' => $plan->getRuleType(), + 'dateCreated' => $plan->getDateCreated(), + 'dateModified' => $plan->getDateModified(), + ); + + if ($need_conditions_and_actions) { + $conditions = is_array($plan->getConditions()) + ? $plan->getConditions() : array(); + $actions = is_array($plan->getActions()) + ? $plan->getActions() : array(); + $item['conditions'] = + $this->buildConditionsInfoDictionaries($conditions); + $item['actions'] = + $this->buildActionsInfoDictionaries($actions); + } + + $result[$plan->getPHID()] = $item; + } + + return $result; + } + + protected function buildConditionsInfoDictionaries(array $conditions) { + assert_instances_of($conditions, 'HeraldCondition'); + + $result = array(); + + if (!empty($conditions)) { + foreach ($conditions as $cond) { + $result[$cond->getID()] = array( + 'id' => $cond->getID(), + 'fieldName' => $cond->getFieldName(), + 'fieldCondition' => $cond->getFieldCondition(), + 'value' => $cond->getValue(), + ); + } + } + + return $result; + } + + protected function buildActionsInfoDictionaries(array $actions) { + assert_instances_of($actions, 'HeraldAction'); + + $result = array(); + + if (!empty($actions)) { + foreach ($actions as $action) { + $result[$action->getID()] = array( + 'id' => $action->getID(), + 'action' => $action->getAction(), + 'target' => $action->getTarget(), + ); + } + } + + return $result; + } +} diff --git a/src/applications/herald/conduit/HeraldQueryRulesConduitAPIMethod.php b/src/applications/herald/conduit/HeraldQueryRulesConduitAPIMethod.php new file mode 100644 --- /dev/null +++ b/src/applications/herald/conduit/HeraldQueryRulesConduitAPIMethod.php @@ -0,0 +1,96 @@ + 'optional list', + 'phids' => 'optional list', + 'authorPHIDs' => 'optional list', + 'contentTypes' => 'optional list', + 'ruleTypes' => 'optional list (' + .implode(', ', $this->getRuleTypes()).')', + 'needConditionsAndActions' => 'optional int<0|1> (default = 0)', + + ) + self::getPagerParamTypes(); + } + + public function defineReturnType() { + return 'list'; + } + + public function defineErrorTypes() { + return array(); + } + + protected function execute(ConduitAPIRequest $request) { + $viewer = $request->getUser(); + + $this->requireApplicationCapability( + HeraldManageGlobalRulesCapability::CAPABILITY, $viewer); + + $query = id(new HeraldRuleQuery()) + ->setViewer($viewer); + + $ids = $request->getValue('ids'); + if ($ids !== null) { + $query->withIDs($ids); + } + + $phids = $request->getValue('phids'); + if ($phids !== null) { + $query->withPHIDs($phids); + } + + $author_phids = $request->getValue('authorPHIDs'); + if ($author_phids !== null) { + $query->withAuthorPHIDs($author_phids); + } + + $content_types = $request->getValue('contentTypes'); + if ($content_types !== null) { + $query->withContentTypes($content_types); + } + + $rule_types = $request->getValue('ruleTypes'); + if ($rule_types !== null) { + $query->withRuleTypes($rule_types); + } + + $need_conditions_and_actions = + (bool) $request->getValue('needConditionsAndActions', false); + if (true === $need_conditions_and_actions) { + $query->needConditionsAndActions(true); + } + + $pager = $this->newPager($request); + + $results = $query->executeWithCursorPager($pager); + + $rules = $this->buildRuleInfoDictionaries( + $results, $need_conditions_and_actions); + + $result = array( + 'data' => $rules, + ); + + return $this->addPagerResults($result, $pager); + } +}