1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-23 15:22:41 +01:00

Move most Herald actions and values into dynamic adapters

Summary: Ref T2769. Shift the bulk of value and action config into Adapters.

Test Plan: Viewed and edited Herald list and rules.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran, vrana

Maniphest Tasks: T2769

Differential Revision: https://secure.phabricator.com/D6660
This commit is contained in:
epriestley 2013-08-02 11:32:50 -07:00
parent 2e87f9f53c
commit 9c637604a2
8 changed files with 226 additions and 127 deletions

View file

@ -635,7 +635,6 @@ phutil_register_library_map(array(
'HeraldTranscript' => 'applications/herald/storage/transcript/HeraldTranscript.php', 'HeraldTranscript' => 'applications/herald/storage/transcript/HeraldTranscript.php',
'HeraldTranscriptController' => 'applications/herald/controller/HeraldTranscriptController.php', 'HeraldTranscriptController' => 'applications/herald/controller/HeraldTranscriptController.php',
'HeraldTranscriptListController' => 'applications/herald/controller/HeraldTranscriptListController.php', 'HeraldTranscriptListController' => 'applications/herald/controller/HeraldTranscriptListController.php',
'HeraldValueTypeConfig' => 'applications/herald/config/HeraldValueTypeConfig.php',
'Javelin' => 'infrastructure/javelin/Javelin.php', 'Javelin' => 'infrastructure/javelin/Javelin.php',
'JavelinReactorExample' => 'applications/uiexample/examples/JavelinReactorExample.php', 'JavelinReactorExample' => 'applications/uiexample/examples/JavelinReactorExample.php',
'JavelinUIExample' => 'applications/uiexample/examples/JavelinUIExample.php', 'JavelinUIExample' => 'applications/uiexample/examples/JavelinUIExample.php',

View file

@ -34,6 +34,24 @@ abstract class HeraldAdapter {
const CONDITION_NOT_EXISTS = '!exists'; const CONDITION_NOT_EXISTS = '!exists';
const CONDITION_REGEXP_PAIR = 'regexp-pair'; const CONDITION_REGEXP_PAIR = 'regexp-pair';
const ACTION_ADD_CC = 'addcc';
const ACTION_REMOVE_CC = 'remcc';
const ACTION_EMAIL = 'email';
const ACTION_NOTHING = 'nothing';
const ACTION_AUDIT = 'audit';
const ACTION_FLAG = 'flag';
const VALUE_TEXT = 'text';
const VALUE_NONE = 'none';
const VALUE_EMAIL = 'email';
const VALUE_USER = 'user';
const VALUE_TAG = 'tag';
const VALUE_RULE = 'rule';
const VALUE_REPOSITORY = 'repository';
const VALUE_OWNERS_PACKAGE = 'package';
const VALUE_PROJECT = 'project';
const VALUE_FLAG_COLOR = 'flagcolor';
abstract public function getPHID(); abstract public function getPHID();
abstract public function getHeraldName(); abstract public function getHeraldName();
abstract public function getHeraldTypeName(); abstract public function getHeraldTypeName();
@ -53,6 +71,11 @@ abstract class HeraldAdapter {
} }
abstract public function getAdapterContentName(); abstract public function getAdapterContentName();
/* -( Fields )------------------------------------------------------------- */
abstract public function getFields(); abstract public function getFields();
public function getFieldNameMap() { public function getFieldNameMap() {
@ -74,6 +97,10 @@ abstract class HeraldAdapter {
); );
} }
/* -( Conditions )--------------------------------------------------------- */
public function getConditionNameMap() { public function getConditionNameMap() {
return array( return array(
self::CONDITION_CONTAINS => pht('contains'), self::CONDITION_CONTAINS => pht('contains'),
@ -151,6 +178,120 @@ abstract class HeraldAdapter {
} }
/* -( Actions )------------------------------------------------------------ */
abstract public function getActions($rule_type);
public function getActionNameMap($rule_type) {
switch ($rule_type) {
case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL:
return array(
self::ACTION_NOTHING => pht('Do nothing'),
self::ACTION_ADD_CC => pht('Add emails to CC'),
self::ACTION_REMOVE_CC => pht('Remove emails from CC'),
self::ACTION_EMAIL => pht('Send an email to'),
self::ACTION_AUDIT => pht('Trigger an Audit by'),
);
case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL:
return array(
self::ACTION_NOTHING => pht('Do nothing'),
self::ACTION_ADD_CC => pht('Add me to CC'),
self::ACTION_REMOVE_CC => pht('Remove me from CC'),
self::ACTION_EMAIL => pht('Send me an email'),
self::ACTION_AUDIT => pht('Trigger an Audit by me'),
self::ACTION_FLAG => pht('Mark with flag'),
);
default:
throw new Exception("Unknown rule type '{$rule_type}'!");
}
}
/* -( Values )------------------------------------------------------------- */
public function getValueTypeForFieldAndCondition($field, $condition) {
switch ($condition) {
case self::CONDITION_CONTAINS:
case self::CONDITION_NOT_CONTAINS:
case self::CONDITION_IS:
case self::CONDITION_IS_NOT:
case self::CONDITION_REGEXP:
case self::CONDITION_REGEXP_PAIR:
return self::VALUE_TEXT;
case self::CONDITION_IS_ANY:
case self::CONDITION_IS_NOT_ANY:
switch ($field) {
case self::FIELD_REPOSITORY:
return self::VALUE_REPOSITORY;
default:
return self::VALUE_USER;
}
break;
case self::CONDITION_INCLUDE_ALL:
case self::CONDITION_INCLUDE_ANY:
case self::CONDITION_INCLUDE_NONE:
switch ($field) {
case self::FIELD_REPOSITORY:
return self::VALUE_REPOSITORY;
case self::FIELD_CC:
return self::VALUE_EMAIL;
case self::FIELD_TAGS:
return self::VALUE_TAG;
case self::FIELD_AFFECTED_PACKAGE:
return self::VALUE_OWNERS_PACKAGE;
default:
return self::VALUE_USER;
}
break;
case self::CONDITION_IS_ME:
case self::CONDITION_IS_NOT_ME:
case self::CONDITION_EXISTS:
case self::CONDITION_NOT_EXISTS:
return self::VALUE_NONE;
case self::CONDITION_RULE:
case self::CONDITION_NOT_RULE:
return self::VALUE_RULE;
default:
throw new Exception("Unknown condition '{$condition}'.");
}
}
public static function getValueTypeForAction($action, $rule_type) {
$is_personal = ($rule_type == HeraldRuleTypeConfig::RULE_TYPE_PERSONAL);
if ($is_personal) {
switch ($action) {
case self::ACTION_ADD_CC:
case self::ACTION_REMOVE_CC:
case self::ACTION_EMAIL:
case self::ACTION_NOTHING:
case self::ACTION_AUDIT:
return self::VALUE_NONE;
case self::ACTION_FLAG:
return self::VALUE_FLAG_COLOR;
default:
throw new Exception("Unknown or invalid action '{$action}'.");
}
} else {
switch ($action) {
case self::ACTION_ADD_CC:
case self::ACTION_REMOVE_CC:
case self::ACTION_EMAIL:
return self::VALUE_EMAIL;
case self::ACTION_NOTHING:
return self::VALUE_NONE;
case self::ACTION_AUDIT:
return self::VALUE_PROJECT;
case self::ACTION_FLAG:
return self::VALUE_FLAG_COLOR;
default:
throw new Exception("Unknown or invalid action '{$action}'.");
}
}
}
public static function applyFlagEffect(HeraldEffect $effect, $phid) { public static function applyFlagEffect(HeraldEffect $effect, $phid) {
$color = $effect->getTarget(); $color = $effect->getTarget();

View file

@ -86,6 +86,36 @@ final class HeraldCommitAdapter extends HeraldAdapter {
return parent::getConditionsForField($field); return parent::getConditionsForField($field);
} }
public function getActions($rule_type) {
switch ($rule_type) {
case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL:
return array(
self::ACTION_ADD_CC,
self::ACTION_REMOVE_CC,
self::ACTION_EMAIL,
self::ACTION_NOTHING,
);
case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL:
return array(
self::ACTION_ADD_CC,
self::ACTION_REMOVE_CC,
self::ACTION_EMAIL,
self::ACTION_NOTHING,
);
}
}
public function getValueTypeForFieldAndCondition($field, $condition) {
switch ($field) {
case self::FIELD_DIFFERENTIAL_CCS:
return self::VALUE_EMAIL;
case self::FIELD_NEED_AUDIT_FOR_PACKAGE:
return self::VALUE_OWNERS_PACKAGE;
}
return parent::getValueTypeForFieldAndCondition($field, $condition);
}
public static function newLegacyAdapter( public static function newLegacyAdapter(
PhabricatorRepository $repository, PhabricatorRepository $repository,
PhabricatorRepositoryCommit $commit, PhabricatorRepositoryCommit $commit,

View file

@ -242,6 +242,25 @@ final class HeraldDifferentialRevisionAdapter extends HeraldAdapter {
} }
} }
public function getActions($rule_type) {
switch ($rule_type) {
case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL:
return array(
self::ACTION_ADD_CC,
self::ACTION_REMOVE_CC,
self::ACTION_EMAIL,
self::ACTION_NOTHING,
);
case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL:
return array(
self::ACTION_ADD_CC,
self::ACTION_REMOVE_CC,
self::ACTION_EMAIL,
self::ACTION_NOTHING,
);
}
}
public function applyHeraldEffects(array $effects) { public function applyHeraldEffects(array $effects) {
assert_instances_of($effects, 'HeraldEffect'); assert_instances_of($effects, 'HeraldEffect');

View file

@ -30,6 +30,10 @@ final class HeraldDryRunAdapter extends HeraldAdapter {
return array(); return array();
} }
public function getActions($rule_type) {
return array();
}
public function applyHeraldEffects(array $effects) { public function applyHeraldEffects(array $effects) {
assert_instances_of($effects, 'HeraldEffect'); assert_instances_of($effects, 'HeraldEffect');
$results = array(); $results = array();

View file

@ -9,6 +9,7 @@ final class HeraldActionConfig {
const ACTION_AUDIT = 'audit'; const ACTION_AUDIT = 'audit';
const ACTION_FLAG = 'flag'; const ACTION_FLAG = 'flag';
// TODO: Remove; still used by transcripts.
public static function getActionMessageMapForRuleType($rule_type) { public static function getActionMessageMapForRuleType($rule_type) {
$generic_mappings = array( $generic_mappings = array(
self::ACTION_NOTHING => pht('Do nothing'), self::ACTION_NOTHING => pht('Do nothing'),
@ -43,6 +44,7 @@ final class HeraldActionConfig {
return $specific_mappings + $generic_mappings; return $specific_mappings + $generic_mappings;
} }
// TODO: Remove; still used by transcripts.
public static function getActionMessageMap($content_type, public static function getActionMessageMap($content_type,
$rule_type) { $rule_type) {
$map = self::getActionMessageMapForRuleType($rule_type); $map = self::getActionMessageMapForRuleType($rule_type);

View file

@ -1,98 +0,0 @@
<?php
final class HeraldValueTypeConfig {
const VALUE_TEXT = 'text';
const VALUE_NONE = 'none';
const VALUE_EMAIL = 'email';
const VALUE_USER = 'user';
const VALUE_TAG = 'tag';
const VALUE_RULE = 'rule';
const VALUE_REPOSITORY = 'repository';
const VALUE_OWNERS_PACKAGE = 'package';
const VALUE_PROJECT = 'project';
const VALUE_FLAG_COLOR = 'flagcolor';
public static function getValueTypeForFieldAndCondition($field, $condition) {
switch ($condition) {
case HeraldConditionConfig::CONDITION_CONTAINS:
case HeraldConditionConfig::CONDITION_NOT_CONTAINS:
case HeraldConditionConfig::CONDITION_IS:
case HeraldConditionConfig::CONDITION_IS_NOT:
case HeraldConditionConfig::CONDITION_REGEXP:
case HeraldConditionConfig::CONDITION_REGEXP_PAIR:
return self::VALUE_TEXT;
case HeraldConditionConfig::CONDITION_IS_ANY:
case HeraldConditionConfig::CONDITION_IS_NOT_ANY:
switch ($field) {
case HeraldFieldConfig::FIELD_REPOSITORY:
return self::VALUE_REPOSITORY;
default:
return self::VALUE_USER;
}
break;
case HeraldConditionConfig::CONDITION_INCLUDE_ALL:
case HeraldConditionConfig::CONDITION_INCLUDE_ANY:
case HeraldConditionConfig::CONDITION_INCLUDE_NONE:
switch ($field) {
case HeraldFieldConfig::FIELD_REPOSITORY:
return self::VALUE_REPOSITORY;
case HeraldFieldConfig::FIELD_CC:
case HeraldFieldConfig::FIELD_DIFFERENTIAL_CCS:
return self::VALUE_EMAIL;
case HeraldFieldConfig::FIELD_TAGS:
return self::VALUE_TAG;
case HeraldFieldConfig::FIELD_AFFECTED_PACKAGE:
case HeraldFieldConfig::FIELD_NEED_AUDIT_FOR_PACKAGE:
return self::VALUE_OWNERS_PACKAGE;
default:
return self::VALUE_USER;
}
break;
case HeraldConditionConfig::CONDITION_IS_ME:
case HeraldConditionConfig::CONDITION_IS_NOT_ME:
case HeraldConditionConfig::CONDITION_EXISTS:
case HeraldConditionConfig::CONDITION_NOT_EXISTS:
return self::VALUE_NONE;
case HeraldConditionConfig::CONDITION_RULE:
case HeraldConditionConfig::CONDITION_NOT_RULE:
return self::VALUE_RULE;
default:
throw new Exception("Unknown condition.");
}
}
public static function getValueTypeForAction($action, $rule_type) {
$is_personal = ($rule_type == HeraldRuleTypeConfig::RULE_TYPE_PERSONAL);
if ($is_personal) {
switch ($action) {
case HeraldActionConfig::ACTION_ADD_CC:
case HeraldActionConfig::ACTION_REMOVE_CC:
case HeraldActionConfig::ACTION_EMAIL:
case HeraldActionConfig::ACTION_NOTHING:
case HeraldActionConfig::ACTION_AUDIT:
return self::VALUE_NONE;
case HeraldActionConfig::ACTION_FLAG:
return self::VALUE_FLAG_COLOR;
default:
throw new Exception("Unknown or invalid action '{$action}'.");
}
} else {
switch ($action) {
case HeraldActionConfig::ACTION_ADD_CC:
case HeraldActionConfig::ACTION_REMOVE_CC:
case HeraldActionConfig::ACTION_EMAIL:
return self::VALUE_EMAIL;
case HeraldActionConfig::ACTION_NOTHING:
return self::VALUE_NONE;
case HeraldActionConfig::ACTION_AUDIT:
return self::VALUE_PROJECT;
case HeraldActionConfig::ACTION_FLAG:
return self::VALUE_FLAG_COLOR;
default:
throw new Exception("Unknown or invalid action '{$action}'.");
}
}
}
}

View file

@ -389,13 +389,19 @@ final class HeraldRuleController extends HeraldController {
$all_fields = $adapter->getFieldNameMap(); $all_fields = $adapter->getFieldNameMap();
$all_conditions = $adapter->getConditionNameMap(); $all_conditions = $adapter->getConditionNameMap();
$all_actions = $adapter->getActionNameMap($rule->getRuleType());
$fields = $adapter->getFields(); $fields = $adapter->getFields();
$field_map = array_select_keys($all_fields, $fields); $field_map = array_select_keys($all_fields, $fields);
$actions = $adapter->getActions($rule->getRuleType());
$action_map = array_select_keys($all_actions, $actions);
$config_info = array(); $config_info = array();
$config_info['fields'] = $field_map; $config_info['fields'] = $field_map;
$config_info['conditions'] = $all_conditions; $config_info['conditions'] = $all_conditions;
$config_info['actions'] = $action_map;
foreach ($config_info['fields'] as $field => $name) { foreach ($config_info['fields'] as $field => $name) {
$field_conditions = $adapter->getConditionsForField($field); $field_conditions = $adapter->getConditionsForField($field);
$config_info['conditionMap'][$field] = $field_conditions; $config_info['conditionMap'][$field] = $field_conditions;
@ -403,22 +409,18 @@ final class HeraldRuleController extends HeraldController {
foreach ($config_info['fields'] as $field => $fname) { foreach ($config_info['fields'] as $field => $fname) {
foreach ($config_info['conditionMap'][$field] as $condition) { foreach ($config_info['conditionMap'][$field] as $condition) {
$config_info['values'][$field][$condition] = $value_type = $adapter->getValueTypeForFieldAndCondition(
HeraldValueTypeConfig::getValueTypeForFieldAndCondition(
$field, $field,
$condition); $condition);
$config_info['values'][$field][$condition] = $value_type;
} }
} }
$config_info['actions'] =
HeraldActionConfig::getActionMessageMap($rule->getContentType(),
$rule->getRuleType());
$config_info['rule_type'] = $rule->getRuleType(); $config_info['rule_type'] = $rule->getRuleType();
foreach ($config_info['actions'] as $action => $name) { foreach ($config_info['actions'] as $action => $name) {
$config_info['targets'][$action] = $config_info['targets'][$action] = $adapter->getValueTypeForAction(
HeraldValueTypeConfig::getValueTypeForAction($action, $action,
$rule->getRuleType()); $rule->getRuleType());
} }