Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / herald / management / HeraldRuleManagementWorkflow.php
blob3380b21da772fca343f836ed81d379cfeb2920fc
1 <?php
3 final class HeraldRuleManagementWorkflow
4 extends HeraldManagementWorkflow {
6 protected function didConstruct() {
7 $this
8 ->setName('rule')
9 ->setExamples('**rule** --rule __rule__ --disable')
10 ->setSynopsis(
11 pht(
12 'Modify a rule, bypassing policies. This workflow can disable '.
13 'problematic personal rules.'))
14 ->setArguments(
15 array(
16 array(
17 'name' => 'rule',
18 'param' => 'rule',
19 'help' => pht('Apply changes to this rule.'),
21 array(
22 'name' => 'disable',
23 'help' => pht('Disable the rule.'),
25 array(
26 'name' => 'enable',
27 'help' => pht('Enable the rule.'),
29 ));
32 public function execute(PhutilArgumentParser $args) {
33 $viewer = $this->getViewer();
35 $rule_name = $args->getArg('rule');
36 if (!strlen($rule_name)) {
37 throw new PhutilArgumentUsageException(
38 pht('Specify a rule to edit with "--rule <id|monogram>".'));
41 if (preg_match('/^H\d+/', $rule_name)) {
42 $rule_id = substr($rule_name, 1);
43 } else {
44 $rule_id = $rule_name;
47 $rule = id(new HeraldRuleQuery())
48 ->setViewer($viewer)
49 ->withIDs(array($rule_id))
50 ->executeOne();
51 if (!$rule) {
52 throw new PhutilArgumentUsageException(
53 pht(
54 'Unable to load Herald rule with ID or monogram "%s".',
55 $rule_name));
58 $is_disable = $args->getArg('disable');
59 $is_enable = $args->getArg('enable');
61 $xactions = array();
63 if ($is_disable && $is_enable) {
64 throw new PhutilArgumentUsageException(
65 pht(
66 'Specify "--enable" or "--disable", but not both.'));
67 } else if ($is_disable || $is_enable) {
68 $xactions[] = $rule->getApplicationTransactionTemplate()
69 ->setTransactionType(HeraldRuleDisableTransaction::TRANSACTIONTYPE)
70 ->setNewValue($is_disable);
73 if (!$xactions) {
74 throw new PhutilArgumentUsageException(
75 pht(
76 'Use flags to specify at least one edit to apply to the '.
77 'rule (for example, use "--disable" to disable a rule).'));
80 $herald_phid = id(new PhabricatorHeraldApplication())->getPHID();
82 $editor = $rule->getApplicationTransactionEditor()
83 ->setActor($viewer)
84 ->setActingAsPHID($herald_phid)
85 ->setContentSource($this->newContentSource())
86 ->setContinueOnMissingFields(true)
87 ->setContinueOnNoEffect(true);
89 echo tsprintf(
90 "%s\n",
91 pht(
92 'Applying changes to %s: %s...',
93 $rule->getMonogram(),
94 $rule->getName()));
96 $editor->applyTransactions($rule, $xactions);
98 echo tsprintf(
99 "%s\n",
100 pht('Done.'));
103 return 0;