1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-24 21:48:21 +01:00

Allow Herald Adapters to choose applicable rule types (global, personal, etc).

Summary: Ref T4264. Lays the groundwork for new "Object" rule types. Prevents personal "Hook" rules, which don't make any sense.

Test Plan: Created new Maniphest (global/personal available) and Ref Hook (global only) rules.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T4264

Differential Revision: https://secure.phabricator.com/D7852
This commit is contained in:
epriestley 2013-12-30 16:48:07 -08:00
parent db3228844a
commit 472b0f983e
9 changed files with 106 additions and 12 deletions

View file

@ -41,6 +41,17 @@ final class HeraldPreCommitContentAdapter extends HeraldAdapter {
"Hook rules can block changes."); "Hook rules can block changes.");
} }
public function supportsRuleType($rule_type) {
switch ($rule_type) {
case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL:
return true;
case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL:
case HeraldRuleTypeConfig::RULE_TYPE_OBJECT:
default:
return false;
}
}
public function getFieldNameMap() { public function getFieldNameMap() {
return array( return array(
) + parent::getFieldNameMap(); ) + parent::getFieldNameMap();

View file

@ -44,6 +44,17 @@ final class HeraldPreCommitRefAdapter extends HeraldAdapter {
"Hook rules can block changes."); "Hook rules can block changes.");
} }
public function supportsRuleType($rule_type) {
switch ($rule_type) {
case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL:
return true;
case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL:
case HeraldRuleTypeConfig::RULE_TYPE_OBJECT:
default:
return false;
}
}
public function getFieldNameMap() { public function getFieldNameMap() {
return array( return array(
self::FIELD_REF_TYPE => pht('Ref type'), self::FIELD_REF_TYPE => pht('Ref type'),

View file

@ -140,6 +140,10 @@ abstract class HeraldAdapter {
abstract public function getAdapterApplicationClass(); abstract public function getAdapterApplicationClass();
abstract public function getObject(); abstract public function getObject();
public function supportsRuleType($rule_type) {
return false;
}
public function getAdapterSortKey() { public function getAdapterSortKey() {
return sprintf( return sprintf(
'%08d%s', '%08d%s',

View file

@ -49,6 +49,17 @@ final class HeraldCommitAdapter extends HeraldAdapter {
"and run build plans."); "and run build plans.");
} }
public function supportsRuleType($rule_type) {
switch ($rule_type) {
case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL:
case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL:
return true;
case HeraldRuleTypeConfig::RULE_TYPE_OBJECT:
default:
return false;
}
}
public function getFieldNameMap() { public function getFieldNameMap() {
return array( return array(
self::FIELD_NEED_AUDIT_FOR_PACKAGE => self::FIELD_NEED_AUDIT_FOR_PACKAGE =>

View file

@ -43,6 +43,17 @@ final class HeraldDifferentialRevisionAdapter extends HeraldAdapter {
"and run build plans."); "and run build plans.");
} }
public function supportsRuleType($rule_type) {
switch ($rule_type) {
case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL:
case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL:
return true;
case HeraldRuleTypeConfig::RULE_TYPE_OBJECT:
default:
return false;
}
}
public function getFields() { public function getFields() {
return array_merge( return array_merge(
array( array(

View file

@ -19,6 +19,17 @@ final class HeraldManiphestTaskAdapter extends HeraldAdapter {
'React to tasks being created or updated.'); 'React to tasks being created or updated.');
} }
public function supportsRuleType($rule_type) {
switch ($rule_type) {
case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL:
case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL:
return true;
case HeraldRuleTypeConfig::RULE_TYPE_OBJECT:
default:
return false;
}
}
public function setTask(ManiphestTask $task) { public function setTask(ManiphestTask $task) {
$this->task = $task; $this->task = $task;
return $this; return $this;

View file

@ -41,6 +41,17 @@ final class HeraldPholioMockAdapter extends HeraldAdapter {
return pht('Pholio Mocks'); return pht('Pholio Mocks');
} }
public function supportsRuleType($rule_type) {
switch ($rule_type) {
case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL:
case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL:
return true;
case HeraldRuleTypeConfig::RULE_TYPE_OBJECT:
default:
return false;
}
}
public function getFields() { public function getFields() {
return array_merge( return array_merge(
array( array(

View file

@ -53,17 +53,13 @@ final class HeraldNewController extends HeraldController {
->setUser($user) ->setUser($user)
->setAction($this->getApplicationURI('new/')); ->setAction($this->getApplicationURI('new/'));
switch ($step) {
case 0:
default:
$content_types = $this->renderContentTypeControl( $content_types = $this->renderContentTypeControl(
$content_type_map, $content_type_map,
$e_type); $e_type);
$rule_types = $this->renderRuleTypeControl(
$rule_type_map,
$e_rule);
switch ($step) {
case 0:
default:
$form $form
->addHiddenInput('step', 1) ->addHiddenInput('step', 1)
->appendChild($content_types); ->appendChild($content_types);
@ -72,6 +68,10 @@ final class HeraldNewController extends HeraldController {
$cancel_uri = $this->getApplicationURI(); $cancel_uri = $this->getApplicationURI();
break; break;
case 1: case 1:
$rule_types = $this->renderRuleTypeControl(
$rule_type_map,
$e_rule);
$form $form
->addHiddenInput('content_type', $request->getStr('content_type')) ->addHiddenInput('content_type', $request->getStr('content_type'))
->addHiddenInput('step', 2) ->addHiddenInput('step', 2)
@ -185,14 +185,31 @@ final class HeraldNewController extends HeraldController {
->setValue($request->getStr('rule_type')) ->setValue($request->getStr('rule_type'))
->setError($e_rule); ->setError($e_rule);
$adapter = HeraldAdapter::getAdapterForContentType(
$request->getStr('content_type'));
foreach ($rule_type_map as $value => $name) { foreach ($rule_type_map as $value => $name) {
$caption = idx($captions, $value);
$disabled = ($value == HeraldRuleTypeConfig::RULE_TYPE_GLOBAL) && $disabled = ($value == HeraldRuleTypeConfig::RULE_TYPE_GLOBAL) &&
(!$can_global); (!$can_global);
if (!$adapter->supportsRuleType($value)) {
$disabled = true;
$caption = array(
$caption,
"\n\n",
phutil_tag(
'em',
array(),
pht(
'This rule type is not supported by the selected content type.')),
);
}
$radio->addButton( $radio->addButton(
$value, $value,
$name, $name,
idx($captions, $value), phutil_escape_html_newlines($caption),
$disabled ? 'disabled' : null, $disabled ? 'disabled' : null,
$disabled); $disabled);
} }

View file

@ -59,9 +59,16 @@ final class HeraldRuleController extends HeraldController {
$local_version = id(new HeraldRule())->getConfigVersion(); $local_version = id(new HeraldRule())->getConfigVersion();
if ($rule->getConfigVersion() > $local_version) { if ($rule->getConfigVersion() > $local_version) {
throw new Exception( throw new Exception(
pht(
"This rule was created with a newer version of Herald. You can not ". "This rule was created with a newer version of Herald. You can not ".
"view or edit it in this older version. Upgrade your Phabricator ". "view or edit it in this older version. Upgrade your Phabricator ".
"deployment."); "deployment."));
}
if (!$adapter->supportsRuleType($rule->getRuleType())) {
throw new Exception(
pht(
"This rule's content type does not support the selected rule type."));
} }
// Upgrade rule version to our version, since we might add newly-defined // Upgrade rule version to our version, since we might add newly-defined