1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-24 22:40:55 +01:00

Add "bin/herald rule ..." to modify Herald rules from the CLI

Summary:
Depends on D20566. Ref T13298. See PHI1280. Currently, there's no clean way to disable problematic personal rules. This comes up occasionally and sometimes isn't really the best approach to solving a problem, but is a generally reasonable capability to provide.

Allow Herald rules (including personal rules) to be disabled/enabled via `bin/herald rule ... --disable/--enable`.

Test Plan: Used the CLI to disable and enable a personal rule.

Reviewers: amckinley

Reviewed By: amckinley

Subscribers: jmeador

Maniphest Tasks: T13298

Differential Revision: https://secure.phabricator.com/D20567
This commit is contained in:
epriestley 2019-05-31 07:56:59 -07:00
parent 760406762a
commit d7890d08b8
2 changed files with 108 additions and 0 deletions

View file

@ -1560,6 +1560,7 @@ phutil_register_library_map(array(
'HeraldRuleIndexEngineExtension' => 'applications/herald/engineextension/HeraldRuleIndexEngineExtension.php', 'HeraldRuleIndexEngineExtension' => 'applications/herald/engineextension/HeraldRuleIndexEngineExtension.php',
'HeraldRuleListController' => 'applications/herald/controller/HeraldRuleListController.php', 'HeraldRuleListController' => 'applications/herald/controller/HeraldRuleListController.php',
'HeraldRuleListView' => 'applications/herald/view/HeraldRuleListView.php', 'HeraldRuleListView' => 'applications/herald/view/HeraldRuleListView.php',
'HeraldRuleManagementWorkflow' => 'applications/herald/management/HeraldRuleManagementWorkflow.php',
'HeraldRuleNameTransaction' => 'applications/herald/xaction/HeraldRuleNameTransaction.php', 'HeraldRuleNameTransaction' => 'applications/herald/xaction/HeraldRuleNameTransaction.php',
'HeraldRulePHIDType' => 'applications/herald/phid/HeraldRulePHIDType.php', 'HeraldRulePHIDType' => 'applications/herald/phid/HeraldRulePHIDType.php',
'HeraldRuleQuery' => 'applications/herald/query/HeraldRuleQuery.php', 'HeraldRuleQuery' => 'applications/herald/query/HeraldRuleQuery.php',
@ -7381,6 +7382,7 @@ phutil_register_library_map(array(
'HeraldRuleIndexEngineExtension' => 'PhabricatorEdgeIndexEngineExtension', 'HeraldRuleIndexEngineExtension' => 'PhabricatorEdgeIndexEngineExtension',
'HeraldRuleListController' => 'HeraldController', 'HeraldRuleListController' => 'HeraldController',
'HeraldRuleListView' => 'AphrontView', 'HeraldRuleListView' => 'AphrontView',
'HeraldRuleManagementWorkflow' => 'HeraldManagementWorkflow',
'HeraldRuleNameTransaction' => 'HeraldRuleTransactionType', 'HeraldRuleNameTransaction' => 'HeraldRuleTransactionType',
'HeraldRulePHIDType' => 'PhabricatorPHIDType', 'HeraldRulePHIDType' => 'PhabricatorPHIDType',
'HeraldRuleQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 'HeraldRuleQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',

View file

@ -0,0 +1,106 @@
<?php
final class HeraldRuleManagementWorkflow
extends HeraldManagementWorkflow {
protected function didConstruct() {
$this
->setName('rule')
->setExamples('**rule** --rule __rule__ --disable')
->setSynopsis(
pht(
'Modify a rule, bypassing policies. This workflow can disable '.
'problematic personal rules.'))
->setArguments(
array(
array(
'name' => 'rule',
'param' => 'rule',
'help' => pht('Apply changes to this rule.'),
),
array(
'name' => 'disable',
'help' => pht('Disable the rule.'),
),
array(
'name' => 'enable',
'help' => pht('Enable the rule.'),
),
));
}
public function execute(PhutilArgumentParser $args) {
$viewer = $this->getViewer();
$rule_name = $args->getArg('rule');
if (!strlen($rule_name)) {
throw new PhutilArgumentUsageException(
pht('Specify a rule to edit with "--rule <id|monogram>".'));
}
if (preg_match('/^H\d+/', $rule_name)) {
$rule_id = substr($rule_name, 1);
} else {
$rule_id = $rule_name;
}
$rule = id(new HeraldRuleQuery())
->setViewer($viewer)
->withIDs(array($rule_id))
->executeOne();
if (!$rule) {
throw new PhutilArgumentUsageException(
pht(
'Unable to load Herald rule with ID or monogram "%s".',
$rule_name));
}
$is_disable = $args->getArg('disable');
$is_enable = $args->getArg('enable');
$xactions = array();
if ($is_disable && $is_enable) {
throw new PhutilArgumentUsageException(
pht(
'Specify "--enable" or "--disable", but not both.'));
} else if ($is_disable || $is_enable) {
$xactions[] = $rule->getApplicationTransactionTemplate()
->setTransactionType(HeraldRuleDisableTransaction::TRANSACTIONTYPE)
->setNewValue($is_disable);
}
if (!$xactions) {
throw new PhutilArgumentUsageException(
pht(
'Use flags to specify at least one edit to apply to the '.
'rule (for example, use "--disable" to disable a rule).'));
}
$herald_phid = id(new PhabricatorHeraldApplication())->getPHID();
$editor = $rule->getApplicationTransactionEditor()
->setActor($viewer)
->setActingAsPHID($herald_phid)
->setContentSource($this->newContentSource())
->setContinueOnMissingFields(true)
->setContinueOnNoEffect(true);
echo tsprintf(
"%s\n",
pht(
'Applying changes to %s: %s...',
$rule->getMonogram(),
$rule->getName()));
$editor->applyTransactions($rule, $xactions);
echo tsprintf(
"%s\n",
pht('Done.'));
return 0;
}
}